wrapper 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. case "$platform" in
  128. pmac|pseries|chrp)
  129. platformo=$object/of.o
  130. ;;
  131. coff)
  132. platformo=$object/of.o
  133. lds=$object/zImage.coff.lds
  134. ;;
  135. miboot|uboot)
  136. # miboot and U-boot want just the bare bits, not an ELF binary
  137. ext=bin
  138. objflags="-O binary"
  139. tmp="$ofile"
  140. ksection=image
  141. isection=initrd
  142. ;;
  143. cuboot*)
  144. binary=y
  145. gzip=
  146. case "$platform" in
  147. *-mpc885ads|*-adder875*|*-ep88xc)
  148. platformo=$object/cuboot-8xx.o
  149. ;;
  150. *5200*|*-motionpro)
  151. platformo=$object/cuboot-52xx.o
  152. ;;
  153. *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
  154. platformo=$object/cuboot-pq2.o
  155. ;;
  156. *-mpc824*)
  157. platformo=$object/cuboot-824x.o
  158. ;;
  159. *-mpc83*)
  160. platformo=$object/cuboot-83xx.o
  161. ;;
  162. *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555*)
  163. platformo=$object/cuboot-85xx-cpm2.o
  164. ;;
  165. *-mpc85*)
  166. platformo=$object/cuboot-85xx.o
  167. ;;
  168. esac
  169. ;;
  170. ps3)
  171. platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
  172. lds=$object/zImage.ps3.lds
  173. gzip=
  174. ext=bin
  175. objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
  176. ksection=.kernel:vmlinux.bin
  177. isection=.kernel:initrd
  178. ;;
  179. ep88xc|ep405|redboot*|ep8248e)
  180. platformo="$object/fixed-head.o $object/$platform.o"
  181. binary=y
  182. ;;
  183. esac
  184. vmz="$tmpdir/`basename \"$kernel\"`.$ext"
  185. if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
  186. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  187. if [ -n "$gzip" ]; then
  188. gzip -f -9 "$vmz.$$"
  189. fi
  190. if [ -n "$cacheit" ]; then
  191. mv -f "$vmz.$$$gzip" "$vmz$gzip"
  192. else
  193. vmz="$vmz.$$"
  194. fi
  195. fi
  196. vmz="$vmz$gzip"
  197. # Extract kernel version information, some platforms want to include
  198. # it in the image header
  199. version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
  200. cut -d' ' -f3`
  201. if [ -n "$version" ]; then
  202. uboot_version="-n Linux-$version"
  203. fi
  204. case "$platform" in
  205. uboot)
  206. rm -f "$ofile"
  207. mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
  208. $uboot_version -d "$vmz" "$ofile"
  209. if [ -z "$cacheit" ]; then
  210. rm -f "$vmz"
  211. fi
  212. exit 0
  213. ;;
  214. esac
  215. addsec() {
  216. ${CROSS}objcopy $4 $1 \
  217. --add-section=$3="$2" \
  218. --set-section-flags=$3=contents,alloc,load,readonly,data
  219. }
  220. addsec $tmp "$vmz" $ksection $object/empty.o
  221. if [ -z "$cacheit" ]; then
  222. rm -f "$vmz"
  223. fi
  224. if [ -n "$initrd" ]; then
  225. addsec $tmp "$initrd" $isection
  226. fi
  227. if [ -n "$dtb" ]; then
  228. addsec $tmp "$dtb" .kernel:dtb
  229. if [ -n "$dts" ]; then
  230. rm $dtb
  231. fi
  232. fi
  233. if [ "$platform" != "miboot" ]; then
  234. ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
  235. $platformo $tmp $object/wrapper.a
  236. rm $tmp
  237. fi
  238. # Some platforms need the zImage's entry point and base address
  239. base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
  240. entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
  241. if [ -n "$binary" ]; then
  242. mv "$ofile" "$ofile".elf
  243. ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
  244. fi
  245. # post-processing needed for some platforms
  246. case "$platform" in
  247. pseries|chrp)
  248. $objbin/addnote "$ofile"
  249. ;;
  250. coff)
  251. ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
  252. $objbin/hack-coff "$ofile"
  253. ;;
  254. cuboot*)
  255. gzip -f -9 "$ofile"
  256. mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
  257. $uboot_version -d "$ofile".gz "$ofile"
  258. ;;
  259. treeboot*)
  260. mv "$ofile" "$ofile.elf"
  261. $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
  262. if [ -z "$cacheit" ]; then
  263. rm -f "$ofile.elf"
  264. fi
  265. exit 0
  266. ;;
  267. ps3)
  268. # The ps3's loader supports loading gzipped binary images from flash
  269. # rom to addr zero. The loader enters the image at addr 0x100. A
  270. # bootwrapper overlay is use to arrange for the kernel to be loaded
  271. # to addr zero and to have a suitable bootwrapper entry at 0x100.
  272. # To construct the rom image, 0x100 bytes from offset 0x100 in the
  273. # kernel is copied to the bootwrapper symbol __system_reset_kernel.
  274. # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is
  275. # then copied to offset 0x100. At runtime the bootwrapper program
  276. # copies the 0x100 bytes at __system_reset_kernel to addr 0x100.
  277. system_reset_overlay=0x`${CROSS}nm "$ofile" \
  278. | grep ' __system_reset_overlay$' \
  279. | cut -d' ' -f1`
  280. system_reset_overlay=`printf "%d" $system_reset_overlay`
  281. system_reset_kernel=0x`${CROSS}nm "$ofile" \
  282. | grep ' __system_reset_kernel$' \
  283. | cut -d' ' -f1`
  284. system_reset_kernel=`printf "%d" $system_reset_kernel`
  285. overlay_dest="256"
  286. overlay_size="256"
  287. ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
  288. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  289. skip=$overlay_dest seek=$system_reset_kernel \
  290. count=$overlay_size bs=1
  291. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  292. skip=$system_reset_overlay seek=$overlay_dest \
  293. count=$overlay_size bs=1
  294. odir="$(dirname "$ofile.bin")"
  295. rm -f "$odir/otheros.bld"
  296. gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
  297. ;;
  298. esac