wrapper 14.5 KB
Newer Older
1
#!/bin/sh
2
# SPDX-License-Identifier: GPL-2.0-only
3 4 5 6 7 8 9 10 11 12 13 14 15

# Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>

# This script takes a kernel binary and optionally an initrd image
# and/or a device-tree blob, and creates a bootable zImage for a
# given platform.

# Options:
# -o zImage	specify output file
# -p platform	specify platform (links in $platform.o)
# -i initrd	specify initrd file
# -d devtree	specify device-tree blob
# -s tree.dts	specify device-tree source file (needs dtc installed)
16
# -e esm_blob   specify ESM blob for secure images
17 18 19 20 21 22
# -c		cache $kernel.strip.gz (use if present & newer, else make)
# -C prefix	specify command prefix for cross-building tools
#		(strip, objcopy, ld)
# -D dir	specify directory containing data files used by script
#		(default ./arch/powerpc/boot)
# -W dir	specify working directory for temporary files (default .)
23 24
# -z		use gzip (legacy)
# -Z zsuffix    compression to use (gz, xz or none)
25

26 27 28
# Stop execution if any command fails
set -e

29 30
export LC_ALL=C

31 32 33
# Allow for verbose output
if [ "$V" = 1 ]; then
    set -x
34
    map="-Map wrapper.map"
35 36
fi

37 38 39 40 41 42 43
# defaults
kernel=
ofile=zImage
platform=of
initrd=
dtb=
dts=
44
esm_blob=
45
cacheit=
46
binary=
47
compression=.gz
48
uboot_comp=gzip
49
pie=
50
format=
51
notext=
52
rodynamic=
53 54 55 56

# cross-compilation prefix
CROSS=

57 58 59
# mkimage wrapper script
MKIMAGE=$srctree/scripts/mkuboot.sh

60 61
# directory for object and other files used by this script
object=arch/powerpc/boot
62
objbin=$object
63
dtc=scripts/dtc/dtc
64 65 66 67 68 69

# directory for working files
tmpdir=.

usage() {
    echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
70 71 72
    echo '       [-d devtree] [-s tree.dts] [-e esm_blob]' >&2
    echo '       [-c] [-C cross-prefix] [-D datadir] [-W workingdir]' >&2
    echo '       [-Z (gz|xz|none)] [--no-compression] [vmlinux]' >&2
73 74 75
    exit 1
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
run_cmd() {
    if [ "$V" = 1 ]; then
        $* 2>&1
    else
        local msg

        set +e
        msg=$($* 2>&1)

        if [ $? -ne "0" ]; then
                echo $msg
                exit 1
        fi
        set -e
    fi
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
while [ "$#" -gt 0 ]; do
    case "$1" in
    -o)
	shift
	[ "$#" -gt 0 ] || usage
	ofile="$1"
	;;
    -p)
	shift
	[ "$#" -gt 0 ] || usage
	platform="$1"
	;;
    -i)
	shift
	[ "$#" -gt 0 ] || usage
	initrd="$1"
	;;
    -d)
	shift
	[ "$#" -gt 0 ] || usage
	dtb="$1"
	;;
115 116 117 118 119
    -e)
	shift
	[ "$#" -gt 0 ] || usage
	esm_blob="$1"
	;;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    -s)
	shift
	[ "$#" -gt 0 ] || usage
	dts="$1"
	;;
    -c)
	cacheit=y
	;;
    -C)
	shift
	[ "$#" -gt 0 ] || usage
	CROSS="$1"
	;;
    -D)
	shift
	[ "$#" -gt 0 ] || usage
	object="$1"
137
	objbin="$1"
138 139 140 141 142 143
	;;
    -W)
	shift
	[ "$#" -gt 0 ] || usage
	tmpdir="$1"
	;;
144 145
    -z)
	compression=.gz
146
	uboot_comp=gzip
147 148 149 150
	;;
    -Z)
	shift
	[ "$#" -gt 0 ] || usage
