xref: /openbmc/linux/arch/powerpc/boot/wrapper (revision cd238eff)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4# Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
5
6# This script takes a kernel binary and optionally an initrd image
7# and/or a device-tree blob, and creates a bootable zImage for a
8# given platform.
9
10# Options:
11# -o zImage	specify output file
12# -p platform	specify platform (links in $platform.o)
13# -i initrd	specify initrd file
14# -d devtree	specify device-tree blob
15# -s tree.dts	specify device-tree source file (needs dtc installed)
16# -c		cache $kernel.strip.gz (use if present & newer, else make)
17# -C prefix	specify command prefix for cross-building tools
18#		(strip, objcopy, ld)
19# -D dir	specify directory containing data files used by script
20#		(default ./arch/powerpc/boot)
21# -W dir	specify working directory for temporary files (default .)
22# -z		use gzip (legacy)
23# -Z zsuffix    compression to use (gz, xz or none)
24
25# Stop execution if any command fails
26set -e
27
28# Allow for verbose output
29if [ "$V" = 1 ]; then
30    set -x
31fi
32
33# defaults
34kernel=
35ofile=zImage
36platform=of
37initrd=
38dtb=
39dts=
40cacheit=
41binary=
42compression=.gz
43pie=
44format=
45
46# cross-compilation prefix
47CROSS=
48
49# mkimage wrapper script
50MKIMAGE=$srctree/scripts/mkuboot.sh
51
52# directory for object and other files used by this script
53object=arch/powerpc/boot
54objbin=$object
55dtc=scripts/dtc/dtc
56
57# directory for working files
58tmpdir=.
59
60usage() {
61    echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
62    echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
63    echo '       [-D datadir] [-W workingdir] [-Z (gz|xz|none)]' >&2
64    echo '       [--no-compression] [vmlinux]' >&2
65    exit 1
66}
67
68run_cmd() {
69    if [ "$V" = 1 ]; then
70        $* 2>&1
71    else
72        local msg
73
74        set +e
75        msg=$($* 2>&1)
76
77        if [ $? -ne "0" ]; then
78                echo $msg
79                exit 1
80        fi
81        set -e
82    fi
83}
84
85while [ "$#" -gt 0 ]; do
86    case "$1" in
87    -o)
88	shift
89	[ "$#" -gt 0 ] || usage
90	ofile="$1"
91	;;
92    -p)
93	shift
94	[ "$#" -gt 0 ] || usage
95	platform="$1"
96	;;
97    -i)
98	shift
99	[ "$#" -gt 0 ] || usage
100	initrd="$1"
101	;;
102    -d)
103	shift
104	[ "$#" -gt 0 ] || usage
105	dtb="$1"
106	;;
107    -s)
108	shift
109	[ "$#" -gt 0 ] || usage
110	dts="$1"
111	;;
112    -c)
113	cacheit=y
114	;;
115    -C)
116	shift
117	[ "$#" -gt 0 ] || usage
118	CROSS="$1"
119	;;
120    -D)
121	shift
122	[ "$#" -gt 0 ] || usage
123	object="$1"
124	objbin="$1"
125	;;
126    -W)
127	shift
128	[ "$#" -gt 0 ] || usage
129	tmpdir="$1"
130	;;
131    -z)
132	compression=.gz
133	;;
134    -Z)
135	shift
136	[ "$#" -gt 0 ] || usage
137        [ "$1" != "gz" -o "$1" != "xz" -o "$1" != "none" ] || usage
138
139	compression=".$1"
140
141        if [ $compression = ".none" ]; then
142                compression=
143        fi
144	;;
145    --no-gzip)
146        # a "feature" of the the wrapper script is that it can be used outside
147        # the kernel tree. So keeping this around for backwards compatibility.
148        compression=
149        ;;
150    -?)
151	usage
152	;;
153    *)
154	[ -z "$kernel" ] || usage
155	kernel="$1"
156	;;
157    esac
158    shift
159done
160
161
162if [ -n "$dts" ]; then
163    if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
164	dts="$object/dts/$dts"
165    fi
166    if [ -z "$dtb" ]; then
167	dtb="$platform.dtb"
168    fi
169    $dtc -O dtb -o "$dtb" -b 0 "$dts"
170fi
171
172if [ -z "$kernel" ]; then
173    kernel=vmlinux
174fi
175
176LANG=C elfformat="`${CROSS}objdump -p "$kernel" | grep 'file format' | awk '{print $4}'`"
177case "$elfformat" in
178    elf64-powerpcle)	format=elf64lppc	;;
179    elf64-powerpc)	format=elf32ppc	;;
180    elf32-powerpc)	format=elf32ppc	;;
181esac
182
183ld_version()
184{
185    # Poached from scripts/ld-version.sh, but we don't want to call that because
186    # this script (wrapper) is distributed separately from the kernel source.
187    # Extract linker version number from stdin and turn into single number.
188    awk '{
189	gsub(".*\\)", "");
190	gsub(".*version ", "");
191	gsub("-.*", "");
192	split($1,a, ".");
193	print a[1]*100000000 + a[2]*1000000 + a[3]*10000;
194	exit
195    }'
196}
197
198# Do not include PT_INTERP segment when linking pie. Non-pie linking
199# just ignores this option.
200LD_VERSION=$(${CROSS}ld --version | ld_version)
201LD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
202if [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
203	nodl="--no-dynamic-linker"
204fi
205
206platformo=$object/"$platform".o
207lds=$object/zImage.lds
208ext=strip
209objflags=-S
210tmp=$tmpdir/zImage.$$.o
211ksection=.kernel:vmlinux.strip
212isection=.kernel:initrd
213link_address='0x400000'
214make_space=y
215
216case "$platform" in
217of)
218    platformo="$object/of.o $object/epapr.o"
219    make_space=n
220    ;;
221pseries)
222    platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
223    link_address='0x4000000'
224    if [ "$format" != "elf32ppc" ]; then
225	link_address=
226	pie=-pie
227    fi
228    make_space=n
229    ;;
230maple)
231    platformo="$object/of.o $object/epapr.o"
232    link_address='0x400000'
233    make_space=n
234    ;;
235pmac|chrp)
236    platformo="$object/of.o $object/epapr.o"
237    make_space=n
238    ;;
239coff)
240    platformo="$object/crt0.o $object/of.o $object/epapr.o"
241    lds=$object/zImage.coff.lds
242    link_address='0x500000'
243    make_space=n
244    pie=
245    ;;
246miboot|uboot*)
247    # miboot and U-boot want just the bare bits, not an ELF binary
248    ext=bin
249    objflags="-O binary"
250    tmp="$ofile"
251    ksection=image
252    isection=initrd
253    ;;
254cuboot*)
255    binary=y
256    compression=
257    case "$platform" in
258    *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
259        platformo=$object/cuboot-8xx.o
260        ;;
261    *5200*|*-motionpro)
262        platformo=$object/cuboot-52xx.o
263        ;;
264    *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
265        platformo=$object/cuboot-pq2.o
266        ;;
267    *-mpc824*)
268        platformo=$object/cuboot-824x.o
269        ;;
270    *-mpc83*|*-asp834x*)
271        platformo=$object/cuboot-83xx.o
272        ;;
273    *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
274        platformo=$object/cuboot-85xx-cpm2.o
275        ;;
276    *-mpc85*|*-tqm85*|*-sbc85*)
277        platformo=$object/cuboot-85xx.o
278        ;;
279    *-amigaone)
280        link_address='0x800000'
281        ;;
282    esac
283    ;;
284ps3)
285    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
286    lds=$object/zImage.ps3.lds
287    compression=
288    ext=bin
289    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
290    ksection=.kernel:vmlinux.bin
291    isection=.kernel:initrd
292    link_address=''
293    make_space=n
294    pie=
295    ;;
296ep88xc|ep405|ep8248e)
297    platformo="$object/fixed-head.o $object/$platform.o"
298    binary=y
299    ;;
300adder875-redboot)
301    platformo="$object/fixed-head.o $object/redboot-8xx.o"
302    binary=y
303    ;;
304simpleboot-virtex405-*)
305    platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o"
306    binary=y
307    ;;
308simpleboot-virtex440-*)
309    platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o"
310    binary=y
311    ;;
312simpleboot-*)
313    platformo="$object/fixed-head.o $object/simpleboot.o"
314    binary=y
315    ;;
316asp834x-redboot)
317    platformo="$object/fixed-head.o $object/redboot-83xx.o"
318    binary=y
319    ;;
320xpedite52*)
321    link_address='0x1400000'
322    platformo=$object/cuboot-85xx.o
323    ;;
324gamecube|wii)
325    link_address='0x600000'
326    platformo="$object/$platform-head.o $object/$platform.o"
327    ;;
328treeboot-currituck)
329    link_address='0x1000000'
330    ;;
331treeboot-akebono)
332    link_address='0x1000000'
333    ;;
334treeboot-iss4xx-mpic)
335    platformo="$object/treeboot-iss4xx.o"
336    ;;
337epapr)
338    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
339    link_address='0x20000000'
340    pie=-pie
341    ;;
342mvme5100)
343    platformo="$object/fixed-head.o $object/mvme5100.o"
344    binary=y
345    ;;
346mvme7100)
347    platformo="$object/motload-head.o $object/mvme7100.o"
348    link_address='0x4000000'
349    binary=y
350    ;;
351esac
352
353vmz="$tmpdir/`basename \"$kernel\"`.$ext"
354
355# Calculate the vmlinux.strip size
356${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
357strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
358
359if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
360    # recompress the image if we need to
361    case $compression in
362    .xz)
363        xz --check=crc32 -f -6 "$vmz.$$"
364        ;;
365    .gz)
366        gzip -n -f -9 "$vmz.$$"
367        ;;
368    *)
369        # drop the compression suffix so the stripped vmlinux is used
370        compression=
371	;;
372    esac
373
374    if [ -n "$cacheit" ]; then
375	mv -f "$vmz.$$$compression" "$vmz$compression"
376    else
377	vmz="$vmz.$$"
378    fi
379else
380    rm -f $vmz.$$
381fi
382
383vmz="$vmz$compression"
384
385if [ "$make_space" = "y" ]; then
386	# Round the size to next higher MB limit
387	round_size=$(((strip_size + 0xfffff) & 0xfff00000))
388
389	round_size=0x$(printf "%x" $round_size)
390	link_addr=$(printf "%d" $link_address)
391
392	if [ $link_addr -lt $strip_size ]; then
393	    echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
394			"overlaps the address of the wrapper($link_address)"
395	    echo "INFO: Fixing the link_address of wrapper to ($round_size)"
396	    link_address=$round_size
397	fi
398fi
399
400# Extract kernel version information, some platforms want to include
401# it in the image header
402version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
403    cut -d' ' -f3`
404if [ -n "$version" ]; then
405    uboot_version="-n Linux-$version"
406fi
407
408# physical offset of kernel image
409membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
410
411case "$platform" in
412uboot)
413    rm -f "$ofile"
414    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
415	$uboot_version -d "$vmz" "$ofile"
416    if [ -z "$cacheit" ]; then
417	rm -f "$vmz"
418    fi
419    exit 0
420    ;;
421uboot-obs600)
422    rm -f "$ofile"
423    # obs600 wants a multi image with an initrd, so we need to put a fake
424    # one in even when building a "normal" image.
425    if [ -n "$initrd" ]; then
426	real_rd="$initrd"
427    else
428	real_rd=`mktemp`
429	echo "\0" >>"$real_rd"
430    fi
431    ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
432	$uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
433    if [ -z "$initrd" ]; then
434	rm -f "$real_rd"
435    fi
436    if [ -z "$cacheit" ]; then
437	rm -f "$vmz"
438    fi
439    exit 0
440    ;;
441esac
442
443addsec() {
444    ${CROSS}objcopy $4 $1 \
445	--add-section=$3="$2" \
446	--set-section-flags=$3=contents,alloc,load,readonly,data
447}
448
449addsec $tmp "$vmz" $ksection $object/empty.o
450if [ -z "$cacheit" ]; then
451    rm -f "$vmz"
452fi
453
454if [ -n "$initrd" ]; then
455    addsec $tmp "$initrd" $isection
456fi
457
458if [ -n "$dtb" ]; then
459    addsec $tmp "$dtb" .kernel:dtb
460    if [ -n "$dts" ]; then
461	rm $dtb
462    fi
463fi
464
465if [ "$platform" != "miboot" ]; then
466    if [ -n "$link_address" ] ; then
467        text_start="-Ttext $link_address"
468    fi
469#link everything
470    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" \
471	$platformo $tmp $object/wrapper.a
472    rm $tmp
473fi
474
475# Some platforms need the zImage's entry point and base address
476base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
477entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
478
479if [ -n "$binary" ]; then
480    mv "$ofile" "$ofile".elf
481    ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
482fi
483
484# post-processing needed for some platforms
485case "$platform" in
486pseries|chrp|maple)
487    $objbin/addnote "$ofile"
488    ;;
489coff)
490    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
491    $objbin/hack-coff "$ofile"
492    ;;
493cuboot*)
494    gzip -n -f -9 "$ofile"
495    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
496            $uboot_version -d "$ofile".gz "$ofile"
497    ;;
498treeboot*)
499    mv "$ofile" "$ofile.elf"
500    $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
501    if [ -z "$cacheit" ]; then
502	rm -f "$ofile.elf"
503    fi
504    exit 0
505    ;;
506ps3)
507    # The ps3's loader supports loading a gzipped binary image from flash
508    # rom to ram addr zero. The loader then enters the system reset
509    # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
510    # a binary image of the kernel to be at addr zero, and yet have a
511    # suitable bootwrapper entry at 0x100.  To construct the final rom
512    # image 512 bytes from offset 0x100 is copied to the bootwrapper
513    # place holder at symbol __system_reset_kernel.  The 512 bytes of the
514    # bootwrapper entry code at symbol __system_reset_overlay is then
515    # copied to offset 0x100.  At runtime the bootwrapper program copies
516    # the data at __system_reset_kernel back to addr 0x100.
517
518    system_reset_overlay=0x`${CROSS}nm "$ofile" \
519        | grep ' __system_reset_overlay$'       \
520        | cut -d' ' -f1`
521    system_reset_overlay=`printf "%d" $system_reset_overlay`
522    system_reset_kernel=0x`${CROSS}nm "$ofile" \
523        | grep ' __system_reset_kernel$'       \
524        | cut -d' ' -f1`
525    system_reset_kernel=`printf "%d" $system_reset_kernel`
526    overlay_dest="256"
527    overlay_size="512"
528
529    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
530
531    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
532        skip=$overlay_dest seek=$system_reset_kernel          \
533        count=$overlay_size bs=1
534
535    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
536        skip=$system_reset_overlay seek=$overlay_dest         \
537        count=$overlay_size bs=1
538
539    odir="$(dirname "$ofile.bin")"
540    rm -f "$odir/otheros.bld"
541    gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
542    ;;
543esac
544