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