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 463f884bf5SPeter Tyser# mkimage wrapper script 473f884bf5SPeter TyserMKIMAGE=$srctree/scripts/mkuboot.sh 483f884bf5SPeter Tyser 492bf11819SPaul Mackerras# directory for object and other files used by this script 502bf11819SPaul Mackerrasobject=arch/powerpc/boot 515c539ee3SDavid Woodhouseobjbin=$object 52c79b2973SLucian Adrian Grijincudtc=scripts/dtc/dtc 532bf11819SPaul Mackerras 542bf11819SPaul Mackerras# directory for working files 552bf11819SPaul Mackerrastmpdir=. 562bf11819SPaul Mackerras 572bf11819SPaul Mackerrasusage() { 582bf11819SPaul Mackerras echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2 592bf11819SPaul Mackerras echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2 60a9903811SScott Wood echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2 612bf11819SPaul Mackerras exit 1 622bf11819SPaul Mackerras} 632bf11819SPaul Mackerras 642bf11819SPaul Mackerraswhile [ "$#" -gt 0 ]; do 652bf11819SPaul Mackerras case "$1" in 662bf11819SPaul Mackerras -o) 672bf11819SPaul Mackerras shift 682bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 692bf11819SPaul Mackerras ofile="$1" 702bf11819SPaul Mackerras ;; 712bf11819SPaul Mackerras -p) 722bf11819SPaul Mackerras shift 732bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 742bf11819SPaul Mackerras platform="$1" 752bf11819SPaul Mackerras ;; 762bf11819SPaul Mackerras -i) 772bf11819SPaul Mackerras shift 782bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 792bf11819SPaul Mackerras initrd="$1" 802bf11819SPaul Mackerras ;; 812bf11819SPaul Mackerras -d) 822bf11819SPaul Mackerras shift 832bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 842bf11819SPaul Mackerras dtb="$1" 852bf11819SPaul Mackerras ;; 862bf11819SPaul Mackerras -s) 872bf11819SPaul Mackerras shift 882bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 892bf11819SPaul Mackerras dts="$1" 902bf11819SPaul Mackerras ;; 912bf11819SPaul Mackerras -c) 922bf11819SPaul Mackerras cacheit=y 932bf11819SPaul Mackerras ;; 942bf11819SPaul Mackerras -C) 952bf11819SPaul Mackerras shift 962bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 972bf11819SPaul Mackerras CROSS="$1" 982bf11819SPaul Mackerras ;; 992bf11819SPaul Mackerras -D) 1002bf11819SPaul Mackerras shift 1012bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 1022bf11819SPaul Mackerras object="$1" 1035c539ee3SDavid Woodhouse objbin="$1" 1042bf11819SPaul Mackerras ;; 1052bf11819SPaul Mackerras -W) 1062bf11819SPaul Mackerras shift 1072bf11819SPaul Mackerras [ "$#" -gt 0 ] || usage 1082bf11819SPaul Mackerras tmpdir="$1" 1092bf11819SPaul Mackerras ;; 110a9903811SScott Wood --no-gzip) 111a9903811SScott Wood gzip= 112a9903811SScott Wood ;; 1132bf11819SPaul Mackerras -?) 1142bf11819SPaul Mackerras usage 1152bf11819SPaul Mackerras ;; 1162bf11819SPaul Mackerras *) 1172bf11819SPaul Mackerras [ -z "$kernel" ] || usage 1182bf11819SPaul Mackerras kernel="$1" 1192bf11819SPaul Mackerras ;; 1202bf11819SPaul Mackerras esac 1212bf11819SPaul Mackerras shift 1222bf11819SPaul Mackerrasdone 1232bf11819SPaul Mackerras 1242bf11819SPaul Mackerrasif [ -n "$dts" ]; then 125701172d1SDavid Woodhouse if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then 126701172d1SDavid Woodhouse dts="$object/dts/$dts" 127701172d1SDavid Woodhouse fi 1282bf11819SPaul Mackerras if [ -z "$dtb" ]; then 1292bf11819SPaul Mackerras dtb="$platform.dtb" 1302bf11819SPaul Mackerras fi 131c79b2973SLucian Adrian Grijincu $dtc -O dtb -o "$dtb" -b 0 "$dts" 1322bf11819SPaul Mackerrasfi 1332bf11819SPaul Mackerras 1342bf11819SPaul Mackerrasif [ -z "$kernel" ]; then 1352bf11819SPaul Mackerras kernel=vmlinux 1362bf11819SPaul Mackerrasfi 1372bf11819SPaul Mackerras 1382bf11819SPaul Mackerrasplatformo=$object/"$platform".o 1392bf11819SPaul Mackerraslds=$object/zImage.lds 1402bf11819SPaul Mackerrasext=strip 1412bf11819SPaul Mackerrasobjflags=-S 1422bf11819SPaul Mackerrastmp=$tmpdir/zImage.$$.o 1432bf11819SPaul Mackerrasksection=.kernel:vmlinux.strip 1442bf11819SPaul Mackerrasisection=.kernel:initrd 1459b09c6d9STony Breedslink_address='0x400000' 1462bf11819SPaul Mackerras 1472bf11819SPaul Mackerrascase "$platform" in 1489b09c6d9STony Breedspseries) 1499b09c6d9STony Breeds platformo=$object/of.o 1509b09c6d9STony Breeds link_address='0x4000000' 1519b09c6d9STony Breeds ;; 152*58706ef9SCorey Minyardmaple) 153*58706ef9SCorey Minyard platformo=$object/of.o 154*58706ef9SCorey Minyard link_address='0x400000' 155*58706ef9SCorey Minyard ;; 1569b09c6d9STony Breedspmac|chrp) 1572bf11819SPaul Mackerras platformo=$object/of.o 1582bf11819SPaul Mackerras ;; 159627aa944SMilton Millercoff) 1602bf11819SPaul Mackerras platformo=$object/of.o 1612bf11819SPaul Mackerras lds=$object/zImage.coff.lds 1629b09c6d9STony Breeds link_address='0x500000' 1632bf11819SPaul Mackerras ;; 1642bf11819SPaul Mackerrasmiboot|uboot) 1652bf11819SPaul Mackerras # miboot and U-boot want just the bare bits, not an ELF binary 1662bf11819SPaul Mackerras ext=bin 1672bf11819SPaul Mackerras objflags="-O binary" 1682bf11819SPaul Mackerras tmp="$ofile" 1692bf11819SPaul Mackerras ksection=image 1702bf11819SPaul Mackerras isection=initrd 1712bf11819SPaul Mackerras ;; 1720fdd717eSScott Woodcuboot*) 17311c146ccSScott Wood binary=y 1740fdd717eSScott Wood gzip= 17525431333SGrant Likely case "$platform" in 1768dd217b2SScott Wood *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc) 17725431333SGrant Likely platformo=$object/cuboot-8xx.o 17825431333SGrant Likely ;; 17925431333SGrant Likely *5200*|*-motionpro) 18025431333SGrant Likely platformo=$object/cuboot-52xx.o 18125431333SGrant Likely ;; 18225431333SGrant Likely *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter) 18325431333SGrant Likely platformo=$object/cuboot-pq2.o 18425431333SGrant Likely ;; 18525431333SGrant Likely *-mpc824*) 18625431333SGrant Likely platformo=$object/cuboot-824x.o 18725431333SGrant Likely ;; 18859d13f9dSBryan O'Donoghue *-mpc83*|*-asp834x*) 18925431333SGrant Likely platformo=$object/cuboot-83xx.o 19025431333SGrant Likely ;; 191ff880112SAlexandr Smirnov *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*) 19225431333SGrant Likely platformo=$object/cuboot-85xx-cpm2.o 19325431333SGrant Likely ;; 1946dd1b64aSWolfgang Grandegger *-mpc85*|*-tqm85*|*-sbc85*) 19525431333SGrant Likely platformo=$object/cuboot-85xx.o 19625431333SGrant Likely ;; 1978f23735dSGerhard Pircher *-amigaone) 1988f23735dSGerhard Pircher link_address='0x800000' 1998f23735dSGerhard Pircher ;; 20025431333SGrant Likely esac 2010fdd717eSScott Wood ;; 202bafdb645SGeoff Levandps3) 203bafdb645SGeoff Levand platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o" 204bafdb645SGeoff Levand lds=$object/zImage.ps3.lds 205bafdb645SGeoff Levand gzip= 206bafdb645SGeoff Levand ext=bin 207bafdb645SGeoff Levand objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data" 208bafdb645SGeoff Levand ksection=.kernel:vmlinux.bin 209bafdb645SGeoff Levand isection=.kernel:initrd 2109b09c6d9STony Breeds link_address='' 211bafdb645SGeoff Levand ;; 212a55387e5SScott Woodep88xc|ep405|ep8248e) 21311c146ccSScott Wood platformo="$object/fixed-head.o $object/$platform.o" 21411c146ccSScott Wood binary=y 21511c146ccSScott Wood ;; 216a55387e5SScott Woodadder875-redboot) 217a55387e5SScott Wood platformo="$object/fixed-head.o $object/redboot-8xx.o" 218a55387e5SScott Wood binary=y 219a55387e5SScott Wood ;; 220d2477b5cSGrant Likelysimpleboot-virtex405-*) 221d58577d8SJohn Linn platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o" 222d58577d8SJohn Linn binary=y 223d58577d8SJohn Linn ;; 224d58577d8SJohn Linnsimpleboot-virtex440-*) 225a7e1cf0cSGrant Likely platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o" 226d2477b5cSGrant Likely binary=y 227d2477b5cSGrant Likely ;; 2281d46e379SGrant Likelysimpleboot-*) 229a7e1cf0cSGrant Likely platformo="$object/fixed-head.o $object/simpleboot.o" 2301d46e379SGrant Likely binary=y 2311d46e379SGrant Likely ;; 23259d13f9dSBryan O'Donoghueasp834x-redboot) 23359d13f9dSBryan O'Donoghue platformo="$object/fixed-head.o $object/redboot-83xx.o" 23459d13f9dSBryan O'Donoghue binary=y 23559d13f9dSBryan O'Donoghue ;; 23624760823SNate Casexpedite52*) 23724760823SNate Case link_address='0x1400000' 23824760823SNate Case platformo=$object/cuboot-85xx.o 23924760823SNate Case ;; 2406cdd2417SAlbert Herranzgamecube|wii) 241b68a24bcSAlbert Herranz link_address='0x600000' 242b68a24bcSAlbert Herranz platformo="$object/$platform-head.o $object/$platform.o" 243b68a24bcSAlbert Herranz ;; 2442bf11819SPaul Mackerrasesac 2452bf11819SPaul Mackerras 2462bf11819SPaul Mackerrasvmz="$tmpdir/`basename \"$kernel\"`.$ext" 2471383a34fSMilton Millerif [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then 2482bf11819SPaul Mackerras ${CROSS}objcopy $objflags "$kernel" "$vmz.$$" 249a9903811SScott Wood 250a9903811SScott Wood if [ -n "$gzip" ]; then 2512bf11819SPaul Mackerras gzip -f -9 "$vmz.$$" 252a9903811SScott Wood fi 253a9903811SScott Wood 2542bf11819SPaul Mackerras if [ -n "$cacheit" ]; then 255a9903811SScott Wood mv -f "$vmz.$$$gzip" "$vmz$gzip" 2562bf11819SPaul Mackerras else 2572bf11819SPaul Mackerras vmz="$vmz.$$" 2582bf11819SPaul Mackerras fi 2592bf11819SPaul Mackerrasfi 2602bf11819SPaul Mackerras 261a9903811SScott Woodvmz="$vmz$gzip" 262a9903811SScott Wood 263a6afacb6SDavid Gibson# Extract kernel version information, some platforms want to include 264a6afacb6SDavid Gibson# it in the image header 2652bf11819SPaul Mackerrasversion=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \ 2662bf11819SPaul Mackerras cut -d' ' -f3` 2672bf11819SPaul Mackerrasif [ -n "$version" ]; then 268a6afacb6SDavid Gibson uboot_version="-n Linux-$version" 2692bf11819SPaul Mackerrasfi 2700fdd717eSScott Wood 271b18796d3SKumar Gala# physical offset of kernel image 272b18796d3SKumar Galamembase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'` 273b18796d3SKumar Gala 2740fdd717eSScott Woodcase "$platform" in 2750fdd717eSScott Wooduboot) 2760fdd717eSScott Wood rm -f "$ofile" 2773f884bf5SPeter Tyser ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \ 278a6afacb6SDavid Gibson $uboot_version -d "$vmz" "$ofile" 2792bf11819SPaul Mackerras if [ -z "$cacheit" ]; then 280a9903811SScott Wood rm -f "$vmz" 2812bf11819SPaul Mackerras fi 2822bf11819SPaul Mackerras exit 0 2832bf11819SPaul Mackerras ;; 2842bf11819SPaul Mackerrasesac 2852bf11819SPaul Mackerras 2862bf11819SPaul Mackerrasaddsec() { 2872bf11819SPaul Mackerras ${CROSS}objcopy $4 $1 \ 2882bf11819SPaul Mackerras --add-section=$3="$2" \ 2892bf11819SPaul Mackerras --set-section-flags=$3=contents,alloc,load,readonly,data 2902bf11819SPaul Mackerras} 2912bf11819SPaul Mackerras 292a9903811SScott Woodaddsec $tmp "$vmz" $ksection $object/empty.o 2932bf11819SPaul Mackerrasif [ -z "$cacheit" ]; then 294a9903811SScott Wood rm -f "$vmz" 2952bf11819SPaul Mackerrasfi 2962bf11819SPaul Mackerras 2972bf11819SPaul Mackerrasif [ -n "$initrd" ]; then 298c888554bSMark A. Greer addsec $tmp "$initrd" $isection 2992bf11819SPaul Mackerrasfi 3002bf11819SPaul Mackerras 3012bf11819SPaul Mackerrasif [ -n "$dtb" ]; then 302c888554bSMark A. Greer addsec $tmp "$dtb" .kernel:dtb 303e9c4b4bdSMark A. Greer if [ -n "$dts" ]; then 304e9c4b4bdSMark A. Greer rm $dtb 305e9c4b4bdSMark A. Greer fi 3062bf11819SPaul Mackerrasfi 3072bf11819SPaul Mackerras 3082bf11819SPaul Mackerrasif [ "$platform" != "miboot" ]; then 3099b09c6d9STony Breeds if [ -n "$link_address" ] ; then 3109b09c6d9STony Breeds text_start="-Ttext $link_address --defsym _start=$link_address" 3119b09c6d9STony Breeds fi 3129b09c6d9STony Breeds ${CROSS}ld -m elf32ppc -T $lds $text_start -o "$ofile" \ 313cd197ffcSDavid Gibson $platformo $tmp $object/wrapper.a 3142bf11819SPaul Mackerras rm $tmp 3152bf11819SPaul Mackerrasfi 3162bf11819SPaul Mackerras 317a6afacb6SDavid Gibson# Some platforms need the zImage's entry point and base address 318a6afacb6SDavid Gibsonbase=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1` 319a6afacb6SDavid Gibsonentry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3` 320a6afacb6SDavid Gibson 32111c146ccSScott Woodif [ -n "$binary" ]; then 32211c146ccSScott Wood mv "$ofile" "$ofile".elf 323aeb4552fSScott Wood ${CROSS}objcopy -O binary "$ofile".elf "$ofile" 32411c146ccSScott Woodfi 32511c146ccSScott Wood 3262bf11819SPaul Mackerras# post-processing needed for some platforms 3272bf11819SPaul Mackerrascase "$platform" in 328*58706ef9SCorey Minyardpseries|chrp|maple) 3295663a123SPaul Mackerras $objbin/addnote "$ofile" 3300dcd4401SPaul Mackerras ;; 331627aa944SMilton Millercoff) 332cd197ffcSDavid Gibson ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile" 3335c539ee3SDavid Woodhouse $objbin/hack-coff "$ofile" 3342bf11819SPaul Mackerras ;; 3350fdd717eSScott Woodcuboot*) 336aeb4552fSScott Wood gzip -f -9 "$ofile" 3373f884bf5SPeter Tyser ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \ 338aeb4552fSScott Wood $uboot_version -d "$ofile".gz "$ofile" 3390fdd717eSScott Wood ;; 340f6dfc805SDavid Gibsontreeboot*) 341f6dfc805SDavid Gibson mv "$ofile" "$ofile.elf" 3425c539ee3SDavid Woodhouse $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry" 343f6dfc805SDavid Gibson if [ -z "$cacheit" ]; then 344f6dfc805SDavid Gibson rm -f "$ofile.elf" 345f6dfc805SDavid Gibson fi 346f6dfc805SDavid Gibson exit 0 347f6dfc805SDavid Gibson ;; 348bafdb645SGeoff Levandps3) 3495761eaa3SGeoff Levand # The ps3's loader supports loading a gzipped binary image from flash 3505761eaa3SGeoff Levand # rom to ram addr zero. The loader then enters the system reset 3515761eaa3SGeoff Levand # vector at addr 0x100. A bootwrapper overlay is used to arrange for 3525761eaa3SGeoff Levand # a binary image of the kernel to be at addr zero, and yet have a 3535761eaa3SGeoff Levand # suitable bootwrapper entry at 0x100. To construct the final rom 3545761eaa3SGeoff Levand # image 512 bytes from offset 0x100 is copied to the bootwrapper 3555761eaa3SGeoff Levand # place holder at symbol __system_reset_kernel. The 512 bytes of the 3565761eaa3SGeoff Levand # bootwrapper entry code at symbol __system_reset_overlay is then 3575761eaa3SGeoff Levand # copied to offset 0x100. At runtime the bootwrapper program copies 3585761eaa3SGeoff Levand # the data at __system_reset_kernel back to addr 0x100. 359bafdb645SGeoff Levand 360aeb4552fSScott Wood system_reset_overlay=0x`${CROSS}nm "$ofile" \ 361bafdb645SGeoff Levand | grep ' __system_reset_overlay$' \ 362bafdb645SGeoff Levand | cut -d' ' -f1` 363bafdb645SGeoff Levand system_reset_overlay=`printf "%d" $system_reset_overlay` 364aeb4552fSScott Wood system_reset_kernel=0x`${CROSS}nm "$ofile" \ 365bafdb645SGeoff Levand | grep ' __system_reset_kernel$' \ 366bafdb645SGeoff Levand | cut -d' ' -f1` 367bafdb645SGeoff Levand system_reset_kernel=`printf "%d" $system_reset_kernel` 368bafdb645SGeoff Levand overlay_dest="256" 3695761eaa3SGeoff Levand overlay_size="512" 370bafdb645SGeoff Levand 371aeb4552fSScott Wood ${CROSS}objcopy -O binary "$ofile" "$ofile.bin" 372aeb4552fSScott Wood 373d4740373SGrant Likely dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \ 374bafdb645SGeoff Levand skip=$overlay_dest seek=$system_reset_kernel \ 375d4740373SGrant Likely count=$overlay_size bs=1 376bafdb645SGeoff Levand 377d4740373SGrant Likely dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \ 378bafdb645SGeoff Levand skip=$system_reset_overlay seek=$overlay_dest \ 379d4740373SGrant Likely count=$overlay_size bs=1 380bafdb645SGeoff Levand 381928b9695SDavid Woodhouse odir="$(dirname "$ofile.bin")" 382928b9695SDavid Woodhouse rm -f "$odir/otheros.bld" 383928b9695SDavid Woodhouse gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld" 384bafdb645SGeoff Levand ;; 3852bf11819SPaul Mackerrasesac 386