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