1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
#!/bin/bash
# linaro-media-create - Create an installation media from a Linaro image
# Copyright 2010 Robert Nelson <robertcnelson@gmail.com>
# Copyright 2010 Linaro
# Based on rcn's setup_sdcard.sh script.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
unset MMC MMC1 MMC2 MMC3 IMAGE_FILE HWPACK_FILE
#Defaults
RFS=ext3
BINARY_TARBALL='binary-tar.tar.gz'
BOOT_LABEL=boot
RFS_LABEL=rootfs
CODENAME="Chessy 10.05"
IS_LIVE=
FAT_SIZE=32
IMAGE_SIZE=2G
DIR=$PWD
TMP_DIR=$(mktemp -d)
BOOT_DISK=${DIR}/boot-disc
ROOT_DISK=${DIR}/root-disc
ensure_command() {
# ensure_command foo foo-package
which "$1" 2>/dev/null 1>/dev/null || ( echo "Installing required command $1 from package $2" && sudo apt-get install "$2" )
}
ensure_command sfdisk util-linux
ensure_command fdisk util-linux
usage() {
echo "usage: $(basename $0) --mmc /dev/sdd"
echo "<or>"
echo "usage: $(basename $0) --image_file mmc.img"
cat <<EOF
required options:
--mmc </dev/sdX>
Unformated MMC Card
<or>
--image_file <xxx>
specify name of image file
--dev <board>
use development boot options; this includes setting up serial ttys as well
as enabling normal debug options for the target board. Current board values:
* beagle
* igep
* vexpress
Additional/Optional options:
-h --help
this help
--rootfs <fs_type>
ext2
ext3 - <set as default>
ext4
btrfs
--boot_label <boot_label>
boot partition label
--rfs_label <rfs_label>
rootfs partition label
--swap_file <xxx>
Creats a Swap file of (xxx)MB's
--live
Create boot command for casper/live images; if this is not
provided a UUID for the rootfs is generated and used as the root=
option
--live-256m
Create boot command for casper/live images; adds only-ubiquity option
to allow use of live installer on boards with 256M memory - like beagle
--console <ttyXY>
add a console to kernel boot parameter; this parameter can be defined
multiple times.
--hwpack <filename>
A hardware pack that should be installed in the rootfs.
--image_size nnnG or nnnM
specify size of SD image to create; use with --image_file only (default: 2G)
--binary <filename>
specify file used to create the bootable system. Default binary-tar.tar.gz
EOF
exit
}
check_mmc() {
FDISK=$(sudo LC_ALL=C sfdisk -l | grep "[Disk] ${MMC}" | awk '{print $2}')
if test "-$FDISK-" = "-$MMC:-"
then
echo ""
echo "I see..."
echo "sudo sfdisk -l:"
sudo LC_ALL=C sfdisk -l | grep "[Disk] /dev/" --color=never
echo ""
echo "mount:"
mount | grep -v none | grep "/dev/" --color=never
echo ""
read -p "Are you 100% sure, on selecting [${MMC}] (y/n)? "
[ "$REPLY" == "y" ] || exit
echo ""
else
echo ""
echo "Are you sure? I Don't see [${MMC}], here is what I do see..."
echo ""
echo "sudo sfdisk -l:"
sudo LC_ALL=C sfdisk -l | grep "[Disk] /dev/" --color=never
echo ""
echo "mount:"
mount | grep -v none | grep "/dev/" --color=never
echo ""
exit
fi
}
check_fs_type() {
case "$RFS" in
ext2|ext3|ext4|btrfs)
:
;;
*)
usage
;;
esac
}
checkparm() {
if [ "$(echo $1|grep ^'\-')" ];then
echo "E: Need an argument"
usage
fi
}
consoles=""
# parse commandline options
while [ ! -z "$1" ]; do
case $1 in
-h|--help)
usage
;;
--hwpack)
checkparm $2
HWPACK_FILE="$2"
;;
--mmc)
checkparm $2
MMC="$2"
check_mmc
;;
--image_file)
checkparm $2
IMAGE_FILE="$2"
;;
--image_size)
checkparm $2
IMAGE_SIZE=$2
;;
--rootfs)
checkparm $2
RFS="$2"
check_fs_type
;;
--boot_label)
checkparm $2
BOOT_LABEL="$2"
;;
--rfs_label)
checkparm $2
RFS_LABEL="$2"
;;
--swap_file)
checkparm $2
SWAP_SIZE="$2"
CREATE_SWAP=1
;;
--live)
IS_LIVE=1
;;
--live-256m)
IS_LIVE=1
IS_LOWMEM=1
;;
--console)
checkparm $2
consoles="$consoles $2"
;;
--dev)
checkparm $2
DEVIMAGE=$2
;;
--binary)
checkparm $2
BINARY_TARBALL="$2"
;;
esac
shift
done
ensure_command uuidgen uuid-runtime
ensure_command parted parted
ensure_command wget wget
ensure_command md5sum coreutils
ensure_command realpath realpath
case "$RFS" in
ext2|ext3|ext4)
ensure_command "mkfs.$RFS" e2fsprogs
;;
btrfs)
ensure_command mkfs.btrfs btrfs-tools
;;
esac
case $DEVNAME in
beagle|igep|vexpress)
ensure_command mkimage uboot-mkimage
;;
esac
RFS_UUID=`uuidgen -r`
get_mmcs_by_id() {
if [ ! ${IMAGE_FILE} ]; then
for device in /dev/disk/by-id/*; do
if [ `realpath $device` = $MMC ]; then
if echo "$device" | grep -q -- "-part[0-9]*$"; then
echo "device $MMC must not be a partition part ($device)" 1>&2
exit 1
fi
for part_id in `ls "$device-part"*`; do
part=`realpath $part_id`
part_no=`echo $part_id | sed -e 's/.*-part//g'`
# echo "part $part_no found: $part_id" 1>&2
if test "$part_no" = 1; then
MMC1=$part
elif test "$part_no" = 2; then
MMC2=$part
elif test "$part_no" = 3; then
MMC3=$part
fi
done
break
fi
done
fi
}
install_hwpack() {
ensure_command qemu-arm-static qemu-arm-static
chroot=${DIR}/binary
# Make sure we unmount /proc in the chroot or else it can't be moved to the
# rootfs.
trap "sudo umount ${chroot}/proc || true" EXIT
# XXX: Assume linaro-hwpack-install lives on the same directory as this
# script. This is far from optimal but should do for now.
LINARO_HWPACK_INSTALL=$(dirname $0)/linaro-hwpack-install
sudo mv -f ${chroot}/etc/resolv.conf ${TMP_DIR}/resolv.conf.orig
sudo cp /etc/resolv.conf ${chroot}/etc/resolv.conf
sudo mv -f ${chroot}/etc/hosts ${TMP_DIR}/hosts.orig
sudo cp /etc/hosts ${chroot}/etc/hosts
sudo cp /usr/bin/qemu-arm-static ${chroot}/usr/bin
sudo cp $LINARO_HWPACK_INSTALL ${chroot}/usr/bin
sudo cp "$HWPACK_FILE" "$chroot"
# Actually install the hwpack.
sudo mount proc ${chroot}/proc -t proc
sudo chroot "$chroot" linaro-hwpack-install /$(basename "$HWPACK_FILE")
# Revert some changes we did to the rootfs as we don't want them in the
# image.
sudo umount ${chroot}/proc
sudo mv -f ${TMP_DIR}/resolv.conf.orig ${chroot}/etc/resolv.conf
sudo mv -f ${TMP_DIR}/hosts.orig ${chroot}/etc/hosts
sudo rm -f ${chroot}/usr/bin/qemu-arm-static
sudo rm -f ${chroot}/usr/bin/linaro-hwpack-install
sudo rm -f ${chroot}/$(basename "$HWPACK_FILE")
}
unpack_binary_tarball() {
# Remove the binary/ directory so that previous runs don't interfere here.
remove_binary_dir
sudo tar -xf $BINARY_TARBALL
}
create_boot_cmd() {
if [ "$IS_LIVE" ]; then
boot_snippet='boot=casper'
[ "$IS_LOWMEM" ] && lowmem_opt=only-ubiquity
else
boot_snippet='root=UUID='${RFS_UUID}
fi
if [ "${IMAGE_FILE}" ]; then
cat > ${TMP_DIR}/boot.cmd << BOOTCMD
setenv bootcmd 'mmc init; fatload mmc 0:1 0x80000000 uImage; bootm 0x80000000'
setenv bootargs '${serial_opts} ${splash_opts} earlyprintk fixrtc nocompcache ${lowmem_opt} root=/dev/mmcblk0p2 rootwait rw vram=12M omapfb.debug=y omapfb.mode=dvi:1280x720MR-16@60'
boot
BOOTCMD
else
cat > ${TMP_DIR}/boot.cmd << BOOTCMD
setenv bootcmd 'mmc init; fatload mmc 0:1 0x80000000 uImage; fatload mmc 0:1 0x81600000 uInitrd; bootm 0x80000000 0x81600000'
setenv bootargs '${serial_opts} ${splash_opts} earlyprintk fixrtc nocompcache ${lowmem_opt} ${boot_snippet} rootwait ro vram=12M omapfb.debug=y omapfb.mode=dvi:1280x720MR-16@60'
boot
BOOTCMD
fi
}
cleanup_sd() {
echo ""
echo "Umounting Partitions"
echo ""
if test -n "$MMC1"; then
sudo umount ${MMC1} &> /dev/null || true
fi
if test -n "$MMC2"; then
sudo umount ${MMC2} &> /dev/null || true
fi
if [ "${MMC}" ]; then
sudo parted -s ${MMC} mklabel msdos
fi
}
create_partitions() {
if [ "${IMAGE_FILE}" ]; then
partdev=${IMAGE_FILE}
else
partdev=${MMC}
fi
if [ "$FAT_SIZE" = "32" ]; then
PARTITION_TYPE="0x0C"
else
PARTITION_TYPE="0x0E"
fi
# Create a VFAT or FAT16 partition of 9 cylinders which is about 64M
# and a linux partition of the rest
sudo sfdisk -D -H $HEADS -S $SECTORS $CYLINDER_ARG $partdev << THEEND
,9,$PARTITION_TYPE,*
,,,-
THEEND
if [ "${IMAGE_FILE}" ]; then
VFATOFFSET=$(($(fdisk -l -u $IMAGE_FILE | grep FAT | awk '{print $3}')*512))
VFATSIZE=$(($(fdisk -l -u $IMAGE_FILE | grep FAT | awk '{print $5}')*1024))
ROOTOFFSET=$(($(fdisk -l -u $IMAGE_FILE | grep Linux | awk '{print $2}')*512))
ROOTSIZE2=$(($(fdisk -l -u $IMAGE_FILE | grep Linux | awk '{print $3}')))
ROOTSIZE1=$(($(fdisk -l -u $IMAGE_FILE | grep Linux | awk '{print $2}')))
ROOTSIZE=$((((ROOTSIZE2-ROOTSIZE1)/2)*1024))
MMC1=$(sudo losetup -f --show $IMAGE_FILE --offset $VFATOFFSET --sizelimit $VFATSIZE)
MMC2=$(sudo losetup -f --show $IMAGE_FILE --offset $ROOTOFFSET --sizelimit $ROOTSIZE)
fi
}
prepare_partitions() {
echo ""
echo "Formating Boot Partition"
echo ""
sudo mkfs.vfat -F ${FAT_SIZE} ${MMC1} -n ${BOOT_LABEL}
echo ""
echo "Formating ${RFS} Partition"
echo ""
sudo mkfs.${RFS} -U "$RFS_UUID" ${MMC2} -L ${RFS_LABEL}
}
populate_boot() {
echo ""
echo "Populating Boot Partition"
echo ""
echo ""
echo "Installing Boot Loader"
echo ""
if [ "$IS_LIVE" ]; then
parts_dir=casper
else
parts_dir=boot
fi
mkdir -p ${BOOT_DISK}
sudo mount ${MMC1} ${BOOT_DISK}
case "$DEVIMAGE" in
beagle|igep)
if [ "$DEVIMAGE" = "beagle" ]; then
sudo cp -v binary/usr/lib/x-loader-omap/MLO \
binary/usr/lib/u-boot/omap3_beagle/u-boot.bin \
${BOOT_DISK}
fi
sync
echo "done"
sudo mkimage -A arm -O linux -T kernel -C none -a 0x80008000 \
-e 0x80008000 -n Linux \
-d "${DIR}/binary/${parts_dir}"/vmlinuz-*-linaro-omap \
"${BOOT_DISK}/uImage"
sudo mkimage -A arm -O linux -T ramdisk -C none -a 0 \
-e 0 -n initramfs \
-d "${DIR}/binary/${parts_dir}"/initrd.img-*-linaro-omap \
"${BOOT_DISK}/uInitrd"
sudo mkimage -A arm -O linux -T script -C none -a 0 \
-e 0 -n "$CODENAME" -d "${TMP_DIR}/boot.cmd" \
"${BOOT_DISK}/boot.scr"
sudo cp -v ${BOOT_DISK}/boot.scr ${BOOT_DISK}/boot.ini
;;
vexpress)
sudo cp -v binary/usr/lib/u-boot/ca9x4_ct_vxp/u-boot.bin ${BOOT_DISK}
sudo mkimage -A arm -O linux -T kernel -C none -a 0x60008000 \
-e 0x60008000 -n Linux \
-d "${DIR}/binary/${parts_dir}"/vmlinuz-*-linaro-vexpress \
"${BOOT_DISK}/uImage"
sudo mkimage -A arm -O linux -T ramdisk -C none -a 0x81000000 \
-e 0x81000000 -n initramfs \
-d "${DIR}/binary/${parts_dir}"/initrd.img-*-linaro-vexpress \
"${BOOT_DISK}/uInitrd"
;;
*)
echo "Set --dev parameter: --dev <beagle|igep|vexpress>" 1>&2
exit
;;
esac
sync
sync
sudo umount ${BOOT_DISK} || true
}
populate_rootfs() {
echo ""
echo "Populating rootfs Partition"
echo "Be patient, this may take a few minutes"
echo ""
mkdir -p ${ROOT_DISK}
sudo mount ${MMC2} ${ROOT_DISK}
sudo mv ${DIR}/binary/* $ROOT_DISK
# add fstab entry for rootfs and boot
echo "UUID=${RFS_UUID} / ${RFS} errors=remount-ro 0 1 " | sudo tee -a ${ROOT_DISK}/etc/fstab
if [ "$CREATE_SWAP" ] ; then
echo ""
echo "Creating SWAP File"
echo ""
SPACE_LEFT=$(df ${ROOT_DISK} | grep ${MMC2} | awk '{print $4}')
let SIZE=$SWAP_SIZE*1024
if [ $SPACE_LEFT -ge $SIZE ] ; then
sudo dd if=/dev/zero of=${ROOT_DISK}/SWAP.swap bs=1M count=$SWAP_SIZE
sudo mkswap ${ROOT_DISK}/SWAP.swap
echo "/SWAP.swap none swap sw 0 0" | sudo tee -a ${ROOT_DISK}/etc/fstab
else
echo "SWAP file bigger then whats left on partition"
fi
fi
echo ""
echo "Creating /etc/flash-kernel.conf"
echo ""
echo "UBOOT_PART=/dev/mmcblk0p1" | sudo tee ${ROOT_DISK}/etc/flash-kernel.conf >/dev/null
sync
sync
sudo umount ${ROOT_DISK} || true
}
calculatesize() {
IMAGE_SIZE=${IMAGE_SIZE/G/M*1024}
IMAGE_SIZE=${IMAGE_SIZE/M/K*1024}
IMAGE_SIZE=${IMAGE_SIZE/K/*1024}
IMAGE_SIZE=$(($IMAGE_SIZE))
}
setup_sizes() {
HEADS=255
SECTORS=63
SECTORSIZE=512
if [ "${IMAGE_FILE}" ]; then
calculatesize
CYLINDERSIZE=$(($HEADS*$SECTORS*$SECTORSIZE))
CYLINDERS=$(($IMAGE_SIZE/$CYLINDERSIZE))
CYLINDER_ARG="-C $CYLINDERS"
IMAGE_SIZE=$(($CYLINDERS*$CYLINDERSIZE))
else
CYLINDER_ARG=""
fi
}
setup_image() {
sudo qemu-img create -f raw $IMAGE_FILE $IMAGE_SIZE
}
remove_binary_dir() {
if [ -d binary/ ]; then
sudo rm -rf binary/
fi
}
setup_sizes
if [ "${IMAGE_FILE}" ]; then
setup_image
fi
serial_opts=""
if [ "$consoles" ]; then
for c in ${consoles}; do
serial_opts="$serial_opts console=$c"
done
if [ "$IS_LIVE" ]; then
serial_opts="$serial_opts serialtty=ttyS2"
fi
fi
if [ "$DEVIMAGE" ]; then
case "$DEVIMAGE" in
beagle|igep)
serial_opts="$serial_opts console=tty0 console=ttyS2,115200n8"
if [ "$IS_LIVE" ]; then
serial_opts="$serial_opts serialtty=ttyS2"
fi
;;
vexpress)
serial_opts="$serial_opts console=tty0 console=ttyAMA0,38400n8"
if [ "$IS_LIVE" ]; then
serial_opts="$serial_opts serialtty=ttyAMA0"
fi
# ARM Boot Monitor is used to load u-boot, uImage etc. into flash and
# only allows for FAT16
FAT_SIZE=16
;;
*)
echo "unknown --dev parameter: $DEVIMAGE" 1>&2
;;
esac
else
if [ "$IS_LIVE" ]; then
splash_opts="quiet splash"
fi
fi
if [ ! "${MMC}" -a ! "${IMAGE_FILE}" ]; then
usage
fi
unpack_binary_tarball
if [ "$HWPACK_FILE" ]; then
install_hwpack
fi
create_boot_cmd
get_mmcs_by_id
cleanup_sd
create_partitions
echo -n "waiting for partitioning to settle ..."
sync
sleep 3
echo "done."
get_mmcs_by_id
if test -z "$MMC1" -o -z "$MMC2"; then
echo "MMC1: $MMC1 nor MMC2: $MMC2 must be empty"
exit 2
fi
prepare_partitions
populate_boot
populate_rootfs
if [ "${IMAGE_FILE}" ]; then
echo "Create ${IMAGE_FILE}.gz"
gzip -f ${IMAGE_FILE} > ${IMAGE_FILE}.gz
fi
remove_binary_dir
|