wok-next view linux/stuff/bootloader.sh @ rev 5833

Up: xfce4-settings (4.6.5)
author Eric Joseph-Alexandre <erjo@slitaz.org>
date Fri Jul 16 08:08:20 2010 +0200 (2010-07-16)
parents 7798d8b688a9
children 85241294b8f8
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 [--format 1440|1680|1720|2880 ] [--initrd initrdfile]...
15 Example:
16 $0 /boot/vmlinuz-2.6.30.6 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' --initrd /boot/rootfs.gz --initrd ./myconfig.gz
17 EOT
18 exit 1
19 }
21 KERNEL=""
22 INITRD=""
23 CMDLINE=""
24 PREFIX="floppy"
25 FORMAT="1440"
26 while [ -n "$1" ]; do
27 case "$1" in
28 --cmdline) CMDLINE="$2"; shift;;
29 --initrd) INITRD="$INITRD $2"; shift;;
30 --prefix) PREFIX="$2"; shift;;
31 --format) FORMAT="$2"; shift;;
32 *) KERNEL="$1";;
33 esac
34 shift
35 done
36 [ -n "$KERNEL" -a -f "$KERNEL" ] || usage
38 # write a 32 bits data
39 # usage: storelong offset data32 file
40 storelong()
41 {
42 echo $2 | awk '{ printf "%c%c%c%c",$1%256,($1/256)%256,($1/256/256)%256,
43 ($1/256/256/256)%256 }' | \
44 dd bs=1 conv=notrunc of=$3 seek=$(( $1 )) 2> /dev/null
45 }
47 # read a 32 bits data
48 # usage: getlong offset file
49 getlong()
50 {
51 dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \
52 hexdump -e '"" 1/4 "%d" "\n"'
53 }
55 floppyset()
56 {
57 # bzImage offsets
58 SetupSzOfs=497
59 SyssizeOfs=500
60 CodeAdrOfs=0x214
61 RamfsAdrOfs=0x218
62 RamfsLenOfs=0x21C
63 ArgPtrOfs=0x228
65 # boot+setup address
66 SetupBase=0x90000
68 stacktop=0x9E00
70 bs=/tmp/bs$$
72 # Get and patch boot sector
73 # See http://hg.slitaz.org/wok/raw-file/tip/linux/stuff/linux-header-2.6.30.6.u
74 dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null
75 uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null
76 begin-base64 644 -
77 /L+6nWgAkAcGF4n8McC5HQDzq1sfD6mg8X1ABlfFd3ixBvOlZWaPR3gGH8ZF
78 +D/6l1hB6DQBvgACA3QO6HYBWwseKAJ0LFNH6AoBXuhmAbAgzRCwCM0QTuhl
79 ATwIdAOIBK05NigCdPDoPgE8CnXgiHz+ieb/TBD/TBi/9AGBTRz/gMdFMACc
80 sBCxBUi0k4lEHLABiUQUmGaY0+BIZgMFZtPoaAAQB7+AACn4nHMCAccx21BW
81 6J4AXrkAgLSH/kQczRVYnXfcoRoCvxwCsQk4RBxyuJPNE+oAACCQsEYoyL7b
82 AejSAF3rI4D5E3IEOMF3a4D+AnIEOOZ3bGCB/QAGdCoGUlFTlrQCULEGtQTB
83 xQSwDyHoBJAnFEAn6IwA/s117LAgzRDitOiWAJjNE2FSUCjIdwKwAZg5+HIC
84 ifhQtALNE5VeWFpyoJVBjuGAxwJPdFFOdfSM4ZU4wXVFiMj+xrEBOOZ1O4j0
85 /sW2AID9UHIwOi7wAXIqtQBgvt4B/kQMU+gxAFvoOAB1FlKYzRO4AQLNE1rQ
86 1Dpk/nXqRgjkdeVh64sWB7AxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79s
87 BLFbZQINuA0BZToNdArNFnT0mM0Wju9Hw1g6AEluc2VydCBkaXNrIDEuBw0A
88 AA==
89 ====
90 EOT
92 # Get setup
93 setupsz=$(getlong $SetupSzOfs $bs)
94 setupszb=$(( $setupsz & 255 ))
95 dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs
97 # Store cmdline after setup
98 if [ -n "$CMDLINE" ]; then
99 echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs
100 storelong ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
101 fi
103 # Compute initramfs size
104 initrdlen=0
105 padding=0
106 for i in $( echo $INITRD | sed 's/,/ /' ); do
107 [ -s "$i" ] || continue
108 initrdlen=$(( $initrdlen + $padding ))
109 padding=$(stat -c %s $i)
110 initrdlen=$(( $initrdlen + $padding ))
111 padding=$(( 4096 - ($padding & 4095) ))
112 [ $padding -eq 4096 ] && padding=0
113 done
114 Ksize=$(( $(getlong $SyssizeOfs $bs)*16 ))
115 Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize ))
116 if [ $initrdlen -ne 0 ]; then
117 Kbase=$(getlong $CodeAdrOfs $bs)
118 storelong $RamfsAdrOfs \
119 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
120 storelong $RamfsLenOfs $initrdlen $bs
121 fi
123 # Output boot sector + setup + cmdline
124 dd if=$bs 2> /dev/null
126 # Output kernel code
127 dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null
129 # Pad to next sector
130 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
131 [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null
133 # Output initramfs
134 padding=0
135 for i in $( echo $INITRD | sed 's/,/ /' ); do
136 [ -s "$i" ] || continue
137 [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding
138 dd if=$i 2> /dev/null
139 padding=$(( 4096 - ($(stat -c %s $i) & 4095) ))
140 [ $padding -eq 4096 ] && padding=0
141 done
143 # Cleanup
144 rm -f $bs
145 }
147 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
148 i=1
149 ls floppy$$* | while read file ; do
150 output=$PREFIX.$(printf "%03d" $i)
151 cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null
152 echo $output
153 rm -f $file
154 i=$(( $i + 1 ))
155 done