wrapper 8.8 KB

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