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