summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/mkefidisk.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/contrib/mkefidisk.sh')
-rwxr-xr-xscripts/contrib/mkefidisk.sh255
1 files changed, 255 insertions, 0 deletions
diff --git a/scripts/contrib/mkefidisk.sh b/scripts/contrib/mkefidisk.sh
new file mode 100755
index 0000000000..38e22176e3
--- /dev/null
+++ b/scripts/contrib/mkefidisk.sh
@@ -0,0 +1,255 @@
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
21#
22# Defaults
23#
24# 20 Mb for the boot partition
25BOOT_SIZE=20
26# 5% for swap
27SWAP_RATIO=5
28
29function usage() {
30 echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
31 echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
32 echo " HDDIMG: The hddimg file to generate the efi disk from"
33 echo " TARGET_DEVICE: The device the target will boot from, e.g. /dev/mmcblk0"
34}
35
36function image_details() {
37 IMG=$1
38 echo "Image details"
39 echo "============="
40 echo " image: $(stat --printf '%N\n' $IMG)"
41 echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
42 echo " modified: $(stat -L --printf '%y\n' $IMG)"
43 echo " type: $(file -L -b $IMG)"
44 echo ""
45}
46
47function device_details() {
48 DEV=$1
49 BLOCK_SIZE=512
50
51 echo "Device details"
52 echo "=============="
53 echo " device: $DEVICE"
54 if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
55 echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
56 else
57 echo " vendor: UNKOWN"
58 fi
59 if [ -f "/sys/class/block/$DEV/device/model" ]; then
60 echo " model: $(cat /sys/class/block/$DEV/device/model)"
61 else
62 echo " model: UNKNOWN"
63 fi
64 if [ -f "/sys/class/block/$DEV/size" ]; then
65 echo " size: $[$(cat /sys/class/block/$DEV/size)*BLOCK_SIZE] bytes"
66 else
67 echo " size: UNKNOWN"
68 fi
69 echo ""
70}
71
72
73#
74# Parse and validate arguments
75#
76if [ $# -ne 3 ]; then
77 usage
78 exit 1
79fi
80
81DEVICE=$1
82HDDIMG=$2
83TARGET_DEVICE=$3
84
85if [ ! -w "$DEVICE" ]; then
86 echo "ERROR: Device $DEVICE does not exist or is not writable"
87 usage
88 exit 1
89fi
90
91if [ ! -e "$HDDIMG" ]; then
92 echo "ERROR: HDDIMG $HDDIMG does not exist"
93 usage
94 exit 1
95fi
96
97
98#
99# Check if any $DEVICE partitions are mounted
100#
101grep -q $DEVICE /proc/mounts
102if [ $? -eq 0 ]; then
103 echo "ERROR: $DEVICE partitions mounted:"
104 grep $DEVICE /proc/mounts | cut -f 1 -d " "
105 echo "Unmount the partitions listed and try again."
106 exit 1
107fi
108
109
110#
111# Confirm device with user
112#
113image_details $HDDIMG
114device_details $(basename $DEVICE)
115echo -n "Prepare EFI image on $DEVICE [y/N]? "
116read RESPONSE
117if [ "$RESPONSE" != "y" ]; then
118 echo "Image creation aborted"
119 exit 0
120fi
121
122
123#
124# Partition $DEVICE
125#
126DEVICE_SIZE=$(parted $DEVICE unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//")
127SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
128ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
129ROOTFS_START=$((BOOT_SIZE))
130ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
131SWAP_START=$((ROOTFS_END))
132
133# MMC devices are special in a couple of ways
134# 1) they use a partition prefix character 'p'
135# 2) they are detected asynchronously (need ROOTWAIT)
136PART_PREFIX=""
137if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ]; then
138 PART_PREFIX="p"
139fi
140BOOTFS=$DEVICE${PART_PREFIX}1
141ROOTFS=$DEVICE${PART_PREFIX}2
142SWAP=$DEVICE${PART_PREFIX}3
143
144ROOTWAIT=""
145TARGET_PART_PREFIX=""
146if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
147 TARGET_PART_PREFIX="p"
148 ROOTWAIT="rootwait"
149fi
150TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
151TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
152
153echo "*****************"
154echo "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
155echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
156echo "Swap partition size: $SWAP_SIZE MB ($SWAP)"
157echo "*****************"
158echo "Deleting partition table on $DEVICE ..."
159dd if=/dev/zero of=$DEVICE bs=512 count=2
160
161echo "Creating new partition table (GPT) on $DEVICE ..."
162parted $DEVICE mklabel gpt
163
164echo "Creating boot partition on $BOOTFS"
165parted $DEVICE mkpart primary 0% $BOOT_SIZE
166
167echo "Creating ROOTFS partition on $ROOTFS"
168parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END
169
170echo "Creating swap partition on $SWAP"
171parted $DEVICE mkpart primary $SWAP_START 100%
172
173parted $DEVICE print
174
175
176#
177# Format $DEVICE partitions
178#
179echo ""
180echo "Formatting $BOOTFS as vfat..."
181mkfs.vfat $BOOTFS
182
183echo "Formatting $ROOTFS as ext3..."
184mkfs.ext3 $ROOTFS
185
186echo "Formatting swap partition...($SWAP)"
187mkswap $SWAP
188
189
190#
191# Installing to $DEVICE
192#
193echo ""
194echo "Mounting images and device in preparation for installation..."
195TMPDIR=$(mktemp -d mkefidisk-XXX)
196if [ $? -ne 0 ]; then
197 echo "ERROR: Failed to create temporary mounting directory."
198 exit 1
199fi
200HDDIMG_MNT=$TMPDIR/hddimg
201HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
202ROOTFS_MNT=$TMPDIR/rootfs
203BOOTFS_MNT=$TMPDIR/bootfs
204mkdir $HDDIMG_MNT
205mkdir $HDDIMG_ROOTFS_MNT
206mkdir $ROOTFS_MNT
207mkdir $BOOTFS_MNT
208
209mount -o loop $HDDIMG $HDDIMG_MNT
210mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT
211mount $ROOTFS $ROOTFS_MNT
212mount $BOOTFS $BOOTFS_MNT
213
214echo "Copying ROOTFS files..."
215cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT
216
217echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
218
219# We dont want udev to mount our root device while we're booting...
220if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
221 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
222fi
223
224umount $ROOTFS_MNT
225umount $HDDIMG_ROOTFS_MNT
226
227echo "Preparing boot partition..."
228EFIDIR="$BOOTFS_MNT/EFI/BOOT"
229mkdir -p $EFIDIR
230GRUBCFG="$EFIDIR/grub.cfg"
231
232cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT
233# Copy the efi loader and config (booti*.efi and grub.cfg)
234cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR
235
236# Update grub config for the installed image
237# Delete the install entry
238sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
239# Delete the initrd lines
240sed -i "/initrd /d" $GRUBCFG
241# Delete any LABEL= strings
242sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
243# Replace the ramdisk root with the install device and include other options
244sed -i "s@ root=[^ ]*@ root=$TARGET_ROOTFS rw $ROOTWAIT quiet@" $GRUBCFG
245
246# Provide a startup.nsh script for older firmware with non-standard boot
247# directories and paths.
248echo "bootia32.efi" > $BOOTFS_MNT/startup.nsh
249
250umount $BOOTFS_MNT
251umount $HDDIMG_MNT
252rm -rf $TMPDIR
253sync
254
255echo "Installation complete."