wrapper 8.6 KB

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