12bf11819SPaul Mackerras#!/bin/sh 22bf11819SPaul Mackerras 32bf11819SPaul Mackerras# Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org> 42bf11819SPaul Mackerras# This program may be used under the terms of version 2 of the GNU 52bf11819SPaul Mackerras# General Public License. 62bf11819SPaul Mackerras 72bf11819SPaul Mackerras# This script takes a kernel binary and optionally an initrd image 82bf11819SPaul Mackerras# and/or a device-tree blob, and creates a bootable zImage for a 92bf11819SPaul Mackerras# given platform. 102bf11819SPaul Mackerras 112bf11819SPaul Mackerras# Options: 122bf11819SPaul Mackerras# -o zImage specify output file 132bf11819SPaul Mackerras# -p platform specify platform (links in $platform.o) 142bf11819SPaul Mackerras# -i initrd specify initrd file 152bf11819SPaul Mackerras# -d devtree specify device-tree blob 162bf11819SPaul Mackerras# -s tree.dts specify device-tree source file (needs dtc installed) 172bf11819SPaul Mackerras# -c cache $kernel.strip.gz (use if present & newer, else make) 182bf11819SPaul Mackerras# -C prefix specify command prefix for cross-building tools 192bf11819SPaul Mackerras# (strip, objcopy, ld) 202bf11819SPaul Mackerras# -D dir specify directory containing data files used by script 212bf11819SPaul Mackerras# (default ./arch/powerpc/boot) 222bf11819SPaul Mackerras# -W dir specify working directory for temporary files (default .) 232bf11819SPaul Mackerras 24d4740373SGrant Likely# Stop execution if any command fails 25d4740373SGrant Likelyset -e 26d4740373SGrant Likely 277f66c1fdSGrant Likely# Allow for verbose output 287f66c1fdSGrant Likelyif [ "$V" = 1 ]; then 297f66c1fdSGrant Likely set -x 307f66c1fdSGrant Likelyfi 317f66c1fdSGrant Likely 322bf11819SPaul Mackerras# defaults 332bf11819SPaul Mackerraskernel= 342bf11819SPaul Mackerrasofile=zImage 352bf11819SPaul Mackerrasplatform=of 362bf11819SPaul Mackerrasinitrd= 372bf11819SPaul Mackerrasdtb= 382bf11819SPaul Mackerrasdts= 392bf11819SPaul Mackerrascacheit= 4011c146ccSScott Woodbinary= 41a9903811SScott Woodgzip=.gz 422bf11819SPaul Mackerras 432bf11819SPaul Mackerras# cross-compilation prefix 442bf11819SPaul MackerrasCROSS= 452bf11819SPaul Mackerras 462bf11819SPaul Mackerras# directory for object and other files used by this script 472bf11819SPaul Mackerrasobject=arch/powerpc/boot 482bf11819SPaul Mackerras 492bf11819SPaul Mackerras# directory for working files 502bf11819SPaul Mackerrastmpdir=. 512bf11819SPaul Mackerras 522bf11819SPaul Mackerrasusage() { 532bf11819SPaul Mackerras echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2 542bf11819SPaul Mackerras echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2 55a9903811SScott Wood echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2 562bf11819SPaul Mackerras exit 1 572bf11819SPaul Mackerras} 582bf11819SPaul Mackerras 592bf11819SPaul Mackerraswhile [ "$#" -gt 0 ]; do 602bf11819SPaul Mackerras case "$1" in 612bf11819SPaul Mackerras -o) 622bf11819SPaul Mackerras shift 632bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 642bf11819SPaul Mackerras ofile="$1" 652bf11819SPaul Mackerras ;; 662bf11819SPaul Mackerras -p) 672bf11819SPaul Mackerras shift 682bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 692bf11819SPaul Mackerras platform="$1" 702bf11819SPaul Mackerras ;; 712bf11819SPaul Mackerras -i) 722bf11819SPaul Mackerras shift 732bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 742bf11819SPaul Mackerras initrd="$1" 752bf11819SPaul Mackerras ;; 762bf11819SPaul Mackerras -d) 772bf11819SPaul Mackerras shift 782bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 792bf11819SPaul Mackerras dtb="$1" 802bf11819SPaul Mackerras ;; 812bf11819SPaul Mackerras -s) 822bf11819SPaul Mackerras shift 832bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 842bf11819SPaul Mackerras dts="$1" 852bf11819SPaul Mackerras ;; 862bf11819SPaul Mackerras -c) 872bf11819SPaul Mackerras cacheit=y 882bf11819SPaul Mackerras ;; 892bf11819SPaul Mackerras -C) 902bf11819SPaul Mackerras shift 912bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 922bf11819SPaul Mackerras CROSS="$1" 932bf11819SPaul Mackerras ;; 942bf11819SPaul Mackerras -D) 952bf11819SPaul Mackerras shift 962bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 972bf11819SPaul Mackerras object="$1" 982bf11819SPaul Mackerras ;; 992bf11819SPaul Mackerras -W) 1002bf11819SPaul Mackerras shift 1012bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 1022bf11819SPaul Mackerras tmpdir="$1" 1032bf11819SPaul Mackerras ;; 104a9903811SScott Wood --no-gzip) 105a9903811SScott Wood gzip= 106a9903811SScott Wood ;; 1072bf11819SPaul Mackerras -?) 1082bf11819SPaul Mackerras usage 1092bf11819SPaul Mackerras ;; 1102bf11819SPaul Mackerras *) 1112bf11819SPaul Mackerras [ -z "$kernel" ] || usage 1122bf11819SPaul Mackerras kernel="$1" 1132bf11819SPaul Mackerras ;; 1142bf11819SPaul Mackerras esac 1152bf11819SPaul Mackerras shift 1162bf11819SPaul Mackerrasdone 1172bf11819SPaul Mackerras 1182bf11819SPaul Mackerrasif [ -n "$dts" ]; then 119*701172d1SDavid Woodhouse if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then 120*701172d1SDavid Woodhouse dts="$object/dts/$dts" 121*701172d1SDavid Woodhouse fi 1222bf11819SPaul Mackerras if [ -z "$dtb" ]; then 1232bf11819SPaul Mackerras dtb="$platform.dtb" 1242bf11819SPaul Mackerras fi 125d4740373SGrant Likely dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts" 1262bf11819SPaul Mackerrasfi 1272bf11819SPaul Mackerras 1282bf11819SPaul Mackerrasif [ -z "$kernel" ]; then 1292bf11819SPaul Mackerras kernel=vmlinux 1302bf11819SPaul Mackerrasfi 1312bf11819SPaul Mackerras 1322bf11819SPaul Mackerrasplatformo=$object/"$platform".o 1332bf11819SPaul Mackerraslds=$object/zImage.lds 1342bf11819SPaul Mackerrasext=strip 1352bf11819SPaul Mackerrasobjflags=-S 1362bf11819SPaul Mackerrastmp=$tmpdir/zImage.$$.o 1372bf11819SPaul Mackerrasksection=.kernel:vmlinux.strip 1382bf11819SPaul Mackerrasisection=.kernel:initrd 1392bf11819SPaul Mackerras 1402bf11819SPaul Mackerrascase "$platform" in 1412bf11819SPaul Mackerraspmac|pseries|chrp) 1422bf11819SPaul Mackerras platformo=$object/of.o 1432bf11819SPaul Mackerras ;; 144627aa944SMilton Millercoff) 1452bf11819SPaul Mackerras platformo=$object/of.o 1462bf11819SPaul Mackerras lds=$object/zImage.coff.lds 1472bf11819SPaul Mackerras ;; 1482bf11819SPaul Mackerrasmiboot|uboot) 1492bf11819SPaul Mackerras # miboot and U-boot want just the bare bits, not an ELF binary 1502bf11819SPaul Mackerras ext=bin 1512bf11819SPaul Mackerras objflags="-O binary" 1522bf11819SPaul Mackerras tmp="$ofile" 1532bf11819SPaul Mackerras ksection=image 1542bf11819SPaul Mackerras isection=initrd 1552bf11819SPaul Mackerras ;; 1560fdd717eSScott Woodcuboot*) 15711c146ccSScott Wood binary=y 1580fdd717eSScott Wood gzip= 1590fdd717eSScott Wood ;; 160bafdb645SGeoff Levandps3) 161bafdb645SGeoff Levand platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o" 162bafdb645SGeoff Levand lds=$object/zImage.ps3.lds 163bafdb645SGeoff Levand gzip= 164bafdb645SGeoff Levand ext=bin 165bafdb645SGeoff Levand objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data" 166bafdb645SGeoff Levand ksection=.kernel:vmlinux.bin 167bafdb645SGeoff Levand isection=.kernel:initrd 168bafdb645SGeoff Levand ;; 16911c146ccSScott Woodep88xc) 17011c146ccSScott Wood platformo="$object/fixed-head.o $object/$platform.o" 17111c146ccSScott Wood binary=y 17211c146ccSScott Wood ;; 1732bf11819SPaul Mackerrasesac 1742bf11819SPaul Mackerras 1752bf11819SPaul Mackerrasvmz="$tmpdir/`basename \"$kernel\"`.$ext" 1761383a34fSMilton Millerif [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then 1772bf11819SPaul Mackerras ${CROSS}objcopy $objflags "$kernel" "$vmz.$$" 178a9903811SScott Wood 179a9903811SScott Wood if [ -n "$gzip" ]; then 1802bf11819SPaul Mackerras gzip -f -9 "$vmz.$$" 181a9903811SScott Wood fi 182a9903811SScott Wood 1832bf11819SPaul Mackerras if [ -n "$cacheit" ]; then 184a9903811SScott Wood mv -f "$vmz.$$$gzip" "$vmz$gzip" 1852bf11819SPaul Mackerras else 1862bf11819SPaul Mackerras vmz="$vmz.$$" 1872bf11819SPaul Mackerras fi 1882bf11819SPaul Mackerrasfi 1892bf11819SPaul Mackerras 190a9903811SScott Woodvmz="$vmz$gzip" 191a9903811SScott Wood 192a6afacb6SDavid Gibson# Extract kernel version information, some platforms want to include 193a6afacb6SDavid Gibson# it in the image header 1942bf11819SPaul Mackerrasversion=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \ 1952bf11819SPaul Mackerras cut -d' ' -f3` 1962bf11819SPaul Mackerrasif [ -n "$version" ]; then 197a6afacb6SDavid Gibson uboot_version="-n Linux-$version" 1982bf11819SPaul Mackerrasfi 1990fdd717eSScott Wood 2000fdd717eSScott Woodcase "$platform" in 2010fdd717eSScott Wooduboot) 2020fdd717eSScott Wood rm -f "$ofile" 2032bf11819SPaul Mackerras mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \ 204a6afacb6SDavid Gibson $uboot_version -d "$vmz" "$ofile" 2052bf11819SPaul Mackerras if [ -z "$cacheit" ]; then 206a9903811SScott Wood rm -f "$vmz" 2072bf11819SPaul Mackerras fi 2082bf11819SPaul Mackerras exit 0 2092bf11819SPaul Mackerras ;; 2102bf11819SPaul Mackerrasesac 2112bf11819SPaul Mackerras 2122bf11819SPaul Mackerrasaddsec() { 2132bf11819SPaul Mackerras ${CROSS}objcopy $4 $1 \ 2142bf11819SPaul Mackerras --add-section=$3="$2" \ 2152bf11819SPaul Mackerras --set-section-flags=$3=contents,alloc,load,readonly,data 2162bf11819SPaul Mackerras} 2172bf11819SPaul Mackerras 218a9903811SScott Woodaddsec $tmp "$vmz" $ksection $object/empty.o 2192bf11819SPaul Mackerrasif [ -z "$cacheit" ]; then 220a9903811SScott Wood rm -f "$vmz" 2212bf11819SPaul Mackerrasfi 2222bf11819SPaul Mackerras 2232bf11819SPaul Mackerrasif [ -n "$initrd" ]; then 224c888554bSMark A. Greer addsec $tmp "$initrd" $isection 2252bf11819SPaul Mackerrasfi 2262bf11819SPaul Mackerras 2272bf11819SPaul Mackerrasif [ -n "$dtb" ]; then 228c888554bSMark A. Greer addsec $tmp "$dtb" .kernel:dtb 229e9c4b4bdSMark A. Greer if [ -n "$dts" ]; then 230e9c4b4bdSMark A. Greer rm $dtb 231e9c4b4bdSMark A. Greer fi 2322bf11819SPaul Mackerrasfi 2332bf11819SPaul Mackerras 2342bf11819SPaul Mackerrasif [ "$platform" != "miboot" ]; then 2352bf11819SPaul Mackerras ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \ 236cd197ffcSDavid Gibson $platformo $tmp $object/wrapper.a 2372bf11819SPaul Mackerras rm $tmp 2382bf11819SPaul Mackerrasfi 2392bf11819SPaul Mackerras 240a6afacb6SDavid Gibson# Some platforms need the zImage's entry point and base address 241a6afacb6SDavid Gibsonbase=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1` 242a6afacb6SDavid Gibsonentry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3` 243a6afacb6SDavid Gibson 24411c146ccSScott Woodif [ -n "$binary" ]; then 24511c146ccSScott Wood mv "$ofile" "$ofile".elf 246aeb4552fSScott Wood ${CROSS}objcopy -O binary "$ofile".elf "$ofile" 24711c146ccSScott Woodfi 24811c146ccSScott Wood 2492bf11819SPaul Mackerras# post-processing needed for some platforms 2502bf11819SPaul Mackerrascase "$platform" in 2512bf11819SPaul Mackerraspseries|chrp) 2522bf11819SPaul Mackerras $object/addnote "$ofile" 2532bf11819SPaul Mackerras ;; 254627aa944SMilton Millercoff) 255cd197ffcSDavid Gibson ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile" 2562bf11819SPaul Mackerras $object/hack-coff "$ofile" 2572bf11819SPaul Mackerras ;; 2580fdd717eSScott Woodcuboot*) 259aeb4552fSScott Wood gzip -f -9 "$ofile" 2600fdd717eSScott Wood mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \ 261aeb4552fSScott Wood $uboot_version -d "$ofile".gz "$ofile" 2620fdd717eSScott Wood ;; 263f6dfc805SDavid Gibsontreeboot*) 264f6dfc805SDavid Gibson mv "$ofile" "$ofile.elf" 265f6dfc805SDavid Gibson $object/mktree "$ofile.elf" "$ofile" "$base" "$entry" 266f6dfc805SDavid Gibson if [ -z "$cacheit" ]; then 267f6dfc805SDavid Gibson rm -f "$ofile.elf" 268f6dfc805SDavid Gibson fi 269f6dfc805SDavid Gibson exit 0 270f6dfc805SDavid Gibson ;; 271bafdb645SGeoff Levandps3) 272bafdb645SGeoff Levand # The ps3's loader supports loading gzipped binary images from flash 273bafdb645SGeoff Levand # rom to addr zero. The loader enters the image at addr 0x100. A 274bafdb645SGeoff Levand # bootwrapper overlay is use to arrange for the kernel to be loaded 275bafdb645SGeoff Levand # to addr zero and to have a suitable bootwrapper entry at 0x100. 276bafdb645SGeoff Levand # To construct the rom image, 0x100 bytes from offset 0x100 in the 277bafdb645SGeoff Levand # kernel is copied to the bootwrapper symbol __system_reset_kernel. 278bafdb645SGeoff Levand # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is 279bafdb645SGeoff Levand # then copied to offset 0x100. At runtime the bootwrapper program 280bafdb645SGeoff Levand # copies the 0x100 bytes at __system_reset_kernel to addr 0x100. 281bafdb645SGeoff Levand 282aeb4552fSScott Wood system_reset_overlay=0x`${CROSS}nm "$ofile" \ 283bafdb645SGeoff Levand | grep ' __system_reset_overlay$' \ 284bafdb645SGeoff Levand | cut -d' ' -f1` 285bafdb645SGeoff Levand system_reset_overlay=`printf "%d" $system_reset_overlay` 286aeb4552fSScott Wood system_reset_kernel=0x`${CROSS}nm "$ofile" \ 287bafdb645SGeoff Levand | grep ' __system_reset_kernel$' \ 288bafdb645SGeoff Levand | cut -d' ' -f1` 289bafdb645SGeoff Levand system_reset_kernel=`printf "%d" $system_reset_kernel` 290bafdb645SGeoff Levand overlay_dest="256" 291bafdb645SGeoff Levand overlay_size="256" 292bafdb645SGeoff Levand 293aeb4552fSScott Wood ${CROSS}objcopy -O binary "$ofile" "$ofile.bin" 294aeb4552fSScott Wood 295d4740373SGrant Likely dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \ 296bafdb645SGeoff Levand skip=$overlay_dest seek=$system_reset_kernel \ 297d4740373SGrant Likely count=$overlay_size bs=1 298bafdb645SGeoff Levand 299d4740373SGrant Likely dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \ 300bafdb645SGeoff Levand skip=$system_reset_overlay seek=$overlay_dest \ 301d4740373SGrant Likely count=$overlay_size bs=1 302bafdb645SGeoff Levand 303928b9695SDavid Woodhouse odir="$(dirname "$ofile.bin")" 304928b9695SDavid Woodhouse rm -f "$odir/otheros.bld" 305928b9695SDavid Woodhouse gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld" 306bafdb645SGeoff Levand ;; 3072bf11819SPaul Mackerrasesac 308