wok annotate linux-libre/stuff/bootloader.sh @ rev 10111
Up: postfixadmin to 2.3.3.
author | Christopher Rogers <slaxemulator@gmail.com> |
---|---|
date | Fri May 20 15:31:35 2011 +0000 (2011-05-20) |
parents | |
children | 903c03d31ebf |
rev | line source |
---|---|
gokhlayeh@9257 | 1 #!/bin/sh |
gokhlayeh@9257 | 2 # |
gokhlayeh@9257 | 3 # This script creates a floppy image set from a linux bzImage and can merge |
gokhlayeh@9257 | 4 # a cmdline and/or one or more initramfs. |
gokhlayeh@9257 | 5 # The total size can not exceed 15M because INT 15H function 87H limitations. |
gokhlayeh@9257 | 6 # |
gokhlayeh@9257 | 7 # (C) 2009 Pascal Bellard - GNU General Public License v3. |
gokhlayeh@9257 | 8 |
gokhlayeh@9257 | 9 usage() |
gokhlayeh@9257 | 10 { |
gokhlayeh@9257 | 11 cat <<EOT |
gokhlayeh@9257 | 12 Usage: $0 bzImage [--prefix image_prefix] [--cmdline 'args'] |
gokhlayeh@9257 | 13 [--rdev device] [--video mode] [--flags rootflags] [--tracks cnt] |
gokhlayeh@9257 | 14 [--format 1440|1680|1920|2880 ] [--initrd initrdfile]... |
gokhlayeh@9257 | 15 |
gokhlayeh@9257 | 16 Default values: --format 1440 --tracks 80 --prefix floppy. |
gokhlayeh@9257 | 17 |
gokhlayeh@9257 | 18 Example: |
gokhlayeh@9257 | 19 $0 /boot/vmlinuz-2.6.30.6 --rdev /dev/ram0 --video -3 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' --initrd /boot/rootfs.gz --initrd ./myconfig.gz |
gokhlayeh@9257 | 20 EOT |
gokhlayeh@9257 | 21 exit 1 |
gokhlayeh@9257 | 22 } |
gokhlayeh@9257 | 23 |
gokhlayeh@9257 | 24 KERNEL="" |
gokhlayeh@9257 | 25 INITRD="" |
gokhlayeh@9257 | 26 CMDLINE="" |
gokhlayeh@9257 | 27 PREFIX="floppy." |
gokhlayeh@9257 | 28 FORMAT="1440" |
gokhlayeh@9257 | 29 RDEV="" |
gokhlayeh@9257 | 30 VIDEO="" |
gokhlayeh@9257 | 31 FLAGS="" |
gokhlayeh@9257 | 32 TRACKS="" |
gokhlayeh@9257 | 33 DEBUG="" |
gokhlayeh@9257 | 34 while [ -n "$1" ]; do |
gokhlayeh@9257 | 35 case "$1" in |
gokhlayeh@9257 | 36 --c*|-c*) CMDLINE="$2"; shift;; |
gokhlayeh@9257 | 37 --i*|-i*) INITRD="$INITRD $2"; shift;; |
gokhlayeh@9257 | 38 --p*|-p*) PREFIX="$2"; shift;; |
gokhlayeh@9257 | 39 --fo*|-f*) FORMAT="$2"; shift;; |
gokhlayeh@9257 | 40 --fl*) FLAGS="$2"; shift;; # 1 read-only, 0 read-write |
gokhlayeh@9257 | 41 --r*|-r*) RDEV="$2"; shift;; # /dev/??? |
gokhlayeh@9257 | 42 --v*|-v*) VIDEO="$2"; shift;; # -3 .. n |
gokhlayeh@9257 | 43 --t*|-t*) TRACKS="$2"; shift;; # likely 81 .. 84 |
gokhlayeh@9257 | 44 --debug) DEBUG="1";; |
gokhlayeh@9257 | 45 *) KERNEL="$1";; |
gokhlayeh@9257 | 46 esac |
gokhlayeh@9257 | 47 shift |
gokhlayeh@9257 | 48 done |
gokhlayeh@9257 | 49 [ -n "$KERNEL" -a -f "$KERNEL" ] || usage |
gokhlayeh@9257 | 50 if [ -n "$TRACKS" ]; then |
gokhlayeh@9257 | 51 if [ $(( $FORMAT % $TRACKS )) -ne 0 ]; then |
gokhlayeh@9257 | 52 echo "Invalid track count for format $FORMAT." |
gokhlayeh@9257 | 53 usage |
gokhlayeh@9257 | 54 fi |
gokhlayeh@9257 | 55 fi |
gokhlayeh@9257 | 56 |
gokhlayeh@9257 | 57 # write a 16 bits data |
gokhlayeh@9257 | 58 # usage: store16 offset data16 file |
gokhlayeh@9257 | 59 store16() |
gokhlayeh@9257 | 60 { |
gokhlayeh@9257 | 61 n=$2; i=2; while [ $i -ne 0 ]; do |
gokhlayeh@9257 | 62 printf '\\\\x%02X' $(($n & 255)) |
gokhlayeh@9257 | 63 i=$(($i-1)); n=$(($n >> 8)) |
gokhlayeh@9257 | 64 done | xargs echo -en | \ |
gokhlayeh@9257 | 65 dd bs=2 conv=notrunc of=$3 seek=$(( $1 / 2 )) 2> /dev/null |
gokhlayeh@9257 | 66 [ -n "$DEBUG" ] && printf "store16(%04X) = %04X\n" $1 $2 1>&2 |
gokhlayeh@9257 | 67 } |
gokhlayeh@9257 | 68 |
gokhlayeh@9257 | 69 # write a 32 bits data |
gokhlayeh@9257 | 70 # usage: storelong offset data32 file |
gokhlayeh@9257 | 71 storelong() |
gokhlayeh@9257 | 72 { |
gokhlayeh@9257 | 73 n=$2; i=4; while [ $i -ne 0 ]; do |
gokhlayeh@9257 | 74 printf '\\\\x%02X' $(($n & 255)) |
gokhlayeh@9257 | 75 i=$(($i-1)); n=$(($n >> 8)) |
gokhlayeh@9257 | 76 done | xargs echo -en | \ |
gokhlayeh@9257 | 77 dd bs=4 conv=notrunc of=$3 seek=$(( $1 / 4 )) 2> /dev/null |
gokhlayeh@9257 | 78 [ -n "$DEBUG" ] && printf "storelong(%04X) = %08X\n" $1 $2 1>&2 |
gokhlayeh@9257 | 79 } |
gokhlayeh@9257 | 80 |
gokhlayeh@9257 | 81 # read a 32 bits data |
gokhlayeh@9257 | 82 # usage: getlong offset file |
gokhlayeh@9257 | 83 getlong() |
gokhlayeh@9257 | 84 { |
gokhlayeh@9257 | 85 dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \ |
gokhlayeh@9257 | 86 hexdump -e '"" 1/4 "%d" "\n"' |
gokhlayeh@9257 | 87 } |
gokhlayeh@9257 | 88 |
gokhlayeh@9257 | 89 floppyset() |
gokhlayeh@9257 | 90 { |
gokhlayeh@9257 | 91 # bzImage offsets |
gokhlayeh@9257 | 92 CylinderCount=496 |
gokhlayeh@9257 | 93 SetupSzOfs=497 |
gokhlayeh@9257 | 94 FlagsOfs=498 |
gokhlayeh@9257 | 95 SyssizeOfs=500 |
gokhlayeh@9257 | 96 VideoModeOfs=506 |
gokhlayeh@9257 | 97 RootDevOfs=508 |
gokhlayeh@9257 | 98 CodeAdrOfs=0x214 |
gokhlayeh@9257 | 99 RamfsAdrOfs=0x218 |
gokhlayeh@9257 | 100 RamfsLenOfs=0x21C |
gokhlayeh@9257 | 101 ArgPtrOfs=0x228 |
gokhlayeh@9257 | 102 |
gokhlayeh@9257 | 103 # boot+setup address |
gokhlayeh@9257 | 104 SetupBase=0x90000 |
gokhlayeh@9257 | 105 |
gokhlayeh@9257 | 106 stacktop=0x9E00 |
gokhlayeh@9257 | 107 |
gokhlayeh@9257 | 108 bs=/tmp/bs$$ |
gokhlayeh@9257 | 109 |
gokhlayeh@9257 | 110 # Get and patch boot sector |
gokhlayeh@9257 | 111 # See http://hg.slitaz.org/wok/raw-file/711d076b277c/linux/stuff/linux-header-2.6.34.u |
gokhlayeh@9257 | 112 dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null |
gokhlayeh@9257 | 113 uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null |
gokhlayeh@9257 | 114 begin-base64 644 - |
gokhlayeh@9257 | 115 /L+6nWgAkAcGF4n8McC5HQDzq1sfD6mg8X1ABlfFd3ixBvOlZWaPR3gGH8ZF |
gokhlayeh@9257 | 116 +D/6l1hB6DQBvgACA3QO6HYBWwseKAJ0LFNH6AoBXuhmAbAgzRCwCM0QTuhl |
gokhlayeh@9257 | 117 ATwIdAOIBK05NigCdPDoPgE8CnXgiHz+ieb/TBD/TBi/9AGBTRz/gMdFMACc |
gokhlayeh@9257 | 118 sBCxBUi0k4lEHLABiUQUmGaY0+BIZgMFZtPoaAAQB7+AACn4nHMCAccx21BW |
gokhlayeh@9257 | 119 6J4AXrkAgLSH/kQczRVYnXfcoRoCvxwCsQk4RBxyuJPNE+oAACCQsEYoyL7b |
gokhlayeh@9257 | 120 AejSAF3rI4D5E3IEOMF3a4D+AnIEOOZ3bGCB/QAGdCoGUlFTlrQCULEGtQTB |
gokhlayeh@9257 | 121 xQSwDyHoBJAnFEAn6IwA/s117LAgzRDitOiWAJjNE2FSUCjIdwKwAZg5+HIC |
gokhlayeh@9257 | 122 ifhQtALNE5VeWFpyoJVBjuGAxwJPdFFOdfSM4ZU4wXVFiMj+xrEBOOZ1O4j0 |
gokhlayeh@9257 | 123 /sW2AID9UHIwOi7wAXIqtQBgvt4B/kQMU+gxAFvoOAB1FlKYzRO4AQLNE1rQ |
gokhlayeh@9257 | 124 1Dpk/nXqRgjkdeVh64sWB7AxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79s |
gokhlayeh@9257 | 125 BLFbZQINuA0BZToNdArNFnT0mM0Wju9Hw1g6AEluc2VydCBkaXNrIDEuBw0A |
gokhlayeh@9257 | 126 AA== |
gokhlayeh@9257 | 127 ==== |
gokhlayeh@9257 | 128 EOT |
gokhlayeh@9257 | 129 |
gokhlayeh@9257 | 130 # Get setup |
gokhlayeh@9257 | 131 setupsz=$(getlong $SetupSzOfs $bs) |
gokhlayeh@9257 | 132 setupszb=$(( $setupsz & 255 )) |
gokhlayeh@9257 | 133 dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs |
gokhlayeh@9257 | 134 |
gokhlayeh@9257 | 135 if [ -n "$TRACKS" ]; then |
gokhlayeh@9257 | 136 [ -n "$DEBUG" ] && echo -n "--tracks " 1>&2 |
gokhlayeh@9257 | 137 n=$(getlong $CylinderCount $bs) |
gokhlayeh@9257 | 138 store16 $CylinderCount $(( ($n & -256) + $TRACKS )) $bs |
gokhlayeh@9257 | 139 fi |
gokhlayeh@9257 | 140 if [ -n "$FLAGS" ]; then |
gokhlayeh@9257 | 141 [ -n "$DEBUG" ] && echo -n "--flags " 1>&2 |
gokhlayeh@9257 | 142 store16 $FlagsOfs $FLAGS $bs |
gokhlayeh@9257 | 143 fi |
gokhlayeh@9257 | 144 if [ -n "$VIDEO" ]; then |
gokhlayeh@9257 | 145 [ -n "$DEBUG" ] && echo -n "--video " 1>&2 |
gokhlayeh@9257 | 146 store16 $VideoModeOfs $VIDEO $bs |
gokhlayeh@9257 | 147 fi |
gokhlayeh@9257 | 148 if [ -n "$RDEV" ]; then |
gokhlayeh@9257 | 149 [ -n "$DEBUG" ] && echo -n "--rdev " 1>&2 |
gokhlayeh@9257 | 150 n=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null) |
gokhlayeh@9257 | 151 [ -n "$n" ] || n=$RDEV |
gokhlayeh@9257 | 152 store16 $RootDevOfs $n $bs |
gokhlayeh@9257 | 153 fi |
gokhlayeh@9257 | 154 |
gokhlayeh@9257 | 155 # Store cmdline after setup |
gokhlayeh@9257 | 156 if [ -n "$CMDLINE" ]; then |
gokhlayeh@9257 | 157 [ -n "$DEBUG" ] && echo -n "--cmdline '$CMDLINE' " 1>&2 |
gokhlayeh@9257 | 158 echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs |
gokhlayeh@9257 | 159 storelong $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs |
gokhlayeh@9257 | 160 fi |
gokhlayeh@9257 | 161 |
gokhlayeh@9257 | 162 # Compute initramfs size |
gokhlayeh@9257 | 163 initrdlen=0 |
gokhlayeh@9257 | 164 padding=0 |
gokhlayeh@9257 | 165 for i in $( echo $INITRD | sed 's/,/ /' ); do |
gokhlayeh@9257 | 166 [ -s "$i" ] || continue |
gokhlayeh@9257 | 167 [ -n "$DEBUG" ] && echo "--initrd $i " 1>&2 |
gokhlayeh@9257 | 168 initrdlen=$(( $initrdlen + $padding )) |
gokhlayeh@9257 | 169 padding=$(stat -c %s $i) |
gokhlayeh@9257 | 170 initrdlen=$(( $initrdlen + $padding )) |
gokhlayeh@9257 | 171 padding=$(( 4096 - ($padding & 4095) )) |
gokhlayeh@9257 | 172 [ $padding -eq 4096 ] && padding=0 |
gokhlayeh@9257 | 173 done |
gokhlayeh@9257 | 174 Ksize=$(( $(getlong $SyssizeOfs $bs)*16 )) |
gokhlayeh@9257 | 175 Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize )) |
gokhlayeh@9257 | 176 if [ $initrdlen -ne 0 ]; then |
gokhlayeh@9257 | 177 [ -n "$DEBUG" ] && echo "initrdlen = $initrdlen " 1>&2 |
gokhlayeh@9257 | 178 Kbase=$(getlong $CodeAdrOfs $bs) |
gokhlayeh@9257 | 179 storelong $RamfsAdrOfs \ |
gokhlayeh@9257 | 180 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs |
gokhlayeh@9257 | 181 storelong $RamfsLenOfs $(( ($initrdlen + 3) & -4 )) $bs |
gokhlayeh@9257 | 182 fi |
gokhlayeh@9257 | 183 |
gokhlayeh@9257 | 184 # Output boot sector + setup + cmdline |
gokhlayeh@9257 | 185 dd if=$bs 2> /dev/null |
gokhlayeh@9257 | 186 |
gokhlayeh@9257 | 187 # Output kernel code |
gokhlayeh@9257 | 188 dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null |
gokhlayeh@9257 | 189 |
gokhlayeh@9257 | 190 # Pad to next sector |
gokhlayeh@9257 | 191 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) )) |
gokhlayeh@9257 | 192 [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null |
gokhlayeh@9257 | 193 |
gokhlayeh@9257 | 194 # Output initramfs |
gokhlayeh@9257 | 195 padding=0 |
gokhlayeh@9257 | 196 for i in $( echo $INITRD | sed 's/,/ /' ); do |
gokhlayeh@9257 | 197 [ -s "$i" ] || continue |
gokhlayeh@9257 | 198 [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding 2> /dev/null |
gokhlayeh@9257 | 199 dd if=$i 2> /dev/null |
gokhlayeh@9257 | 200 padding=$(( 4 - ($(stat -c %s $i) & 3) )) |
gokhlayeh@9257 | 201 [ $padding -eq 4 ] && padding=0 |
gokhlayeh@9257 | 202 done |
gokhlayeh@9257 | 203 |
gokhlayeh@9257 | 204 # Cleanup |
gokhlayeh@9257 | 205 rm -f $bs |
gokhlayeh@9257 | 206 } |
gokhlayeh@9257 | 207 |
gokhlayeh@9257 | 208 if [ "$FORMAT" == "0" ]; then # unsplitted |
gokhlayeh@9257 | 209 floppyset > $PREFIX |
gokhlayeh@9257 | 210 exit |
gokhlayeh@9257 | 211 fi |
gokhlayeh@9257 | 212 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$ |
gokhlayeh@9257 | 213 i=1 |
gokhlayeh@9257 | 214 ls floppy$$* | while read file ; do |
gokhlayeh@9257 | 215 output=$PREFIX$(printf "%03d" $i) |
gokhlayeh@9257 | 216 cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null |
gokhlayeh@9257 | 217 echo $output |
gokhlayeh@9257 | 218 rm -f $file |
gokhlayeh@9257 | 219 i=$(( $i + 1 )) |
gokhlayeh@9257 | 220 done |