summaryrefslogtreecommitdiffstats
path: root/scripts/genhdimg
blob: 58d4e38435b89796dbcebeeb0a90a92fafee24d7 (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
#!/bin/bash

# Create a bootable harddisk (or compact flash) image from
# a partition definition given from command line
#
# (C) 2006 Pengutronix
#


Usage() {
cat <<-EOF

Usage: `basename "$0"` OPTIONS <svn rep>

    -h              this help
    -m <stage1>     path to grub stage1
    -n <stafe2>     path to grub stage2
    -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 [ ! -f "$from" ]; then
		echo "$from does not exist"
		exit 1
	fi

	fromsize=$(stat --printf "%s" "$from")

	if [ $(($fromsize)) -gt $(($blocks * 512)) ]; then
		echo "warning: $from truncated. Need $fromsize bytes, but have only $(($blocks * 512))!"
	fi

	cat $from /dev/zero | dd bs=512 count=$(($blocks)) >> "$to" 2>/dev/null
}


partcount=0

while getopts "hm:n:o:p:" OPT
do
    case "$OPT" in
        h)  Usage
	    exit 1
            ;;
	m)  STAGE1="$OPTARG"
	    ;;
	n)  STAGE2="$OPTARG"
	    ;;
	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

IMAGETMP=$(mktemp)

# get the first 446 bytes of grub stage1 or from /dev/zero
if [ -n "$STAGE1" ]; then
	echo "using stage1 file $STAGE1"
	if [ ! -r "$STAGE1" ]; then
		echo "cannot open stage1 file $STAGE1"
		exit 1
	fi

	dd if="$STAGE1" bs=446 count=1 > $IMAGEFILE 2>/dev/null
else
	dd if=/dev/zero bs=446 count=1 > $IMAGEFILE 2>/dev/null
fi

# add offset to first partition and fill it with grub stage2 if given
offset=$(( $(echo ${PART[1]} | awk -F: '{print $1}') - 1))
if [ -n "$STAGE2" ]; then
	echo "using stage2 file $STAGE2"
	if [ ! -r "$STAGE2" ]; then
		echo "cannot open stage2 file $STAGE2"
		exit 1
	fi
	pad "$STAGE2" "$IMAGETMP" $(($offset))
else
	cat /dev/zero | dd bs=512 count=$offset > "$IMAGETMP" 2>/dev/null
fi

# append the partitions

# mark the first partition bootable
extraarg="-c"

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

	echo "Generating partition $i (start=$pstart, size=$psize type=$ptype)"

	# 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 [ -n "$pimage" ]; then
		echo "Initializing partition $i with $pimage"
		pad "$pimage" "$IMAGETMP" $(($psize))
	fi

	extraarg=""
done

cat $IMAGETMP >> $IMAGEFILE
rm -f $IMAGETMP