2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
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
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)
25 # Stop execution if any command fails
28 # Allow for verbose output
47 # cross-compilation prefix
50 # mkimage wrapper script
51 MKIMAGE=$srctree/scripts/mkuboot.sh
53 # directory for object and other files used by this script
54 object=arch/powerpc/boot
58 # directory for working files
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
78 if [ $? -ne "0" ]; then
86 while [ "$#" -gt 0 ]; do
90 [ "$#" -gt 0 ] || usage
95 [ "$#" -gt 0 ] || usage
100 [ "$#" -gt 0 ] || usage
105 [ "$#" -gt 0 ] || usage
110 [ "$#" -gt 0 ] || usage
118 [ "$#" -gt 0 ] || usage
123 [ "$#" -gt 0 ] || usage
129 [ "$#" -gt 0 ] || usage
138 [ "$#" -gt 0 ] || usage
139 [ "$1" != "gz" -o "$1" != "xz" -o "$1" != "lzma" -o "$1" != "lzo" -o "$1" != "none" ] || usage
144 if [ $compression = ".none" ]; then
148 if [ $uboot_comp = "gz" ]; then
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.
162 [ -z "$kernel" ] || usage
170 if [ -n "$dts" ]; then
171 if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
172 dts="$object/dts/$dts"
174 if [ -z "$dtb" ]; then
177 $dtc -O dtb -o "$dtb" -b 0 "$dts"
180 if [ -z "$kernel" ]; then
184 LANG=C elfformat="`${CROSS}objdump -p "$kernel" | grep 'file format' | awk '{print $4}'`"
186 elf64-powerpcle) format=elf64lppc ;;
187 elf64-powerpc) format=elf32ppc ;;
188 elf32-powerpc) format=elf32ppc ;;
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.
198 gsub(".*version ", "");
201 print a[1]*100000000 + a[2]*1000000 + a[3]*10000;
206 # Do not include PT_INTERP segment when linking pie. Non-pie linking
207 # just ignores this option.
208 LD_VERSION=$(${CROSS}ld --version | ld_version)
209 LD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
210 if [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
211 nodl="--no-dynamic-linker"
214 platformo=$object/"$platform".o
215 lds=$object/zImage.lds
218 tmp=$tmpdir/zImage.$$.o
219 ksection=.kernel:vmlinux.strip
220 isection=.kernel:initrd
221 link_address='0x400000'
226 platformo="$object/of.o $object/epapr.o"
230 platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
231 link_address='0x4000000'
232 if [ "$format" != "elf32ppc" ]; then
239 platformo="$object/of.o $object/epapr.o"
240 link_address='0x400000'
244 platformo="$object/of.o $object/epapr.o"
248 platformo="$object/crt0.o $object/of.o $object/epapr.o"
249 lds=$object/zImage.coff.lds
250 link_address='0x500000'
255 # miboot and U-boot want just the bare bits, not an ELF binary
266 *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
267 platformo=$object/cuboot-8xx.o
270 platformo=$object/cuboot-52xx.o
272 *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
273 platformo=$object/cuboot-pq2.o
276 platformo=$object/cuboot-824x.o
279 platformo=$object/cuboot-83xx.o
281 *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
282 platformo=$object/cuboot-85xx-cpm2.o
284 *-mpc85*|*-tqm85*|*-sbc85*)
285 platformo=$object/cuboot-85xx.o
288 link_address='0x800000'
293 platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
294 lds=$object/zImage.ps3.lds
297 objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
298 ksection=.kernel:vmlinux.bin
299 isection=.kernel:initrd
304 ep88xc|ep405|ep8248e)
305 platformo="$object/fixed-head.o $object/$platform.o"
309 platformo="$object/fixed-head.o $object/redboot-8xx.o"
312 simpleboot-virtex405-*)
313 platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o"
316 simpleboot-virtex440-*)
317 platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o"
321 platformo="$object/fixed-head.o $object/simpleboot.o"
325 platformo="$object/fixed-head.o $object/redboot-83xx.o"
329 link_address='0x1400000'
330 platformo=$object/cuboot-85xx.o
333 link_address='0x600000'
334 platformo="$object/$platform-head.o $object/$platform.o"
337 link_address='0x1000000'
340 link_address='0x1000000'
342 treeboot-iss4xx-mpic)
343 platformo="$object/treeboot-iss4xx.o"
346 platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
347 link_address='0x20000000'
351 platformo="$object/fixed-head.o $object/mvme5100.o"
355 platformo="$object/motload-head.o $object/mvme7100.o"
356 link_address='0x4000000'
361 vmz="$tmpdir/`basename \"$kernel\"`.$ext"
363 # Calculate the vmlinux.strip size
364 ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
365 strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
367 if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
368 # recompress the image if we need to
371 xz --check=crc32 -f -6 "$vmz.$$"
374 gzip -n -f -9 "$vmz.$$"
377 xz --format=lzma -f -6 "$vmz.$$"
383 # drop the compression suffix so the stripped vmlinux is used
389 if [ -n "$cacheit" ]; then
390 mv -f "$vmz.$$$compression" "$vmz$compression"
398 vmz="$vmz$compression"
400 if [ "$make_space" = "y" ]; then
401 # Round the size to next higher MB limit
402 round_size=$(((strip_size + 0xfffff) & 0xfff00000))
404 round_size=0x$(printf "%x" $round_size)
405 link_addr=$(printf "%d" $link_address)
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
415 # Extract kernel version information, some platforms want to include
416 # it in the image header
417 version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
419 if [ -n "$version" ]; then
420 uboot_version="-n Linux-$version"
423 # physical offset of kernel image
424 membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
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
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
444 echo "\0" >>"$real_rd"
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
451 if [ -z "$cacheit" ]; then
459 ${CROSS}objcopy $4 $1 \
460 --add-section=$3="$2" \
461 --set-section-flags=$3=contents,alloc,load,readonly,data
464 addsec $tmp "$vmz" $ksection $object/empty.o
465 if [ -z "$cacheit" ]; then
469 if [ -n "$initrd" ]; then
470 addsec $tmp "$initrd" $isection
473 if [ -n "$dtb" ]; then
474 addsec $tmp "$dtb" .kernel:dtb
475 if [ -n "$dts" ]; then
480 if [ "$platform" != "miboot" ]; then
481 if [ -n "$link_address" ] ; then
482 text_start="-Ttext $link_address"
485 ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" \
486 $platformo $tmp $object/wrapper.a
490 # Some platforms need the zImage's entry point and base address
491 base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
492 entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
494 if [ -n "$binary" ]; then
495 mv "$ofile" "$ofile".elf
496 ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
499 # post-processing needed for some platforms
502 $objbin/addnote "$ofile"
505 ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
506 $objbin/hack-coff "$ofile"
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"
514 mv "$ofile" "$ofile.elf"
515 $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
516 if [ -z "$cacheit" ]; then
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.
533 system_reset_overlay=0x`${CROSS}nm "$ofile" \
534 | grep ' __system_reset_overlay$' \
536 system_reset_overlay=`printf "%d" $system_reset_overlay`
537 system_reset_kernel=0x`${CROSS}nm "$ofile" \
538 | grep ' __system_reset_kernel$' \
540 system_reset_kernel=`printf "%d" $system_reset_kernel`
544 ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
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
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
554 odir="$(dirname "$ofile.bin")"
555 rm -f "$odir/otheros.bld"
556 gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"