blob: 333284ff5043e9acc5628643da8c5c3997b6479e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
21LANG=C
22
23# Set to 1 to enable additional output
24DEBUG=0
25OUT="/dev/null"
26
27#
28# Defaults
29#
30# 20 Mb for the boot partition
31BOOT_SIZE=20
32# 5% for swap
33SWAP_RATIO=5
34
35# Cleanup after die()
36cleanup() {
37 debug "Syncing and unmounting devices"
38 # Unmount anything we mounted
39 unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
40 unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
41 unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
42 unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT"
43
44 # Remove the TMPDIR
45 debug "Removing temporary files"
46 if [ -d "$TMPDIR" ]; then
47 rm -rf $TMPDIR || error "Failed to remove $TMPDIR"
48 fi
49}
50
51trap 'die "Signal Received, Aborting..."' HUP INT TERM
52
53# Logging routines
54WARNINGS=0
55ERRORS=0
56CLEAR="$(tput sgr0)"
57INFO="$(tput bold)"
58RED="$(tput setaf 1)$(tput bold)"
59GREEN="$(tput setaf 2)$(tput bold)"
60YELLOW="$(tput setaf 3)$(tput bold)"
61info() {
62 echo "${INFO}$1${CLEAR}"
63}
64error() {
65 ERRORS=$((ERRORS+1))
66 echo "${RED}$1${CLEAR}"
67}
68warn() {
69 WARNINGS=$((WARNINGS+1))
70 echo "${YELLOW}$1${CLEAR}"
71}
72success() {
73 echo "${GREEN}$1${CLEAR}"
74}
75die() {
76 error "$1"
77 cleanup
78 exit 1
79}
80debug() {
81 if [ $DEBUG -eq 1 ]; then
82 echo "$1"
83 fi
84}
85
86usage() {
87 echo "Usage: $(basename $0) [-v] DEVICE HDDIMG TARGET_DEVICE"
88 echo " -v: Verbose debug"
89 echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
90 echo " HDDIMG: The hddimg file to generate the efi disk from"
91 echo " TARGET_DEVICE: The device the target will boot from, e.g. /dev/mmcblk0"
92}
93
94image_details() {
95 IMG=$1
96 info "Image details"
97 echo " image: $(stat --printf '%N\n' $IMG)"
98 echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
99 echo " modified: $(stat -L --printf '%y\n' $IMG)"
100 echo " type: $(file -L -b $IMG)"
101 echo ""
102}
103
104device_details() {
105 DEV=$1
106 BLOCK_SIZE=512
107
108 info "Device details"
109 echo " device: $DEVICE"
110 if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
111 echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
112 else
113 echo " vendor: UNKOWN"
114 fi
115 if [ -f "/sys/class/block/$DEV/device/model" ]; then
116 echo " model: $(cat /sys/class/block/$DEV/device/model)"
117 else
118 echo " model: UNKNOWN"
119 fi
120 if [ -f "/sys/class/block/$DEV/size" ]; then
121 echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
122 else
123 echo " size: UNKNOWN"
124 fi
125 echo ""
126}
127
128unmount_device() {
129 grep -q $DEVICE /proc/mounts
130 if [ $? -eq 0 ]; then
131 warn "$DEVICE listed in /proc/mounts, attempting to unmount"
132 umount $DEVICE* 2>/dev/null
133 return $?
134 fi
135 return 0
136}
137
138unmount() {
139 if [ "$1" = "" ] ; then
140 return 0
141 fi
142 grep -q $1 /proc/mounts
143 if [ $? -eq 0 ]; then
144 debug "Unmounting $1"
145 umount $1
146 return $?
147 fi
148 return 0
149}
150
151#
152# Parse and validate arguments
153#
154if [ $# -lt 3 ] || [ $# -gt 4 ]; then
Patrick Williamsd7e96312015-09-22 08:09:05 -0500155 if [ $# -eq 1 ]; then
156 AVAILABLE_DISK=`lsblk | grep "disk" | cut -f 1 -d " "`
157 X=0
158 for disk in `echo $AVAILABLE_DISK`; do
159 mounted=`lsblk /dev/$disk | awk {'print $7'} | sed "s/MOUNTPOINT//"`
160 if [ -z "$mounted" ]; then
161 UNMOUNTED_AVAILABLES="$UNMOUNTED_AVAILABLES /dev/$disk"
162 info "$X - /dev/$disk"
163 X=`expr $X + 1`
164 fi
165 done
166 if [ $X -eq 0 ]; then
167 die "No unmounted device found."
168 fi
169 read -p "Choose unmounted device number: " DISK_NUMBER
170 X=0
171 for line in `echo $UNMOUNTED_AVAILABLES`; do
172 if [ $DISK_NUMBER -eq $X ]; then
173 DISK_TO_BE_FLASHED=$line
174 break
175 else
176 X=`expr $X + 1`
177 fi
178 done
179 if [ -z "$DISK_TO_BE_FLASHED" ]; then
180 die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
181 else
182 if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
183 TARGET_TO_BE_BOOT="/dev/sda"
184 else
185 TARGET_TO_BE_BOOT="/dev/mmcblk0"
186 fi
187 fi
188 echo ""
189 echo "Choose a name of the device that will be boot from"
190 echo -n "Recommended name is: "
191 info "$TARGET_TO_BE_BOOT"
192 read -p "Is target device okay? [y/N]: " RESPONSE
193 if [ "$RESPONSE" != "y" ]; then
194 read -p "Choose target device name: " TARGET_TO_BE_BOOT
195 fi
196 echo ""
197 if [ -z "$TARGET_TO_BE_BOOT" ]; then
198 die "Error: choose a valid target name"
199 fi
200 else
201 usage
202 exit 1
203 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204fi
205
206if [ "$1" = "-v" ]; then
207 DEBUG=1
208 OUT="1"
209 shift
210fi
211
Patrick Williamsd7e96312015-09-22 08:09:05 -0500212if [ -z "$AVAILABLE_DISK" ]; then
213 DEVICE=$1
214 HDDIMG=$2
215 TARGET_DEVICE=$3
216else
217 DEVICE=$DISK_TO_BE_FLASHED
218 HDDIMG=$1
219 TARGET_DEVICE=$TARGET_TO_BE_BOOT
220fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221
222LINK=$(readlink $DEVICE)
223if [ $? -eq 0 ]; then
224 DEVICE="$LINK"
225fi
226
227if [ ! -w "$DEVICE" ]; then
228 usage
229 if [ ! -e "${DEVICE}" ] ; then
230 die "Device $DEVICE cannot be found"
231 else
232 die "Device $DEVICE is not writable (need to run under sudo?)"
233 fi
234fi
235
236if [ ! -e "$HDDIMG" ]; then
237 usage
238 die "HDDIMG $HDDIMG does not exist"
239fi
240
241#
242# Ensure the hddimg is not mounted
243#
244unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
245
246#
247# Check if any $DEVICE partitions are mounted
248#
249unmount_device || die "Failed to unmount $DEVICE"
250
251#
252# Confirm device with user
253#
254image_details $HDDIMG
255device_details $(basename $DEVICE)
256echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
257read RESPONSE
258if [ "$RESPONSE" != "y" ]; then
259 echo "Image creation aborted"
260 exit 0
261fi
262
263
264#
265# Prepare the temporary working space
266#
267TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
268HDDIMG_MNT=$TMPDIR/hddimg
269HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
270ROOTFS_MNT=$TMPDIR/rootfs
271BOOTFS_MNT=$TMPDIR/bootfs
272mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
273mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
274mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
275mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
276
277
278#
279# Partition $DEVICE
280#
281DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
282# If the device size is not reported there may not be a valid label
283if [ "$DEVICE_SIZE" = "" ] ; then
284 parted -s $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
285 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
286fi
287SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
288ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
289ROOTFS_START=$((BOOT_SIZE))
290ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
291SWAP_START=$((ROOTFS_END))
292
293# MMC devices use a partition prefix character 'p'
294PART_PREFIX=""
295if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
296 PART_PREFIX="p"
297fi
298BOOTFS=$DEVICE${PART_PREFIX}1
299ROOTFS=$DEVICE${PART_PREFIX}2
300SWAP=$DEVICE${PART_PREFIX}3
301
302TARGET_PART_PREFIX=""
303if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
304 TARGET_PART_PREFIX="p"
305fi
306TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
307TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
308
309echo ""
310info "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
311info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
312info "Swap partition size: $SWAP_SIZE MB ($SWAP)"
313echo ""
314
315# Use MSDOS by default as GPT cannot be reliably distributed in disk image form
316# as it requires the backup table to be on the last block of the device, which
317# of course varies from device to device.
318
319info "Partitioning installation media ($DEVICE)"
320
321debug "Deleting partition table on $DEVICE"
322dd if=/dev/zero of=$DEVICE bs=512 count=2 >$OUT 2>&1 || die "Failed to zero beginning of $DEVICE"
323
324debug "Creating new partition table (MSDOS) on $DEVICE"
325parted -s $DEVICE mklabel msdos >$OUT 2>&1 || die "Failed to create MSDOS partition table"
326
327debug "Creating boot partition on $BOOTFS"
328parted -s $DEVICE mkpart primary 0% $BOOT_SIZE >$OUT 2>&1 || die "Failed to create BOOT partition"
329
330debug "Enabling boot flag on $BOOTFS"
331parted -s $DEVICE set 1 boot on >$OUT 2>&1 || die "Failed to enable boot flag"
332
333debug "Creating ROOTFS partition on $ROOTFS"
334parted -s $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END >$OUT 2>&1 || die "Failed to create ROOTFS partition"
335
336debug "Creating swap partition on $SWAP"
337parted -s $DEVICE mkpart primary $SWAP_START 100% >$OUT 2>&1 || die "Failed to create SWAP partition"
338
339if [ $DEBUG -eq 1 ]; then
340 parted -s $DEVICE print
341fi
342
343
344#
345# Check if any $DEVICE partitions are mounted after partitioning
346#
347unmount_device || die "Failed to unmount $DEVICE partitions"
348
349
350#
351# Format $DEVICE partitions
352#
353info "Formatting partitions"
354debug "Formatting $BOOTFS as vfat"
355if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
356 mkfs.vfat -I $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
357else
358 mkfs.vfat $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
359fi
360
361debug "Formatting $ROOTFS as ext3"
362mkfs.ext3 -F $ROOTFS -L "ROOT" >$OUT 2>&1 || die "Failed to format $ROOTFS"
363
364debug "Formatting swap partition ($SWAP)"
365mkswap $SWAP >$OUT 2>&1 || die "Failed to prepare swap"
366
367
368#
369# Installing to $DEVICE
370#
371debug "Mounting images and device in preparation for installation"
372mount -o loop $HDDIMG $HDDIMG_MNT >$OUT 2>&1 || error "Failed to mount $HDDIMG"
373mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount rootfs.img"
374mount $ROOTFS $ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
375mount $BOOTFS $BOOTFS_MNT >$OUT 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
376
377info "Preparing boot partition"
378EFIDIR="$BOOTFS_MNT/EFI/BOOT"
379cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy vmlinuz"
380# Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
381cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy EFI dir"
382# Silently ignore a missing gummiboot loader dir (we might just be a GRUB image)
383cp -r $HDDIMG_MNT/loader $BOOTFS_MNT >$OUT 2>&1
384
385# Update the boot loaders configurations for an installed image
386# Remove any existing root= kernel parameters and:
387# o Add a root= parameter with the target rootfs
388# o Specify ro so fsck can be run during boot
389# o Specify rootwait in case the target media is an asyncronous block device
390# such as MMC or USB disks
391# o Specify "quiet" to minimize boot time when using slow serial consoles
392
393# Look for a GRUB installation
394GRUB_CFG="$EFIDIR/grub.cfg"
395if [ -e "$GRUB_CFG" ]; then
396 info "Configuring GRUB"
397 # Delete the install entry
398 sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
399 # Delete the initrd lines
400 sed -i "/initrd /d" $GRUB_CFG
401 # Delete any LABEL= strings
402 sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
403
404 sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500405 sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait console=ttyS0 console=tty0 @" $GRUB_CFG
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406fi
407
408# Look for a gummiboot installation
409GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries"
410GUMMI_CFG="$GUMMI_ENTRIES/boot.conf"
411if [ -d "$GUMMI_ENTRIES" ]; then
412 info "Configuring Gummiboot"
413 # remove the install target if it exists
414 rm $GUMMI_ENTRIES/install.conf >$OUT 2>&1
415
416 if [ ! -e "$GUMMI_CFG" ]; then
417 echo "ERROR: $GUMMI_CFG not found"
418 fi
419
420 sed -i "/initrd /d" $GUMMI_CFG
421 sed -i "s@ root=[^ ]*@ @" $GUMMI_CFG
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500422 sed -i "s@options *LABEL=boot @options LABEL=Boot root=$TARGET_ROOTFS ro rootwait console=ttyS0 console=tty0 @" $GUMMI_CFG
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500423fi
424
425# Ensure we have at least one EFI bootloader configured
426if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then
427 die "No EFI bootloader configuration found"
428fi
429
430
431info "Copying ROOTFS files (this may take a while)"
432cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT >$OUT 2>&1 || die "Root FS copy failed"
433
434echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
435
436# We dont want udev to mount our root device while we're booting...
437if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
438 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
439fi
440
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500441# Add startup.nsh script for automated boot
442echo "fs0:\EFI\BOOT\bootx64.efi" > $BOOTFS_MNT/startup.nsh
443
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500444
445# Call cleanup to unmount devices and images and remove the TMPDIR
446cleanup
447
448echo ""
449if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
450 echo "${YELLOW}Installation completed with warnings${CLEAR}"
451 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
452elif [ $ERRORS -ne 0 ]; then
453 echo "${RED}Installation encountered errors${CLEAR}"
454 echo "${RED}Errors: $ERRORS${CLEAR}"
455 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
456else
457 success "Installation completed successfully"
458fi
459echo ""