xref: /linux/arch/powerpc/boot/wrapper (revision 59d13f9dba56c444e5356b42d3d57b46e44ef975)
12bf11819SPaul Mackerras#!/bin/sh
22bf11819SPaul Mackerras
32bf11819SPaul Mackerras# Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
42bf11819SPaul Mackerras# This program may be used under the terms of version 2 of the GNU
52bf11819SPaul Mackerras# General Public License.
62bf11819SPaul Mackerras
72bf11819SPaul Mackerras# This script takes a kernel binary and optionally an initrd image
82bf11819SPaul Mackerras# and/or a device-tree blob, and creates a bootable zImage for a
92bf11819SPaul Mackerras# given platform.
102bf11819SPaul Mackerras
112bf11819SPaul Mackerras# Options:
122bf11819SPaul Mackerras# -o zImage	specify output file
132bf11819SPaul Mackerras# -p platform	specify platform (links in $platform.o)
142bf11819SPaul Mackerras# -i initrd	specify initrd file
152bf11819SPaul Mackerras# -d devtree	specify device-tree blob
162bf11819SPaul Mackerras# -s tree.dts	specify device-tree source file (needs dtc installed)
172bf11819SPaul Mackerras# -c		cache $kernel.strip.gz (use if present & newer, else make)
182bf11819SPaul Mackerras# -C prefix	specify command prefix for cross-building tools
192bf11819SPaul Mackerras#		(strip, objcopy, ld)
202bf11819SPaul Mackerras# -D dir	specify directory containing data files used by script
212bf11819SPaul Mackerras#		(default ./arch/powerpc/boot)
222bf11819SPaul Mackerras# -W dir	specify working directory for temporary files (default .)
232bf11819SPaul Mackerras
24d4740373SGrant Likely# Stop execution if any command fails
25d4740373SGrant Likelyset -e
26d4740373SGrant Likely
277f66c1fdSGrant Likely# Allow for verbose output
287f66c1fdSGrant Likelyif [ "$V" = 1 ]; then
297f66c1fdSGrant Likely    set -x
307f66c1fdSGrant Likelyfi
317f66c1fdSGrant Likely
322bf11819SPaul Mackerras# defaults
332bf11819SPaul Mackerraskernel=
342bf11819SPaul Mackerrasofile=zImage
352bf11819SPaul Mackerrasplatform=of
362bf11819SPaul Mackerrasinitrd=
372bf11819SPaul Mackerrasdtb=
382bf11819SPaul Mackerrasdts=
392bf11819SPaul Mackerrascacheit=
4011c146ccSScott Woodbinary=
41a9903811SScott Woodgzip=.gz
422bf11819SPaul Mackerras
432bf11819SPaul Mackerras# cross-compilation prefix
442bf11819SPaul MackerrasCROSS=
452bf11819SPaul Mackerras
462bf11819SPaul Mackerras# directory for object and other files used by this script
472bf11819SPaul Mackerrasobject=arch/powerpc/boot
485c539ee3SDavid Woodhouseobjbin=$object
492bf11819SPaul Mackerras
502bf11819SPaul Mackerras# directory for working files
512bf11819SPaul Mackerrastmpdir=.
522bf11819SPaul Mackerras
532bf11819SPaul Mackerrasusage() {
542bf11819SPaul Mackerras    echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
552bf11819SPaul Mackerras    echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
56a9903811SScott Wood    echo '       [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
572bf11819SPaul Mackerras    exit 1
582bf11819SPaul Mackerras}
592bf11819SPaul Mackerras
602bf11819SPaul Mackerraswhile [ "$#" -gt 0 ]; do
612bf11819SPaul Mackerras    case "$1" in
622bf11819SPaul Mackerras    -o)
632bf11819SPaul Mackerras	shift
642bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
652bf11819SPaul Mackerras	ofile="$1"
662bf11819SPaul Mackerras	;;
672bf11819SPaul Mackerras    -p)
682bf11819SPaul Mackerras	shift
692bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
702bf11819SPaul Mackerras	platform="$1"
712bf11819SPaul Mackerras	;;
722bf11819SPaul Mackerras    -i)
732bf11819SPaul Mackerras	shift
742bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
752bf11819SPaul Mackerras	initrd="$1"
762bf11819SPaul Mackerras	;;
772bf11819SPaul Mackerras    -d)
782bf11819SPaul Mackerras	shift
792bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
802bf11819SPaul Mackerras	dtb="$1"
812bf11819SPaul Mackerras	;;
822bf11819SPaul Mackerras    -s)
832bf11819SPaul Mackerras	shift
842bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
852bf11819SPaul Mackerras	dts="$1"
862bf11819SPaul Mackerras	;;
872bf11819SPaul Mackerras    -c)
882bf11819SPaul Mackerras	cacheit=y
892bf11819SPaul Mackerras	;;
902bf11819SPaul Mackerras    -C)
912bf11819SPaul Mackerras	shift
922bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
932bf11819SPaul Mackerras	CROSS="$1"
942bf11819SPaul Mackerras	;;
952bf11819SPaul Mackerras    -D)
962bf11819SPaul Mackerras	shift
972bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
982bf11819SPaul Mackerras	object="$1"
995c539ee3SDavid Woodhouse	objbin="$1"
1002bf11819SPaul Mackerras	;;
1012bf11819SPaul Mackerras    -W)
1022bf11819SPaul Mackerras	shift
1032bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1042bf11819SPaul Mackerras	tmpdir="$1"
1052bf11819SPaul Mackerras	;;
106a9903811SScott Wood    --no-gzip)
107a9903811SScott Wood        gzip=
108a9903811SScott Wood        ;;
1092bf11819SPaul Mackerras    -?)
1102bf11819SPaul Mackerras	usage
1112bf11819SPaul Mackerras	;;
1122bf11819SPaul Mackerras    *)
1132bf11819SPaul Mackerras	[ -z "$kernel" ] || usage
1142bf11819SPaul Mackerras	kernel="$1"
1152bf11819SPaul Mackerras	;;
1162bf11819SPaul Mackerras    esac
1172bf11819SPaul Mackerras    shift
1182bf11819SPaul Mackerrasdone
1192bf11819SPaul Mackerras
1202bf11819SPaul Mackerrasif [ -n "$dts" ]; then
121701172d1SDavid Woodhouse    if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
122701172d1SDavid Woodhouse	dts="$object/dts/$dts"
123701172d1SDavid Woodhouse    fi
1242bf11819SPaul Mackerras    if [ -z "$dtb" ]; then
1252bf11819SPaul Mackerras	dtb="$platform.dtb"
1262bf11819SPaul Mackerras    fi
127e2dc87a1SDavid Gibson    $object/dtc -O dtb -o "$dtb" -b 0 "$dts"
1282bf11819SPaul Mackerrasfi
1292bf11819SPaul Mackerras
1302bf11819SPaul Mackerrasif [ -z "$kernel" ]; then
1312bf11819SPaul Mackerras    kernel=vmlinux
1322bf11819SPaul Mackerrasfi
1332bf11819SPaul Mackerras
1342bf11819SPaul Mackerrasplatformo=$object/"$platform".o
1352bf11819SPaul Mackerraslds=$object/zImage.lds
1362bf11819SPaul Mackerrasext=strip
1372bf11819SPaul Mackerrasobjflags=-S
1382bf11819SPaul Mackerrastmp=$tmpdir/zImage.$$.o
1392bf11819SPaul Mackerrasksection=.kernel:vmlinux.strip
1402bf11819SPaul Mackerrasisection=.kernel:initrd
1412bf11819SPaul Mackerras
1422bf11819SPaul Mackerrascase "$platform" in
1432bf11819SPaul Mackerraspmac|pseries|chrp)
1442bf11819SPaul Mackerras    platformo=$object/of.o
1452bf11819SPaul Mackerras    ;;
146627aa944SMilton Millercoff)
1472bf11819SPaul Mackerras    platformo=$object/of.o
1482bf11819SPaul Mackerras    lds=$object/zImage.coff.lds
1492bf11819SPaul Mackerras    ;;
1502bf11819SPaul Mackerrasmiboot|uboot)
1512bf11819SPaul Mackerras    # miboot and U-boot want just the bare bits, not an ELF binary
1522bf11819SPaul Mackerras    ext=bin
1532bf11819SPaul Mackerras    objflags="-O binary"
1542bf11819SPaul Mackerras    tmp="$ofile"
1552bf11819SPaul Mackerras    ksection=image
1562bf11819SPaul Mackerras    isection=initrd
1572bf11819SPaul Mackerras    ;;
1580fdd717eSScott Woodcuboot*)
15911c146ccSScott Wood    binary=y
1600fdd717eSScott Wood    gzip=
16125431333SGrant Likely    case "$platform" in
16225431333SGrant Likely    *-mpc885ads|*-adder875*|*-ep88xc)
16325431333SGrant Likely        platformo=$object/cuboot-8xx.o
16425431333SGrant Likely        ;;
16525431333SGrant Likely    *5200*|*-motionpro)
16625431333SGrant Likely        platformo=$object/cuboot-52xx.o
16725431333SGrant Likely        ;;
16825431333SGrant Likely    *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
16925431333SGrant Likely        platformo=$object/cuboot-pq2.o
17025431333SGrant Likely        ;;
17125431333SGrant Likely    *-mpc824*)
17225431333SGrant Likely        platformo=$object/cuboot-824x.o
17325431333SGrant Likely        ;;
174*59d13f9dSBryan O'Donoghue    *-mpc83*|*-asp834x*)
17525431333SGrant Likely        platformo=$object/cuboot-83xx.o
17625431333SGrant Likely        ;;
177ff880112SAlexandr Smirnov    *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
17825431333SGrant Likely        platformo=$object/cuboot-85xx-cpm2.o
17925431333SGrant Likely        ;;
180a72a6f53SPaul Gortmaker    *-mpc85*|*-tqm8540|*-sbc85*)
18125431333SGrant Likely        platformo=$object/cuboot-85xx.o
18225431333SGrant Likely        ;;
18325431333SGrant Likely    esac
1840fdd717eSScott Wood    ;;
185bafdb645SGeoff Levandps3)
186bafdb645SGeoff Levand    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
187bafdb645SGeoff Levand    lds=$object/zImage.ps3.lds
188bafdb645SGeoff Levand    gzip=
189bafdb645SGeoff Levand    ext=bin
190bafdb645SGeoff Levand    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
191bafdb645SGeoff Levand    ksection=.kernel:vmlinux.bin
192bafdb645SGeoff Levand    isection=.kernel:initrd
193bafdb645SGeoff Levand    ;;
194a55387e5SScott Woodep88xc|ep405|ep8248e)
19511c146ccSScott Wood    platformo="$object/fixed-head.o $object/$platform.o"
19611c146ccSScott Wood    binary=y
19711c146ccSScott Wood    ;;
198a55387e5SScott Woodadder875-redboot)
199a55387e5SScott Wood    platformo="$object/fixed-head.o $object/redboot-8xx.o"
200a55387e5SScott Wood    binary=y
201a55387e5SScott Wood    ;;
202d2477b5cSGrant Likelysimpleboot-virtex405-*)
203d2477b5cSGrant Likely    platformo="$object/virtex405-head.o $object/simpleboot.o"
204d2477b5cSGrant Likely    binary=y
205d2477b5cSGrant Likely    ;;
206*59d13f9dSBryan O'Donoghueasp834x-redboot)
207*59d13f9dSBryan O'Donoghue    platformo="$object/fixed-head.o $object/redboot-83xx.o"
208*59d13f9dSBryan O'Donoghue    binary=y
209*59d13f9dSBryan O'Donoghue    ;;
2102bf11819SPaul Mackerrasesac
2112bf11819SPaul Mackerras
2122bf11819SPaul Mackerrasvmz="$tmpdir/`basename \"$kernel\"`.$ext"
2131383a34fSMilton Millerif [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
2142bf11819SPaul Mackerras    ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
215a9903811SScott Wood
216a9903811SScott Wood    if [ -n "$gzip" ]; then
2172bf11819SPaul Mackerras        gzip -f -9 "$vmz.$$"
218a9903811SScott Wood    fi
219a9903811SScott Wood
2202bf11819SPaul Mackerras    if [ -n "$cacheit" ]; then
221a9903811SScott Wood	mv -f "$vmz.$$$gzip" "$vmz$gzip"
2222bf11819SPaul Mackerras    else
2232bf11819SPaul Mackerras	vmz="$vmz.$$"
2242bf11819SPaul Mackerras    fi
2252bf11819SPaul Mackerrasfi
2262bf11819SPaul Mackerras
227a9903811SScott Woodvmz="$vmz$gzip"
228a9903811SScott Wood
229a6afacb6SDavid Gibson# Extract kernel version information, some platforms want to include
230a6afacb6SDavid Gibson# it in the image header
2312bf11819SPaul Mackerrasversion=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
2322bf11819SPaul Mackerras    cut -d' ' -f3`
2332bf11819SPaul Mackerrasif [ -n "$version" ]; then
234a6afacb6SDavid Gibson    uboot_version="-n Linux-$version"
2352bf11819SPaul Mackerrasfi
2360fdd717eSScott Wood
237b18796d3SKumar Gala# physical offset of kernel image
238b18796d3SKumar Galamembase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
239b18796d3SKumar Gala
2400fdd717eSScott Woodcase "$platform" in
2410fdd717eSScott Wooduboot)
2420fdd717eSScott Wood    rm -f "$ofile"
243b18796d3SKumar Gala    mkimage -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
244a6afacb6SDavid Gibson	$uboot_version -d "$vmz" "$ofile"
2452bf11819SPaul Mackerras    if [ -z "$cacheit" ]; then
246a9903811SScott Wood	rm -f "$vmz"
2472bf11819SPaul Mackerras    fi
2482bf11819SPaul Mackerras    exit 0
2492bf11819SPaul Mackerras    ;;
2502bf11819SPaul Mackerrasesac
2512bf11819SPaul Mackerras
2522bf11819SPaul Mackerrasaddsec() {
2532bf11819SPaul Mackerras    ${CROSS}objcopy $4 $1 \
2542bf11819SPaul Mackerras	--add-section=$3="$2" \
2552bf11819SPaul Mackerras	--set-section-flags=$3=contents,alloc,load,readonly,data
2562bf11819SPaul Mackerras}
2572bf11819SPaul Mackerras
258a9903811SScott Woodaddsec $tmp "$vmz" $ksection $object/empty.o
2592bf11819SPaul Mackerrasif [ -z "$cacheit" ]; then
260a9903811SScott Wood    rm -f "$vmz"
2612bf11819SPaul Mackerrasfi
2622bf11819SPaul Mackerras
2632bf11819SPaul Mackerrasif [ -n "$initrd" ]; then
264c888554bSMark A. Greer    addsec $tmp "$initrd" $isection
2652bf11819SPaul Mackerrasfi
2662bf11819SPaul Mackerras
2672bf11819SPaul Mackerrasif [ -n "$dtb" ]; then
268c888554bSMark A. Greer    addsec $tmp "$dtb" .kernel:dtb
269e9c4b4bdSMark A. Greer    if [ -n "$dts" ]; then
270e9c4b4bdSMark A. Greer	rm $dtb
271e9c4b4bdSMark A. Greer    fi
2722bf11819SPaul Mackerrasfi
2732bf11819SPaul Mackerras
2742bf11819SPaul Mackerrasif [ "$platform" != "miboot" ]; then
2752bf11819SPaul Mackerras    ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
276cd197ffcSDavid Gibson	$platformo $tmp $object/wrapper.a
2772bf11819SPaul Mackerras    rm $tmp
2782bf11819SPaul Mackerrasfi
2792bf11819SPaul Mackerras
280a6afacb6SDavid Gibson# Some platforms need the zImage's entry point and base address
281a6afacb6SDavid Gibsonbase=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
282a6afacb6SDavid Gibsonentry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
283a6afacb6SDavid Gibson
28411c146ccSScott Woodif [ -n "$binary" ]; then
28511c146ccSScott Wood    mv "$ofile" "$ofile".elf
286aeb4552fSScott Wood    ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
28711c146ccSScott Woodfi
28811c146ccSScott Wood
2892bf11819SPaul Mackerras# post-processing needed for some platforms
2902bf11819SPaul Mackerrascase "$platform" in
2912bf11819SPaul Mackerraspseries|chrp)
2925c539ee3SDavid Woodhouse    $objbin/addnote "$ofile"
2932bf11819SPaul Mackerras    ;;
294627aa944SMilton Millercoff)
295cd197ffcSDavid Gibson    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
2965c539ee3SDavid Woodhouse    $objbin/hack-coff "$ofile"
2972bf11819SPaul Mackerras    ;;
2980fdd717eSScott Woodcuboot*)
299aeb4552fSScott Wood    gzip -f -9 "$ofile"
3000fdd717eSScott Wood    mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
301aeb4552fSScott Wood            $uboot_version -d "$ofile".gz "$ofile"
3020fdd717eSScott Wood    ;;
303f6dfc805SDavid Gibsontreeboot*)
304f6dfc805SDavid Gibson    mv "$ofile" "$ofile.elf"
3055c539ee3SDavid Woodhouse    $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
306f6dfc805SDavid Gibson    if [ -z "$cacheit" ]; then
307f6dfc805SDavid Gibson	rm -f "$ofile.elf"
308f6dfc805SDavid Gibson    fi
309f6dfc805SDavid Gibson    exit 0
310f6dfc805SDavid Gibson    ;;
311bafdb645SGeoff Levandps3)
3125761eaa3SGeoff Levand    # The ps3's loader supports loading a gzipped binary image from flash
3135761eaa3SGeoff Levand    # rom to ram addr zero. The loader then enters the system reset
3145761eaa3SGeoff Levand    # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
3155761eaa3SGeoff Levand    # a binary image of the kernel to be at addr zero, and yet have a
3165761eaa3SGeoff Levand    # suitable bootwrapper entry at 0x100.  To construct the final rom
3175761eaa3SGeoff Levand    # image 512 bytes from offset 0x100 is copied to the bootwrapper
3185761eaa3SGeoff Levand    # place holder at symbol __system_reset_kernel.  The 512 bytes of the
3195761eaa3SGeoff Levand    # bootwrapper entry code at symbol __system_reset_overlay is then
3205761eaa3SGeoff Levand    # copied to offset 0x100.  At runtime the bootwrapper program copies
3215761eaa3SGeoff Levand    # the data at __system_reset_kernel back to addr 0x100.
322bafdb645SGeoff Levand
323aeb4552fSScott Wood    system_reset_overlay=0x`${CROSS}nm "$ofile" \
324bafdb645SGeoff Levand        | grep ' __system_reset_overlay$'       \
325bafdb645SGeoff Levand        | cut -d' ' -f1`
326bafdb645SGeoff Levand    system_reset_overlay=`printf "%d" $system_reset_overlay`
327aeb4552fSScott Wood    system_reset_kernel=0x`${CROSS}nm "$ofile" \
328bafdb645SGeoff Levand        | grep ' __system_reset_kernel$'       \
329bafdb645SGeoff Levand        | cut -d' ' -f1`
330bafdb645SGeoff Levand    system_reset_kernel=`printf "%d" $system_reset_kernel`
331bafdb645SGeoff Levand    overlay_dest="256"
3325761eaa3SGeoff Levand    overlay_size="512"
333bafdb645SGeoff Levand
334aeb4552fSScott Wood    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
335aeb4552fSScott Wood
336d4740373SGrant Likely    dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
337bafdb645SGeoff Levand        skip=$overlay_dest seek=$system_reset_kernel  \
338d4740373SGrant Likely        count=$overlay_size bs=1
339bafdb645SGeoff Levand
340d4740373SGrant Likely    dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
341bafdb645SGeoff Levand        skip=$system_reset_overlay seek=$overlay_dest \
342d4740373SGrant Likely        count=$overlay_size bs=1
343bafdb645SGeoff Levand
344928b9695SDavid Woodhouse    odir="$(dirname "$ofile.bin")"
345928b9695SDavid Woodhouse    rm -f "$odir/otheros.bld"
346928b9695SDavid Woodhouse    gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
347bafdb645SGeoff Levand    ;;
3482bf11819SPaul Mackerrasesac
349