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