wok-next annotate linux/stuff/bootloader.sh @ rev 9914

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