summaryrefslogtreecommitdiffstats
path: root/scripts/genhdimg
blob: 4c4bc2e313f26b45747a89214ec8eacae2c176f9 (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
#!/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
}

part_opts() {
	pstart=$(( $(echo ${PART[$1]} | awk -F: '{print $1}') ))
	pend=$(( $(echo ${PART[$1]} | awk -F: '{print $2}') ))
	ptype=$(( $(echo ${PART[$1]} | awk -F: '{print $3}') ))
	pimage=$(echo ${PART[$1]} | awk -F: '{print $4}')
	psize=$(($pend - $pstart + 1))
}

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

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 1 2 3 4; do
	part_opts "$i"

	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

	extraarg=""
done

for i in 1 2 3 4; do
	part_opts "$i"

	if [ -z "${pimage}" ]; then
		continue
	fi

	echo "    Initializing partition $i with '$(basename $pimage)'"
	dd if="$pimage" of="$IMAGEFILE" bs=512 count="$psize" seek="$pstart" conv=notrunc 2>/dev/null
done