wrapper 11 KB

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