bootgrub.mksh 10 KB

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