151
        [ "$1" != "gz" -o "$1" != "xz" -o "$1" != "lzma" -o "$1" != "lzo" -o "$1" != "none" ] || usage
152 153

	compression=".$1"
154
	uboot_comp=$1
155 156 157

        if [ $compression = ".none" ]; then
                compression=
158
		uboot_comp=none
159
        fi
160 161 162
	if [ $uboot_comp = "gz" ]; then
		uboot_comp=gzip
	fi
163
	;;
164
    --no-gzip)
165
        # a "feature" of the wrapper script is that it can be used outside
166 167
        # the kernel tree. So keeping this around for backwards compatibility.
        compression=
168
	uboot_comp=none
169
        ;;
170 171 172 173 174 175 176 177 178 179 180
    -?)
	usage
	;;
    *)
	[ -z "$kernel" ] || usage
	kernel="$1"
	;;
    esac
    shift
done

181

182
if [ -n "$dts" ]; then
183 184 185
    if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
	dts="$object/dts/$dts"
    fi
186 187 188
    if [ -z "$dtb" ]; then
	dtb="$platform.dtb"
    fi
189
    $dtc -O dtb -o "$dtb" -b 0 "$dts"
190 191 192 193 194 195
fi

if [ -z "$kernel" ]; then
    kernel=vmlinux
fi

196
LC_ALL=C elfformat="`${CROSS}objdump -p "$kernel" | grep 'file format' | awk '{print $4}'`"
197 198 199 200 201 202
case "$elfformat" in
    elf64-powerpcle)	format=elf64lppc	;;
    elf64-powerpc)	format=elf32ppc	;;
    elf32-powerpc)	format=elf32ppc	;;
esac

203 204 205 206 207 208 209 210 211 212
ld_version()
{
    # Poached from scripts/ld-version.sh, but we don't want to call that because
    # this script (wrapper) is distributed separately from the kernel source.
    # Extract linker version number from stdin and turn into single number.
    awk '{
	gsub(".*\\)", "");
	gsub(".*version ", "");
	gsub("-.*", "");
	split($1,a, ".");
213 214 215 216
	if( length(a[3]) == "8" )
		# a[3] is probably a date of format yyyymmdd used for release snapshots. We
		# can assume it to be zero as it does not signify a new version as such.
		a[3] = 0;
217 218 219 220 221
	print a[1]*100000000 + a[2]*1000000 + a[3]*10000;
	exit
    }'
}

222 223 224 225 226
ld_is_lld()
{
	${CROSS}ld -V 2>&1 | grep -q LLD
}

227 228 229 230 231 232 233
# Do not include PT_INTERP segment when linking pie. Non-pie linking
# just ignores this option.
LD_VERSION=$(${CROSS}ld --version | ld_version)
LD_NO_DL_MIN_VERSION=$(echo 2.26 | ld_version)
if [ "$LD_VERSION" -ge "$LD_NO_DL_MIN_VERSION" ] ; then
	nodl="--no-dynamic-linker"
fi
234

235 236 237 238 239 240 241 242
# suppress some warnings in recent ld versions
nowarn="-z noexecstack"
if ! ld_is_lld; then
	if [ "$LD_VERSION" -ge "$(echo 2.39 | ld_version)" ]; then
		nowarn="$nowarn --no-warn-rwx-segments"
	fi
fi

243 244 245 246 247 248 249
platformo=$object/"$platform".o
lds=$object/zImage.lds
ext=strip
objflags=-S
tmp=$tmpdir/zImage.$$.o
ksection=.kernel:vmlinux.strip
isection=.kernel:initrd
250
esection=.kernel:esm_blob
251
link_address='0x400000'
252
make_space=y
253

254 255 256 257 258 259

if [ -n "$esm_blob" -a "$platform" != "pseries" ]; then
    echo "ESM blob not support on non-pseries platforms" >&2
    exit 1
fi

