blob: 84d2501a010705f9817e559d94f0f6266b6812fb (
plain)
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
|
#!/bin/bash
# Create a bootable harddisk (or compact flash) image from
# a partition definition given from command line
#
# (C) 2006, 2011 Pengutronix
#
Usage() {
cat <<-EOF
Usage: `basename "$0"` OPTIONS <svn rep>
-h this help
-o <hdimg file> resulting hd image
-i <img file> filesystem image
-p <def> partition definition (can be given multiple times)
A partition definition has the form
start_sector:end_sector:partition_type:[imagefile]
EOF
}
# usage: pad from to blocks
# cat $from to $to and pad the output to $blocks * 512 bytes
pad() {
from="$1"
to="$2"
blocks="$3"
if [ ! -e "$from" ]; then
echo "$from does not exist"
exit 1
fi
fromsize=$(stat -c "%s" "$from")
if [ $(($fromsize)) -gt $(($blocks * 512)) ]; then
echo "warning: $from truncated. Need $fromsize bytes, but have only $(($blocks * 512))!"
fi
tosize=$(stat -c "%s" "$to")
toblocks_before=$(( $tosize / 512 ))
dd if="$from" bs=512 count=$blocks conv=sync >> "$to" 2> /dev/null
tosize=$(stat -c "%s" "$to")
toblocks=$(( $tosize / 512 ))
blocksleft=$(( $blocks - $toblocks + $toblocks_before ))
if [ $blocksleft -gt 0 ]; then
dd if=/dev/zero bs=512 count=$blocksleft >> "$to" 2>/dev/null
fi
}
partcount=0
while getopts "hm:n:o:p:" OPT
do
case "$OPT" in
h) Usage
exit 1
;;
o) IMAGEFILE="$OPTARG"
;;
p) partcount=$(($partcount + 1))
PART[$partcount]="$OPTARG"
;;
esac
done
if [ "$partcount" == 0 ]; then
echo "need at least one partition definition"
exit 1
fi
if [ "$partcount" -gt 4 ]; then
echo "a maximum of four partitions is allowed"
exit 1
fi
if [ -z "$IMAGEFILE" ]; then
echo "no output image file given"
exit 1
fi
IMAGETMP1=$(mktemp -p ${PTXDIST_TEMPDIR})
IMAGETMP2=$(mktemp -p ${PTXDIST_TEMPDIR})
IMAGETMP3=$(mktemp -p ${PTXDIST_TEMPDIR})
IMAGETMP4=$(mktemp -p ${PTXDIST_TEMPDIR})
echo "------------------------"
echo "Creating hard disk's MBR"
echo "------------------------"
# create the first 512 byte sector (MBR)
# fill it up to offset 446
dd if=/dev/zero bs=446 count=1 > $IMAGEFILE 2>/dev/null
# append the partitions
# mark the first partition bootable
extraarg="-c"
echo "------------------------------------"
echo "Creating hard disk's partition table"
echo "------------------------------------"
for i in $(seq 4); do
pstart=$(( $(echo ${PART[$i]} | awk -F: '{print $1}') ))
pend=$(( $(echo ${PART[$i]} | awk -F: '{print $2}') ))
ptype=$(( $(echo ${PART[$i]} | awk -F: '{print $3}') ))
pimage=$(echo ${PART[$i]} | awk -F: '{print $4}')
psize=$(($pend - $pstart + 1))
if [ $i -gt $partcount ]; then
psize=0
fi
if [ ${psize} -ne 0 ]; then
echo "Partition $i (start=$pstart, size=$psize type=$ptype)"
fi
# add 0x55aa magic to last partition in table
if [ $i == 4 ]; then
extraarg="-m"
fi
genpart $extraarg -b $pstart -s $psize -t $ptype >> $IMAGEFILE
if [ -z "${pimage}" ]; then
pimage=/dev/zero
fi
echo " Initializing partition $i with '$(basename $pimage)'"
eval TMPFILE=\$IMAGETMP${i}
pad "$pimage" "${TMPFILE}" $psize
extraarg=""
done
# add offset to first partition
offset=$(( $(echo ${PART[1]} | awk -F: '{print $1}') - 1))
if [ ${offset} -gt 1 ]; then
echo
echo "-------------------------------------------"
echo "Creating hard disk's first partition offset"
echo "-------------------------------------------"
echo -n "padding ${offset} empty sectors..."
dd if=/dev/zero bs=512 count=$offset >> $IMAGEFILE 2>/dev/null
echo "done"
fi
echo
echo "----------------------------"
echo "Appending partitions content"
echo "----------------------------"
if [ -s ${IMAGETMP1} ]; then
echo -n "adding content for partition 1..."
cat ${IMAGETMP1} >> ${IMAGEFILE}
echo "done"
fi
# FIXME padding sectors?
if [ -s ${IMAGETMP2} ]; then
echo -n "adding content for partition 2..."
cat ${IMAGETMP2} >> ${IMAGEFILE}
echo "done"
fi
# FIXME padding sectors?
if [ -s ${IMAGETMP3} ]; then
echo -n "adding content for partition 3..."
cat ${IMAGETMP3} >> ${IMAGEFILE}
echo "done"
fi
# FIXME padding sectors?
if [ -s ${IMAGETMP4} ]; then
echo -n "adding content for partition 4..."
cat ${IMAGETMP4} >> ${IMAGEFILE}
echo "done"
fi
rm -f ${IMAGETMP1} ${IMAGETMP2} ${IMAGETMP3} ${IMAGETMP4}
|