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" != "lzma" -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 .lzma) 378 xz --format=lzma -f -6 "$vmz.$$" 379 ;; 380 *) 381 # drop the compression suffix so the stripped vmlinux is used 382 compression= 383 uboot_comp=none 384 ;; 385 esac 386 387 if [ -n "$cacheit" ]; then 388 mv -f "$vmz.$$$compression" "$vmz$compression" 389 else 390 vmz="$vmz.$$" 391 fi 392else 393 rm -f $vmz.$$ 394fi 395 396vmz="$vmz$compression" 397 398if [ "$make_space" = "y" ]; then 399 # Round the size to next higher MB limit 400 round_size=$(((strip_size + 0xfffff) & 0xfff00000)) 401 402 round_size=0x$(printf "%x" $round_size) 403 link_addr=$(printf "%d" $link_address) 404 405 if [ $link_addr -lt $strip_size ]; then 406 echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \ 407 "overlaps the address of the wrapper($link_address)" 408 echo "INFO: Fixing the link_address of wrapper to ($round_size)" 409 link_address=$round_size 410 fi 411fi 412 413# Extract kernel version information, some platforms want to include 414# it in the image header 415version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \ 416 cut -d' ' -f3` 417if [ -n "$version" ]; then 418 uboot_version="-n Linux-$version" 419fi 420 421# physical offset of kernel image 422membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'` 423 424case "$platform" in 425uboot) 426 rm -f "$ofile" 427 ${MKIMAGE} -A ppc -O linux -T kernel -C $uboot_comp -a $membase -e $membase \ 428 $uboot_version -d "$vmz" "$ofile" 429 if [ -z "$cacheit" ]; then 430 rm -f "$vmz" 431 fi 432 exit 0 433 ;; 434uboot-obs600) 435 rm -f "$ofile" 436 # obs600 wants a multi image with an initrd, so we need to put a fake 437 # one in even when building a "normal" image. 438 if [ -n "$initrd" ]; then 439 real_rd="$initrd" 440 else 441 real_rd=`mktemp` 442 echo "\0" >>"$real_rd" 443 fi 444 ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \ 445 $uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile" 446 if [ -z "$initrd" ]; then 447 rm -f "$real_rd" 448 fi 449 if [ -z "$cacheit" ]; then 450 rm -f "$vmz" 451 fi 452 exit 0 453 ;; 454esac 455 456addsec() { 457 ${CROSS}objcopy $4 $1 \ 458 --add-section=$3="$2" \ 459 --set-section-flags=$3=contents,alloc,load,readonly,data 460} 461 462addsec $tmp "$vmz" $ksection $object/empty.o 463if [ -z "$cacheit" ]; then 464 rm -f "$vmz" 465fi 466 467if [ -n "$initrd" ]; then 468 addsec $tmp "$initrd" $isection 469fi 470 471if [ -n "$dtb" ]; then 472 addsec $tmp "$dtb" .kernel:dtb 473 if [ -n "$dts" ]; then 474 rm $dtb 475 fi 476fi 477 478if [ "$platform" != "miboot" ]; then 479 if [ -n "$link_address" ] ; then 480 text_start="-Ttext $link_address" 481 fi 482#link everything 483 ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" \ 484 $platformo $tmp $object/wrapper.a 485 rm $tmp 486fi 487 488# Some platforms need the zImage's entry point and base address 489base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1` 490entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3` 491 492if [ -n "$binary" ]; then 493 mv "$ofile" "$ofile".elf 494 ${CROSS}objcopy -O binary "$ofile".elf "$ofile" 495fi 496 497# post-processing needed for some platforms 498case "$platform" in 499pseries|chrp|maple) 500 $objbin/addnote "$ofile" 501 ;; 502coff) 503 ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile" 504 $objbin/hack-coff "$ofile" 505 ;; 506cuboot*) 507 gzip -n -f -9 "$ofile" 508 ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \ 509 $uboot_version -d "$ofile".gz "$ofile" 510 ;; 511treeboot*) 512 mv "$ofile" "$ofile.elf" 513 $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry" 514 if [ -z "$cacheit" ]; then 515 rm -f "$ofile.elf" 516 fi 517 exit 0 518 ;; 519ps3) 520 # The ps3's loader supports loading a gzipped binary image from flash 521 # rom to ram addr zero. The loader then enters the system reset 522 # vector at addr 0x100. A bootwrapper overlay is used to arrange for 523 # a binary image of the kernel to be at addr zero, and yet have a 524 # suitable bootwrapper entry at 0x100. To construct the final rom 525 # image 512 bytes from offset 0x100 is copied to the bootwrapper 526 # place holder at symbol __system_reset_kernel. The 512 bytes of the 527 # bootwrapper entry code at symbol __system_reset_overlay is then 528 # copied to offset 0x100. At runtime the bootwrapper program copies 529 # the data at __system_reset_kernel back to addr 0x100. 530 531 system_reset_overlay=0x`${CROSS}nm "$ofile" \ 532 | grep ' __system_reset_overlay$' \ 533 | cut -d' ' -f1` 534 system_reset_overlay=`printf "%d" $system_reset_overlay` 535 system_reset_kernel=0x`${CROSS}nm "$ofile" \ 536 | grep ' __system_reset_kernel$' \ 537 | cut -d' ' -f1` 538 system_reset_kernel=`printf "%d" $system_reset_kernel` 539 overlay_dest="256" 540 overlay_size="512" 541 542 ${CROSS}objcopy -O binary "$ofile" "$ofile.bin" 543 544 run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \ 545 skip=$overlay_dest seek=$system_reset_kernel \ 546 count=$overlay_size bs=1 547 548 run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \ 549 skip=$system_reset_overlay seek=$overlay_dest \ 550 count=$overlay_size bs=1 551 552 odir="$(dirname "$ofile.bin")" 553 rm -f "$odir/otheros.bld" 554 gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld" 555 ;; 556esac 557