260
case "$platform" in
261 262 263 264
of)
    platformo="$object/of.o $object/epapr.o"
    make_space=n
    ;;
265
pseries)
266
    platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
267
    link_address='0x4000000'
268 269 270 271
    if [ "$format" != "elf32ppc" ]; then
	link_address=
	pie=-pie
    fi
272
    make_space=n
273
    ;;
274
maple)
275
    platformo="$object/of.o $object/epapr.o"
276
    link_address='0x400000'
277
    make_space=n
278
    ;;
279
pmac|chrp)
280
    platformo="$object/of.o $object/epapr.o"
281
    make_space=n
282
    ;;
283
coff)
284
    platformo="$object/crt0.o $object/of.o $object/epapr.o"
285
    lds=$object/zImage.coff.lds
286
    link_address='0x500000'
287
    make_space=n
288
    pie=
289
    ;;
290
miboot|uboot*)
291 292 293 294 295 296 297
    # miboot and U-boot want just the bare bits, not an ELF binary
    ext=bin
    objflags="-O binary"
    tmp="$ofile"
    ksection=image
    isection=initrd
    ;;
298
cuboot*)
299
    binary=y
300
    compression=
301
    case "$platform" in
302
    *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
303 304 305 306 307 308 309 310 311 312 313
        platformo=$object/cuboot-8xx.o
        ;;
    *5200*|*-motionpro)
        platformo=$object/cuboot-52xx.o
        ;;
    *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
        platformo=$object/cuboot-pq2.o
        ;;
    *-mpc824*)
        platformo=$object/cuboot-824x.o
        ;;
314
    *-mpc83*|*-asp834x*)
315 316
        platformo=$object/cuboot-83xx.o
        ;;
317
    *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
318 319
        platformo=$object/cuboot-85xx-cpm2.o
        ;;
320
    *-mpc85*|*-tqm85*)
321 322
        platformo=$object/cuboot-85xx.o
        ;;
323 324 325
    *-amigaone)
        link_address='0x800000'
        ;;
326
    esac
327
    ;;
328 329 330
ps3)
    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
    lds=$object/zImage.ps3.lds
331
    compression=
332 333 334 335
    ext=bin
    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
    ksection=.kernel:vmlinux.bin
    isection=.kernel:initrd
336
    link_address=''
337
    make_space=n
338
    pie=
339
    ;;
340
ep88xc|ep405|ep8248e)
341 342 343
    platformo="$object/fixed-head.o $object/$platform.o"
    binary=y
    ;;
344 345 346 347
adder875-redboot)
    platformo="$object/fixed-head.o $object/redboot-8xx.o"
    binary=y
    ;;
348
simpleboot-*)
349
    platformo="$object/fixed-head.o $object/simpleboot.o"
350 351
    binary=y
    ;;
352 353 354 355
asp834x-redboot)
    platformo="$object/fixed-head.o $object/redboot-83xx.o"
    binary=y
    ;;
356 357 358 359
xpedite52*)
    link_address='0x1400000'
    platformo=$object/cuboot-85xx.o
    ;;
360
gamecube|wii)
361 362 363
    link_address='0x600000'
    platformo="$object/$platform-head.o $object/$platform.o"
    ;;
364 365 366 367 368
microwatt)
    link_address='0x500000'
    platformo="$object/fixed-head.o $object/$platform.o"
    binary=y
    ;;
369 370 371
treeboot-currituck)
    link_address='0x1000000'
    ;;
372 373 374
treeboot-akebono)
    link_address='0x1000000'
    ;;
375 376 377
treeboot-iss4xx-mpic)
    platformo="$object/treeboot-iss4xx.o"
    ;;
378
epapr)
379
    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
380 381
    link_address='0x20000000'
    pie=-pie
382
    notext='-z notext'
383
    rodynamic=$(if ${CROSS}ld -V 2>&1 | grep -q LLD ; then echo "-z rodynamic"; fi)
