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
|
#!/bin/bash
# This script generates SD card disk images suitable for use with QEMU.
#
# Copyright (C) 2011 Ash Charles
# Based on:
# Narcissus - Online image builder for the angstrom distribution
# Copyright (C) 2008 - 2011 Koen Kooi
# Copyright (C) 2010 Denys Dmytriyenko
# and
# Linaro Images Tools.
# Author: Guilherme Salgado <guilherme.salgado@linaro.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
LC_ALL=C
set -e
function usage()
{
echo "This utility generates SD card images suitable for use with QEMU."
echo "Usage:"
echo " $0 <output name> <mlo> <u-boot> <kernel> <rootfs>"
echo "Example:"
echo " $0 sd.img ~/MLO ~/u-boot.bin ~/uImage ~/rootfs.tar.bz2"
}
function check_args()
{
if [ $# -ne 9 ]; then
usage
exit 1
fi
OUTFILE=$1
MLO=$2
UBOOT=$3
KERNEL=$4
ROOTFS=$5
QEMU_IMG=$6
UENVTXT=$7
BOOTTXT=$8
BOOTSCR=$9
if ! [[ -e ${MLO} ]]; then
echo "MLO not found at ${MLO}! Quitting..."
exit 1
fi
if ! [[ -e ${UBOOT} ]]; then
echo "U-boot not found at ${UBOOT}! Quitting..."
exit 1
fi
if ! [[ -e ${KERNEL} ]]; then
echo "Kernel not found at ${KERNEL}! Quitting..."
exit 1
fi
if ! [[ -e ${ROOTFS} ]]; then
echo "Rootfs not found at ${ROOTFS}! Quitting..."
exit 1
fi
if ! [[ -e ${QEMU_IMG} ]]; then
echo "Qemu-img not found at ${QEMU_IMG}! Quitting..."
exit 1
fi
if ! [[ -e ${UENVTXT} ]]; then
echo "UENVTXT not found at ${UENVTXT}! Quitting..."
exit 1
fi
if ! [[ -e ${BOOTTXT} ]]; then
echo "BOOTTXT not found at ${BOOTTXT}! Quitting..."
exit 1
fi
if ! [[ -e ${BOOTSCR} ]]; then
echo "BOOTSCR not found at ${BOOTSCR}! Quitting..."
exit 1
fi
}
SIZE=1073741824 # 1G by default
function make_image()
{
$QEMU_IMG create -f raw ${OUTFILE} ${SIZE}
CYLINDERS=`echo ${SIZE}/255/63/512 | bc`
{
echo ,9,0x0C,*
echo ,,,-
} | sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${OUTFILE} &> /dev/null
# Reverse-engineer the partition setup
BYTES_PER_SECTOR="$(/sbin/fdisk -l -u ${OUTFILE} | grep Units | awk '{print $9}')"
VFAT_SECTOR_OFFSET="$(/sbin/fdisk -l -u ${OUTFILE} | grep FAT32 | awk '{print $3}')"
EXT3_SECTOR_OFFSET="$(/sbin/fdisk -l -u ${OUTFILE} | grep Linux | awk '{print $2}')"
}
function populate_image()
{
LOOP_DEV="/dev/loop1"
LOOP_DEV_FS="/dev/loop2"
echo "[ Format vfat partition ]"
sudo /sbin/losetup -v -o $(expr ${BYTES_PER_SECTOR} "*" ${VFAT_SECTOR_OFFSET}) ${LOOP_DEV} ${OUTFILE}
sudo mkfs.vfat -F 32 -n "boot" ${LOOP_DEV}
echo "[ Format ext3 partition ]"
sudo /sbin/losetup -v -o $(expr ${BYTES_PER_SECTOR} "*" ${EXT3_SECTOR_OFFSET}) ${LOOP_DEV_FS} ${OUTFILE}
sudo /sbin/mkfs.ext3 -L rootfs ${LOOP_DEV_FS}
echo "[ Copying files to vfat ]"
sudo mount ${LOOP_DEV} /mnt
sudo cp -v ${MLO} /mnt/MLO
sudo cp -v ${UBOOT} /mnt/u-boot.bin
sudo cp -v ${KERNEL} /mnt/uImage
sudo cp -v ${UENVTXT} /mnt/uEnv.txt
sudo cp -v ${BOOTTXT} /mnt/boot.txt
sudo cp -v ${BOOTSCR} /mnt/boot.scr
sudo sync
sudo umount ${LOOP_DEV}
echo "[ Copying file system ]"
sudo mount ${LOOP_DEV_FS} /mnt
sudo tar xaf ${ROOTFS} -C /mnt
sudo sync
sudo umount ${LOOP_DEV_FS}
echo "[ Clean up ]"
sudo /sbin/losetup -d ${LOOP_DEV}
sudo /sbin/losetup -d ${LOOP_DEV_FS}
}
ARGS=$*
check_args $ARGS
make_image
populate_image
|