wrapper 9.0 KB

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