wrapper 6.7 KB

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