wrapper 14 KB

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