384
    ;;
385 386 387 388
mvme5100)
    platformo="$object/fixed-head.o $object/mvme5100.o"
    binary=y
    ;;
389 390 391 392 393
mvme7100)
    platformo="$object/motload-head.o $object/mvme7100.o"
    link_address='0x4000000'
    binary=y
    ;;
394 395 396
esac

vmz="$tmpdir/`basename \"$kernel\"`.$ext"
397

398 399
# Calculate the vmlinux.strip size
${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
400
strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
401

402 403 404 405 406 407 408
if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
    # recompress the image if we need to
    case $compression in
    .xz)
        xz --check=crc32 -f -6 "$vmz.$$"
        ;;
    .gz)
Michal Marek's avatar
Michal Marek committed
409
        gzip -n -f -9 "$vmz.$$"
410
        ;;
411 412 413
    .lzma)
        xz --format=lzma -f -6 "$vmz.$$"
	;;
414 415 416
    .lzo)
        lzop -f -9 "$vmz.$$"
	;;
417 418 419
    *)
        # drop the compression suffix so the stripped vmlinux is used
        compression=
420
	uboot_comp=none
421 422
	;;
    esac
423

424
    if [ -n "$cacheit" ]; then
425
	mv -f "$vmz.$$$compression" "$vmz$compression"
426 427 428
    else
	vmz="$vmz.$$"
    fi
429 430 431 432
else
    rm -f $vmz.$$
fi

433 434
vmz="$vmz$compression"

435 436 437
if [ "$make_space" = "y" ]; then
	# Round the size to next higher MB limit
	round_size=$(((strip_size + 0xfffff) & 0xfff00000))
438

439 440
	round_size=0x$(printf "%x" $round_size)
	link_addr=$(printf "%d" $link_address)
441

442 443 444 445 446 447
	if [ $link_addr -lt $strip_size ]; then
	    echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
			"overlaps the address of the wrapper($link_address)"
	    echo "INFO: Fixing the link_address of wrapper to ($round_size)"
	    link_address=$round_size
	fi
448 449
fi

450 451 452
# Extract kernel version information, some platforms want to include
# it in the image header
version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
453
    head -n1 | cut -d' ' -f3`
454 455 456
if [ -n "$version" ]; then
    uboot_version="-n Linux-$version"
fi
457

458 459 460
# physical offset of kernel image
membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`

461 462 463
case "$platform" in
uboot)
    rm -f "$ofile"
464
    ${MKIMAGE} -A ppc -O linux -T kernel -C $uboot_comp -a $membase -e $membase \
465
	$uboot_version -d "$vmz" "$ofile"
466
    if [ -z "$cacheit" ]; then
467
	rm -f "$vmz"
468 469 470
    fi
    exit 0
    ;;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
uboot-obs600)
    rm -f "$ofile"
    # obs600 wants a multi image with an initrd, so we need to put a fake
    # one in even when building a "normal" image.
    if [ -n "$initrd" ]; then
	real_rd="$initrd"
    else
	real_rd=`mktemp`
	echo "\0" >>"$real_rd"
    fi
    ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
	$uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
    if [ -z "$initrd" ]; then
	rm -f "$real_rd"
    fi
    if [ -z "$cacheit" ]; then
	rm -f "$vmz"
    fi
    exit 0
    ;;
491 492 493 494 495 496 497 498
esac

addsec() {
    ${CROSS}objcopy $4 $1 \
	--add-section=$3="$2" \
	--set-section-flags=$3=contents,alloc,load,readonly,data
}

499
addsec $tmp "$vmz" $ksection $object/empty.o
500
if [ -z "$cacheit" ]; then
501
    rm -f "$vmz"
502 503 504
fi

if [ -n "$initrd" ]; then
505
    addsec $tmp "$initrd" $isection
506 507 508
fi

if [ -n "$dtb" ]; then
509
    addsec $tmp "$dtb" .kernel:dtb
