wok-stable view linux/stuff/bootloader.sh @ rev 6714

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