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
218# Do not include PT_INTERP segment when linking pie. Non-pie linking
219# just ignores this option.
220LD_VERSION=$(${CROSS}ld --version | ld_version)
221LD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
222if [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
223	nodl="--no-dynamic-linker"
224fi
225
226platformo=$object/"$platform".o
227lds=$object/zImage.lds
228ext=strip
229objflags=-S
230tmp=$tmpdir/zImage.$$.o
231ksection=.kernel:vmlinux.strip
232isection=.kernel:initrd
233esection=.kernel:esm_blob
234link_address='0x400000'
235make_space=y
236
237
238if [ -n "$esm_blob" -a "$platform" != "pseries" ]; then
239    echo "ESM blob not support on non-pseries platforms" >&2
240    exit 1
241fi
242
243case "$platform" in
244of)
245    platformo="$object/of.o $object/epapr.o"
246    make_space=n
247    ;;
248pseries)
249    platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
250    link_address='0x4000000'
251    if [ "$format" != "elf32ppc" ]; then
252	link_address=
253	pie=-pie
254    fi
255    make_space=n
256    ;;
257maple)
258    platformo="$object/of.o $object/epapr.o"
259    link_address='0x400000'
260    make_space=n
261    ;;
262pmac|chrp)
263    platformo="$object/of.o $object/epapr.o"
264    make_space=n
265    ;;
266coff)
267    platformo="$object/crt0.o $object/of.o $object/epapr.o"
268    lds=$object/zImage.coff.lds
269    link_address='0x500000'
270    make_space=n
271    pie=
272    ;;
273miboot|uboot*)
274    # miboot and U-boot want just the bare bits, not an ELF binary
275    ext=bin
276    objflags="-O binary"
277    tmp="$ofile"
278    ksection=image
279    isection=initrd
280    ;;
281cuboot*)
282    binary=y
283    compression=
284    case "$platform" in
285    *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
286        platformo=$object/cuboot-8xx.o
287        ;;
288    *5200*|*-motionpro)
289        platformo=$object/cuboot-52xx.o
290        ;;
291    *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
292        platformo=$object/cuboot-pq2.o
293        ;;
294    *-mpc824*)
295        platformo=$object/cuboot-824x.o
296        ;;
297    *-mpc83*|*-asp834x*)
298        platformo=$object/cuboot-83xx.o
299        ;;
300    *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
301        platformo=$object/cuboot-85xx-cpm2.o
302        ;;
303    *-mpc85*|*-tqm85*)
304        platformo=$object/cuboot-85xx.o
305        ;;
306    *-amigaone)
307        link_address='0x800000'
308        ;;
309    esac
310    ;;
311ps3)
312    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
313    lds=$object/zImage.ps3.lds
314    compression=
315    ext=bin
316    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
317    ksection=.kernel:vmlinux.bin
318    isection=.kernel:initrd
319    link_address=''
320    make_space=n
321    pie=
322    ;;
323ep88xc|ep405|ep8248e)
324    platformo="$object/fixed-head.o $object/$platform.o"
325    binary=y
326    ;;
327adder875-redboot)
328    platformo="$object/fixed-head.o $object/redboot-8xx.o"
329    binary=y
330    ;;
331simpleboot-*)
332    platformo="$object/fixed-head.o $object/simpleboot.o"
333    binary=y
334    ;;
335asp834x-redboot)
336    platformo="$object/fixed-head.o $object/redboot-83xx.o"
337    binary=y
338    ;;
339xpedite52*)
340    link_address='0x1400000'
341    platformo=$object/cuboot-85xx.o
342    ;;
343gamecube|wii)
344    link_address='0x600000'
345    platformo="$object/$platform-head.o $object/$platform.o"
346    ;;
347microwatt)
348    link_address='0x500000'
349    platformo="$object/fixed-head.o $object/$platform.o"
350    binary=y
351    ;;
352treeboot-currituck)
353    link_address='0x1000000'
354    ;;
355treeboot-akebono)
356    link_address='0x1000000'
357    ;;
358treeboot-iss4xx-mpic)
359    platformo="$object/treeboot-iss4xx.o"
360    ;;
361epapr)
362    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
363    link_address='0x20000000'
364    pie=-pie
365    notext='-z notext'
366    rodynamic=$(if ${CROSS}ld -V 2>&1 | grep -q LLD ; then echo "-z rodynamic"; fi)
367    ;;
368mvme5100)
369    platformo="$object/fixed-head.o $object/mvme5100.o"
370    binary=y
371    ;;
372mvme7100)
373    platformo="$object/motload-head.o $object/mvme7100.o"
374    link_address='0x4000000'
375    binary=y
376    ;;
377esac
378
379vmz="$tmpdir/`basename \"$kernel\"`.$ext"
380
381# Calculate the vmlinux.strip size
382${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
383strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
384
385if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
386    # recompress the image if we need to
387    case $compression in
388    .xz)
389        xz --check=crc32 -f -6 "$vmz.$$"
390        ;;
391    .gz)
392        gzip -n -f -9 "$vmz.$$"
393        ;;
394    .lzma)
395        xz --format=lzma -f -6 "$vmz.$$"
396	;;
397    .lzo)
398        lzop -f -9 "$vmz.$$"
399	;;
400    *)
401        # drop the compression suffix so the stripped vmlinux is used
402        compression=
403	uboot_comp=none
404	;;
405    esac
406
407    if [ -n "$cacheit" ]; then
408	mv -f "$vmz.$$$compression" "$vmz$compression"
409    else
410	vmz="$vmz.$$"
411    fi
412else
413    rm -f $vmz.$$
414fi
415
416vmz="$vmz$compression"
417
418if [ "$make_space" = "y" ]; then
419	# Round the size to next higher MB limit
420	round_size=$(((strip_size + 0xfffff) & 0xfff00000))
421
422	round_size=0x$(printf "%x" $round_size)
423	link_addr=$(printf "%d" $link_address)
424
425	if [ $link_addr -lt $strip_size ]; then
426	    echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
427			"overlaps the address of the wrapper($link_address)"
428	    echo "INFO: Fixing the link_address of wrapper to ($round_size)"
429	    link_address=$round_size
430	fi
431fi
432
433# Extract kernel version information, some platforms want to include
434# it in the image header
435version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
436    head -n1 | cut -d' ' -f3`
437if [ -n "$version" ]; then
438    uboot_version="-n Linux-$version"
439fi
440
441# physical offset of kernel image
442membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
443
444case "$platform" in
445uboot)
446    rm -f "$ofile"
447    ${MKIMAGE} -A ppc -O linux -T kernel -C $uboot_comp -a $membase -e $membase \
448	$uboot_version -d "$vmz" "$ofile"
449    if [ -z "$cacheit" ]; then
450	rm -f "$vmz"
451    fi
452    exit 0
453    ;;
454uboot-obs600)
455    rm -f "$ofile"
456    # obs600 wants a multi image with an initrd, so we need to put a fake
457    # one in even when building a "normal" image.
458    if [ -n "$initrd" ]; then
459	real_rd="$initrd"
460    else
461	real_rd=`mktemp`
462	echo "\0" >>"$real_rd"
463    fi
464    ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
465	$uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
466    if [ -z "$initrd" ]; then
467	rm -f "$real_rd"
468    fi
469    if [ -z "$cacheit" ]; then
470	rm -f "$vmz"
471    fi
472    exit 0
473    ;;
474esac
475
476addsec() {
477    ${CROSS}objcopy $4 $1 \
478	--add-section=$3="$2" \
479	--set-section-flags=$3=contents,alloc,load,readonly,data
480}
481
482addsec $tmp "$vmz" $ksection $object/empty.o
483if [ -z "$cacheit" ]; then
484    rm -f "$vmz"
485fi
486
487if [ -n "$initrd" ]; then
488    addsec $tmp "$initrd" $isection
489fi
490
491if [ -n "$dtb" ]; then
492    addsec $tmp "$dtb" .kernel:dtb
493    if [ -n "$dts" ]; then
494	rm $dtb
495    fi
496fi
497
498if [ -n "$esm_blob" ]; then
499    addsec $tmp "$esm_blob" $esection
500fi
501
502if [ "$platform" != "miboot" ]; then
503    if [ -n "$link_address" ] ; then
504        text_start="-Ttext $link_address"
505    fi
506#link everything
507    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl $rodynamic $notext -o "$ofile" $map \
508	$platformo $tmp $object/wrapper.a
509    rm $tmp
510fi
511
512# Some platforms need the zImage's entry point and base address
513base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
514entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
515
516if [ -n "$binary" ]; then
517    mv "$ofile" "$ofile".elf
518    ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
519fi
520
521# post-processing needed for some platforms
522case "$platform" in
523pseries|chrp|maple)
524    $objbin/addnote "$ofile"
525    ;;
526coff)
527    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
528    $objbin/hack-coff "$ofile"
529    ;;
530cuboot*)
531    gzip -n -f -9 "$ofile"
532    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
533            $uboot_version -d "$ofile".gz "$ofile"
534    ;;
535treeboot*)
536    mv "$ofile" "$ofile.elf"
537    $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
538    if [ -z "$cacheit" ]; then
539	rm -f "$ofile.elf"
540    fi
541    exit 0
542    ;;
543ps3)
544    # The ps3's loader supports loading a gzipped binary image from flash
545    # rom to ram addr zero. The loader then enters the system reset
546    # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
547    # a binary image of the kernel to be at addr zero, and yet have a
548    # suitable bootwrapper entry at 0x100.  To construct the final rom
549    # image 512 bytes from offset 0x100 is copied to the bootwrapper
550    # place holder at symbol __system_reset_kernel.  The 512 bytes of the
551    # bootwrapper entry code at symbol __system_reset_overlay is then
552    # copied to offset 0x100.  At runtime the bootwrapper program copies
553    # the data at __system_reset_kernel back to addr 0x100.
554
555    system_reset_overlay=0x`${CROSS}nm "$ofile" \
556        | grep ' __system_reset_overlay$'       \
557        | cut -d' ' -f1`
558    system_reset_overlay=`printf "%d" $system_reset_overlay`
559    system_reset_kernel=0x`${CROSS}nm "$ofile" \
560        | grep ' __system_reset_kernel$'       \
561        | cut -d' ' -f1`
562    system_reset_kernel=`printf "%d" $system_reset_kernel`
563    overlay_dest="256"
564    overlay_size="512"
565
566    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
567
568    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
569        skip=$overlay_dest seek=$system_reset_kernel          \
570        count=$overlay_size bs=1
571
572    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
573        skip=$system_reset_overlay seek=$overlay_dest         \
574        count=$overlay_size bs=1
575
576    odir="$(dirname "$ofile.bin")"
577
578    # The ps3's flash loader has a size limit of 16 MiB for the uncompressed
579    # image.  If a compressed image that exceeded this limit is written to
580    # flash the loader will decompress that image until the 16 MiB limit is
581    # reached, then enter the system reset vector of the partially decompressed
582    # image.  No warning is issued.
583    rm -f "$odir"/{otheros,otheros-too-big}.bld
584    size=$(${CROSS}nm --no-sort --radix=d "$ofile" | egrep ' _end$' | cut -d' ' -f1)
585    bld="otheros.bld"
586    if [ $size -gt $((0x1000000)) ]; then
587        bld="otheros-too-big.bld"
588    fi
589    gzip -n --force -9 --stdout "$ofile.bin" > "$odir/$bld"
590    ;;
591esac
592