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