wrapper 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. esac
  219. vmz="$tmpdir/`basename \"$kernel\"`.$ext"
  220. if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
  221. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  222. if [ -n "$gzip" ]; then
  223. gzip -f -9 "$vmz.$$"
  224. fi
  225. if [ -n "$cacheit" ]; then
  226. mv -f "$vmz.$$$gzip" "$vmz$gzip"
  227. else
  228. vmz="$vmz.$$"
  229. fi
  230. fi
  231. vmz="$vmz$gzip"
  232. # Extract kernel version information, some platforms want to include
  233. # it in the image header
  234. version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
  235. cut -d' ' -f3`
  236. if [ -n "$version" ]; then
  237. uboot_version="-n Linux-$version"
  238. fi
  239. # physical offset of kernel image
  240. membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
  241. case "$platform" in
  242. uboot)
  243. rm -f "$ofile"
  244. mkimage -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
  245. $uboot_version -d "$vmz" "$ofile"
  246. if [ -z "$cacheit" ]; then
  247. rm -f "$vmz"
  248. fi
  249. exit 0
  250. ;;
  251. esac
  252. addsec() {
  253. ${CROSS}objcopy $4 $1 \
  254. --add-section=$3="$2" \
  255. --set-section-flags=$3=contents,alloc,load,readonly,data
  256. }
  257. addsec $tmp "$vmz" $ksection $object/empty.o
  258. if [ -z "$cacheit" ]; then
  259. rm -f "$vmz"
  260. fi
  261. if [ -n "$initrd" ]; then
  262. addsec $tmp "$initrd" $isection
  263. fi
  264. if [ -n "$dtb" ]; then
  265. addsec $tmp "$dtb" .kernel:dtb
  266. if [ -n "$dts" ]; then
  267. rm $dtb
  268. fi
  269. fi
  270. if [ "$platform" != "miboot" ]; then
  271. if [ -n "$link_address" ] ; then
  272. text_start="-Ttext $link_address --defsym _start=$link_address"
  273. fi
  274. ${CROSS}ld -m elf32ppc -T $lds $text_start -o "$ofile" \
  275. $platformo $tmp $object/wrapper.a
  276. rm $tmp
  277. fi
  278. # Some platforms need the zImage's entry point and base address
  279. base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
  280. entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
  281. if [ -n "$binary" ]; then
  282. mv "$ofile" "$ofile".elf
  283. ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
  284. fi
  285. # post-processing needed for some platforms
  286. case "$platform" in
  287. pseries|chrp)
  288. $objbin/addnote "$ofile"
  289. ;;
  290. coff)
  291. ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
  292. $objbin/hack-coff "$ofile"
  293. ;;
  294. cuboot*)
  295. gzip -f -9 "$ofile"
  296. mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
  297. $uboot_version -d "$ofile".gz "$ofile"
  298. ;;
  299. treeboot*)
  300. mv "$ofile" "$ofile.elf"
  301. $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
  302. if [ -z "$cacheit" ]; then
  303. rm -f "$ofile.elf"
  304. fi
  305. exit 0
  306. ;;
  307. ps3)
  308. # The ps3's loader supports loading a gzipped binary image from flash
  309. # rom to ram addr zero. The loader then enters the system reset
  310. # vector at addr 0x100. A bootwrapper overlay is used to arrange for
  311. # a binary image of the kernel to be at addr zero, and yet have a
  312. # suitable bootwrapper entry at 0x100. To construct the final rom
  313. # image 512 bytes from offset 0x100 is copied to the bootwrapper
  314. # place holder at symbol __system_reset_kernel. The 512 bytes of the
  315. # bootwrapper entry code at symbol __system_reset_overlay is then
  316. # copied to offset 0x100. At runtime the bootwrapper program copies
  317. # the data at __system_reset_kernel back to addr 0x100.
  318. system_reset_overlay=0x`${CROSS}nm "$ofile" \
  319. | grep ' __system_reset_overlay$' \
  320. | cut -d' ' -f1`
  321. system_reset_overlay=`printf "%d" $system_reset_overlay`
  322. system_reset_kernel=0x`${CROSS}nm "$ofile" \
  323. | grep ' __system_reset_kernel$' \
  324. | cut -d' ' -f1`
  325. system_reset_kernel=`printf "%d" $system_reset_kernel`
  326. overlay_dest="256"
  327. overlay_size="512"
  328. ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
  329. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  330. skip=$overlay_dest seek=$system_reset_kernel \
  331. count=$overlay_size bs=1
  332. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  333. skip=$system_reset_overlay seek=$overlay_dest \
  334. count=$overlay_size bs=1
  335. odir="$(dirname "$ofile.bin")"
  336. rm -f "$odir/otheros.bld"
  337. gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
  338. ;;
  339. esac