34421f70f736788a559f26bf4792cbec1ba866b8
[grml-live.git] / scripts / bootgrub.mksh
1 #!/usr/bin/env mksh
2 # $Id$
3 # $miros: src/sys/arch/i386/stand/bootxx/mkbxinst.sh,v 1.19 2009/06/07 17:53:29 tg Exp $
4 #-
5 # Copyright (c) 2007, 2008, 2009
6 #       Thorsten Glaser <tg@mirbsd.org>
7 #
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un‐
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
13 #
14 # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person’s immediate fault when using the work as intended.
22 #-
23 # Self-installing 32-bit x86 boot blocks for GNU GRUB2 on i386-pc
24 # Reads a list of extents (firstblock lastblock) from standard input
25 # and writes bootxx to standard output, which can subsequentially be
26 # stored as partition boot record (or floppy boot sector) on disc.
27
28 set -A thecode 0x66 0x31 0xC9 0xBB 0x00 0x30 0x8E 0xD3 0xBC 0xFC 0xFF 0x66 0x51 0x66 0x9D 0x8E 0xC1 0xBF 0x00 0x7C 0x57 0xB1 0x10 0xF3 0xA4 0x5E 0x8E 0xD9 0xBB 0x00 0x08 0x53 0x53 0x8E 0xC3 0xBF 0x00 0xFE 0xB5 0x02 0xF3 0xA4 0x1F 0x68 0x7D 0xFE 0xCB 0x30 0x41 0x41 0x38 0x20 0x4C 0x6F 0x61 0x64 0x69 0x6E 0x67 0x20 0x00 0x62 0x61 0x64 0x20 0x6D 0x61 0x67 0x69 0x63 0x20 0x65 0x72 0x72 0x6F 0x72 0x0D 0x0A 0x00 0x84 0x02 0x00 0x12 0x00 0x00 0x80 0x77 0xFF 0xB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0xF9 0xCD 0x13 0xFB 0x07 0xBE 0x46 0xFE 0x0F 0x82 0x21 0xFF 0x80 0xE1 0x3F 0x88 0x0E 0x52 0xFE 0x0F 0xB6 0xC6 0x40 0xA3 0x50 0xFE 0x61 0x8B 0x0E 0x52 0xFE 0xE3 0xD7 0x8B 0x44 0x08 0x8B 0x54 0x0A 0xF7 0xF1 0x42 0x31 0xC9 0x87 0xD1 0xF7 0x36 0x50 0xFE 0xC0 0xE4 0x06 0x86 0xC4 0x09 0xC1 0x88 0xD6 0xB8 0x01 0x02 0xC3 0xB4 0x42 0xC3
29 typeset -i ofs_bkcnt=79
30 typeset -i ofs_geomh=80
31 typeset -i ofs_geoms=82
32 typeset -i ofs_partp=84
33 typeset -i ofs_secsz=252
34 typeset -i begptr=378
35 typeset -Uui8 thecode
36
37 typeset -Uui16 curptr=begptr
38 typeset -i wnum=0 wofs=0 wrec=0 bkend=0x1FE
39
40 function do_record {
41         typeset -i blk=$1 cnt=$2 n
42         typeset -Uui16 x=blk y
43
44         (( blk && cnt )) || return
45
46         print -u2 "$wrec @0x${curptr#16#}: $cnt @$blk (0x${x#16#})"
47
48         while (( cnt )); do
49                 let wrec++
50                 (( n = blk < 0x00000100 ? 0 :
51                     blk < 0x00010000 ? 1 :
52                     blk < 0x01000000 ? 2 : 3 ))
53                 (( x = cnt < 33 ? cnt : 32 ))
54                 (( y = blk ))
55                 print -u2 " - 0x${curptr#16#}: $((x)) (0x${x#16#}) @ $blk" \
56                     "(0x${y#16#})"
57                 (( thecode[curptr++] = (n++ << 5) | (x - 1) ))
58                 (( blk += x ))
59                 (( cnt -= x ))
60                 while (( n-- )); do
61                         (( thecode[curptr++] = y & 0xFF ))
62                         (( y >>= 8 ))
63                 done
64         done
65 }
66
67 function record_block {
68         typeset -i sv blk=$1
69
70         if (( !blk || (wofs && blk != (wofs + wnum)) )); then
71                 # flush the blocks from the cache
72                 (( wnum )) && do_record $wofs $wnum
73                 wofs=0
74                 wnum=0
75         fi
76         if (( blk )); then
77                 # record some new block into the cache
78                 (( wofs )) || let wofs=blk
79                 (( wnum += 1 << sscale ))
80         fi
81 }
82
83 typeset -i partp=0 numheads=0 numsecs=0 sscale=0 bsh=9 mbrpno=0
84 set -A g_code 0 0 0
85
86 while getopts ":0:1AB:g:h:M:p:S:s:" ch; do
87         case $ch {
88         (0)     ;;
89         (1)     ;;
90         (A)     numheads=0
91                 numsecs=99
92                 ;;
93         (B)     if (( (bsh = OPTARG) < 9 || OPTARG > 15 )); then
94                         print -u2 error: invalid block size "2^'$OPTARG'"
95                         exit 1
96                 fi
97                 ;;
98         (g)     if [[ $OPTARG != +([0-9]):+([0-9]):+([0-9]) ]]; then
99                         print -u2 Error: invalid geometry code "'$OPTARG'"
100                         exit 1
101                 fi
102                 saveIFS=$IFS
103                 IFS=:
104                 set -A g_code -- $OPTARG
105                 IFS=$saveIFS ;;
106         (h)     if (( (numheads = OPTARG) < 1 || OPTARG > 256 )); then
107                         print -u2 warning: invalid head count "'$OPTARG'"
108                         numheads=0
109                 fi ;;
110         (M)     if (( (mbrpno = OPTARG) < 1 || OPTARG > 4 )); then
111                         print -u2 warning: invalid partition number "'$OPTARG'"
112                         mbrpno=0
113                 fi ;;
114         (p)     if (( (partp = OPTARG) < 1 || OPTARG > 255 )); then
115                         print -u2 warning: invalid partition type "'$OPTARG'"
116                         partp=0
117                 fi ;;
118         (S)     if (( (sscale = OPTARG) < 0 || OPTARG > 24 )); then
119                         print -u2 error: invalid input scale "'$OPTARG'"
120                         exit 1
121                 fi ;;
122         (s)     if (( (numsecs = OPTARG) < 1 || OPTARG > 63 )); then
123                         print -u2 warning: invalid sector count "'$OPTARG'"
124                         numsecs=0
125                 fi ;;
126         (*)     print -u2 'Syntax:
127         bxinst [-1A] [-B blocksize] [-g C:H:S] [-h heads] [-M partno(1..4)]
128             [-p partitiontype] [-S scale] [-s sectors] <sectorlist | \\
129             dd of=image conv=notrunc
130 Default values: blocksize=9 heads=16 sectors=63 partitiontype=0x27 scale=0
131     partno=4 if -g (create MBR partition) is given; -A = auto boot geometry'
132                 exit 1 ;;
133         }
134 done
135 shift $((OPTIND - 1))
136
137 typeset -Ui psz=0       # must be unsigned
138 if (( g_code[0] )); then
139         # bounds check partition table values, calculate total sectors
140         if (( g_code[0] < 1 || g_code[1] < 1 || g_code[1] > 256 ||
141             g_code[2] < 1 || g_code[2] > 63 )); then
142                 print -u2 Invalid geometry, values out of bounds.
143         elif [[ $(print "(${g_code[0]} * ${g_code[1]} * ${g_code[2]})" \
144             "> 4294967295" | bc) = 1 ]]; then
145                 print -u2 Invalid geometry, more than 2 TiB of data.
146         else
147                 # we know it's <= 2^32-1
148                 (( psz = g_code[0] * g_code[1] * g_code[2] ))
149         fi
150 fi
151 if (( psz )); then
152         print -u2 geometry is $psz sectors \($(print \
153             "$psz * $((1 << bsh))" | bc) bytes\) in ${g_code[0]} cylinders, \
154             ${g_code[1]} heads, ${g_code[2]} sectors per track
155         if (( numsecs == 0 || (numsecs != 99 && numheads == 0) )); then
156                 print -u2 warning: using these values for C/H/S boot
157                 numheads=${g_code[1]}
158                 numsecs=${g_code[2]}
159         fi
160         (( mbrpno )) || mbrpno=4        # default partition number
161 fi
162 if (( mbrpno )); then
163         bkend=0x1BE
164         (( psz )) || print -u2 warning: no geometry given, will not \
165             create an MBR partition table entry
166 fi
167
168 if (( numsecs == 99 )); then
169         numheads=0
170         numsecs=0
171 else
172         if (( !numheads )); then
173                 print -u2 warning: using default value of 16 heads
174                 numheads=16
175         fi
176
177         if (( !numsecs )); then
178                 print -u2 warning: using default value of 63 sectors
179                 numsecs=63
180         fi
181 fi
182
183 # read in the extents
184 while read firstblock lastblock junk; do
185         while (( firstblock <= lastblock )); do
186                 record_block $((firstblock++ << sscale))
187         done
188 done
189 record_block 0  # just flush
190 print -u2 "using $wrec blocks, $((curptr-begptr)) bytes ($((bkend-curptr)) free)"
191
192 # fill the block table
193 if (( curptr-- > bkend )); then
194         print -u2 error: too many blocks
195         exit 1
196 fi
197 while (( ++curptr < bkend )); do
198         (( thecode[curptr] = (curptr & 0xFCF) == 0x1C2 ? 0 : RANDOM & 0xFF ))
199         # ensure the “active” flag is never set to 0x00 or 0x80
200         if (( ((curptr + 2) & 0xFCF) == 0x01C0 )); then
201                 (( thecode[curptr] & 0x7F )) || let --curptr
202         fi
203 done
204 thecode[510]=0x55
205 thecode[511]=0xAA
206
207 # fill in other data
208 (( thecode[ofs_bkcnt] = wrec ))
209 (( thecode[ofs_geomh] = numheads & 0xFF ))
210 (( thecode[ofs_geomh + 1] = numheads >> 8 ))
211 (( thecode[ofs_geoms] = numsecs ))
212 (( thecode[ofs_partp] = partp ))
213 print -u2 "using sectors of 2^$bsh = $((1 << bsh)) bytes"
214 (( thecode[ofs_secsz] = (1 << (bsh - 8)) ))
215
216 # create an MBR partition if desired
217 if (( psz )); then
218         (( mbrpno = 0x1BE + ((mbrpno - 1) * 16) ))
219         thecode[mbrpno++]=0x80
220         thecode[mbrpno++]=0
221         thecode[mbrpno++]=1
222         thecode[mbrpno++]=0
223         (( thecode[mbrpno++] = (partp ? partp : 0x27) ))
224         (( thecode[mbrpno++] = g_code[1] - 1 ))
225         (( cylno = g_code[0] > 1023 ? 1023 : g_code[0] ))
226         (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
227         (( thecode[mbrpno++] = cylno & 0x00FF ))
228         thecode[mbrpno++]=0
229         thecode[mbrpno++]=0
230         thecode[mbrpno++]=0
231         thecode[mbrpno++]=0
232         (( thecode[mbrpno++] = psz & 0xFF ))
233         (( thecode[mbrpno++] = (psz >> 8) & 0xFF ))
234         (( thecode[mbrpno++] = (psz >> 16) & 0xFF ))
235         (( thecode[mbrpno++] = (psz >> 24) & 0xFF ))
236 fi
237
238 # create the output string
239 ostr=
240 curptr=0
241 while (( curptr < 512 )); do
242         ostr=$ostr\\0${thecode[curptr++]#8#}
243 done
244
245 # over and out
246 print -n "$ostr"
247 exit 0