b933b190f209fcad9263d3ccf6f7b53fe5b680d1
[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 exec 3>/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                 ! grep -q $DEVICE /proc/mounts && info "Unmounted successfully"
147                 return $?
148         fi
149         return 0
150 }
151
152 unmount() {
153         if [ "$1" = "" ] ; then
154                 return 0
155         fi
156         grep -q $1 /proc/mounts
157         if [ $? -eq 0 ]; then
158                 debug "Unmounting $1"
159                 umount $1
160                 ! grep -q $1 /proc/mounts # check if unmounted successfully
161                 return $?
162         fi
163         return 0
164 }
165
166 #
167 # Parse and validate arguments
168 #
169 if [ "$1" != "-v" ] && [ $# -ne 2 ]; then
170         usage
171 fi
172 if [ "$1" = "-v" ] && [ $# -ne 3 ]; then
173         usage
174 fi
175
176 if [ "$1" = "-v" ] ; then
177         DEBUG=1
178         exec 3>&1
179         shift
180 fi
181
182 HDDIMG=$1
183 DEVICE=$2
184
185 LINK=$(readlink $DEVICE)
186 if [ $? -eq 0 ]; then
187         DEVICE="$LINK"
188 fi
189
190 if [ ! -w "$DEVICE" ]; then
191         if [ ! -e "${DEVICE}" ] ; then
192                 die "Device $DEVICE cannot be found"
193         else
194                 die "Device $DEVICE is not writable (need to run under sudo?)"
195         fi
196 fi
197
198 if [ ! -e "$HDDIMG" ]; then
199         die "HDDIMG $HDDIMG does not exist"
200 fi
201
202 #
203 # Ensure the hddimg is not mounted
204 #
205 unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
206
207 #
208 # Check if any $DEVICE partitions are mounted
209 #
210 unmount_device || die "Failed to unmount $DEVICE"
211
212 #
213 # Confirm device with user
214 #
215 image_details $HDDIMG
216 device_details $(basename $DEVICE)
217 echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
218 read RESPONSE
219 if [ "$RESPONSE" != "y" ]; then
220         echo "Image creation aborted"
221         exit 0
222 fi
223
224
225 #
226 # Prepare the temporary working space
227 #
228 TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
229 HDDIMG_MNT=$TMPDIR/hddimg
230 HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
231 ROOTFS_MNT=$TMPDIR/rootfs
232 BOOTFS_MNT=$TMPDIR/bootfs
233 mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
234 mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
235 mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
236 mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
237
238
239 #
240 # Partition $DEVICE
241 #
242 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
243 # If the device size is not reported there may not be a valid label
244 if [ "$DEVICE_SIZE" = "" ] ; then
245         parted -s $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
246         DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
247 fi
248 ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE))
249 ROOTFS_START=$((BOOT_SIZE))
250 ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
251
252 # MMC devices use a partition prefix character 'p'
253 PART_PREFIX=""
254 if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
255         PART_PREFIX="p"
256 fi
257 BOOTFS=$DEVICE${PART_PREFIX}1
258 ROOTFS=$DEVICE${PART_PREFIX}2
259
260 TARGET_PART_PREFIX=""
261 if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
262         TARGET_PART_PREFIX="p"
263 fi
264 TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
265
266 echo ""
267 info "Boot partition size:   $BOOT_SIZE MB ($BOOTFS)"
268 info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
269 echo ""
270
271 # Use MSDOS by default as GPT cannot be reliably distributed in disk image form
272 # as it requires the backup table to be on the last block of the device, which
273 # of course varies from device to device.
274
275 info "Partitioning installation media ($DEVICE)"
276
277 debug "Deleting partition table on $DEVICE"
278 dd if=/dev/zero of=$DEVICE bs=512 count=2 1>&3 2>&1 || die "Failed to zero beginning of $DEVICE"
279
280 debug "Creating new partition table (MSDOS) on $DEVICE"
281 parted -s $DEVICE mklabel msdos 1>&3 2>&1 || die "Failed to create MSDOS partition table"
282
283 debug "Creating boot partition on $BOOTFS"
284 parted -s $DEVICE mkpart primary 0% $BOOT_SIZE 1>&3 2>&1 || die "Failed to create BOOT partition"
285
286 debug "Enabling boot flag on $BOOTFS"
287 parted -s $DEVICE set 1 boot on 1>&3 2>&1 || die "Failed to enable boot flag"
288
289 debug "Creating ROOTFS partition on $ROOTFS"
290 parted -s $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END 1>&3 2>&1 || die "Failed to create ROOTFS partition"
291
292 # as blkid does not provide PARTUUID on Ubuntu LTS 14.04 we myst hack via fdisk
293 #ROOTFS_PARTUUID=$(blkid |grep -e "$ROOTFS" |sed -n 's/^.*PARTUUID=/PARTUUID=/p')
294 export LC_ALL=C
295 ROOTFS_DISKID=$(fdisk -l "$DEVICE" | grep -e "Disk identifier" | sed -n 's/^.*Disk identifier: 0x/PARTUUID=/p')
296 if [ $ROOTFS_DISKID = "" ]; then
297     die "Failed to read DISKID"
298 fi
299 ROOTFS_PARTUUID="$ROOTFS_DISKID-02"
300 debug "PARTUUID for ROOTFS is $ROOTFS_PARTUUID"
301
302 if [ $DEBUG -eq 1 ]; then
303         parted -s $DEVICE print
304 fi
305
306
307 #
308 # Check if any $DEVICE partitions are mounted after partitioning
309 #
310 unmount_device || die "Failed to unmount $DEVICE partitions"
311
312
313 #
314 # Format $DEVICE partitions
315 #
316 info "Formatting partitions"
317 debug "Formatting $BOOTFS as vfat"
318 if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
319         mkfs.vfat -I $BOOTFS -n "EFI" 1>&3 2>&1 || die "Failed to format $BOOTFS"
320 else
321         mkfs.vfat $BOOTFS -n "EFI" 1>&3 2>&1 || die "Failed to format $BOOTFS"
322 fi
323
324 debug "Formatting $ROOTFS as ext4"
325 mkfs.ext4 -F $ROOTFS -L "ROOT" 1>&3 2>&1 || die "Failed to format $ROOTFS"
326
327
328 #
329 # Installing to $DEVICE
330 #
331 debug "Mounting images and device in preparation for installation"
332 mount -o loop $HDDIMG $HDDIMG_MNT 1>&3 2>&1 || error "Failed to mount $HDDIMG"
333 mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT 1>&3 2>&1 || error "Failed to mount rootfs.img"
334 mount $ROOTFS $ROOTFS_MNT 1>&3 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
335 mount $BOOTFS $BOOTFS_MNT 1>&3 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
336
337 info "Preparing boot partition"
338 EFIDIR="$BOOTFS_MNT/EFI/BOOT"
339 cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT 1>&3 2>&1 || error "Failed to copy vmlinuz"
340 if [ -f $HDDIMG_MNT/initrd ]; then
341   cp $HDDIMG_MNT/initrd $BOOTFS_MNT 1>&3 2>&1 || error "Failed to copy initrd"
342 fi
343 echo "bootx64.efi" > $BOOTFS_MNT/startup.nsh || error "Failed to create startup.nsh"
344 # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
345 cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT 1>&3 2>&1 || error "Failed to copy EFI dir"
346 # Silently ignore a missing systemd-boot or gummiboot loader dir (we might just be a GRUB image)
347 cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 1>&3 2>&1
348
349 # Update the boot loaders configurations for an installed image
350 # Remove any existing root= kernel parameters and:
351 # o Add a root= parameter with the target rootfs
352 # o Specify ro so fsck can be run during boot
353 # o Specify rootwait in case the target media is an asyncronous block device
354 #   such as MMC or USB disks
355 # o Specify "quiet" to minimize boot time when using slow serial consoles
356
357 # Look for a GRUB installation
358 GRUB_CFG="$EFIDIR/grub.cfg"
359 if [ -e "$GRUB_CFG" ]; then
360         info "Configuring GRUB"
361         # Delete the install entry
362         sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
363         # Delete any LABEL= strings
364         sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
365
366         sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
367         sed -i "s@vmlinuz @vmlinuz root=$ROOTFS_PARTUUID @" $GRUB_CFG
368 fi
369
370 # look for a systemd-boot loader.conf file and create a default boot entry
371 SYSTEMDBOOT_CFG="$BOOTFS_MNT/loader/loader.conf"
372 if [ -e "$SYSTEMDBOOT_CFG" ]; then
373         info "Configuring SYSTEMD-BOOT"
374     SYSTEMDBOOT_BOOT="$BOOTFS_MNT/loader/entries/boot.conf"
375     SYSTEMDBOOT_DEBUG="$BOOTFS_MNT/loader/entries/debug.conf"
376     # Delete the install entry
377         sed -i "/menuentry 'install'/,/^}/d" $SYSTEMDBOOT_CFG
378         rm -rf "$BOOTFS_MNT/loader/entries/install.conf" 1>&3 2>&1
379         # Add PARTUUID to the boot entry file
380         if [ ! -e "$SYSTEMDBOOT_BOOT" ]; then
381         die "no boot.conf entry found in systemd-boot directories"
382     fi
383         # Delete any LABEL= strings
384         sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_BOOT
385
386         sed -i "s@ root=[^ ]*@ @" $SYSTEMDBOOT_BOOT
387         sed -i "s@options @options root=$ROOTFS_PARTUUID @" $SYSTEMDBOOT_BOOT
388 fi
389
390
391 # Ensure we have at least one EFI bootloader configured
392 if [ ! -e $GRUB_CFG ]  && [ ! -e $SYSTEMDBOOT_CFG ] ; then
393         die "No EFI bootloader configuration found"
394 fi
395
396 info "Copying ROOTFS files (this may take a while)"
397 command -v rsync >/dev/null 2>&1 # check if rsync exists
398 if [ $DEBUG -eq 1 ] && [ $? -eq 0 ]; then
399         rsync --info=progress2 -h -aHAXW --no-compress  $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT 1>&3 2>&1 || die "Root FS copy failed"
400 else
401         cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT 1>&3 2>&1 || die "Root FS copy failed"
402 fi
403
404 # We dont want udev to mount our root device while we're booting...
405 if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
406         echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
407 fi
408
409
410 # Call cleanup to unmount devices and images and remove the TMPDIR
411 cleanup
412
413 echo ""
414 if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
415         echo "${YELLOW}Installation completed with warnings${CLEAR}"
416         echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
417 elif [ $ERRORS -ne 0 ]; then
418         echo "${RED}Installation encountered errors${CLEAR}"
419         echo "${RED}Errors: $ERRORS${CLEAR}"
420         echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
421 else
422         success "Installation completed successfully"
423 fi
424 echo ""