wrapper 6.8 KB

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