wrapper 14 KB

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