wrapper 14 KB

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