2dafa9edd7cf3e0300d32ccb851946ff6d113d1e
[AGL/meta-agl.git] / scripts / mkefi-agl.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012, Intel Corporation.
4 # All rights reserved.
5 #
6 # This program is free software;  you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY;  without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14 # the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program;  if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 # Modification from mkefidisk.sh provided by the Yocto project by Dominig 
21 # to install Automotive Grade Linux (AGL) on Minnowboard and any PC with UEFI boot
22 #
23 # changes
24 #         - simpler use model
25 #         - keep initrd if present
26 #         - create a grub config with PARTUID to ease boot from various devices automaticaly
27 #         - add a UEFI startup.nsh script for autoboot
28 #         - does not allocate swap
29 #         
30
31 LANG=C
32
33 # Set to 1 to enable additional output
34 DEBUG=0
35 OUT="/dev/null"
36
37 #
38 # Defaults
39 #
40 # 100 Mb for the boot partition
41 BOOT_SIZE=100
42
43 # Cleanup after die()
44 cleanup() {
45         debug "Syncing and unmounting devices"
46         # Unmount anything we mounted
47         unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
48         unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
49         unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
50         unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT"
51
52         # Remove the TMPDIR
53         debug "Removing temporary files"
54         if [ -d "$TMPDIR" ]; then
55                 rm -rf $TMPDIR || error "Failed to remove $TMPDIR"
56         fi
57 }
58
59 trap 'die "Signal Received, Aborting..."' HUP INT TERM
60
61 # Logging routines
62 WARNINGS=0
63 ERRORS=0
64 CLEAR="$(tput sgr0)"
65 INFO="$(tput bold)"
66 RED="$(tput setaf 1)$(tput bold)"
67 GREEN="$(tput setaf 2)$(tput bold)"
68 YELLOW="$(tput setaf 3)$(tput bold)"
69 info() {
70         echo "${INFO}$1${CLEAR}"
71 }
72 error() {
73         ERRORS=$((ERRORS+1))
74         echo "${RED}$1${CLEAR}"
75 }
76 warn() {
77         WARNINGS=$((WARNINGS+1))
78         echo "${YELLOW}$1${CLEAR}"
79 }
80 success() {
81         echo "${GREEN}$1${CLEAR}"
82 }
83 die() {
84         error "$1"
85         cleanup
86         exit 1
87 }
88 debug() {
89         if [ $DEBUG -eq 1 ]; then
90                 echo "$1"
91         fi
92 }
93
94 usage() {
95         echo "Install AGL on a removable device to boot on IA UEFI based computer"
96         echo "In particular is can create USB or SD bootable support for Minnowboard"
97         echo ""
98         echo "Usage: $(basename $0) [-v] HDDIMG REMOVABLE_DEVICE"
99         echo "       -v: Verbose debug"
100         echo "       HDDIMG: The hddimg file to generate the efi disk from"
101         echo "       REMOVABLE_DEVICE: The block device to write the image to, e.g. /dev/sdh"
102         echo "ex:"
103         echo "   mkefi-agl.sh   agl-demo-platform-intel-corei7-64.hddimg /dev/sdd"
104         exit 1
105 }
106
107 image_details() {
108         IMG=$1
109         info "Image details"
110         echo "    image: $(stat --printf '%N\n' $IMG)"
111         echo "     size: $(stat -L --printf '%s bytes\n' $IMG)"
112         echo " modified: $(stat -L --printf '%y\n' $IMG)"
113         echo "     type: $(file -L -b $IMG)"
114         echo ""
115 }
116
117 device_details() {
118         DEV=$1
119         BLOCK_SIZE=512
120
121         info "Device details"
122         echo "  device: $DEVICE"
123         if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
124                 echo "  vendor: $(cat /sys/class/block/$DEV/device/vendor)"
125         else
126                 echo "  vendor: UNKOWN"
127         fi
128         if [ -f "/sys/class/block/$DEV/device/model" ]; then
129                 echo "   model: $(cat /sys/class/block/$DEV/device/model)"
130         else
131                 echo "   model: UNKNOWN"
132         fi
133         if [ -f "/sys/class/block/$DEV/size" ]; then
134                 echo "    size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
135         else
136                 echo "    size: UNKNOWN"
137         fi
138         echo ""
139 }
140
141 unmount_device() {
142         grep -q $DEVICE /proc/mounts
143         if [ $? -eq 0 ]; then
144                 warn "$DEVICE listed in /proc/mounts, attempting to unmount"
145                 umount $DEVICE* 2>/dev/null
146                 return $?
147         fi
148         return 0
149 }
150
151 unmount() {
152         if [ "$1" = "" ] ; then
153                 return 0
154         fi
155         grep -q $1 /proc/mounts
156         if [ $? -eq 0 ]; then
157                 debug "Unmounting $1"
158                 umount $1
159                 return $?
160         fi
161         return 0
162 }
163
164 #
165 # Parse and validate arguments
166 #
167 if [ "$1" != "-v" ] && [ $# -ne 2 ]; then
168         usage
169 fi
170 if [ "$1" == "-v" ] && [ $# -ne 3 ]; then
171         usage
172 fi
173
174 if [ "$1" = "-v" ] ; then
175         DEBUG=1
176         OUT="1"
177         shift
178 fi
179
180 HDDIMG=$1
181 DEVICE=$2
182
183 LINK=$(readlink $DEVICE)
184 if [ $? -eq 0 ]; then
185         DEVICE="$LINK"
186 fi
187
188 if [ ! -w "$DEVICE" ]; then
189         if [ ! -e "${DEVICE}" ] ; then
190                 die "Device $DEVICE cannot be found"
191         else
192                 die "Device $DEVICE is not writable (need to run under sudo?)"
193         fi
194 fi
195
196 if [ ! -e "$HDDIMG" ]; then
197         die "HDDIMG $HDDIMG does not exist"
198 fi
199
200 #
201 # Ensure the hddimg is not mounted
202 #
203 unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
204
205 #
206 # Check if any $DEVICE partitions are mounted
207 #
208 unmount_device || die "Failed to unmount $DEVICE"
209
210 #
211 # Confirm device with user
212 #
213 image_details $HDDIMG
214 device_details $(basename $DEVICE)
215 echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
216 read RESPONSE
217 if [ "$RESPONSE" != "y" ]; then
218         echo "Image creation aborted"
219         exit 0
220 fi
221
222
223 #
224 # Prepare the temporary working space
225 #
226 TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
227 HDDIMG_MNT=$TMPDIR/hddimg
228 HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
229 ROOTFS_MNT=$TMPDIR/rootfs
230 BOOTFS_MNT=$TMPDIR/bootfs
231 mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
232 mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
233 mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
234 mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
235
236
237 #
238 # Partition $DEVICE
239 #
240 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
241 # If the device size is not reported there may not be a valid label
242 if [ "$DEVICE_SIZE" = "" ] ; then
243         parted -s $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
244         DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
245 fi
246 ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE))
247 ROOTFS_START=$((BOOT_SIZE))
248 ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
249
250 # MMC devices use a partition prefix character 'p'
251 PART_PREFIX=""
252 if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
253         PART_PREFIX="p"
254 fi
255 BOOTFS=$DEVICE${PART_PREFIX}1
256 ROOTFS=$DEVICE${PART_PREFIX}2
257
258 TARGET_PART_PREFIX=""
259 if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
260         TARGET_PART_PREFIX="p"
261 fi
262 TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
263
264 echo ""
265 info "Boot partition size:   $BOOT_SIZE MB ($BOOTFS)"
266 info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
267 echo ""
268
269 # Use MSDOS by default as GPT cannot be reliably distributed in disk image form
270 # as it requires the backup table to be on the last block of the device, which
271 # of course varies from device to device.
272
273 info "Partitioning installation media ($DEVICE)"
274
275 debug "Deleting partition table on $DEVICE"
276 dd if=/dev/zero of=$DEVICE bs=512 count=2 >$OUT 2>&1 || die "Failed to zero beginning of $DEVICE"
277
278 debug "Creating new partition table (MSDOS) on $DEVICE"
279 parted -s $DEVICE mklabel msdos >$OUT 2>&1 || die "Failed to create MSDOS partition table"
280
281 debug "Creating boot partition on $BOOTFS"
282 parted -s $DEVICE mkpart primary 0% $BOOT_SIZE >$OUT 2>&1 || die "Failed to create BOOT partition"
283
284 debug "Enabling boot flag on $BOOTFS"
285 parted -s $DEVICE set 1 boot on >$OUT 2>&1 || die "Failed to enable boot flag"
286
287 debug "Creating ROOTFS partition on $ROOTFS"
288 parted -s $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END >$OUT 2>&1 || die "Failed to create ROOTFS partition"
289
290 # as blkid does not provide PARTUUID on Ubuntu LTS 14.04 we myst hack via fdisk
291 #ROOTFS_PARTUUID=$(blkid |grep -e "$ROOTFS" |sed -n 's/^.*PARTUUID=/PARTUUID=/p')
292 export LC_ALL=C
293 ROOTFS_DISKID=$(fdisk -l "$DEVICE" | grep -e "Disk identifier" | sed -n 's/^.*Disk identifier: 0x/PARTUUID=/p')
294 if [ $ROOTFS_DISKID == "" ]; then
295     die "Failed to read DISKID"
296 fi
297 ROOTFS_PARTUUID="$ROOTFS_DISKID-02"
298 debug "PARTUUID for ROOTFS is $ROOTFS_PARTUUID"
299
300 if [ $DEBUG -eq 1 ]; then
301         parted -s $DEVICE print
302 fi
303
304
305 #
306 # Check if any $DEVICE partitions are mounted after partitioning
307 #
308 unmount_device || die "Failed to unmount $DEVICE partitions"
309
310
311 #
312 # Format $DEVICE partitions
313 #
314 info "Formatting partitions"
315 debug "Formatting $BOOTFS as vfat"
316 if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
317         mkfs.vfat -I $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
318 else
319         mkfs.vfat $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
320 fi
321
322 debug "Formatting $ROOTFS as ext4"
323 mkfs.ext4 -F $ROOTFS -L "ROOT" >$OUT 2>&1 || die "Failed to format $ROOTFS"
324
325
326 #
327 # Installing to $DEVICE
328 #
329 debug "Mounting images and device in preparation for installation"
330 mount -o loop $HDDIMG $HDDIMG_MNT >$OUT 2>&1 || error "Failed to mount $HDDIMG"
331 mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount rootfs.img"
332 mount $ROOTFS $ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
333 mount $BOOTFS $BOOTFS_MNT >$OUT 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
334
335 info "Preparing boot partition"
336 EFIDIR="$BOOTFS_MNT/EFI/BOOT"
337 cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy vmlinuz"
338 if [ -f $HDDIMG_MNT/initrd ]; then
339   cp $HDDIMG_MNT/initrd $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy initrd"
340 fi
341 echo "bootx64.efi" > $BOOTFS_MNT/startup.nsh || error "Failed to create startup.nsh"
342 # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
343 cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy EFI dir"
344 # Silently ignore a missing systemd-boot or gummiboot loader dir (we might just be a GRUB image)
345 cp -r $HDDIMG_MNT/loader $BOOTFS_MNT >$OUT 2>&1
346
347 # Update the boot loaders configurations for an installed image
348 # Remove any existing root= kernel parameters and:
349 # o Add a root= parameter with the target rootfs
350 # o Specify ro so fsck can be run during boot
351 # o Specify rootwait in case the target media is an asyncronous block device
352 #   such as MMC or USB disks
353 # o Specify "quiet" to minimize boot time when using slow serial consoles
354
355 # Look for a GRUB installation
356 GRUB_CFG="$EFIDIR/grub.cfg"
357 if [ -e "$GRUB_CFG" ]; then
358         info "Configuring GRUB"
359         # Delete the install entry
360         sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
361         # Delete any LABEL= strings
362         sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
363
364         sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
365         sed -i "s@vmlinuz @vmlinuz root=$ROOTFS_PARTUUID @" $GRUB_CFG
366 fi
367
368 # look for a systemd-boot loader.conf file and create a default boot entry
369 SYSTEMDBOOT_CFG="$BOOTFS_MNT/loader/loader.conf"
370 if [ -e "$SYSTEMDBOOT_CFG" ]; then
371         info "Configuring SYSTEMD-BOOT"
372     SYSTEMDBOOT_BOOT="$BOOTFS_MNT/loader/entries/boot.conf"
373     SYSTEMDBOOT_DEBUG="$BOOTFS_MNT/loader/entries/debug.conf"
374     # Delete the install entry
375         sed -i "/menuentry 'install'/,/^}/d" $SYSTEMDBOOT_CFG
376         rm -rf "$BOOTFS_MNT/loader/entries/install.conf" >$OUT 2>&1
377         # Add PARTUUID to the boot entry file
378         if [ ! -e "$SYSTEMDBOOT_BOOT" ]; then
379         die "no boot.conf entry found in systemd-boot directories"
380     fi
381         # Delete any LABEL= strings
382         sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_BOOT
383
384         sed -i "s@ root=[^ ]*@ @" $SYSTEMDBOOT_BOOT
385         sed -i "s@options @options root=$ROOTFS_PARTUUID @" $SYSTEMDBOOT_BOOT   
386 fi      
387
388
389 # Ensure we have at least one EFI bootloader configured
390 if [ ! -e $GRUB_CFG ]  && [ ! -e $SYSTEMDBOOT_CFG ] ; then
391         die "No EFI bootloader configuration found"
392 fi
393
394 info "Copying ROOTFS files (this may take a while)"
395 cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT >$OUT 2>&1 || die "Root FS copy failed"
396
397 # We dont want udev to mount our root device while we're booting...
398 if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
399         echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
400 fi
401
402
403 # Call cleanup to unmount devices and images and remove the TMPDIR
404 cleanup
405
406 echo ""
407 if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
408         echo "${YELLOW}Installation completed with warnings${CLEAR}"
409         echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
410 elif [ $ERRORS -ne 0 ]; then
411         echo "${RED}Installation encountered errors${CLEAR}"
412         echo "${RED}Errors: $ERRORS${CLEAR}"
413         echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
414 else
415         success "Installation completed successfully"
416 fi
417 echo ""