Merge branches 'pxa' and 'orion-fixes1'
[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 # Stop execution if any command fails
25 set -e
26
27 # Allow for verbose output
28 if [ "$V" = 1 ]; then
29     set -x
30 fi
31
32 # defaults
33 kernel=
34 ofile=zImage
35 platform=of
36 initrd=
37 dtb=
38 dts=
39 cacheit=
40 binary=
41 gzip=.gz
42
43 # cross-compilation prefix
44 CROSS=
45
46 # directory for object and other files used by this script
47 object=arch/powerpc/boot
48 objbin=$object
49
50 # directory for working files
51 tmpdir=.
52
53 usage() {
54     echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
55     echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
56     echo '       [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
57     exit 1
58 }
59
60 while [ "$#" -gt 0 ]; do
61     case "$1" in
62     -o)
63         shift
64         [ "$#" -gt 0 ] || usage
65         ofile="$1"
66         ;;
67     -p)
68         shift
69         [ "$#" -gt 0 ] || usage
70         platform="$1"
71         ;;
72     -i)
73         shift
74         [ "$#" -gt 0 ] || usage
75         initrd="$1"
76         ;;
77     -d)
78         shift
79         [ "$#" -gt 0 ] || usage
80         dtb="$1"
81         ;;
82     -s)
83         shift
84         [ "$#" -gt 0 ] || usage
85         dts="$1"
86         ;;
87     -c)
88         cacheit=y
89         ;;
90     -C)
91         shift
92         [ "$#" -gt 0 ] || usage
93         CROSS="$1"
94         ;;
95     -D)
96         shift
97         [ "$#" -gt 0 ] || usage
98         object="$1"
99         objbin="$1"
100         ;;
101     -W)
102         shift
103         [ "$#" -gt 0 ] || usage
104         tmpdir="$1"
105         ;;
106     --no-gzip)
107         gzip=
108         ;;
109     -?)
110         usage
111         ;;
112     *)
113         [ -z "$kernel" ] || usage
114         kernel="$1"
115         ;;
116     esac
117     shift
118 done
119
120 if [ -n "$dts" ]; then
121     if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
122         dts="$object/dts/$dts"
123     fi
124     if [ -z "$dtb" ]; then
125         dtb="$platform.dtb"
126     fi
127     $object/dtc -O dtb -o "$dtb" -b 0 "$dts"
128 fi
129
130 if [ -z "$kernel" ]; then
131     kernel=vmlinux
132 fi
133
134 platformo=$object/"$platform".o
135 lds=$object/zImage.lds
136 ext=strip
137 objflags=-S
138 tmp=$tmpdir/zImage.$$.o
139 ksection=.kernel:vmlinux.strip
140 isection=.kernel:initrd
141
142 case "$platform" in
143 pmac|pseries|chrp)
144     platformo=$object/of.o
145     ;;
146 coff)
147     platformo=$object/of.o
148     lds=$object/zImage.coff.lds
149     ;;
150 miboot|uboot)
151     # miboot and U-boot want just the bare bits, not an ELF binary
152     ext=bin
153     objflags="-O binary"
154     tmp="$ofile"
155     ksection=image
156     isection=initrd
157     ;;
158 cuboot*)
159     binary=y
160     gzip=
161     case "$platform" in
162     *-mpc885ads|*-adder875*|*-ep88xc)
163         platformo=$object/cuboot-8xx.o
164         ;;
165     *5200*|*-motionpro)
166         platformo=$object/cuboot-52xx.o
167         ;;
168     *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
169         platformo=$object/cuboot-pq2.o
170         ;;
171     *-mpc824*)
172         platformo=$object/cuboot-824x.o
173         ;;
174     *-mpc83*)
175         platformo=$object/cuboot-83xx.o
176         ;;
177     *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
178         platformo=$object/cuboot-85xx-cpm2.o
179         ;;
180     *-mpc85*|*-tqm8540|*-sbc85*)
181         platformo=$object/cuboot-85xx.o
182         ;;
183     esac
184     ;;
185 ps3)
186     platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
187     lds=$object/zImage.ps3.lds
188     gzip=
189     ext=bin
190     objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
191     ksection=.kernel:vmlinux.bin
192     isection=.kernel:initrd
193     ;;
194 ep88xc|ep405|ep8248e)
195     platformo="$object/fixed-head.o $object/$platform.o"
196     binary=y
197     ;;
198 adder875-redboot)
199     platformo="$object/fixed-head.o $object/redboot-8xx.o"
200     binary=y
201     ;;
202 simpleboot-virtex405-*)
203     platformo="$object/virtex405-head.o $object/simpleboot.o"
204     binary=y
205     ;;
206 esac
207
208 vmz="$tmpdir/`basename \"$kernel\"`.$ext"
209 if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
210     ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
211
212     if [ -n "$gzip" ]; then
213         gzip -f -9 "$vmz.$$"
214     fi
215
216     if [ -n "$cacheit" ]; then
217         mv -f "$vmz.$$$gzip" "$vmz$gzip"
218     else
219         vmz="$vmz.$$"
220     fi
221 fi
222
223 vmz="$vmz$gzip"
224
225 # Extract kernel version information, some platforms want to include
226 # it in the image header
227 version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
228     cut -d' ' -f3`
229 if [ -n "$version" ]; then
230     uboot_version="-n Linux-$version"
231 fi
232
233 # physical offset of kernel image
234 membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
235
236 case "$platform" in
237 uboot)
238     rm -f "$ofile"
239     mkimage -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
240         $uboot_version -d "$vmz" "$ofile"
241     if [ -z "$cacheit" ]; then
242         rm -f "$vmz"
243     fi
244     exit 0
245     ;;
246 esac
247
248 addsec() {
249     ${CROSS}objcopy $4 $1 \
250         --add-section=$3="$2" \
251         --set-section-flags=$3=contents,alloc,load,readonly,data
252 }
253
254 addsec $tmp "$vmz" $ksection $object/empty.o
255 if [ -z "$cacheit" ]; then
256     rm -f "$vmz"
257 fi
258
259 if [ -n "$initrd" ]; then
260     addsec $tmp "$initrd" $isection
261 fi
262
263 if [ -n "$dtb" ]; then
264     addsec $tmp "$dtb" .kernel:dtb
265     if [ -n "$dts" ]; then
266         rm $dtb
267     fi
268 fi
269
270 if [ "$platform" != "miboot" ]; then
271     ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
272         $platformo $tmp $object/wrapper.a
273     rm $tmp
274 fi
275
276 # Some platforms need the zImage's entry point and base address
277 base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
278 entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
279
280 if [ -n "$binary" ]; then
281     mv "$ofile" "$ofile".elf
282     ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
283 fi
284
285 # post-processing needed for some platforms
286 case "$platform" in
287 pseries|chrp)
288     $objbin/addnote "$ofile"
289     ;;
290 coff)
291     ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
292     $objbin/hack-coff "$ofile"
293     ;;
294 cuboot*)
295     gzip -f -9 "$ofile"
296     mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
297             $uboot_version -d "$ofile".gz "$ofile"
298     ;;
299 treeboot*)
300     mv "$ofile" "$ofile.elf"
301     $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
302     if [ -z "$cacheit" ]; then
303         rm -f "$ofile.elf"
304     fi
305     exit 0
306     ;;
307 ps3)
308     # The ps3's loader supports loading a gzipped binary image from flash
309     # rom to ram addr zero. The loader then enters the system reset
310     # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
311     # a binary image of the kernel to be at addr zero, and yet have a
312     # suitable bootwrapper entry at 0x100.  To construct the final rom
313     # image 512 bytes from offset 0x100 is copied to the bootwrapper
314     # place holder at symbol __system_reset_kernel.  The 512 bytes of the
315     # bootwrapper entry code at symbol __system_reset_overlay is then
316     # copied to offset 0x100.  At runtime the bootwrapper program copies
317     # the data at __system_reset_kernel back to addr 0x100.
318
319     system_reset_overlay=0x`${CROSS}nm "$ofile" \
320         | grep ' __system_reset_overlay$'       \
321         | cut -d' ' -f1`
322     system_reset_overlay=`printf "%d" $system_reset_overlay`
323     system_reset_kernel=0x`${CROSS}nm "$ofile" \
324         | grep ' __system_reset_kernel$'       \
325         | cut -d' ' -f1`
326     system_reset_kernel=`printf "%d" $system_reset_kernel`
327     overlay_dest="256"
328     overlay_size="512"
329
330     ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
331
332     dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
333         skip=$overlay_dest seek=$system_reset_kernel  \
334         count=$overlay_size bs=1
335
336     dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
337         skip=$system_reset_overlay seek=$overlay_dest \
338         count=$overlay_size bs=1
339
340     odir="$(dirname "$ofile.bin")"
341     rm -f "$odir/otheros.bld"
342     gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
343     ;;
344 esac