diff options
Diffstat (limited to 'scripts/contrib')
-rwxr-xr-x | scripts/contrib/mkefidisk.sh | 255 |
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 | ||
25 | BOOT_SIZE=20 | ||
26 | # 5% for swap | ||
27 | SWAP_RATIO=5 | ||
28 | |||
29 | function 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 | |||
36 | function 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 | |||
47 | function 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 | # | ||
76 | if [ $# -ne 3 ]; then | ||
77 | usage | ||
78 | exit 1 | ||
79 | fi | ||
80 | |||
81 | DEVICE=$1 | ||
82 | HDDIMG=$2 | ||
83 | TARGET_DEVICE=$3 | ||
84 | |||
85 | if [ ! -w "$DEVICE" ]; then | ||
86 | echo "ERROR: Device $DEVICE does not exist or is not writable" | ||
87 | usage | ||
88 | exit 1 | ||
89 | fi | ||
90 | |||
91 | if [ ! -e "$HDDIMG" ]; then | ||
92 | echo "ERROR: HDDIMG $HDDIMG does not exist" | ||
93 | usage | ||
94 | exit 1 | ||
95 | fi | ||
96 | |||
97 | |||
98 | # | ||
99 | # Check if any $DEVICE partitions are mounted | ||
100 | # | ||
101 | grep -q $DEVICE /proc/mounts | ||
102 | if [ $? -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 | ||
107 | fi | ||
108 | |||
109 | |||
110 | # | ||
111 | # Confirm device with user | ||
112 | # | ||
113 | image_details $HDDIMG | ||
114 | device_details $(basename $DEVICE) | ||
115 | echo -n "Prepare EFI image on $DEVICE [y/N]? " | ||
116 | read RESPONSE | ||
117 | if [ "$RESPONSE" != "y" ]; then | ||
118 | echo "Image creation aborted" | ||
119 | exit 0 | ||
120 | fi | ||
121 | |||
122 | |||
123 | # | ||
124 | # Partition $DEVICE | ||
125 | # | ||
126 | DEVICE_SIZE=$(parted $DEVICE unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//") | ||
127 | SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) | ||
128 | ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE)) | ||
129 | ROOTFS_START=$((BOOT_SIZE)) | ||
130 | ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE)) | ||
131 | SWAP_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) | ||
136 | PART_PREFIX="" | ||
137 | if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ]; then | ||
138 | PART_PREFIX="p" | ||
139 | fi | ||
140 | BOOTFS=$DEVICE${PART_PREFIX}1 | ||
141 | ROOTFS=$DEVICE${PART_PREFIX}2 | ||
142 | SWAP=$DEVICE${PART_PREFIX}3 | ||
143 | |||
144 | ROOTWAIT="" | ||
145 | TARGET_PART_PREFIX="" | ||
146 | if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then | ||
147 | TARGET_PART_PREFIX="p" | ||
148 | ROOTWAIT="rootwait" | ||
149 | fi | ||
150 | TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2 | ||
151 | TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3 | ||
152 | |||
153 | echo "*****************" | ||
154 | echo "Boot partition size: $BOOT_SIZE MB ($BOOTFS)" | ||
155 | echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)" | ||
156 | echo "Swap partition size: $SWAP_SIZE MB ($SWAP)" | ||
157 | echo "*****************" | ||
158 | echo "Deleting partition table on $DEVICE ..." | ||
159 | dd if=/dev/zero of=$DEVICE bs=512 count=2 | ||
160 | |||
161 | echo "Creating new partition table (GPT) on $DEVICE ..." | ||
162 | parted $DEVICE mklabel gpt | ||
163 | |||
164 | echo "Creating boot partition on $BOOTFS" | ||
165 | parted $DEVICE mkpart primary 0% $BOOT_SIZE | ||
166 | |||
167 | echo "Creating ROOTFS partition on $ROOTFS" | ||
168 | parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END | ||
169 | |||
170 | echo "Creating swap partition on $SWAP" | ||
171 | parted $DEVICE mkpart primary $SWAP_START 100% | ||
172 | |||
173 | parted $DEVICE print | ||
174 | |||
175 | |||
176 | # | ||
177 | # Format $DEVICE partitions | ||
178 | # | ||
179 | echo "" | ||
180 | echo "Formatting $BOOTFS as vfat..." | ||
181 | mkfs.vfat $BOOTFS | ||
182 | |||
183 | echo "Formatting $ROOTFS as ext3..." | ||
184 | mkfs.ext3 $ROOTFS | ||
185 | |||
186 | echo "Formatting swap partition...($SWAP)" | ||
187 | mkswap $SWAP | ||
188 | |||
189 | |||
190 | # | ||
191 | # Installing to $DEVICE | ||
192 | # | ||
193 | echo "" | ||
194 | echo "Mounting images and device in preparation for installation..." | ||
195 | TMPDIR=$(mktemp -d mkefidisk-XXX) | ||
196 | if [ $? -ne 0 ]; then | ||
197 | echo "ERROR: Failed to create temporary mounting directory." | ||
198 | exit 1 | ||
199 | fi | ||
200 | HDDIMG_MNT=$TMPDIR/hddimg | ||
201 | HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs | ||
202 | ROOTFS_MNT=$TMPDIR/rootfs | ||
203 | BOOTFS_MNT=$TMPDIR/bootfs | ||
204 | mkdir $HDDIMG_MNT | ||
205 | mkdir $HDDIMG_ROOTFS_MNT | ||
206 | mkdir $ROOTFS_MNT | ||
207 | mkdir $BOOTFS_MNT | ||
208 | |||
209 | mount -o loop $HDDIMG $HDDIMG_MNT | ||
210 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT | ||
211 | mount $ROOTFS $ROOTFS_MNT | ||
212 | mount $BOOTFS $BOOTFS_MNT | ||
213 | |||
214 | echo "Copying ROOTFS files..." | ||
215 | cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT | ||
216 | |||
217 | echo "$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... | ||
220 | if [ -d $ROOTFS_MNT/etc/udev/ ] ; then | ||
221 | echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist | ||
222 | fi | ||
223 | |||
224 | umount $ROOTFS_MNT | ||
225 | umount $HDDIMG_ROOTFS_MNT | ||
226 | |||
227 | echo "Preparing boot partition..." | ||
228 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" | ||
229 | mkdir -p $EFIDIR | ||
230 | GRUBCFG="$EFIDIR/grub.cfg" | ||
231 | |||
232 | cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT | ||
233 | # Copy the efi loader and config (booti*.efi and grub.cfg) | ||
234 | cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR | ||
235 | |||
236 | # Update grub config for the installed image | ||
237 | # Delete the install entry | ||
238 | sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG | ||
239 | # Delete the initrd lines | ||
240 | sed -i "/initrd /d" $GRUBCFG | ||
241 | # Delete any LABEL= strings | ||
242 | sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG | ||
243 | # Replace the ramdisk root with the install device and include other options | ||
244 | sed -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. | ||
248 | echo "bootia32.efi" > $BOOTFS_MNT/startup.nsh | ||
249 | |||
250 | umount $BOOTFS_MNT | ||
251 | umount $HDDIMG_MNT | ||
252 | rm -rf $TMPDIR | ||
253 | sync | ||
254 | |||
255 | echo "Installation complete." | ||