Back to home page

LXR

 
 

    


0001 #!/bin/sh
0002 
0003 # Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
0004 # This program may be used under the terms of version 2 of the GNU
0005 # General Public License.
0006 
0007 # This script takes a kernel binary and optionally an initrd image
0008 # and/or a device-tree blob, and creates a bootable zImage for a
0009 # given platform.
0010 
0011 # Options:
0012 # -o zImage     specify output file
0013 # -p platform   specify platform (links in $platform.o)
0014 # -i initrd     specify initrd file
0015 # -d devtree    specify device-tree blob
0016 # -s tree.dts   specify device-tree source file (needs dtc installed)
0017 # -c            cache $kernel.strip.gz (use if present & newer, else make)
0018 # -C prefix     specify command prefix for cross-building tools
0019 #               (strip, objcopy, ld)
0020 # -D dir        specify directory containing data files used by script
0021 #               (default ./arch/powerpc/boot)
0022 # -W dir        specify working directory for temporary files (default .)
0023 # -z            use gzip (legacy)
0024 # -Z zsuffix    compression to use (gz, xz or none)
0025 
0026 # Stop execution if any command fails
0027 set -e
0028 
0029 # Allow for verbose output
0030 if [ "$V" = 1 ]; then
0031     set -x
0032 fi
0033 
0034 # defaults
0035 kernel=
0036 ofile=zImage
0037 platform=of
0038 initrd=
0039 dtb=
0040 dts=
0041 cacheit=
0042 binary=
0043 compression=.gz
0044 pie=
0045 format=
0046 
0047 # cross-compilation prefix
0048 CROSS=
0049 
0050 # mkimage wrapper script
0051 MKIMAGE=$srctree/scripts/mkuboot.sh
0052 
0053 # directory for object and other files used by this script
0054 object=arch/powerpc/boot
0055 objbin=$object
0056 dtc=scripts/dtc/dtc
0057 
0058 # directory for working files
0059 tmpdir=.
0060 
0061 usage() {
0062     echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
0063     echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
0064     echo '       [-D datadir] [-W workingdir] [-Z (gz|xz|none)]' >&2
0065     echo '       [--no-compression] [vmlinux]' >&2
0066     exit 1
0067 }
0068 
0069 run_cmd() {
0070     if [ "$V" = 1 ]; then
0071         $* 2>&1
0072     else
0073         local msg
0074 
0075         set +e
0076         msg=$($* 2>&1)
0077 
0078         if [ $? -ne "0" ]; then
0079                 echo $msg
0080                 exit 1
0081         fi
0082         set -e
0083     fi
0084 }
0085 
0086 while [ "$#" -gt 0 ]; do
0087     case "$1" in
0088     -o)
0089         shift
0090         [ "$#" -gt 0 ] || usage
0091         ofile="$1"
0092         ;;
0093     -p)
0094         shift
0095         [ "$#" -gt 0 ] || usage
0096         platform="$1"
0097         ;;
0098     -i)
0099         shift
0100         [ "$#" -gt 0 ] || usage
0101         initrd="$1"
0102         ;;
0103     -d)
0104         shift
0105         [ "$#" -gt 0 ] || usage
0106         dtb="$1"
0107         ;;
0108     -s)
0109         shift
0110         [ "$#" -gt 0 ] || usage
0111         dts="$1"
0112         ;;
0113     -c)
0114         cacheit=y
0115         ;;
0116     -C)
0117         shift
0118         [ "$#" -gt 0 ] || usage
0119         CROSS="$1"
0120         ;;
0121     -D)
0122         shift
0123         [ "$#" -gt 0 ] || usage
0124         object="$1"
0125         objbin="$1"
0126         ;;
0127     -W)
0128         shift
0129         [ "$#" -gt 0 ] || usage
0130         tmpdir="$1"
0131         ;;
0132     -z)
0133         compression=.gz
0134         ;;
0135     -Z)
0136         shift
0137         [ "$#" -gt 0 ] || usage
0138         [ "$1" != "gz" -o "$1" != "xz" -o "$1" != "none" ] || usage
0139 
0140         compression=".$1"
0141 
0142         if [ $compression = ".none" ]; then
0143                 compression=
0144         fi
0145         ;;
0146     --no-gzip)
0147         # a "feature" of the the wrapper script is that it can be used outside
0148         # the kernel tree. So keeping this around for backwards compatibility.
0149         compression=
0150         ;;
0151     -?)
0152         usage
0153         ;;
0154     *)
0155         [ -z "$kernel" ] || usage
0156         kernel="$1"
0157         ;;
0158     esac
0159     shift
0160 done
0161 
0162 
0163 if [ -n "$dts" ]; then
0164     if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
0165         dts="$object/dts/$dts"
0166     fi
0167     if [ -z "$dtb" ]; then
0168         dtb="$platform.dtb"
0169     fi
0170     $dtc -O dtb -o "$dtb" -b 0 "$dts"
0171 fi
0172 
0173 if [ -z "$kernel" ]; then
0174     kernel=vmlinux
0175 fi
0176 
0177 LANG=C elfformat="`${CROSS}objdump -p "$kernel" | grep 'file format' | awk '{print $4}'`"
0178 case "$elfformat" in
0179     elf64-powerpcle)    format=elf64lppc        ;;
0180     elf64-powerpc)      format=elf32ppc ;;
0181     elf32-powerpc)      format=elf32ppc ;;
0182 esac
0183 
0184 ld_version()
0185 {
0186     # Poached from scripts/ld-version.sh, but we don't want to call that because
0187     # this script (wrapper) is distributed separately from the kernel source.
0188     # Extract linker version number from stdin and turn into single number.
0189     awk '{
0190         gsub(".*\\)", "");
0191         gsub(".*version ", "");
0192         gsub("-.*", "");
0193         split($1,a, ".");
0194         print a[1]*100000000 + a[2]*1000000 + a[3]*10000;
0195         exit
0196     }'
0197 }
0198 
0199 # Do not include PT_INTERP segment when linking pie. Non-pie linking
0200 # just ignores this option.
0201 LD_VERSION=$(${CROSS}ld --version | ld_version)
0202 LD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
0203 if [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
0204         nodl="--no-dynamic-linker"
0205 fi
0206 
0207 platformo=$object/"$platform".o
0208 lds=$object/zImage.lds
0209 ext=strip
0210 objflags=-S
0211 tmp=$tmpdir/zImage.$$.o
0212 ksection=.kernel:vmlinux.strip
0213 isection=.kernel:initrd
0214 link_address='0x400000'
0215 make_space=y
0216 
0217 case "$platform" in
0218 of)
0219     platformo="$object/of.o $object/epapr.o"
0220     make_space=n
0221     ;;
0222 pseries)
0223     platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
0224     link_address='0x4000000'
0225     if [ "$format" != "elf32ppc" ]; then
0226         link_address=
0227         pie=-pie
0228     fi
0229     make_space=n
0230     ;;
0231 maple)
0232     platformo="$object/of.o $object/epapr.o"
0233     link_address='0x400000'
0234     make_space=n
0235     ;;
0236 pmac|chrp)
0237     platformo="$object/of.o $object/epapr.o"
0238     make_space=n
0239     ;;
0240 coff)
0241     platformo="$object/crt0.o $object/of.o $object/epapr.o"
0242     lds=$object/zImage.coff.lds
0243     link_address='0x500000'
0244     make_space=n
0245     pie=
0246     ;;
0247 miboot|uboot*)
0248     # miboot and U-boot want just the bare bits, not an ELF binary
0249     ext=bin
0250     objflags="-O binary"
0251     tmp="$ofile"
0252     ksection=image
0253     isection=initrd
0254     ;;
0255 cuboot*)
0256     binary=y
0257     compression=
0258     case "$platform" in
0259     *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
0260         platformo=$object/cuboot-8xx.o
0261         ;;
0262     *5200*|*-motionpro)
0263         platformo=$object/cuboot-52xx.o
0264         ;;
0265     *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
0266         platformo=$object/cuboot-pq2.o
0267         ;;
0268     *-mpc824*)
0269         platformo=$object/cuboot-824x.o
0270         ;;
0271     *-mpc83*|*-asp834x*)
0272         platformo=$object/cuboot-83xx.o
0273         ;;
0274     *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
0275         platformo=$object/cuboot-85xx-cpm2.o
0276         ;;
0277     *-mpc85*|*-tqm85*|*-sbc85*)
0278         platformo=$object/cuboot-85xx.o
0279         ;;
0280     *-amigaone)
0281         link_address='0x800000'
0282         ;;
0283     esac
0284     ;;
0285 ps3)
0286     platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
0287     lds=$object/zImage.ps3.lds
0288     compression=
0289     ext=bin
0290     objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
0291     ksection=.kernel:vmlinux.bin
0292     isection=.kernel:initrd
0293     link_address=''
0294     make_space=n
0295     pie=
0296     ;;
0297 ep88xc|ep405|ep8248e)
0298     platformo="$object/fixed-head.o $object/$platform.o"
0299     binary=y
0300     ;;
0301 adder875-redboot)
0302     platformo="$object/fixed-head.o $object/redboot-8xx.o"
0303     binary=y
0304     ;;
0305 simpleboot-virtex405-*)
0306     platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o"
0307     binary=y
0308     ;;
0309 simpleboot-virtex440-*)
0310     platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o"
0311     binary=y
0312     ;;
0313 simpleboot-*)
0314     platformo="$object/fixed-head.o $object/simpleboot.o"
0315     binary=y
0316     ;;
0317 asp834x-redboot)
0318     platformo="$object/fixed-head.o $object/redboot-83xx.o"
0319     binary=y
0320     ;;
0321 xpedite52*)
0322     link_address='0x1400000'
0323     platformo=$object/cuboot-85xx.o
0324     ;;
0325 gamecube|wii)
0326     link_address='0x600000'
0327     platformo="$object/$platform-head.o $object/$platform.o"
0328     ;;
0329 treeboot-currituck)
0330     link_address='0x1000000'
0331     ;;
0332 treeboot-akebono)
0333     link_address='0x1000000'
0334     ;;
0335 treeboot-iss4xx-mpic)
0336     platformo="$object/treeboot-iss4xx.o"
0337     ;;
0338 epapr)
0339     platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
0340     link_address='0x20000000'
0341     pie=-pie
0342     ;;
0343 mvme5100)
0344     platformo="$object/fixed-head.o $object/mvme5100.o"
0345     binary=y
0346     ;;
0347 mvme7100)
0348     platformo="$object/motload-head.o $object/mvme7100.o"
0349     link_address='0x4000000'
0350     binary=y
0351     ;;
0352 esac
0353 
0354 vmz="$tmpdir/`basename \"$kernel\"`.$ext"
0355 
0356 # Calculate the vmlinux.strip size
0357 ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
0358 strip_size=$(stat -c %s $vmz.$$)
0359 
0360 if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
0361     # recompress the image if we need to
0362     case $compression in
0363     .xz)
0364         xz --check=crc32 -f -6 "$vmz.$$"
0365         ;;
0366     .gz)
0367         gzip -n -f -9 "$vmz.$$"
0368         ;;
0369     *)
0370         # drop the compression suffix so the stripped vmlinux is used
0371         compression=
0372         ;;
0373     esac
0374 
0375     if [ -n "$cacheit" ]; then
0376         mv -f "$vmz.$$$compression" "$vmz$compression"
0377     else
0378         vmz="$vmz.$$"
0379     fi
0380 else
0381     rm -f $vmz.$$
0382 fi
0383 
0384 vmz="$vmz$compression"
0385 
0386 if [ "$make_space" = "y" ]; then
0387         # Round the size to next higher MB limit
0388         round_size=$(((strip_size + 0xfffff) & 0xfff00000))
0389 
0390         round_size=0x$(printf "%x" $round_size)
0391         link_addr=$(printf "%d" $link_address)
0392 
0393         if [ $link_addr -lt $strip_size ]; then
0394             echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
0395                         "overlaps the address of the wrapper($link_address)"
0396             echo "INFO: Fixing the link_address of wrapper to ($round_size)"
0397             link_address=$round_size
0398         fi
0399 fi
0400 
0401 # Extract kernel version information, some platforms want to include
0402 # it in the image header
0403 version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
0404     cut -d' ' -f3`
0405 if [ -n "$version" ]; then
0406     uboot_version="-n Linux-$version"
0407 fi
0408 
0409 # physical offset of kernel image
0410 membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
0411 
0412 case "$platform" in
0413 uboot)
0414     rm -f "$ofile"
0415     ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
0416         $uboot_version -d "$vmz" "$ofile"
0417     if [ -z "$cacheit" ]; then
0418         rm -f "$vmz"
0419     fi
0420     exit 0
0421     ;;
0422 uboot-obs600)
0423     rm -f "$ofile"
0424     # obs600 wants a multi image with an initrd, so we need to put a fake
0425     # one in even when building a "normal" image.
0426     if [ -n "$initrd" ]; then
0427         real_rd="$initrd"
0428     else
0429         real_rd=`mktemp`
0430         echo "\0" >>"$real_rd"
0431     fi
0432     ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
0433         $uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
0434     if [ -z "$initrd" ]; then
0435         rm -f "$real_rd"
0436     fi
0437     if [ -z "$cacheit" ]; then
0438         rm -f "$vmz"
0439     fi
0440     exit 0
0441     ;;
0442 esac
0443 
0444 addsec() {
0445     ${CROSS}objcopy $4 $1 \
0446         --add-section=$3="$2" \
0447         --set-section-flags=$3=contents,alloc,load,readonly,data
0448 }
0449 
0450 addsec $tmp "$vmz" $ksection $object/empty.o
0451 if [ -z "$cacheit" ]; then
0452     rm -f "$vmz"
0453 fi
0454 
0455 if [ -n "$initrd" ]; then
0456     addsec $tmp "$initrd" $isection
0457 fi
0458 
0459 if [ -n "$dtb" ]; then
0460     addsec $tmp "$dtb" .kernel:dtb
0461     if [ -n "$dts" ]; then
0462         rm $dtb
0463     fi
0464 fi
0465 
0466 if [ "$platform" != "miboot" ]; then
0467     if [ -n "$link_address" ] ; then
0468         text_start="-Ttext $link_address"
0469     fi
0470 #link everything
0471     ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" \
0472         $platformo $tmp $object/wrapper.a
0473     rm $tmp
0474 fi
0475 
0476 # Some platforms need the zImage's entry point and base address
0477 base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
0478 entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
0479 
0480 if [ -n "$binary" ]; then
0481     mv "$ofile" "$ofile".elf
0482     ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
0483 fi
0484 
0485 # post-processing needed for some platforms
0486 case "$platform" in
0487 pseries|chrp|maple)
0488     $objbin/addnote "$ofile"
0489     ;;
0490 coff)
0491     ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
0492     $objbin/hack-coff "$ofile"
0493     ;;
0494 cuboot*)
0495     gzip -n -f -9 "$ofile"
0496     ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
0497             $uboot_version -d "$ofile".gz "$ofile"
0498     ;;
0499 treeboot*)
0500     mv "$ofile" "$ofile.elf"
0501     $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
0502     if [ -z "$cacheit" ]; then
0503         rm -f "$ofile.elf"
0504     fi
0505     exit 0
0506     ;;
0507 ps3)
0508     # The ps3's loader supports loading a gzipped binary image from flash
0509     # rom to ram addr zero. The loader then enters the system reset
0510     # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
0511     # a binary image of the kernel to be at addr zero, and yet have a
0512     # suitable bootwrapper entry at 0x100.  To construct the final rom
0513     # image 512 bytes from offset 0x100 is copied to the bootwrapper
0514     # place holder at symbol __system_reset_kernel.  The 512 bytes of the
0515     # bootwrapper entry code at symbol __system_reset_overlay is then
0516     # copied to offset 0x100.  At runtime the bootwrapper program copies
0517     # the data at __system_reset_kernel back to addr 0x100.
0518 
0519     system_reset_overlay=0x`${CROSS}nm "$ofile" \
0520         | grep ' __system_reset_overlay$'       \
0521         | cut -d' ' -f1`
0522     system_reset_overlay=`printf "%d" $system_reset_overlay`
0523     system_reset_kernel=0x`${CROSS}nm "$ofile" \
0524         | grep ' __system_reset_kernel$'       \
0525         | cut -d' ' -f1`
0526     system_reset_kernel=`printf "%d" $system_reset_kernel`
0527     overlay_dest="256"
0528     overlay_size="512"
0529 
0530     ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
0531 
0532     run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
0533         skip=$overlay_dest seek=$system_reset_kernel          \
0534         count=$overlay_size bs=1
0535 
0536     run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
0537         skip=$system_reset_overlay seek=$overlay_dest         \
0538         count=$overlay_size bs=1
0539 
0540     odir="$(dirname "$ofile.bin")"
0541     rm -f "$odir/otheros.bld"
0542     gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
0543     ;;
0544 esac