xref: /openbmc/linux/arch/powerpc/boot/wrapper (revision 5a4c98323b01d52382575a7a4d6bf7bf5f326047)
12bf11819SPaul Mackerras#!/bin/sh
200b46d22SThomas 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)
16528229d2SBenjamin Herrenschmidt# -e esm_blob   specify ESM blob for secure images
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 .)
23f1e510bbSOliver O'Halloran# -z		use gzip (legacy)
24f1e510bbSOliver O'Halloran# -Z zsuffix    compression to use (gz, xz or none)
252bf11819SPaul Mackerras
26d4740373SGrant Likely# Stop execution if any command fails
27d4740373SGrant Likelyset -e
28d4740373SGrant Likely
29b949d009SChristophe Leroyexport LC_ALL=C
30b949d009SChristophe Leroy
317f66c1fdSGrant Likely# Allow for verbose output
327f66c1fdSGrant Likelyif [ "$V" = 1 ]; then
337f66c1fdSGrant Likely    set -x
34f61200d3SGeoff Levand    map="-Map wrapper.map"
357f66c1fdSGrant Likelyfi
367f66c1fdSGrant Likely
372bf11819SPaul Mackerras# defaults
382bf11819SPaul Mackerraskernel=
392bf11819SPaul Mackerrasofile=zImage
402bf11819SPaul Mackerrasplatform=of
412bf11819SPaul Mackerrasinitrd=
422bf11819SPaul Mackerrasdtb=
432bf11819SPaul Mackerrasdts=
44528229d2SBenjamin Herrenschmidtesm_blob=
452bf11819SPaul Mackerrascacheit=
4611c146ccSScott Woodbinary=
47f1e510bbSOliver O'Hallorancompression=.gz
48fbded57cSChristophe Leroyuboot_comp=gzip
496975a783SMichael Ellermanpie=
50147c0516SCédric Le Goaterformat=
514c078c86SBill Wendlingnotext=
5226ba9f96SBill Wendlingrodynamic=
532bf11819SPaul Mackerras
542bf11819SPaul Mackerras# cross-compilation prefix
552bf11819SPaul MackerrasCROSS=
562bf11819SPaul Mackerras
573f884bf5SPeter Tyser# mkimage wrapper script
583f884bf5SPeter TyserMKIMAGE=$srctree/scripts/mkuboot.sh
593f884bf5SPeter Tyser
602bf11819SPaul Mackerras# directory for object and other files used by this script
612bf11819SPaul Mackerrasobject=arch/powerpc/boot
625c539ee3SDavid Woodhouseobjbin=$object
63c79b2973SLucian Adrian Grijincudtc=scripts/dtc/dtc
642bf11819SPaul Mackerras
652bf11819SPaul Mackerras# directory for working files
662bf11819SPaul Mackerrastmpdir=.
672bf11819SPaul Mackerras
682bf11819SPaul Mackerrasusage() {
692bf11819SPaul Mackerras    echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
70528229d2SBenjamin Herrenschmidt    echo '       [-d devtree] [-s tree.dts] [-e esm_blob]' >&2
71528229d2SBenjamin Herrenschmidt    echo '       [-c] [-C cross-prefix] [-D datadir] [-W workingdir]' >&2
72528229d2SBenjamin Herrenschmidt    echo '       [-Z (gz|xz|none)] [--no-compression] [vmlinux]' >&2
732bf11819SPaul Mackerras    exit 1
742bf11819SPaul Mackerras}
752bf11819SPaul Mackerras
76879c26d4SGeoff Levandrun_cmd() {
77879c26d4SGeoff Levand    if [ "$V" = 1 ]; then
78879c26d4SGeoff Levand        $* 2>&1
79879c26d4SGeoff Levand    else
80879c26d4SGeoff Levand        local msg
81879c26d4SGeoff Levand
82879c26d4SGeoff Levand        set +e
83879c26d4SGeoff Levand        msg=$($* 2>&1)
84879c26d4SGeoff Levand
85879c26d4SGeoff Levand        if [ $? -ne "0" ]; then
86879c26d4SGeoff Levand                echo $msg
87879c26d4SGeoff Levand                exit 1
88879c26d4SGeoff Levand        fi
89879c26d4SGeoff Levand        set -e
90879c26d4SGeoff Levand    fi
91879c26d4SGeoff Levand}
92879c26d4SGeoff Levand
932bf11819SPaul Mackerraswhile [ "$#" -gt 0 ]; do
942bf11819SPaul Mackerras    case "$1" in
952bf11819SPaul Mackerras    -o)
962bf11819SPaul Mackerras	shift
972bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
982bf11819SPaul Mackerras	ofile="$1"
992bf11819SPaul Mackerras	;;
1002bf11819SPaul Mackerras    -p)
1012bf11819SPaul Mackerras	shift
1022bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1032bf11819SPaul Mackerras	platform="$1"
1042bf11819SPaul Mackerras	;;
1052bf11819SPaul Mackerras    -i)
1062bf11819SPaul Mackerras	shift
1072bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1082bf11819SPaul Mackerras	initrd="$1"
1092bf11819SPaul Mackerras	;;
1102bf11819SPaul Mackerras    -d)
1112bf11819SPaul Mackerras	shift
1122bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1132bf11819SPaul Mackerras	dtb="$1"
1142bf11819SPaul Mackerras	;;
115528229d2SBenjamin Herrenschmidt    -e)
116528229d2SBenjamin Herrenschmidt	shift
117528229d2SBenjamin Herrenschmidt	[ "$#" -gt 0 ] || usage
118528229d2SBenjamin Herrenschmidt	esm_blob="$1"
119528229d2SBenjamin Herrenschmidt	;;
1202bf11819SPaul Mackerras    -s)
1212bf11819SPaul Mackerras	shift
1222bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1232bf11819SPaul Mackerras	dts="$1"
1242bf11819SPaul Mackerras	;;
1252bf11819SPaul Mackerras    -c)
1262bf11819SPaul Mackerras	cacheit=y
1272bf11819SPaul Mackerras	;;
1282bf11819SPaul Mackerras    -C)
1292bf11819SPaul Mackerras	shift
1302bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1312bf11819SPaul Mackerras	CROSS="$1"
1322bf11819SPaul Mackerras	;;
1332bf11819SPaul Mackerras    -D)
1342bf11819SPaul Mackerras	shift
1352bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1362bf11819SPaul Mackerras	object="$1"
1375c539ee3SDavid Woodhouse	objbin="$1"
1382bf11819SPaul Mackerras	;;
1392bf11819SPaul Mackerras    -W)
1402bf11819SPaul Mackerras	shift
1412bf11819SPaul Mackerras	[ "$#" -gt 0 ] || usage
1422bf11819SPaul Mackerras	tmpdir="$1"
1432bf11819SPaul Mackerras	;;
144f1e510bbSOliver O'Halloran    -z)
145f1e510bbSOliver O'Halloran	compression=.gz
146fbded57cSChristophe Leroy	uboot_comp=gzip
147f1e510bbSOliver O'Halloran	;;
148f1e510bbSOliver O'Halloran    -Z)
149f1e510bbSOliver O'Halloran	shift
150f1e510bbSOliver O'Halloran	[ "$#" -gt 0 ] || usage
151264bffadSChristophe Leroy        [ "$1" != "gz" -o "$1" != "xz" -o "$1" != "lzma" -o "$1" != "lzo" -o "$1" != "none" ] || usage
152f1e510bbSOliver O'Halloran
153f1e510bbSOliver O'Halloran	compression=".$1"
154fbded57cSChristophe Leroy	uboot_comp=$1
155f1e510bbSOliver O'Halloran
156f1e510bbSOliver O'Halloran        if [ $compression = ".none" ]; then
157f1e510bbSOliver O'Halloran                compression=
158fbded57cSChristophe Leroy		uboot_comp=none
159fbded57cSChristophe Leroy        fi
160fbded57cSChristophe Leroy	if [ $uboot_comp = "gz" ]; then
161fbded57cSChristophe Leroy		uboot_comp=gzip
162f1e510bbSOliver O'Halloran	fi
163f1e510bbSOliver O'Halloran	;;
164a9903811SScott Wood    --no-gzip)
16587c78b61SMichael Ellerman        # a "feature" of the wrapper script is that it can be used outside
166f1e510bbSOliver O'Halloran        # the kernel tree. So keeping this around for backwards compatibility.
167f1e510bbSOliver O'Halloran        compression=
168fbded57cSChristophe Leroy	uboot_comp=none
169a9903811SScott Wood        ;;
1702bf11819SPaul Mackerras    -?)
1712bf11819SPaul Mackerras	usage
1722bf11819SPaul Mackerras	;;
1732bf11819SPaul Mackerras    *)
1742bf11819SPaul Mackerras	[ -z "$kernel" ] || usage
1752bf11819SPaul Mackerras	kernel="$1"
1762bf11819SPaul Mackerras	;;
1772bf11819SPaul Mackerras    esac
1782bf11819SPaul Mackerras    shift
1792bf11819SPaul Mackerrasdone
1802bf11819SPaul Mackerras
181f1e510bbSOliver O'Halloran
1822bf11819SPaul Mackerrasif [ -n "$dts" ]; then
183701172d1SDavid Woodhouse    if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
184701172d1SDavid Woodhouse	dts="$object/dts/$dts"
185701172d1SDavid Woodhouse    fi
1862bf11819SPaul Mackerras    if [ -z "$dtb" ]; then
1872bf11819SPaul Mackerras	dtb="$platform.dtb"
1882bf11819SPaul Mackerras    fi
189c79b2973SLucian Adrian Grijincu    $dtc -O dtb -o "$dtb" -b 0 "$dts"
1902bf11819SPaul Mackerrasfi
1912bf11819SPaul Mackerras
1922bf11819SPaul Mackerrasif [ -z "$kernel" ]; then
1932bf11819SPaul Mackerras    kernel=vmlinux
1942bf11819SPaul Mackerrasfi
1952bf11819SPaul Mackerras
19677a88274SMasahiro YamadaLC_ALL=C elfformat="`${CROSS}objdump -p "$kernel" | grep 'file format' | awk '{print $4}'`"
197147c0516SCédric Le Goatercase "$elfformat" in
198147c0516SCédric Le Goater    elf64-powerpcle)	format=elf64lppc	;;
199147c0516SCédric Le Goater    elf64-powerpc)	format=elf32ppc	;;
200147c0516SCédric Le Goater    elf32-powerpc)	format=elf32ppc	;;
201147c0516SCédric Le Goateresac
202147c0516SCédric Le Goater
203ff45000fSNicholas Pigginld_version()
204ff45000fSNicholas Piggin{
205ff45000fSNicholas Piggin    # Poached from scripts/ld-version.sh, but we don't want to call that because
206ff45000fSNicholas Piggin    # this script (wrapper) is distributed separately from the kernel source.
207ff45000fSNicholas Piggin    # Extract linker version number from stdin and turn into single number.
208ff45000fSNicholas Piggin    awk '{
209ff45000fSNicholas Piggin	gsub(".*\\)", "");
210ff45000fSNicholas Piggin	gsub(".*version ", "");
211ff45000fSNicholas Piggin	gsub("-.*", "");
212ff45000fSNicholas Piggin	split($1,a, ".");
2133287ebd7SOjaswin Mujoo	if( length(a[3]) == "8" )
2143287ebd7SOjaswin Mujoo		# a[3] is probably a date of format yyyymmdd used for release snapshots. We
2153287ebd7SOjaswin Mujoo		# can assume it to be zero as it does not signify a new version as such.
2163287ebd7SOjaswin Mujoo		a[3] = 0;
217ff45000fSNicholas Piggin	print a[1]*100000000 + a[2]*1000000 + a[3]*10000;
218ff45000fSNicholas Piggin	exit
219ff45000fSNicholas Piggin    }'
220ff45000fSNicholas Piggin}
221ff45000fSNicholas Piggin
222579aee9fSStephen Rothwellld_is_lld()
223579aee9fSStephen Rothwell{
224579aee9fSStephen Rothwell	${CROSS}ld -V 2>&1 | grep -q LLD
225579aee9fSStephen Rothwell}
226579aee9fSStephen Rothwell
227ff45000fSNicholas Piggin# Do not include PT_INTERP segment when linking pie. Non-pie linking
228ff45000fSNicholas Piggin# just ignores this option.
229ff45000fSNicholas PigginLD_VERSION=$(${CROSS}ld --version | ld_version)
230ff45000fSNicholas PigginLD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
231ff45000fSNicholas Pigginif [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
232ff45000fSNicholas Piggin	nodl="--no-dynamic-linker"
233ff45000fSNicholas Pigginfi
234147c0516SCédric Le Goater
235579aee9fSStephen Rothwell# suppress some warnings in recent ld versions
236579aee9fSStephen Rothwellnowarn="-z noexecstack"
237*029f1134SMadhavan Srinivasanif "${CROSS}ld" -v --no-warn-rwx-segments >/dev/null 2>&1; then
238579aee9fSStephen Rothwell	nowarn="$nowarn --no-warn-rwx-segments"
239579aee9fSStephen Rothwellfi
240579aee9fSStephen Rothwell
2412bf11819SPaul Mackerrasplatformo=$object/"$platform".o
2422bf11819SPaul Mackerraslds=$object/zImage.lds
2432bf11819SPaul Mackerrasext=strip
2442bf11819SPaul Mackerrasobjflags=-S
2452bf11819SPaul Mackerrastmp=$tmpdir/zImage.$$.o
2462bf11819SPaul Mackerrasksection=.kernel:vmlinux.strip
2472bf11819SPaul Mackerrasisection=.kernel:initrd
248528229d2SBenjamin Herrenschmidtesection=.kernel:esm_blob
2499b09c6d9STony Breedslink_address='0x400000'
250dfbc2d75SStephen Rothwellmake_space=y
2512bf11819SPaul Mackerras
252528229d2SBenjamin Herrenschmidt
253528229d2SBenjamin Herrenschmidtif [ -n "$esm_blob" -a "$platform" != "pseries" ]; then
254528229d2SBenjamin Herrenschmidt    echo "ESM blob not support on non-pseries platforms" >&2
255528229d2SBenjamin Herrenschmidt    exit 1
256528229d2SBenjamin Herrenschmidtfi
257528229d2SBenjamin Herrenschmidt
2582bf11819SPaul Mackerrascase "$platform" in
25944790a0bSBenjamin Herrenschmidtof)
26044790a0bSBenjamin Herrenschmidt    platformo="$object/of.o $object/epapr.o"
26144790a0bSBenjamin Herrenschmidt    make_space=n
26244790a0bSBenjamin Herrenschmidt    ;;
2639b09c6d9STony Breedspseries)
2642d9afb36SCédric Le Goater    platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
2659b09c6d9STony Breeds    link_address='0x4000000'
266147c0516SCédric Le Goater    if [ "$format" != "elf32ppc" ]; then
267147c0516SCédric Le Goater	link_address=
268147c0516SCédric Le Goater	pie=-pie
269147c0516SCédric Le Goater    fi
270f5467e28SPaul Mackerras    make_space=n
2719b09c6d9STony Breeds    ;;
27258706ef9SCorey Minyardmaple)
2730c9fa291SBenjamin Herrenschmidt    platformo="$object/of.o $object/epapr.o"
27458706ef9SCorey Minyard    link_address='0x400000'
275f5467e28SPaul Mackerras    make_space=n
27658706ef9SCorey Minyard    ;;
2779b09c6d9STony Breedspmac|chrp)
2780c9fa291SBenjamin Herrenschmidt    platformo="$object/of.o $object/epapr.o"
279f5467e28SPaul Mackerras    make_space=n
2802bf11819SPaul Mackerras    ;;
281627aa944SMilton Millercoff)
2820c9fa291SBenjamin Herrenschmidt    platformo="$object/crt0.o $object/of.o $object/epapr.o"
2832bf11819SPaul Mackerras    lds=$object/zImage.coff.lds
2849b09c6d9STony Breeds    link_address='0x500000'
285f5467e28SPaul Mackerras    make_space=n
2866975a783SMichael Ellerman    pie=
2872bf11819SPaul Mackerras    ;;
28811eab297SBenjamin Herrenschmidtmiboot|uboot*)
2892bf11819SPaul Mackerras    # miboot and U-boot want just the bare bits, not an ELF binary
2902bf11819SPaul Mackerras    ext=bin
2912bf11819SPaul Mackerras    objflags="-O binary"
2922bf11819SPaul Mackerras    tmp="$ofile"
2932bf11819SPaul Mackerras    ksection=image
2942bf11819SPaul Mackerras    isection=initrd
2952bf11819SPaul Mackerras    ;;
2960fdd717eSScott Woodcuboot*)
29711c146ccSScott Wood    binary=y
298f1e510bbSOliver O'Halloran    compression=
29925431333SGrant Likely    case "$platform" in
3008dd217b2SScott Wood    *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
30125431333SGrant Likely        platformo=$object/cuboot-8xx.o
30225431333SGrant Likely        ;;
30325431333SGrant Likely    *5200*|*-motionpro)
30425431333SGrant Likely        platformo=$object/cuboot-52xx.o
30525431333SGrant Likely        ;;
30625431333SGrant Likely    *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
30725431333SGrant Likely        platformo=$object/cuboot-pq2.o
30825431333SGrant Likely        ;;
30925431333SGrant Likely    *-mpc824*)
31025431333SGrant Likely        platformo=$object/cuboot-824x.o
31125431333SGrant Likely        ;;
31259d13f9dSBryan O'Donoghue    *-mpc83*|*-asp834x*)
31325431333SGrant Likely        platformo=$object/cuboot-83xx.o
31425431333SGrant Likely        ;;
315ff880112SAlexandr Smirnov    *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
31625431333SGrant Likely        platformo=$object/cuboot-85xx-cpm2.o
31725431333SGrant Likely        ;;
318c12adb06SPaul Gortmaker    *-mpc85*|*-tqm85*)
31925431333SGrant Likely        platformo=$object/cuboot-85xx.o
32025431333SGrant Likely        ;;
3218f23735dSGerhard Pircher    *-amigaone)
3228f23735dSGerhard Pircher        link_address='0x800000'
3238f23735dSGerhard Pircher        ;;
32425431333SGrant Likely    esac
3250fdd717eSScott Wood    ;;
326bafdb645SGeoff Levandps3)
327bafdb645SGeoff Levand    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
328bafdb645SGeoff Levand    lds=$object/zImage.ps3.lds
329f1e510bbSOliver O'Halloran    compression=
330bafdb645SGeoff Levand    ext=bin
331bafdb645SGeoff Levand    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
332bafdb645SGeoff Levand    ksection=.kernel:vmlinux.bin
333bafdb645SGeoff Levand    isection=.kernel:initrd
3349b09c6d9STony Breeds    link_address=''
335dfbc2d75SStephen Rothwell    make_space=n
3366975a783SMichael Ellerman    pie=
337bafdb645SGeoff Levand    ;;
338a55387e5SScott Woodep88xc|ep405|ep8248e)
33911c146ccSScott Wood    platformo="$object/fixed-head.o $object/$platform.o"
34011c146ccSScott Wood    binary=y
34111c146ccSScott Wood    ;;
342a55387e5SScott Woodadder875-redboot)
343a55387e5SScott Wood    platformo="$object/fixed-head.o $object/redboot-8xx.o"
344a55387e5SScott Wood    binary=y
345a55387e5SScott Wood    ;;
3461d46e379SGrant Likelysimpleboot-*)
347a7e1cf0cSGrant Likely    platformo="$object/fixed-head.o $object/simpleboot.o"
3481d46e379SGrant Likely    binary=y
3491d46e379SGrant Likely    ;;
35059d13f9dSBryan O'Donoghueasp834x-redboot)
35159d13f9dSBryan O'Donoghue    platformo="$object/fixed-head.o $object/redboot-83xx.o"
35259d13f9dSBryan O'Donoghue    binary=y
35359d13f9dSBryan O'Donoghue    ;;
35424760823SNate Casexpedite52*)
35524760823SNate Case    link_address='0x1400000'
35624760823SNate Case    platformo=$object/cuboot-85xx.o
35724760823SNate Case    ;;
3586cdd2417SAlbert Herranzgamecube|wii)
359b68a24bcSAlbert Herranz    link_address='0x600000'
360b68a24bcSAlbert Herranz    platformo="$object/$platform-head.o $object/$platform.o"
361b68a24bcSAlbert Herranz    ;;
3624a21192eSJoel Stanleymicrowatt)
3634a21192eSJoel Stanley    link_address='0x500000'
3644a21192eSJoel Stanley    platformo="$object/fixed-head.o $object/$platform.o"
3654a21192eSJoel Stanley    binary=y
3664a21192eSJoel Stanley    ;;
367228d5505STony Breedstreeboot-currituck)
368228d5505STony Breeds    link_address='0x1000000'
369228d5505STony Breeds    ;;
3702a2c74b2SAlistair Poppletreeboot-akebono)
3712a2c74b2SAlistair Popple    link_address='0x1000000'
3722a2c74b2SAlistair Popple    ;;
373b4e8c8ddSTorez Smithtreeboot-iss4xx-mpic)
374b4e8c8ddSTorez Smith    platformo="$object/treeboot-iss4xx.o"
375b4e8c8ddSTorez Smith    ;;
3766c5b59b9SDavid Gibsonepapr)
37790d1d44eSJeremy Kerr    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
3786c5b59b9SDavid Gibson    link_address='0x20000000'
3796c5b59b9SDavid Gibson    pie=-pie
3804c078c86SBill Wendling    notext='-z notext'
38126ba9f96SBill Wendling    rodynamic=$(if ${CROSS}ld -V 2>&1 | grep -q LLD ; then echo "-z rodynamic"; fi)
3826c5b59b9SDavid Gibson    ;;
383be201981SStephen Chiversmvme5100)
384be201981SStephen Chivers    platformo="$object/fixed-head.o $object/mvme5100.o"
385be201981SStephen Chivers    binary=y
386be201981SStephen Chivers    ;;
38797493e2eSAlessio Igor Boganimvme7100)
38897493e2eSAlessio Igor Bogani    platformo="$object/motload-head.o $object/mvme7100.o"
38997493e2eSAlessio Igor Bogani    link_address='0x4000000'
39097493e2eSAlessio Igor Bogani    binary=y
39197493e2eSAlessio Igor Bogani    ;;
3922bf11819SPaul Mackerrasesac
3932bf11819SPaul Mackerras
3942bf11819SPaul Mackerrasvmz="$tmpdir/`basename \"$kernel\"`.$ext"
395a9903811SScott Wood
396f1e510bbSOliver O'Halloran# Calculate the vmlinux.strip size
397f1e510bbSOliver O'Halloran${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
398a670b0b4SMichael Forneystrip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
399c55aef0eSSuzuki Poulose
400f1e510bbSOliver O'Halloranif [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
401f1e510bbSOliver O'Halloran    # recompress the image if we need to
402f1e510bbSOliver O'Halloran    case $compression in
403f1e510bbSOliver O'Halloran    .xz)
404f1e510bbSOliver O'Halloran        xz --check=crc32 -f -6 "$vmz.$$"
405f1e510bbSOliver O'Halloran        ;;
406f1e510bbSOliver O'Halloran    .gz)
407c4f56af0SMichal Marek        gzip -n -f -9 "$vmz.$$"
408f1e510bbSOliver O'Halloran        ;;
4091cc9a21bSChristophe Leroy    .lzma)
4101cc9a21bSChristophe Leroy        xz --format=lzma -f -6 "$vmz.$$"
4111cc9a21bSChristophe Leroy	;;
412264bffadSChristophe Leroy    .lzo)
413264bffadSChristophe Leroy        lzop -f -9 "$vmz.$$"
414264bffadSChristophe Leroy	;;
415f1e510bbSOliver O'Halloran    *)
416f1e510bbSOliver O'Halloran        # drop the compression suffix so the stripped vmlinux is used
417f1e510bbSOliver O'Halloran        compression=
418fbded57cSChristophe Leroy	uboot_comp=none
419f1e510bbSOliver O'Halloran	;;
420f1e510bbSOliver O'Halloran    esac
421a9903811SScott Wood
4222bf11819SPaul Mackerras    if [ -n "$cacheit" ]; then
423f1e510bbSOliver O'Halloran	mv -f "$vmz.$$$compression" "$vmz$compression"
4242bf11819SPaul Mackerras    else
4252bf11819SPaul Mackerras	vmz="$vmz.$$"
4262bf11819SPaul Mackerras    fi
427c55aef0eSSuzuki Pouloseelse
428c55aef0eSSuzuki Poulose    rm -f $vmz.$$
429c55aef0eSSuzuki Poulosefi
430c55aef0eSSuzuki Poulose
431f1e510bbSOliver O'Halloranvmz="$vmz$compression"
432f1e510bbSOliver O'Halloran
433dfbc2d75SStephen Rothwellif [ "$make_space" = "y" ]; then
434c55aef0eSSuzuki Poulose	# Round the size to next higher MB limit
435c55aef0eSSuzuki Poulose	round_size=$(((strip_size + 0xfffff) & 0xfff00000))
436c55aef0eSSuzuki Poulose
437c55aef0eSSuzuki Poulose	round_size=0x$(printf "%x" $round_size)
438c55aef0eSSuzuki Poulose	link_addr=$(printf "%d" $link_address)
439c55aef0eSSuzuki Poulose
440c55aef0eSSuzuki Poulose	if [ $link_addr -lt $strip_size ]; then
441eba3d97dSSuzuki Poulose	    echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
442c55aef0eSSuzuki Poulose			"overlaps the address of the wrapper($link_address)"
443eba3d97dSSuzuki Poulose	    echo "INFO: Fixing the link_address of wrapper to ($round_size)"
444c55aef0eSSuzuki Poulose	    link_address=$round_size
4452bf11819SPaul Mackerras	fi
446dfbc2d75SStephen Rothwellfi
4472bf11819SPaul Mackerras
448a6afacb6SDavid Gibson# Extract kernel version information, some platforms want to include
449a6afacb6SDavid Gibson# it in the image header
4502bf11819SPaul Mackerrasversion=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
4512df8220cSMasahiro Yamada    head -n1 | cut -d' ' -f3`
4522bf11819SPaul Mackerrasif [ -n "$version" ]; then
453a6afacb6SDavid Gibson    uboot_version="-n Linux-$version"
4542bf11819SPaul Mackerrasfi
4550fdd717eSScott Wood
456b18796d3SKumar Gala# physical offset of kernel image
457b18796d3SKumar Galamembase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
458b18796d3SKumar Gala
4590fdd717eSScott Woodcase "$platform" in
4600fdd717eSScott Wooduboot)
4610fdd717eSScott Wood    rm -f "$ofile"
462fbded57cSChristophe Leroy    ${MKIMAGE} -A ppc -O linux -T kernel -C $uboot_comp -a $membase -e $membase \
463a6afacb6SDavid Gibson	$uboot_version -d "$vmz" "$ofile"
4642bf11819SPaul Mackerras    if [ -z "$cacheit" ]; then
465a9903811SScott Wood	rm -f "$vmz"
4662bf11819SPaul Mackerras    fi
4672bf11819SPaul Mackerras    exit 0
4682bf11819SPaul Mackerras    ;;
46911eab297SBenjamin Herrenschmidtuboot-obs600)
47011eab297SBenjamin Herrenschmidt    rm -f "$ofile"
47111eab297SBenjamin Herrenschmidt    # obs600 wants a multi image with an initrd, so we need to put a fake
47211eab297SBenjamin Herrenschmidt    # one in even when building a "normal" image.
47311eab297SBenjamin Herrenschmidt    if [ -n "$initrd" ]; then
47411eab297SBenjamin Herrenschmidt	real_rd="$initrd"
47511eab297SBenjamin Herrenschmidt    else
47611eab297SBenjamin Herrenschmidt	real_rd=`mktemp`
47711eab297SBenjamin Herrenschmidt	echo "\0" >>"$real_rd"
47811eab297SBenjamin Herrenschmidt    fi
47911eab297SBenjamin Herrenschmidt    ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
48011eab297SBenjamin Herrenschmidt	$uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
48111eab297SBenjamin Herrenschmidt    if [ -z "$initrd" ]; then
48211eab297SBenjamin Herrenschmidt	rm -f "$real_rd"
48311eab297SBenjamin Herrenschmidt    fi
48411eab297SBenjamin Herrenschmidt    if [ -z "$cacheit" ]; then
48511eab297SBenjamin Herrenschmidt	rm -f "$vmz"
48611eab297SBenjamin Herrenschmidt    fi
48711eab297SBenjamin Herrenschmidt    exit 0
48811eab297SBenjamin Herrenschmidt    ;;
4892bf11819SPaul Mackerrasesac
4902bf11819SPaul Mackerras
4912bf11819SPaul Mackerrasaddsec() {
4922bf11819SPaul Mackerras    ${CROSS}objcopy $4 $1 \
4932bf11819SPaul Mackerras	--add-section=$3="$2" \
4942bf11819SPaul Mackerras	--set-section-flags=$3=contents,alloc,load,readonly,data
4952bf11819SPaul Mackerras}
4962bf11819SPaul Mackerras
497a9903811SScott Woodaddsec $tmp "$vmz" $ksection $object/empty.o
4982bf11819SPaul Mackerrasif [ -z "$cacheit" ]; then
499a9903811SScott Wood    rm -f "$vmz"
5002bf11819SPaul Mackerrasfi
5012bf11819SPaul Mackerras
5022bf11819SPaul Mackerrasif [ -n "$initrd" ]; then
503c888554bSMark A. Greer    addsec $tmp "$initrd" $isection
5042bf11819SPaul Mackerrasfi
5052bf11819SPaul Mackerras
5062bf11819SPaul Mackerrasif [ -n "$dtb" ]; then
507c888554bSMark A. Greer    addsec $tmp "$dtb" .kernel:dtb
508e9c4b4bdSMark A. Greer    if [ -n "$dts" ]; then
509e9c4b4bdSMark A. Greer	rm $dtb
510e9c4b4bdSMark A. Greer    fi
5112bf11819SPaul Mackerrasfi
5122bf11819SPaul Mackerras
513528229d2SBenjamin Herrenschmidtif [ -n "$esm_blob" ]; then
514528229d2SBenjamin Herrenschmidt    addsec $tmp "$esm_blob" $esection
515528229d2SBenjamin Herrenschmidtfi
516528229d2SBenjamin Herrenschmidt
5172bf11819SPaul Mackerrasif [ "$platform" != "miboot" ]; then
5189b09c6d9STony Breeds    if [ -n "$link_address" ] ; then
5196975a783SMichael Ellerman        text_start="-Ttext $link_address"
5209b09c6d9STony Breeds    fi
521f1e510bbSOliver O'Halloran#link everything
522579aee9fSStephen Rothwell    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl $nowarn $rodynamic $notext -o "$ofile" $map \
523cd197ffcSDavid Gibson	$platformo $tmp $object/wrapper.a
5242bf11819SPaul Mackerras    rm $tmp
5252bf11819SPaul Mackerrasfi
5262bf11819SPaul Mackerras
527a6afacb6SDavid Gibson# Some platforms need the zImage's entry point and base address
528a6afacb6SDavid Gibsonbase=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
529a6afacb6SDavid Gibsonentry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
530a6afacb6SDavid Gibson
53111c146ccSScott Woodif [ -n "$binary" ]; then
53211c146ccSScott Wood    mv "$ofile" "$ofile".elf
533aeb4552fSScott Wood    ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
53411c146ccSScott Woodfi
53511c146ccSScott Wood
5362bf11819SPaul Mackerras# post-processing needed for some platforms
5372bf11819SPaul Mackerrascase "$platform" in
53858706ef9SCorey Minyardpseries|chrp|maple)
5395663a123SPaul Mackerras    $objbin/addnote "$ofile"
5400dcd4401SPaul Mackerras    ;;
541627aa944SMilton Millercoff)
542cd197ffcSDavid Gibson    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
5435c539ee3SDavid Woodhouse    $objbin/hack-coff "$ofile"
5442bf11819SPaul Mackerras    ;;
5450fdd717eSScott Woodcuboot*)
546c4f56af0SMichal Marek    gzip -n -f -9 "$ofile"
5473f884bf5SPeter Tyser    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
548aeb4552fSScott Wood            $uboot_version -d "$ofile".gz "$ofile"
5490fdd717eSScott Wood    ;;
550f6dfc805SDavid Gibsontreeboot*)
551f6dfc805SDavid Gibson    mv "$ofile" "$ofile.elf"
5525c539ee3SDavid Woodhouse    $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
553f6dfc805SDavid Gibson    if [ -z "$cacheit" ]; then
554f6dfc805SDavid Gibson	rm -f "$ofile.elf"
555f6dfc805SDavid Gibson    fi
556f6dfc805SDavid Gibson    exit 0
557f6dfc805SDavid Gibson    ;;
558bafdb645SGeoff Levandps3)
5595761eaa3SGeoff Levand    # The ps3's loader supports loading a gzipped binary image from flash
5605761eaa3SGeoff Levand    # rom to ram addr zero. The loader then enters the system reset
5615761eaa3SGeoff Levand    # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
5625761eaa3SGeoff Levand    # a binary image of the kernel to be at addr zero, and yet have a
5635761eaa3SGeoff Levand    # suitable bootwrapper entry at 0x100.  To construct the final rom
5645761eaa3SGeoff Levand    # image 512 bytes from offset 0x100 is copied to the bootwrapper
5655761eaa3SGeoff Levand    # place holder at symbol __system_reset_kernel.  The 512 bytes of the
5665761eaa3SGeoff Levand    # bootwrapper entry code at symbol __system_reset_overlay is then
5675761eaa3SGeoff Levand    # copied to offset 0x100.  At runtime the bootwrapper program copies
5685761eaa3SGeoff Levand    # the data at __system_reset_kernel back to addr 0x100.
569bafdb645SGeoff Levand
570aeb4552fSScott Wood    system_reset_overlay=0x`${CROSS}nm "$ofile" \
571bafdb645SGeoff Levand        | grep ' __system_reset_overlay$'       \
572bafdb645SGeoff Levand        | cut -d' ' -f1`
573bafdb645SGeoff Levand    system_reset_overlay=`printf "%d" $system_reset_overlay`
574aeb4552fSScott Wood    system_reset_kernel=0x`${CROSS}nm "$ofile" \
575bafdb645SGeoff Levand        | grep ' __system_reset_kernel$'       \
576bafdb645SGeoff Levand        | cut -d' ' -f1`
577bafdb645SGeoff Levand    system_reset_kernel=`printf "%d" $system_reset_kernel`
578bafdb645SGeoff Levand    overlay_dest="256"
5795761eaa3SGeoff Levand    overlay_size="512"
580bafdb645SGeoff Levand
581aeb4552fSScott Wood    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
582aeb4552fSScott Wood
583879c26d4SGeoff Levand    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
584bafdb645SGeoff Levand        skip=$overlay_dest seek=$system_reset_kernel          \
585d4740373SGrant Likely        count=$overlay_size bs=1
586bafdb645SGeoff Levand
587879c26d4SGeoff Levand    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
588bafdb645SGeoff Levand        skip=$system_reset_overlay seek=$overlay_dest         \
589d4740373SGrant Likely        count=$overlay_size bs=1
590bafdb645SGeoff Levand
591928b9695SDavid Woodhouse    odir="$(dirname "$ofile.bin")"
592aa3bc365SGeoff Levand
593aa3bc365SGeoff Levand    # The ps3's flash loader has a size limit of 16 MiB for the uncompressed
594aa3bc365SGeoff Levand    # image.  If a compressed image that exceeded this limit is written to
595aa3bc365SGeoff Levand    # flash the loader will decompress that image until the 16 MiB limit is
596aa3bc365SGeoff Levand    # reached, then enter the system reset vector of the partially decompressed
597aa3bc365SGeoff Levand    # image.  No warning is issued.
598aa3bc365SGeoff Levand    rm -f "$odir"/{otheros,otheros-too-big}.bld
599a9ffb8eeSTiezhu Yang    size=$(${CROSS}nm --no-sort --radix=d "$ofile" | grep -E ' _end$' | cut -d' ' -f1)
600aa3bc365SGeoff Levand    bld="otheros.bld"
601aa3bc365SGeoff Levand    if [ $size -gt $((0x1000000)) ]; then
602aa3bc365SGeoff Levand        bld="otheros-too-big.bld"
603aa3bc365SGeoff Levand    fi
604aa3bc365SGeoff Levand    gzip -n --force -9 --stdout "$ofile.bin" > "$odir/$bld"
605bafdb645SGeoff Levand    ;;
6062bf11819SPaul Mackerrasesac
607