wrapper 13 KB

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