xref: /linux/arch/powerpc/boot/wrapper (revision 00b46d22a47b0b6d0dea8b481f389f4830c45d94)
12bf11819SPaul Mackerras#!/bin/sh
2*00b46d22SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
32bf11819SPaul Mackerras
42bf11819SPaul Mackerras# Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
52bf11819SPaul Mackerras
62bf11819SPaul Mackerras# This script takes a kernel binary and optionally an initrd image
72bf11819SPaul Mackerras# and/or a device-tree blob, and creates a bootable zImage for a
82bf11819SPaul Mackerras# given platform.
92bf11819SPaul Mackerras
102bf11819SPaul Mackerras# Options:
112bf11819SPaul Mackerras# -o zImage	specify output file
122bf11819SPaul Mackerras# -p platform	specify platform (links in $platform.o)
132bf11819SPaul Mackerras# -i initrd	specify initrd file
142bf11819SPaul Mackerras# -d devtree	specify device-tree blob
152bf11819SPaul Mackerras# -s tree.dts	specify device-tree source file (needs dtc installed)
162bf11819SPaul Mackerras# -c		cache $kernel.strip.gz (use if present & newer, else make)
172bf11819SPaul Mackerras# -C prefix	specify command prefix for cross-building tools
182bf11819SPaul Mackerras#		(strip, objcopy, ld)
192bf11819SPaul Mackerras# -D dir	specify directory containing data files used by script
202bf11819SPaul Mackerras#		(default ./arch/powerpc/boot)
212bf11819SPaul Mackerras# -W dir	specify working directory for temporary files (default .)
22f1e510bbSOliver O'Halloran# -z		use gzip (legacy)
23f1e510bbSOliver O'Halloran# -Z zsuffix    compression to use (gz, xz or none)
242bf11819SPaul Mackerras
25d4740373SGrant Likely# Stop execution if any command fails
26d4740373SGrant Likelyset -e
27d4740373SGrant Likely
287f66c1fdSGrant Likely# Allow for verbose output
297f66c1fdSGrant Likelyif [ "$V" = 1 ]; then
307f66c1fdSGrant Likely    set -x
317f66c1fdSGrant Likelyfi
327f66c1fdSGrant Likely
332bf11819SPaul Mackerras# defaults
342bf11819SPaul Mackerraskernel=
352bf11819SPaul Mackerrasofile=zImage
362bf11819SPaul Mackerrasplatform=of
372bf11819SPaul Mackerrasinitrd=
382bf11819SPaul Mackerrasdtb=
392bf11819SPaul Mackerrasdts=
402bf11819SPaul Mackerrascacheit=
4111c146ccSScott Woodbinary=
42f1e510bbSOliver O'Hallorancompression=.gz
436975a783SMichael Ellermanpie=
44147c0516SCédric Le Goaterformat=
452bf11819SPaul Mackerras
462bf11819SPaul Mackerras# cross-compilation prefix
472bf11819SPaul MackerrasCROSS=
482bf11819SPaul Mackerras
493f884bf5SPeter Tyser# mkimage wrapper script
503f884bf5SPeter TyserMKIMAGE=$srctree/scripts/mkuboot.sh
513f884bf5SPeter Tyser
522bf11819SPaul Mackerras# directory for object and other files used by this script
532bf11819SPaul Mackerrasobject=arch/powerpc/boot
545c539ee3SDavid Woodhouseobjbin=$object
55c79b2973SLucian Adrian Grijincudtc=scripts/dtc/dtc
562bf11819SPaul Mackerras
572bf11819SPaul Mackerras# directory for working files
582bf11819SPaul Mackerrastmpdir=.
592bf11819SPaul Mackerras
602bf11819SPaul Mackerrasusage() {
612bf11819SPaul Mackerras    echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
622bf11819SPaul Mackerras    echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
63f1e510bbSOliver O'Halloran    echo '       [-D datadir] [-W workingdir] [-Z (gz|xz|none)]' >&2
64f1e510bbSOliver O'Halloran    echo '       [--no-compression] [vmlinux]' >&2
652bf11819SPaul Mackerras    exit 1
662bf11819SPaul Mackerras}
672bf11819SPaul Mackerras
68879c26d4SGeoff Levandrun_cmd() {
69879c26d4SGeoff Levand    if [ "$V" = 1 ]; then
70879c26d4SGeoff Levand        $* 2>&1
71879c26d4SGeoff Levand    else
72879c26d4SGeoff Levand        local msg
73879c26d4SGeoff Levand
74879c26d4SGeoff Levand        set +e
75879c26d4SGeoff Levand        msg=$($* 2>&1)
76879c26d4SGeoff Levand
77879c26d4SGeoff Levand        if [ $? -ne "0" ]; then
78879c26d4SGeoff Levand                echo $msg
79879c26d4SGeoff Levand                exit 1
80879c26d4SGeoff Levand        fi
81879c26d4SGeoff Levand        set -e
82879c26d4SGeoff Levand    fi
83879c26d4SGeoff Levand}
84879c26d4SGeoff Levand
852bf11819SPaul Mackerraswhile [ "$#" -gt 0 ]; do
862bf11819SPaul Mackerras    case "$1" in
872bf11819SPaul Mackerras    -o)
882bf11819SPaul Mackerras	shift
892bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
902bf11819SPaul Mackerras	ofile="$1"
912bf11819SPaul Mackerras	;;
922bf11819SPaul Mackerras    -p)
932bf11819SPaul Mackerras	shift
942bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
952bf11819SPaul Mackerras	platform="$1"
962bf11819SPaul Mackerras	;;
972bf11819SPaul Mackerras    -i)
982bf11819SPaul Mackerras	shift
992bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1002bf11819SPaul Mackerras	initrd="$1"
1012bf11819SPaul Mackerras	;;
1022bf11819SPaul Mackerras    -d)
1032bf11819SPaul Mackerras	shift
1042bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1052bf11819SPaul Mackerras	dtb="$1"
1062bf11819SPaul Mackerras	;;
1072bf11819SPaul Mackerras    -s)
1082bf11819SPaul Mackerras	shift
1092bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1102bf11819SPaul Mackerras	dts="$1"
1112bf11819SPaul Mackerras	;;
1122bf11819SPaul Mackerras    -c)
1132bf11819SPaul Mackerras	cacheit=y
1142bf11819SPaul Mackerras	;;
1152bf11819SPaul Mackerras    -C)
1162bf11819SPaul Mackerras	shift
1172bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1182bf11819SPaul Mackerras	CROSS="$1"
1192bf11819SPaul Mackerras	;;
1202bf11819SPaul Mackerras    -D)
1212bf11819SPaul Mackerras	shift
1222bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1232bf11819SPaul Mackerras	object="$1"
1245c539ee3SDavid Woodhouse	objbin="$1"
1252bf11819SPaul Mackerras	;;
1262bf11819SPaul Mackerras    -W)
1272bf11819SPaul Mackerras	shift
1282bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1292bf11819SPaul Mackerras	tmpdir="$1"
1302bf11819SPaul Mackerras	;;
131f1e510bbSOliver O'Halloran    -z)
132f1e510bbSOliver O'Halloran	compression=.gz
133f1e510bbSOliver O'Halloran	;;
134f1e510bbSOliver O'Halloran    -Z)
135f1e510bbSOliver O'Halloran	shift
136f1e510bbSOliver O'Halloran	[ "$#" -gt 0 ] || usage
137f1e510bbSOliver O'Halloran        [ "$1" != "gz" -o "$1" != "xz" -o "$1" != "none" ] || usage
138f1e510bbSOliver O'Halloran
139f1e510bbSOliver O'Halloran	compression=".$1"
140f1e510bbSOliver O'Halloran
141f1e510bbSOliver O'Halloran        if [ $compression = ".none" ]; then
142f1e510bbSOliver O'Halloran                compression=
143f1e510bbSOliver O'Halloran        fi
144f1e510bbSOliver O'Halloran	;;
145a9903811SScott Wood    --no-gzip)
146f1e510bbSOliver O'Halloran        # a "feature" of the the wrapper script is that it can be used outside
147f1e510bbSOliver O'Halloran        # the kernel tree. So keeping this around for backwards compatibility.
148f1e510bbSOliver O'Halloran        compression=
149a9903811SScott Wood        ;;
1502bf11819SPaul Mackerras    -?)
1512bf11819SPaul Mackerras	usage
1522bf11819SPaul Mackerras	;;
1532bf11819SPaul Mackerras    *)
1542bf11819SPaul Mackerras	[ -z "$kernel" ] || usage
1552bf11819SPaul Mackerras	kernel="$1"
1562bf11819SPaul Mackerras	;;
1572bf11819SPaul Mackerras    esac
1582bf11819SPaul Mackerras    shift
1592bf11819SPaul Mackerrasdone
1602bf11819SPaul Mackerras
161f1e510bbSOliver O'Halloran
1622bf11819SPaul Mackerrasif [ -n "$dts" ]; then
163701172d1SDavid Woodhouse    if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
164701172d1SDavid Woodhouse	dts="$object/dts/$dts"
165701172d1SDavid Woodhouse    fi
1662bf11819SPaul Mackerras    if [ -z "$dtb" ]; then
1672bf11819SPaul Mackerras	dtb="$platform.dtb"
1682bf11819SPaul Mackerras    fi
169c79b2973SLucian Adrian Grijincu    $dtc -O dtb -o "$dtb" -b 0 "$dts"
1702bf11819SPaul Mackerrasfi
1712bf11819SPaul Mackerras
1722bf11819SPaul Mackerrasif [ -z "$kernel" ]; then
1732bf11819SPaul Mackerras    kernel=vmlinux
1742bf11819SPaul Mackerrasfi
1752bf11819SPaul Mackerras
17658531b0cSLaurent VivierLANG=C elfformat="`${CROSS}objdump -p "$kernel" | grep 'file format' | awk '{print $4}'`"
177147c0516SCédric Le Goatercase "$elfformat" in
178147c0516SCédric Le Goater    elf64-powerpcle)	format=elf64lppc	;;
179147c0516SCédric Le Goater    elf64-powerpc)	format=elf32ppc	;;
180147c0516SCédric Le Goater    elf32-powerpc)	format=elf32ppc	;;
181147c0516SCédric Le Goateresac
182147c0516SCédric Le Goater
183ff45000fSNicholas Pigginld_version()
184ff45000fSNicholas Piggin{
185ff45000fSNicholas Piggin    # Poached from scripts/ld-version.sh, but we don't want to call that because
186ff45000fSNicholas Piggin    # this script (wrapper) is distributed separately from the kernel source.
187ff45000fSNicholas Piggin    # Extract linker version number from stdin and turn into single number.
188ff45000fSNicholas Piggin    awk '{
189ff45000fSNicholas Piggin	gsub(".*\\)", "");
190ff45000fSNicholas Piggin	gsub(".*version ", "");
191ff45000fSNicholas Piggin	gsub("-.*", "");
192ff45000fSNicholas Piggin	split($1,a, ".");
193ff45000fSNicholas Piggin	print a[1]*100000000 + a[2]*1000000 + a[3]*10000;
194ff45000fSNicholas Piggin	exit
195ff45000fSNicholas Piggin    }'
196ff45000fSNicholas Piggin}
197ff45000fSNicholas Piggin
198ff45000fSNicholas Piggin# Do not include PT_INTERP segment when linking pie. Non-pie linking
199ff45000fSNicholas Piggin# just ignores this option.
200ff45000fSNicholas PigginLD_VERSION=$(${CROSS}ld --version | ld_version)
201ff45000fSNicholas PigginLD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
202ff45000fSNicholas Pigginif [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
203ff45000fSNicholas Piggin	nodl="--no-dynamic-linker"
204ff45000fSNicholas Pigginfi
205147c0516SCédric Le Goater
2062bf11819SPaul Mackerrasplatformo=$object/"$platform".o
2072bf11819SPaul Mackerraslds=$object/zImage.lds
2082bf11819SPaul Mackerrasext=strip
2092bf11819SPaul Mackerrasobjflags=-S
2102bf11819SPaul Mackerrastmp=$tmpdir/zImage.$$.o
2112bf11819SPaul Mackerrasksection=.kernel:vmlinux.strip
2122bf11819SPaul Mackerrasisection=.kernel:initrd
2139b09c6d9STony Breedslink_address='0x400000'
214dfbc2d75SStephen Rothwellmake_space=y
2152bf11819SPaul Mackerras
2162bf11819SPaul Mackerrascase "$platform" in
21744790a0bSBenjamin Herrenschmidtof)
21844790a0bSBenjamin Herrenschmidt    platformo="$object/of.o $object/epapr.o"
21944790a0bSBenjamin Herrenschmidt    make_space=n
22044790a0bSBenjamin Herrenschmidt    ;;
2219b09c6d9STony Breedspseries)
2222d9afb36SCédric Le Goater    platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
2239b09c6d9STony Breeds    link_address='0x4000000'
224147c0516SCédric Le Goater    if [ "$format" != "elf32ppc" ]; then
225147c0516SCédric Le Goater	link_address=
226147c0516SCédric Le Goater	pie=-pie
227147c0516SCédric Le Goater    fi
228f5467e28SPaul Mackerras    make_space=n
2299b09c6d9STony Breeds    ;;
23058706ef9SCorey Minyardmaple)
2310c9fa291SBenjamin Herrenschmidt    platformo="$object/of.o $object/epapr.o"
23258706ef9SCorey Minyard    link_address='0x400000'
233f5467e28SPaul Mackerras    make_space=n
23458706ef9SCorey Minyard    ;;
2359b09c6d9STony Breedspmac|chrp)
2360c9fa291SBenjamin Herrenschmidt    platformo="$object/of.o $object/epapr.o"
237f5467e28SPaul Mackerras    make_space=n
2382bf11819SPaul Mackerras    ;;
239627aa944SMilton Millercoff)
2400c9fa291SBenjamin Herrenschmidt    platformo="$object/crt0.o $object/of.o $object/epapr.o"
2412bf11819SPaul Mackerras    lds=$object/zImage.coff.lds
2429b09c6d9STony Breeds    link_address='0x500000'
243f5467e28SPaul Mackerras    make_space=n
2446975a783SMichael Ellerman    pie=
2452bf11819SPaul Mackerras    ;;
24611eab297SBenjamin Herrenschmidtmiboot|uboot*)
2472bf11819SPaul Mackerras    # miboot and U-boot want just the bare bits, not an ELF binary
2482bf11819SPaul Mackerras    ext=bin
2492bf11819SPaul Mackerras    objflags="-O binary"
2502bf11819SPaul Mackerras    tmp="$ofile"
2512bf11819SPaul Mackerras    ksection=image
2522bf11819SPaul Mackerras    isection=initrd
2532bf11819SPaul Mackerras    ;;
2540fdd717eSScott Woodcuboot*)
25511c146ccSScott Wood    binary=y
256f1e510bbSOliver O'Halloran    compression=
25725431333SGrant Likely    case "$platform" in
2588dd217b2SScott Wood    *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
25925431333SGrant Likely        platformo=$object/cuboot-8xx.o
26025431333SGrant Likely        ;;
26125431333SGrant Likely    *5200*|*-motionpro)
26225431333SGrant Likely        platformo=$object/cuboot-52xx.o
26325431333SGrant Likely        ;;
26425431333SGrant Likely    *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
26525431333SGrant Likely        platformo=$object/cuboot-pq2.o
26625431333SGrant Likely        ;;
26725431333SGrant Likely    *-mpc824*)
26825431333SGrant Likely        platformo=$object/cuboot-824x.o
26925431333SGrant Likely        ;;
27059d13f9dSBryan O'Donoghue    *-mpc83*|*-asp834x*)
27125431333SGrant Likely        platformo=$object/cuboot-83xx.o
27225431333SGrant Likely        ;;
273ff880112SAlexandr Smirnov    *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
27425431333SGrant Likely        platformo=$object/cuboot-85xx-cpm2.o
27525431333SGrant Likely        ;;
2766dd1b64aSWolfgang Grandegger    *-mpc85*|*-tqm85*|*-sbc85*)
27725431333SGrant Likely        platformo=$object/cuboot-85xx.o
27825431333SGrant Likely        ;;
2798f23735dSGerhard Pircher    *-amigaone)
2808f23735dSGerhard Pircher        link_address='0x800000'
2818f23735dSGerhard Pircher        ;;
28225431333SGrant Likely    esac
2830fdd717eSScott Wood    ;;
284bafdb645SGeoff Levandps3)
285bafdb645SGeoff Levand    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
286bafdb645SGeoff Levand    lds=$object/zImage.ps3.lds
287f1e510bbSOliver O'Halloran    compression=
288bafdb645SGeoff Levand    ext=bin
289bafdb645SGeoff Levand    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
290bafdb645SGeoff Levand    ksection=.kernel:vmlinux.bin
291bafdb645SGeoff Levand    isection=.kernel:initrd
2929b09c6d9STony Breeds    link_address=''
293dfbc2d75SStephen Rothwell    make_space=n
2946975a783SMichael Ellerman    pie=
295bafdb645SGeoff Levand    ;;
296a55387e5SScott Woodep88xc|ep405|ep8248e)
29711c146ccSScott Wood    platformo="$object/fixed-head.o $object/$platform.o"
29811c146ccSScott Wood    binary=y
29911c146ccSScott Wood    ;;
300a55387e5SScott Woodadder875-redboot)
301a55387e5SScott Wood    platformo="$object/fixed-head.o $object/redboot-8xx.o"
302a55387e5SScott Wood    binary=y
303a55387e5SScott Wood    ;;
304d2477b5cSGrant Likelysimpleboot-virtex405-*)
305d58577d8SJohn Linn    platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o"
306d58577d8SJohn Linn    binary=y
307d58577d8SJohn Linn    ;;
308d58577d8SJohn Linnsimpleboot-virtex440-*)
309a7e1cf0cSGrant Likely    platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o"
310d2477b5cSGrant Likely    binary=y
311d2477b5cSGrant Likely    ;;
3121d46e379SGrant Likelysimpleboot-*)
313a7e1cf0cSGrant Likely    platformo="$object/fixed-head.o $object/simpleboot.o"
3141d46e379SGrant Likely    binary=y
3151d46e379SGrant Likely    ;;
31659d13f9dSBryan O'Donoghueasp834x-redboot)
31759d13f9dSBryan O'Donoghue    platformo="$object/fixed-head.o $object/redboot-83xx.o"
31859d13f9dSBryan O'Donoghue    binary=y
31959d13f9dSBryan O'Donoghue    ;;
32024760823SNate Casexpedite52*)
32124760823SNate Case    link_address='0x1400000'
32224760823SNate Case    platformo=$object/cuboot-85xx.o
32324760823SNate Case    ;;
3246cdd2417SAlbert Herranzgamecube|wii)
325b68a24bcSAlbert Herranz    link_address='0x600000'
326b68a24bcSAlbert Herranz    platformo="$object/$platform-head.o $object/$platform.o"
327b68a24bcSAlbert Herranz    ;;
328228d5505STony Breedstreeboot-currituck)
329228d5505STony Breeds    link_address='0x1000000'
330228d5505STony Breeds    ;;
3312a2c74b2SAlistair Poppletreeboot-akebono)
3322a2c74b2SAlistair Popple    link_address='0x1000000'
3332a2c74b2SAlistair Popple    ;;
334b4e8c8ddSTorez Smithtreeboot-iss4xx-mpic)
335b4e8c8ddSTorez Smith    platformo="$object/treeboot-iss4xx.o"
336b4e8c8ddSTorez Smith    ;;
3376c5b59b9SDavid Gibsonepapr)
33890d1d44eSJeremy Kerr    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
3396c5b59b9SDavid Gibson    link_address='0x20000000'
3406c5b59b9SDavid Gibson    pie=-pie
3416c5b59b9SDavid Gibson    ;;
342be201981SStephen Chiversmvme5100)
343be201981SStephen Chivers    platformo="$object/fixed-head.o $object/mvme5100.o"
344be201981SStephen Chivers    binary=y
345be201981SStephen Chivers    ;;
34697493e2eSAlessio Igor Boganimvme7100)
34797493e2eSAlessio Igor Bogani    platformo="$object/motload-head.o $object/mvme7100.o"
34897493e2eSAlessio Igor Bogani    link_address='0x4000000'
34997493e2eSAlessio Igor Bogani    binary=y
35097493e2eSAlessio Igor Bogani    ;;
3512bf11819SPaul Mackerrasesac
3522bf11819SPaul Mackerras
3532bf11819SPaul Mackerrasvmz="$tmpdir/`basename \"$kernel\"`.$ext"
354a9903811SScott Wood
355f1e510bbSOliver O'Halloran# Calculate the vmlinux.strip size
356f1e510bbSOliver O'Halloran${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
357a670b0b4SMichael Forneystrip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
358c55aef0eSSuzuki Poulose
359f1e510bbSOliver O'Halloranif [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
360f1e510bbSOliver O'Halloran    # recompress the image if we need to
361f1e510bbSOliver O'Halloran    case $compression in
362f1e510bbSOliver O'Halloran    .xz)
363f1e510bbSOliver O'Halloran        xz --check=crc32 -f -6 "$vmz.$$"
364f1e510bbSOliver O'Halloran        ;;
365f1e510bbSOliver O'Halloran    .gz)
366c4f56af0SMichal Marek        gzip -n -f -9 "$vmz.$$"
367f1e510bbSOliver O'Halloran        ;;
368f1e510bbSOliver O'Halloran    *)
369f1e510bbSOliver O'Halloran        # drop the compression suffix so the stripped vmlinux is used
370f1e510bbSOliver O'Halloran        compression=
371f1e510bbSOliver O'Halloran	;;
372f1e510bbSOliver O'Halloran    esac
373a9903811SScott Wood
3742bf11819SPaul Mackerras    if [ -n "$cacheit" ]; then
375f1e510bbSOliver O'Halloran	mv -f "$vmz.$$$compression" "$vmz$compression"
3762bf11819SPaul Mackerras    else
3772bf11819SPaul Mackerras	vmz="$vmz.$$"
3782bf11819SPaul Mackerras    fi
379c55aef0eSSuzuki Pouloseelse
380c55aef0eSSuzuki Poulose    rm -f $vmz.$$
381c55aef0eSSuzuki Poulosefi
382c55aef0eSSuzuki Poulose
383f1e510bbSOliver O'Halloranvmz="$vmz$compression"
384f1e510bbSOliver O'Halloran
385dfbc2d75SStephen Rothwellif [ "$make_space" = "y" ]; then
386c55aef0eSSuzuki Poulose	# Round the size to next higher MB limit
387c55aef0eSSuzuki Poulose	round_size=$(((strip_size + 0xfffff) & 0xfff00000))
388c55aef0eSSuzuki Poulose
389c55aef0eSSuzuki Poulose	round_size=0x$(printf "%x" $round_size)
390c55aef0eSSuzuki Poulose	link_addr=$(printf "%d" $link_address)
391c55aef0eSSuzuki Poulose
392c55aef0eSSuzuki Poulose	if [ $link_addr -lt $strip_size ]; then
393eba3d97dSSuzuki Poulose	    echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
394c55aef0eSSuzuki Poulose			"overlaps the address of the wrapper($link_address)"
395eba3d97dSSuzuki Poulose	    echo "INFO: Fixing the link_address of wrapper to ($round_size)"
396c55aef0eSSuzuki Poulose	    link_address=$round_size
3972bf11819SPaul Mackerras	fi
398dfbc2d75SStephen Rothwellfi
3992bf11819SPaul Mackerras
400a6afacb6SDavid Gibson# Extract kernel version information, some platforms want to include
401a6afacb6SDavid Gibson# it in the image header
4022bf11819SPaul Mackerrasversion=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
4032bf11819SPaul Mackerras    cut -d' ' -f3`
4042bf11819SPaul Mackerrasif [ -n "$version" ]; then
405a6afacb6SDavid Gibson    uboot_version="-n Linux-$version"
4062bf11819SPaul Mackerrasfi
4070fdd717eSScott Wood
408b18796d3SKumar Gala# physical offset of kernel image
409b18796d3SKumar Galamembase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
410b18796d3SKumar Gala
4110fdd717eSScott Woodcase "$platform" in
4120fdd717eSScott Wooduboot)
4130fdd717eSScott Wood    rm -f "$ofile"
4143f884bf5SPeter Tyser    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
415a6afacb6SDavid Gibson	$uboot_version -d "$vmz" "$ofile"
4162bf11819SPaul Mackerras    if [ -z "$cacheit" ]; then
417a9903811SScott Wood	rm -f "$vmz"
4182bf11819SPaul Mackerras    fi
4192bf11819SPaul Mackerras    exit 0
4202bf11819SPaul Mackerras    ;;
42111eab297SBenjamin Herrenschmidtuboot-obs600)
42211eab297SBenjamin Herrenschmidt    rm -f "$ofile"
42311eab297SBenjamin Herrenschmidt    # obs600 wants a multi image with an initrd, so we need to put a fake
42411eab297SBenjamin Herrenschmidt    # one in even when building a "normal" image.
42511eab297SBenjamin Herrenschmidt    if [ -n "$initrd" ]; then
42611eab297SBenjamin Herrenschmidt	real_rd="$initrd"
42711eab297SBenjamin Herrenschmidt    else
42811eab297SBenjamin Herrenschmidt	real_rd=`mktemp`
42911eab297SBenjamin Herrenschmidt	echo "\0" >>"$real_rd"
43011eab297SBenjamin Herrenschmidt    fi
43111eab297SBenjamin Herrenschmidt    ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
43211eab297SBenjamin Herrenschmidt	$uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
43311eab297SBenjamin Herrenschmidt    if [ -z "$initrd" ]; then
43411eab297SBenjamin Herrenschmidt	rm -f "$real_rd"
43511eab297SBenjamin Herrenschmidt    fi
43611eab297SBenjamin Herrenschmidt    if [ -z "$cacheit" ]; then
43711eab297SBenjamin Herrenschmidt	rm -f "$vmz"
43811eab297SBenjamin Herrenschmidt    fi
43911eab297SBenjamin Herrenschmidt    exit 0
44011eab297SBenjamin Herrenschmidt    ;;
4412bf11819SPaul Mackerrasesac
4422bf11819SPaul Mackerras
4432bf11819SPaul Mackerrasaddsec() {
4442bf11819SPaul Mackerras    ${CROSS}objcopy $4 $1 \
4452bf11819SPaul Mackerras	--add-section=$3="$2" \
4462bf11819SPaul Mackerras	--set-section-flags=$3=contents,alloc,load,readonly,data
4472bf11819SPaul Mackerras}
4482bf11819SPaul Mackerras
449a9903811SScott Woodaddsec $tmp "$vmz" $ksection $object/empty.o
4502bf11819SPaul Mackerrasif [ -z "$cacheit" ]; then
451a9903811SScott Wood    rm -f "$vmz"
4522bf11819SPaul Mackerrasfi
4532bf11819SPaul Mackerras
4542bf11819SPaul Mackerrasif [ -n "$initrd" ]; then
455c888554bSMark A. Greer    addsec $tmp "$initrd" $isection
4562bf11819SPaul Mackerrasfi
4572bf11819SPaul Mackerras
4582bf11819SPaul Mackerrasif [ -n "$dtb" ]; then
459c888554bSMark A. Greer    addsec $tmp "$dtb" .kernel:dtb
460e9c4b4bdSMark A. Greer    if [ -n "$dts" ]; then
461e9c4b4bdSMark A. Greer	rm $dtb
462e9c4b4bdSMark A. Greer    fi
4632bf11819SPaul Mackerrasfi
4642bf11819SPaul Mackerras
4652bf11819SPaul Mackerrasif [ "$platform" != "miboot" ]; then
4669b09c6d9STony Breeds    if [ -n "$link_address" ] ; then
4676975a783SMichael Ellerman        text_start="-Ttext $link_address"
4689b09c6d9STony Breeds    fi
469f1e510bbSOliver O'Halloran#link everything
470ff45000fSNicholas Piggin    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" \
471cd197ffcSDavid Gibson	$platformo $tmp $object/wrapper.a
4722bf11819SPaul Mackerras    rm $tmp
4732bf11819SPaul Mackerrasfi
4742bf11819SPaul Mackerras
475a6afacb6SDavid Gibson# Some platforms need the zImage's entry point and base address
476a6afacb6SDavid Gibsonbase=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
477a6afacb6SDavid Gibsonentry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
478a6afacb6SDavid Gibson
47911c146ccSScott Woodif [ -n "$binary" ]; then
48011c146ccSScott Wood    mv "$ofile" "$ofile".elf
481aeb4552fSScott Wood    ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
48211c146ccSScott Woodfi
48311c146ccSScott Wood
4842bf11819SPaul Mackerras# post-processing needed for some platforms
4852bf11819SPaul Mackerrascase "$platform" in
48658706ef9SCorey Minyardpseries|chrp|maple)
4875663a123SPaul Mackerras    $objbin/addnote "$ofile"
4880dcd4401SPaul Mackerras    ;;
489627aa944SMilton Millercoff)
490cd197ffcSDavid Gibson    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
4915c539ee3SDavid Woodhouse    $objbin/hack-coff "$ofile"
4922bf11819SPaul Mackerras    ;;
4930fdd717eSScott Woodcuboot*)
494c4f56af0SMichal Marek    gzip -n -f -9 "$ofile"
4953f884bf5SPeter Tyser    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
496aeb4552fSScott Wood            $uboot_version -d "$ofile".gz "$ofile"
4970fdd717eSScott Wood    ;;
498f6dfc805SDavid Gibsontreeboot*)
499f6dfc805SDavid Gibson    mv "$ofile" "$ofile.elf"
5005c539ee3SDavid Woodhouse    $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
501f6dfc805SDavid Gibson    if [ -z "$cacheit" ]; then
502f6dfc805SDavid Gibson	rm -f "$ofile.elf"
503f6dfc805SDavid Gibson    fi
504f6dfc805SDavid Gibson    exit 0
505f6dfc805SDavid Gibson    ;;
506bafdb645SGeoff Levandps3)
5075761eaa3SGeoff Levand    # The ps3's loader supports loading a gzipped binary image from flash
5085761eaa3SGeoff Levand    # rom to ram addr zero. The loader then enters the system reset
5095761eaa3SGeoff Levand    # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
5105761eaa3SGeoff Levand    # a binary image of the kernel to be at addr zero, and yet have a
5115761eaa3SGeoff Levand    # suitable bootwrapper entry at 0x100.  To construct the final rom
5125761eaa3SGeoff Levand    # image 512 bytes from offset 0x100 is copied to the bootwrapper
5135761eaa3SGeoff Levand    # place holder at symbol __system_reset_kernel.  The 512 bytes of the
5145761eaa3SGeoff Levand    # bootwrapper entry code at symbol __system_reset_overlay is then
5155761eaa3SGeoff Levand    # copied to offset 0x100.  At runtime the bootwrapper program copies
5165761eaa3SGeoff Levand    # the data at __system_reset_kernel back to addr 0x100.
517bafdb645SGeoff Levand
518aeb4552fSScott Wood    system_reset_overlay=0x`${CROSS}nm "$ofile" \
519bafdb645SGeoff Levand        | grep ' __system_reset_overlay$'       \
520bafdb645SGeoff Levand        | cut -d' ' -f1`
521bafdb645SGeoff Levand    system_reset_overlay=`printf "%d" $system_reset_overlay`
522aeb4552fSScott Wood    system_reset_kernel=0x`${CROSS}nm "$ofile" \
523bafdb645SGeoff Levand        | grep ' __system_reset_kernel$'       \
524bafdb645SGeoff Levand        | cut -d' ' -f1`
525bafdb645SGeoff Levand    system_reset_kernel=`printf "%d" $system_reset_kernel`
526bafdb645SGeoff Levand    overlay_dest="256"
5275761eaa3SGeoff Levand    overlay_size="512"
528bafdb645SGeoff Levand
529aeb4552fSScott Wood    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
530aeb4552fSScott Wood
531879c26d4SGeoff Levand    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
532bafdb645SGeoff Levand        skip=$overlay_dest seek=$system_reset_kernel          \
533d4740373SGrant Likely        count=$overlay_size bs=1
534bafdb645SGeoff Levand
535879c26d4SGeoff Levand    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
536bafdb645SGeoff Levand        skip=$system_reset_overlay seek=$overlay_dest         \
537d4740373SGrant Likely        count=$overlay_size bs=1
538bafdb645SGeoff Levand
539928b9695SDavid Woodhouse    odir="$(dirname "$ofile.bin")"
540928b9695SDavid Woodhouse    rm -f "$odir/otheros.bld"
541c4f56af0SMichal Marek    gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
542bafdb645SGeoff Levand    ;;
5432bf11819SPaul Mackerrasesac
544