wrapper 6.8 KB

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