510 511 512
    if [ -n "$dts" ]; then
	rm $dtb
    fi
513 514
fi

515 516 517 518
if [ -n "$esm_blob" ]; then
    addsec $tmp "$esm_blob" $esection
fi

519
if [ "$platform" != "miboot" ]; then
520
    if [ -n "$link_address" ] ; then
521
        text_start="-Ttext $link_address"
522
    fi
523
#link everything
524
    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl $nowarn $rodynamic $notext -o "$ofile" $map \
525
	$platformo $tmp $object/wrapper.a
526 527 528
    rm $tmp
fi

529 530 531 532
# Some platforms need the zImage's entry point and base address
base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`

533 534
if [ -n "$binary" ]; then
    mv "$ofile" "$ofile".elf
535
    ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
536 537
fi

538 539
# post-processing needed for some platforms
case "$platform" in
540
pseries|chrp|maple)
541
    $objbin/addnote "$ofile"
542
    ;;
543
coff)
544
    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
545
    $objbin/hack-coff "$ofile"
546
    ;;
547
cuboot*)
Michal Marek's avatar
Michal Marek committed
548
    gzip -n -f -9 "$ofile"
549
    ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
550
            $uboot_version -d "$ofile".gz "$ofile"
551
    ;;
552 553
treeboot*)
    mv "$ofile" "$ofile.elf"
554
    $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
555 556 557 558 559
    if [ -z "$cacheit" ]; then
	rm -f "$ofile.elf"
    fi
    exit 0
    ;;
560
ps3)
561 562 563 564 565 566 567 568 569 570
    # The ps3's loader supports loading a gzipped binary image from flash
    # rom to ram addr zero. The loader then enters the system reset
    # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
    # a binary image of the kernel to be at addr zero, and yet have a
    # suitable bootwrapper entry at 0x100.  To construct the final rom
    # image 512 bytes from offset 0x100 is copied to the bootwrapper
    # place holder at symbol __system_reset_kernel.  The 512 bytes of the
    # bootwrapper entry code at symbol __system_reset_overlay is then
    # copied to offset 0x100.  At runtime the bootwrapper program copies
    # the data at __system_reset_kernel back to addr 0x100.
571

572
    system_reset_overlay=0x`${CROSS}nm "$ofile" \
573 574 575
        | grep ' __system_reset_overlay$'       \
        | cut -d' ' -f1`
    system_reset_overlay=`printf "%d" $system_reset_overlay`
576
    system_reset_kernel=0x`${CROSS}nm "$ofile" \
577 578 579 580
        | grep ' __system_reset_kernel$'       \
        | cut -d' ' -f1`
    system_reset_kernel=`printf "%d" $system_reset_kernel`
    overlay_dest="256"
581
    overlay_size="512"
582

583 584
    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"

585 586
    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
        skip=$overlay_dest seek=$system_reset_kernel          \
587
        count=$overlay_size bs=1
588

589 590
    run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
        skip=$system_reset_overlay seek=$overlay_dest         \
591
        count=$overlay_size bs=1
592

593
    odir="$(dirname "$ofile.bin")"
594 595 596 597 598 599 600

    # The ps3's flash loader has a size limit of 16 MiB for the uncompressed
    # image.  If a compressed image that exceeded this limit is written to
    # flash the loader will decompress that image until the 16 MiB limit is
    # reached, then enter the system reset vector of the partially decompressed
    # image.  No warning is issued.
    rm -f "$odir"/{otheros,otheros-too-big}.bld
601
    size=$(${CROSS}nm --no-sort --radix=d "$ofile" | grep -E ' _end$' | cut -d' ' -f1)
602 603 604 605 606
    bld="otheros.bld"
    if [ $size -gt $((0x1000000)) ]; then
        bld="otheros-too-big.bld"
    fi
    gzip -n --force -9 --stdout "$ofile.bin" > "$odir/$bld"
607
    ;;
608
esac