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