wrapper 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. # directory for working files
  41. tmpdir=.
  42. usage() {
  43. echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
  44. echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
  45. echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
  46. exit 1
  47. }
  48. while [ "$#" -gt 0 ]; do
  49. case "$1" in
  50. -o)
  51. shift
  52. [ "$#" -gt 0 ] || usage
  53. ofile="$1"
  54. ;;
  55. -p)
  56. shift
  57. [ "$#" -gt 0 ] || usage
  58. platform="$1"
  59. ;;
  60. -i)
  61. shift
  62. [ "$#" -gt 0 ] || usage
  63. initrd="$1"
  64. ;;
  65. -d)
  66. shift
  67. [ "$#" -gt 0 ] || usage
  68. dtb="$1"
  69. ;;
  70. -s)
  71. shift
  72. [ "$#" -gt 0 ] || usage
  73. dts="$1"
  74. ;;
  75. -c)
  76. cacheit=y
  77. ;;
  78. -C)
  79. shift
  80. [ "$#" -gt 0 ] || usage
  81. CROSS="$1"
  82. ;;
  83. -D)
  84. shift
  85. [ "$#" -gt 0 ] || usage
  86. object="$1"
  87. ;;
  88. -W)
  89. shift
  90. [ "$#" -gt 0 ] || usage
  91. tmpdir="$1"
  92. ;;
  93. --no-gzip)
  94. gzip=
  95. ;;
  96. -?)
  97. usage
  98. ;;
  99. *)
  100. [ -z "$kernel" ] || usage
  101. kernel="$1"
  102. ;;
  103. esac
  104. shift
  105. done
  106. if [ -n "$dts" ]; then
  107. if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
  108. dts="$object/dts/$dts"
  109. fi
  110. if [ -z "$dtb" ]; then
  111. dtb="$platform.dtb"
  112. fi
  113. dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts"
  114. fi
  115. if [ -z "$kernel" ]; then
  116. kernel=vmlinux
  117. fi
  118. platformo=$object/"$platform".o
  119. lds=$object/zImage.lds
  120. ext=strip
  121. objflags=-S
  122. tmp=$tmpdir/zImage.$$.o
  123. ksection=.kernel:vmlinux.strip
  124. isection=.kernel:initrd
  125. case "$platform" in
  126. pmac|pseries|chrp)
  127. platformo=$object/of.o
  128. ;;
  129. coff)
  130. platformo=$object/of.o
  131. lds=$object/zImage.coff.lds
  132. ;;
  133. miboot|uboot)
  134. # miboot and U-boot want just the bare bits, not an ELF binary
  135. ext=bin
  136. objflags="-O binary"
  137. tmp="$ofile"
  138. ksection=image
  139. isection=initrd
  140. ;;
  141. cuboot*)
  142. binary=y
  143. gzip=
  144. ;;
  145. ps3)
  146. platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
  147. lds=$object/zImage.ps3.lds
  148. gzip=
  149. ext=bin
  150. objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
  151. ksection=.kernel:vmlinux.bin
  152. isection=.kernel:initrd
  153. ;;
  154. ep88xc)
  155. platformo="$object/fixed-head.o $object/$platform.o"
  156. binary=y
  157. ;;
  158. esac
  159. vmz="$tmpdir/`basename \"$kernel\"`.$ext"
  160. if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
  161. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  162. if [ -n "$gzip" ]; then
  163. gzip -f -9 "$vmz.$$"
  164. fi
  165. if [ -n "$cacheit" ]; then
  166. mv -f "$vmz.$$$gzip" "$vmz$gzip"
  167. else
  168. vmz="$vmz.$$"
  169. fi
  170. fi
  171. vmz="$vmz$gzip"
  172. # Extract kernel version information, some platforms want to include
  173. # it in the image header
  174. version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
  175. cut -d' ' -f3`
  176. if [ -n "$version" ]; then
  177. uboot_version="-n Linux-$version"
  178. fi
  179. case "$platform" in
  180. uboot)
  181. rm -f "$ofile"
  182. mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
  183. $uboot_version -d "$vmz" "$ofile"
  184. if [ -z "$cacheit" ]; then
  185. rm -f "$vmz"
  186. fi
  187. exit 0
  188. ;;
  189. esac
  190. addsec() {
  191. ${CROSS}objcopy $4 $1 \
  192. --add-section=$3="$2" \
  193. --set-section-flags=$3=contents,alloc,load,readonly,data
  194. }
  195. addsec $tmp "$vmz" $ksection $object/empty.o
  196. if [ -z "$cacheit" ]; then
  197. rm -f "$vmz"
  198. fi
  199. if [ -n "$initrd" ]; then
  200. addsec $tmp "$initrd" $isection
  201. fi
  202. if [ -n "$dtb" ]; then
  203. addsec $tmp "$dtb" .kernel:dtb
  204. if [ -n "$dts" ]; then
  205. rm $dtb
  206. fi
  207. fi
  208. if [ "$platform" != "miboot" ]; then
  209. ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
  210. $platformo $tmp $object/wrapper.a
  211. rm $tmp
  212. fi
  213. # Some platforms need the zImage's entry point and base address
  214. base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
  215. entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
  216. if [ -n "$binary" ]; then
  217. mv "$ofile" "$ofile".elf
  218. ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
  219. fi
  220. # post-processing needed for some platforms
  221. case "$platform" in
  222. pseries|chrp)
  223. $object/addnote "$ofile"
  224. ;;
  225. coff)
  226. ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
  227. $object/hack-coff "$ofile"
  228. ;;
  229. cuboot*)
  230. gzip -f -9 "$ofile"
  231. mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
  232. $uboot_version -d "$ofile".gz "$ofile"
  233. ;;
  234. treeboot*)
  235. mv "$ofile" "$ofile.elf"
  236. $object/mktree "$ofile.elf" "$ofile" "$base" "$entry"
  237. if [ -z "$cacheit" ]; then
  238. rm -f "$ofile.elf"
  239. fi
  240. exit 0
  241. ;;
  242. ps3)
  243. # The ps3's loader supports loading gzipped binary images from flash
  244. # rom to addr zero. The loader enters the image at addr 0x100. A
  245. # bootwrapper overlay is use to arrange for the kernel to be loaded
  246. # to addr zero and to have a suitable bootwrapper entry at 0x100.
  247. # To construct the rom image, 0x100 bytes from offset 0x100 in the
  248. # kernel is copied to the bootwrapper symbol __system_reset_kernel.
  249. # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is
  250. # then copied to offset 0x100. At runtime the bootwrapper program
  251. # copies the 0x100 bytes at __system_reset_kernel to addr 0x100.
  252. system_reset_overlay=0x`${CROSS}nm "$ofile" \
  253. | grep ' __system_reset_overlay$' \
  254. | cut -d' ' -f1`
  255. system_reset_overlay=`printf "%d" $system_reset_overlay`
  256. system_reset_kernel=0x`${CROSS}nm "$ofile" \
  257. | grep ' __system_reset_kernel$' \
  258. | cut -d' ' -f1`
  259. system_reset_kernel=`printf "%d" $system_reset_kernel`
  260. overlay_dest="256"
  261. overlay_size="256"
  262. ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
  263. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  264. skip=$overlay_dest seek=$system_reset_kernel \
  265. count=$overlay_size bs=1
  266. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  267. skip=$system_reset_overlay seek=$overlay_dest \
  268. count=$overlay_size bs=1
  269. odir="$(dirname "$ofile.bin")"
  270. rm -f "$odir/otheros.bld"
  271. gzip --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
  272. ;;
  273. esac