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