Pull sbs into release branch
[pandora-kernel.git] / arch / powerpc / boot / wrapper
1 #!/bin/sh
2
3 # Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
4 # This program may be used under the terms of version 2 of the GNU
5 # General Public License.
6
7 # This script takes a kernel binary and optionally an initrd image
8 # and/or a device-tree blob, and creates a bootable zImage for a
9 # given platform.
10
11 # Options:
12 # -o zImage     specify output file
13 # -p platform   specify platform (links in $platform.o)
14 # -i initrd     specify initrd file
15 # -d devtree    specify device-tree blob
16 # -s tree.dts   specify device-tree source file (needs dtc installed)
17 # -c            cache $kernel.strip.gz (use if present & newer, else make)
18 # -C prefix     specify command prefix for cross-building tools
19 #               (strip, objcopy, ld)
20 # -D dir        specify directory containing data files used by script
21 #               (default ./arch/powerpc/boot)
22 # -W dir        specify working directory for temporary files (default .)
23
24 # defaults
25 kernel=
26 ofile=zImage
27 platform=of
28 initrd=
29 dtb=
30 dts=
31 cacheit=
32 gzip=.gz
33
34 # cross-compilation prefix
35 CROSS=
36
37 # directory for object and other files used by this script
38 object=arch/powerpc/boot
39
40 # directory for working files
41 tmpdir=.
42
43 usage() {
44     echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
45     echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
46     echo '       [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
47     exit 1
48 }
49
50 while [ "$#" -gt 0 ]; do
51     case "$1" in
52     -o)
53         shift
54         [ "$#" -gt 0 ] || usage
55         ofile="$1"
56         ;;
57     -p)
58         shift
59         [ "$#" -gt 0 ] || usage
60         platform="$1"
61         ;;
62     -i)
63         shift
64         [ "$#" -gt 0 ] || usage
65         initrd="$1"
66         ;;
67     -d)
68         shift
69         [ "$#" -gt 0 ] || usage
70         dtb="$1"
71         ;;
72     -s)
73         shift
74         [ "$#" -gt 0 ] || usage
75         dts="$1"
76         ;;
77     -c)
78         cacheit=y
79         ;;
80     -C)
81         shift
82         [ "$#" -gt 0 ] || usage
83         CROSS="$1"
84         ;;
85     -D)
86         shift
87         [ "$#" -gt 0 ] || usage
88         object="$1"
89         ;;
90     -W)
91         shift
92         [ "$#" -gt 0 ] || usage
93         tmpdir="$1"
94         ;;
95     --no-gzip)
96         gzip=
97         ;;
98     -?)
99         usage
100         ;;
101     *)
102         [ -z "$kernel" ] || usage
103         kernel="$1"
104         ;;
105     esac
106     shift
107 done
108
109 if [ -n "$dts" ]; then
110     if [ -z "$dtb" ]; then
111         dtb="$platform.dtb"
112     fi
113     dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts" || exit 1
114 fi
115
116 if [ -z "$kernel" ]; then
117     kernel=vmlinux
118 fi
119
120 platformo=$object/"$platform".o
121 lds=$object/zImage.lds
122 ext=strip
123 objflags=-S
124 tmp=$tmpdir/zImage.$$.o
125 ksection=.kernel:vmlinux.strip
126 isection=.kernel:initrd
127
128 case "$platform" in
129 pmac|pseries|chrp)
130     platformo=$object/of.o
131     ;;
132 coff)
133     platformo=$object/of.o
134     lds=$object/zImage.coff.lds
135     ;;
136 miboot|uboot)
137     # miboot and U-boot want just the bare bits, not an ELF binary
138     ext=bin
139     objflags="-O binary"
140     tmp="$ofile"
141     ksection=image
142     isection=initrd
143     ;;
144 cuboot*)
145     gzip=
146     ;;
147 ps3)
148     platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
149     lds=$object/zImage.ps3.lds
150     gzip=
151     ext=bin
152     objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
153     ksection=.kernel:vmlinux.bin
154     isection=.kernel:initrd
155     ;;
156 esac
157
158 vmz="$tmpdir/`basename \"$kernel\"`.$ext"
159 if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
160     ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
161
162     if [ -n "$gzip" ]; then
163         gzip -f -9 "$vmz.$$"
164     fi
165
166     if [ -n "$cacheit" ]; then
167         mv -f "$vmz.$$$gzip" "$vmz$gzip"
168     else
169         vmz="$vmz.$$"
170     fi
171 fi
172
173 vmz="$vmz$gzip"
174
175 # Extract kernel version information, some platforms want to include
176 # it in the image header
177 version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
178     cut -d' ' -f3`
179 if [ -n "$version" ]; then
180     uboot_version="-n Linux-$version"
181 fi
182
183 case "$platform" in
184 uboot)
185     rm -f "$ofile"
186     mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
187         $uboot_version -d "$vmz" "$ofile"
188     if [ -z "$cacheit" ]; then
189         rm -f "$vmz"
190     fi
191     exit 0
192     ;;
193 esac
194
195 addsec() {
196     ${CROSS}objcopy $4 $1 \
197         --add-section=$3="$2" \
198         --set-section-flags=$3=contents,alloc,load,readonly,data
199 }
200
201 addsec $tmp "$vmz" $ksection $object/empty.o
202 if [ -z "$cacheit" ]; then
203     rm -f "$vmz"
204 fi
205
206 if [ -n "$initrd" ]; then
207     addsec $tmp "$initrd" $isection
208 fi
209
210 if [ -n "$dtb" ]; then
211     addsec $tmp "$dtb" .kernel:dtb
212     if [ -n "$dts" ]; then
213         rm $dtb
214     fi
215 fi
216
217 if [ "$platform" != "miboot" ]; then
218     ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
219         $platformo $tmp $object/wrapper.a
220     rm $tmp
221 fi
222
223 # Some platforms need the zImage's entry point and base address
224 base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
225 entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
226
227 # post-processing needed for some platforms
228 case "$platform" in
229 pseries|chrp)
230     $object/addnote "$ofile"
231     ;;
232 coff)
233     ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
234     $object/hack-coff "$ofile"
235     ;;
236 cuboot*)
237     mv "$ofile" "$ofile".elf
238     ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin
239     gzip -f -9 "$ofile".bin
240     mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
241             $uboot_version -d "$ofile".bin.gz "$ofile"
242     ;;
243 treeboot*)
244     mv "$ofile" "$ofile.elf"
245     $object/mktree "$ofile.elf" "$ofile" "$base" "$entry"
246     if [ -z "$cacheit" ]; then
247         rm -f "$ofile.elf"
248     fi
249     exit 0
250     ;;
251 ps3)
252     # The ps3's loader supports loading gzipped binary images from flash
253     # rom to addr zero. The loader enters the image at addr 0x100.  A
254     # bootwrapper overlay is use to arrange for the kernel to be loaded
255     # to addr zero and to have a suitable bootwrapper entry at 0x100.
256     # To construct the rom image, 0x100 bytes from offset 0x100 in the
257     # kernel is copied to the bootwrapper symbol __system_reset_kernel.
258     # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is
259     # then copied to offset 0x100.  At runtime the bootwrapper program
260     # copies the 0x100 bytes at __system_reset_kernel to addr 0x100.
261
262     system_reset_overlay=0x`${CROSS}nm "$ofile" \
263         | grep ' __system_reset_overlay$'       \
264         | cut -d' ' -f1`
265     system_reset_overlay=`printf "%d" $system_reset_overlay`
266     system_reset_kernel=0x`${CROSS}nm "$ofile" \
267         | grep ' __system_reset_kernel$'       \
268         | cut -d' ' -f1`
269     system_reset_kernel=`printf "%d" $system_reset_kernel`
270     overlay_dest="256"
271     overlay_size="256"
272
273     rm -f "$object/otheros.bld"
274
275     ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
276
277     msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
278         skip=$overlay_dest seek=$system_reset_kernel      \
279         count=$overlay_size bs=1 2>&1)
280
281     if [ $? -ne "0" ]; then
282        echo $msg
283        exit 1
284     fi
285
286     msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
287         skip=$system_reset_overlay seek=$overlay_dest     \
288         count=$overlay_size bs=1 2>&1)
289
290     if [ $? -ne "0" ]; then
291        echo $msg
292        exit 2
293     fi
294
295     gzip --force -9 --stdout "$ofile.bin" > "$object/otheros.bld"
296     ;;
297 esac