wrapper 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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, lzma, lzo 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|lzma|lzo|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. nodl="--no-dynamic-linker"
  205. # suppress some warnings in recent ld versions
  206. nowarn="-z noexecstack"
  207. if "${CROSS}ld" -v --no-warn-rwx-segments >/dev/null 2>&1; then
  208. nowarn="$nowarn --no-warn-rwx-segments"
  209. fi
  210. platformo=$object/"$platform".o
  211. lds=$object/zImage.lds
  212. ext=strip
  213. objflags=-S
  214. tmp=$tmpdir/zImage.$$.o
  215. ksection=.kernel:vmlinux.strip
  216. isection=.kernel:initrd
  217. esection=.kernel:esm_blob
  218. link_address='0x400000'
  219. make_space=y
  220. if [ -n "$esm_blob" -a "$platform" != "pseries" ]; then
  221. echo "ESM blob not support on non-pseries platforms" >&2
  222. exit 1
  223. fi
  224. case "$platform" in
  225. of)
  226. platformo="$object/of.o $object/epapr.o"
  227. make_space=n
  228. ;;
  229. pseries)
  230. platformo="$object/pseries-head.o $object/of.o $object/epapr.o"
  231. link_address='0x4000000'
  232. if [ "$format" != "elf32ppc" ]; then
  233. link_address=
  234. pie=-pie
  235. fi
  236. make_space=n
  237. ;;
  238. pmac|chrp)
  239. platformo="$object/of.o $object/epapr.o"
  240. make_space=n
  241. ;;
  242. coff)
  243. platformo="$object/crt0.o $object/of.o $object/epapr.o"
  244. lds=$object/zImage.coff.lds
  245. link_address='0x500000'
  246. make_space=n
  247. pie=
  248. ;;
  249. miboot|uboot*)
  250. # miboot and U-boot want just the bare bits, not an ELF binary
  251. ext=bin
  252. objflags="-O binary"
  253. tmp="$ofile"
  254. ksection=image
  255. isection=initrd
  256. ;;
  257. cuboot*)
  258. binary=y
  259. compression=
  260. case "$platform" in
  261. *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
  262. platformo=$object/cuboot-8xx.o
  263. ;;
  264. *5200*|*-motionpro)
  265. platformo=$object/cuboot-52xx.o
  266. ;;
  267. *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
  268. platformo=$object/cuboot-pq2.o
  269. ;;
  270. *-mpc824*)
  271. platformo=$object/cuboot-824x.o
  272. ;;
  273. *-mpc83*|*-asp834x*)
  274. platformo=$object/cuboot-83xx.o
  275. ;;
  276. *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
  277. platformo=$object/cuboot-85xx-cpm2.o
  278. ;;
  279. *-mpc85*|*-tqm85*)
  280. platformo=$object/cuboot-85xx.o
  281. ;;
  282. *-amigaone)
  283. link_address='0x800000'
  284. ;;
  285. esac
  286. ;;
  287. ps3)
  288. platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
  289. lds=$object/zImage.ps3.lds
  290. compression=
  291. ext=bin
  292. objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
  293. ksection=.kernel:vmlinux.bin
  294. isection=.kernel:initrd
  295. link_address=''
  296. make_space=n
  297. pie=
  298. ;;
  299. ep88xc|ep8248e)
  300. platformo="$object/fixed-head.o $object/$platform.o"
  301. binary=y
  302. ;;
  303. adder875-redboot)
  304. platformo="$object/fixed-head.o $object/redboot-8xx.o"
  305. binary=y
  306. ;;
  307. simpleboot-*)
  308. platformo="$object/fixed-head.o $object/simpleboot.o"
  309. binary=y
  310. ;;
  311. asp834x-redboot)
  312. platformo="$object/fixed-head.o $object/redboot-83xx.o"
  313. binary=y
  314. ;;
  315. xpedite52*)
  316. link_address='0x1400000'
  317. platformo=$object/cuboot-85xx.o
  318. ;;
  319. gamecube|wii)
  320. link_address='0x600000'
  321. platformo="$object/$platform-head.o $object/$platform.o"
  322. ;;
  323. microwatt)
  324. link_address='0x500000'
  325. platformo="$object/fixed-head.o $object/$platform.o"
  326. binary=y
  327. ;;
  328. treeboot-currituck)
  329. link_address='0x1000000'
  330. ;;
  331. treeboot-akebono)
  332. link_address='0x1000000'
  333. ;;
  334. treeboot-iss4xx-mpic)
  335. platformo="$object/treeboot-iss4xx.o"
  336. ;;
  337. epapr)
  338. platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
  339. link_address='0x20000000'
  340. pie=-pie
  341. notext='-z notext'
  342. rodynamic=$(if ${CROSS}ld -V 2>&1 | grep -q LLD ; then echo "-z rodynamic"; fi)
  343. ;;
  344. mvme5100)
  345. platformo="$object/fixed-head.o $object/mvme5100.o"
  346. binary=y
  347. ;;
  348. mvme7100)
  349. platformo="$object/motload-head.o $object/mvme7100.o"
  350. link_address='0x4000000'
  351. binary=y
  352. ;;
  353. esac
  354. vmz="$tmpdir/`basename \"$kernel\"`.$ext"
  355. # Calculate the vmlinux.strip size
  356. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  357. strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")
  358. if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
  359. # recompress the image if we need to
  360. case $compression in
  361. .xz)
  362. xz --check=crc32 -f -6 "$vmz.$$"
  363. ;;
  364. .gz)
  365. gzip -n -f -9 "$vmz.$$"
  366. ;;
  367. .lzma)
  368. xz --format=lzma -f -6 "$vmz.$$"
  369. ;;
  370. .lzo)
  371. lzop -f -9 "$vmz.$$"
  372. ;;
  373. *)
  374. # drop the compression suffix so the stripped vmlinux is used
  375. compression=
  376. uboot_comp=none
  377. ;;
  378. esac
  379. if [ -n "$cacheit" ]; then
  380. mv -f "$vmz.$$$compression" "$vmz$compression"
  381. else
  382. vmz="$vmz.$$"
  383. fi
  384. else
  385. rm -f $vmz.$$
  386. fi
  387. vmz="$vmz$compression"
  388. if [ "$make_space" = "y" ]; then
  389. # Round the size to next higher MB limit
  390. round_size=$(((strip_size + 0xfffff) & 0xfff00000))
  391. round_size=0x$(printf "%x" $round_size)
  392. link_addr=$(printf "%d" $link_address)
  393. if [ $link_addr -lt $strip_size ]; then
  394. echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
  395. "overlaps the address of the wrapper($link_address)"
  396. echo "INFO: Fixing the link_address of wrapper to ($round_size)"
  397. link_address=$round_size
  398. fi
  399. fi
  400. # Extract kernel version information, some platforms want to include
  401. # it in the image header
  402. version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
  403. head -n1 | cut -d' ' -f3`
  404. if [ -n "$version" ]; then
  405. uboot_version="-n Linux-$version"
  406. fi
  407. # physical offset of kernel image
  408. membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
  409. case "$platform" in
  410. uboot)
  411. rm -f "$ofile"
  412. ${MKIMAGE} -A ppc -O linux -T kernel -C $uboot_comp -a $membase -e $membase \
  413. $uboot_version -d "$vmz" "$ofile"
  414. if [ -z "$cacheit" ]; then
  415. rm -f "$vmz"
  416. fi
  417. exit 0
  418. ;;
  419. esac
  420. addsec() {
  421. ${CROSS}objcopy $4 $1 \
  422. --add-section=$3="$2" \
  423. --set-section-flags=$3=contents,alloc,load,readonly,data
  424. }
  425. addsec $tmp "$vmz" $ksection $object/empty.o
  426. if [ -z "$cacheit" ]; then
  427. rm -f "$vmz"
  428. fi
  429. if [ -n "$initrd" ]; then
  430. addsec $tmp "$initrd" $isection
  431. fi
  432. if [ -n "$dtb" ]; then
  433. addsec $tmp "$dtb" .kernel:dtb
  434. if [ -n "$dts" ]; then
  435. rm $dtb
  436. fi
  437. fi
  438. if [ -n "$esm_blob" ]; then
  439. addsec $tmp "$esm_blob" $esection
  440. fi
  441. if [ "$platform" != "miboot" ]; then
  442. if [ -n "$link_address" ] ; then
  443. text_start="-Ttext $link_address"
  444. fi
  445. #link everything
  446. ${CROSS}ld -m $format -T $lds $text_start $pie $nodl $nowarn $rodynamic $notext -o "$ofile" $map \
  447. $platformo $tmp $object/wrapper.a
  448. rm $tmp
  449. fi
  450. # Some platforms need the zImage's entry point and base address
  451. base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
  452. entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
  453. if [ -n "$binary" ]; then
  454. mv "$ofile" "$ofile".elf
  455. ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
  456. fi
  457. # post-processing needed for some platforms
  458. case "$platform" in
  459. pseries|chrp)
  460. $objbin/addnote "$ofile"
  461. ;;
  462. coff)
  463. ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
  464. $objbin/hack-coff "$ofile"
  465. ;;
  466. cuboot*)
  467. gzip -n -f -9 "$ofile"
  468. ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
  469. $uboot_version -d "$ofile".gz "$ofile"
  470. ;;
  471. treeboot*)
  472. mv "$ofile" "$ofile.elf"
  473. $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
  474. if [ -z "$cacheit" ]; then
  475. rm -f "$ofile.elf"
  476. fi
  477. exit 0
  478. ;;
  479. ps3)
  480. # The ps3's loader supports loading a gzipped binary image from flash
  481. # rom to ram addr zero. The loader then enters the system reset
  482. # vector at addr 0x100. A bootwrapper overlay is used to arrange for
  483. # a binary image of the kernel to be at addr zero, and yet have a
  484. # suitable bootwrapper entry at 0x100. To construct the final rom
  485. # image 512 bytes from offset 0x100 is copied to the bootwrapper
  486. # place holder at symbol __system_reset_kernel. The 512 bytes of the
  487. # bootwrapper entry code at symbol __system_reset_overlay is then
  488. # copied to offset 0x100. At runtime the bootwrapper program copies
  489. # the data at __system_reset_kernel back to addr 0x100.
  490. system_reset_overlay=0x`${CROSS}nm "$ofile" \
  491. | grep ' __system_reset_overlay$' \
  492. | cut -d' ' -f1`
  493. system_reset_overlay=`printf "%d" $system_reset_overlay`
  494. system_reset_kernel=0x`${CROSS}nm "$ofile" \
  495. | grep ' __system_reset_kernel$' \
  496. | cut -d' ' -f1`
  497. system_reset_kernel=`printf "%d" $system_reset_kernel`
  498. overlay_dest="256"
  499. overlay_size="512"
  500. ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
  501. run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  502. skip=$overlay_dest seek=$system_reset_kernel \
  503. count=$overlay_size bs=1
  504. run_cmd dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  505. skip=$system_reset_overlay seek=$overlay_dest \
  506. count=$overlay_size bs=1
  507. odir="$(dirname "$ofile.bin")"
  508. # The ps3's flash loader has a size limit of 16 MiB for the uncompressed
  509. # image. If a compressed image that exceeded this limit is written to
  510. # flash the loader will decompress that image until the 16 MiB limit is
  511. # reached, then enter the system reset vector of the partially decompressed
  512. # image. No warning is issued.
  513. rm -f "$odir"/{otheros,otheros-too-big}.bld
  514. size=$(${CROSS}nm --no-sort --radix=d "$ofile" | grep -E ' _end$' | cut -d' ' -f1)
  515. bld="otheros.bld"
  516. if [ $size -gt $((0x1000000)) ]; then
  517. bld="otheros-too-big.bld"
  518. fi
  519. gzip -n --force -9 --stdout "$ofile.bin" > "$odir/$bld"
  520. ;;
  521. esac