wrapper 9.0 KB

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