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