From aefb34fc861b4225134d7c0a6cbc512fd9d9bf6c Mon Sep 17 00:00:00 2001 From: Thorsten Glaser Date: Sun, 7 Jun 2009 16:23:56 +0000 Subject: [PATCH] Update bootgrub.mksh from MirBSD MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit New features: • -A (auto boot-time geometry detection for CHS boot) • partition table generation Signed-off-by: Thorsten Glaser --- scripts/bootgrub.mksh | 112 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 18 deletions(-) diff --git a/scripts/bootgrub.mksh b/scripts/bootgrub.mksh index 6762b0b..7bbe289 100644 --- a/scripts/bootgrub.mksh +++ b/scripts/bootgrub.mksh @@ -1,6 +1,6 @@ #!/usr/bin/env mksh # $Id$ -# $miros: src/sys/arch/i386/stand/bootxx/mkbxinst.sh,v 1.15 2009/02/08 20:47:48 tg Exp $ +# $miros: src/sys/arch/i386/stand/bootxx/mkbxinst.sh,v 1.18 2009/06/07 13:07:46 tg Exp $ #- # Copyright (c) 2007, 2008, 2009 # Thorsten Glaser @@ -25,17 +25,17 @@ # and writes bootxx to standard output, which can subsequentially be # stored as partition boot record (or floppy boot sector) on disc. -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 0x37 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 0xAB 0x02 0x00 0x12 0x00 0x00 0x80 0x50 0xFF 0xB4 0x0E 0x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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 +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 0xB4 0x0E 0xBB 0x07 0x00 0xCD 0x10 0xAC 0x08 0xC0 0x75 0xF4 0xC3 0xE8 0xF7 0xFF 0xB8 0x53 0xFF 0x87 0x06 0x56 0xFE 0x3D 0x53 0xFF 0x75 0x19 0x31 0xC0 0xCD 0x16 0xEA 0xF0 0xFF 0x00 0xF0 0xFB 0x88 0x16 0x55 0xFE 0xBE 0x2F 0xFE 0x80 0xFA 0x80 0x72 0xDB 0xE8 0xD2 0xFF 0x31 0xDB 0xBE 0x7A 0xFF 0x0F 0xB6 0x2E 0x4F 0xFE 0xAC 0x0F 0xB6 0xC8 0xC0 0xE9 0x05 0x83 0xE0 0x1F 0x40 0x41 0x50 0xBF 0x10 0xFE 0xB8 0x10 0x00 0xAB 0xB0 0x01 0xAB 0x89 0xD8 0xAB 0x8C 0xC8 0xAB 0xF3 0xA4 0x31 0xC0 0xAB 0xAB 0xAB 0xAB 0x5F 0x55 0x56 0xBE 0x10 0xFE 0xFF 0x16 0x56 0xFE 0xBD 0x04 0x00 0x8A 0x16 0x55 0xFE 0x60 0xF9 0xCD 0x13 0x9C 0xB8 0x2E 0x0E 0xBB 0x07 0x00 0xCD 0x10 0x9D 0xFB 0x61 0x73 0x1C 0x4D 0x60 0x9C 0x31 0xC0 0xCD 0x13 0x9D 0xBE 0x46 0xFE 0x0F 0x84 0x77 0xFF 0xB8 0x30 0x0E 0x01 0xE8 0xBB 0x07 0x00 0xCD 0x10 0x61 0xEB 0xD2 0xB4 0x02 0x00 0xE7 0x89 0x5C 0x04 0x00 0xFC 0x72 0x14 0x66 0x83 0x44 0x08 0x01 0x66 0x83 0x54 0x0C 0x00 0x4F 0x75 0xAC 0x5E 0x5D 0x4D 0x0F 0x85 0x7D 0xFF 0xBE 0x4C 0xFE 0xE8 0x3F 0xFF 0x66 0x31 0xD2 0x52 0x66 0x4A 0x8A 0x16 0x55 0xFE 0xB8 0x00 0x82 0x50 0xFA 0xCB 0x60 0x06 0xB4 0x08 0x8A 0x16 0x55 0xFE 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 typeset -i ofs_bkcnt=79 typeset -i ofs_geomh=80 typeset -i ofs_geoms=82 typeset -i ofs_partp=84 typeset -i ofs_secsz=252 -typeset -i begptr=339 +typeset -i begptr=378 typeset -Uui8 thecode typeset -Uui16 curptr=begptr -typeset -i wnum=0 wofs=0 wrec=0 +typeset -i wnum=0 wofs=0 wrec=0 bkend=0x1FE function do_record { typeset -i blk=$1 cnt=$2 n @@ -80,21 +80,37 @@ function record_block { fi } -typeset -i partp=0 numheads=0 numsecs=0 sscale=0 bsh=9 +typeset -i partp=0 numheads=0 numsecs=0 sscale=0 bsh=9 mbrpno=0 +set -A g_code 0 0 0 -while getopts ":0:1B:h:p:S:s:" ch; do +while getopts ":0:1AB:g:h:M:p:S:s:" ch; do case $ch { (0) ;; (1) ;; + (A) numheads=0 + numsecs=99 + ;; (B) if (( (bsh = OPTARG) < 9 || OPTARG > 15 )); then print -u2 error: invalid block size "2^'$OPTARG'" exit 1 fi ;; + (g) if [[ $OPTARG != +([0-9]):+([0-9]):+([0-9]) ]]; then + print -u2 Error: invalid geometry code "'$OPTARG'" + exit 1 + fi + saveIFS=$IFS + IFS=: + set -A g_code -- $OPTARG + IFS=$saveIFS ;; (h) if (( (numheads = OPTARG) < 1 || OPTARG > 256 )); then print -u2 warning: invalid head count "'$OPTARG'" numheads=0 fi ;; + (M) if (( (mbrpno = OPTARG) < 1 || OPTARG > 4 )); then + print -u2 warning: invalid partition number "'$OPTARG'" + mbrpno=0 + fi ;; (p) if (( (partp = OPTARG) < 1 || OPTARG > 255 )); then print -u2 warning: invalid partition type "'$OPTARG'" partp=0 @@ -108,22 +124,60 @@ while getopts ":0:1B:h:p:S:s:" ch; do numsecs=0 fi ;; (*) print -u2 'Syntax: - bxinst [-1] [-B blocksize] [-h heads] [-p partitiontype] [-S scale] - [-s sectors] 256 || + g_code[2] < 1 || g_code[2] > 63 )); then + print -u2 Invalid geometry, values out of bounds. + elif [[ $(print "(${g_code[0]} * ${g_code[1]} * {g_code[2]})" \ + "> 4294967295" | bc) = 1 ]]; then + print -u2 Invalid geometry, more than 2 TiB of data. + else + # we know it's <= 2^32-1 + (( psz = g_code[0] * g_code[1] * g_code[2] )) + fi +fi +if (( psz )); then + print -u2 geometry is $psz sectors \($(print \ + "$psz * $((1 << bsh))" | bc) bytes\) in ${g_code[0]} cylinders, \ + ${g_code[1]} heads, ${g_code[2]} sectors per track + if (( numsecs == 0 || (numsecs != 99 && numheads == 0) )); then + print -u2 warning: using these values for C/H/S boot + numheads=${g_code[1]} + numsecs=${g_code[2]} + fi + (( mbrpno )) || mbrpno=4 # default partition number fi +if (( mbrpno )); then + bkend=0x1BE + (( psz )) || print -u2 warning: no geometry given, will not \ + create an MBR partition table entry +fi + +if (( numsecs == 99 )); then + numheads=0 + numsecs=0 +else + if (( !numheads )); then + print -u2 warning: using default value of 16 heads + numheads=16 + fi -if (( !numsecs )); then - print -u2 warning: using default value of 63 sectors - numsecs=63 + if (( !numsecs )); then + print -u2 warning: using default value of 63 sectors + numsecs=63 + fi fi # read in the extents @@ -133,14 +187,14 @@ while read firstblock lastblock junk; do done done record_block 0 # just flush -print -u2 "using $wrec blocks, $((curptr-begptr)) bytes ($((510-curptr)) free)" +print -u2 "using $wrec blocks, $((curptr-begptr)) bytes ($((bkend-curptr)) free)" # fill the block table -if (( curptr-- > 510 )); then +if (( curptr-- > bkend )); then print -u2 error: too many blocks exit 1 fi -while (( ++curptr < 510 )); do +while (( ++curptr < bkend )); do (( thecode[curptr] = (curptr & 0xFCF) == 0x1C2 ? 0 : RANDOM & 0xFF )) # ensure the “active” flag is never set to 0x00 or 0x80 if (( ((curptr + 2) & 0xFCF) == 0x01C0 )); then @@ -159,6 +213,28 @@ thecode[511]=0xAA print -u2 "using sectors of 2^$bsh = $((1 << bsh)) bytes" (( thecode[ofs_secsz] = (1 << (bsh - 8)) )) +# create an MBR partition if desired +if (( psz )); then + (( mbrpno = 0x1BE + ((mbrpno - 1) * 16) )) + thecode[mbrpno++]=0x80 + thecode[mbrpno++]=0 + thecode[mbrpno++]=1 + thecode[mbrpno++]=0 + (( thecode[mbrpno++] = (partp ? partp : 0x27) )) + (( thecode[mbrpno++] = g_code[1] - 1 )) + (( cylno = g_code[0] > 1023 ? 1023 : g_code[0] )) + (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) )) + (( thecode[mbrpno++] = cylno & 0x00FF )) + thecode[mbrpno++]=0 + thecode[mbrpno++]=0 + thecode[mbrpno++]=0 + thecode[mbrpno++]=0 + (( thecode[mbrpno++] = psz & 0xFF )) + (( thecode[mbrpno++] = (psz >> 8) & 0xFF )) + (( thecode[mbrpno++] = (psz >> 16) & 0xFF )) + (( thecode[mbrpno++] = (psz >> 24) & 0xFF )) +fi + # create the output string ostr= curptr=0 -- 2.1.4