wrapper 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. esac
  135. vmz="$tmpdir/`basename \"$kernel\"`.$ext"
  136. if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
  137. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  138. if [ -n "$gzip" ]; then
  139. gzip -f -9 "$vmz.$$"
  140. fi
  141. if [ -n "$cacheit" ]; then
  142. mv -f "$vmz.$$$gzip" "$vmz$gzip"
  143. else
  144. vmz="$vmz.$$"
  145. fi
  146. fi
  147. vmz="$vmz$gzip"
  148. # Extract kernel version information, some platforms want to include
  149. # it in the image header
  150. version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
  151. cut -d' ' -f3`
  152. if [ -n "$version" ]; then
  153. uboot_version="-n Linux-$version"
  154. fi
  155. case "$platform" in
  156. uboot)
  157. rm -f "$ofile"
  158. mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
  159. $uboot_version -d "$vmz" "$ofile"
  160. if [ -z "$cacheit" ]; then
  161. rm -f "$vmz"
  162. fi
  163. exit 0
  164. ;;
  165. esac
  166. addsec() {
  167. ${CROSS}objcopy $4 $1 \
  168. --add-section=$3="$2" \
  169. --set-section-flags=$3=contents,alloc,load,readonly,data
  170. }
  171. addsec $tmp "$vmz" $ksection $object/empty.o
  172. if [ -z "$cacheit" ]; then
  173. rm -f "$vmz"
  174. fi
  175. if [ -n "$initrd" ]; then
  176. addsec $tmp "$initrd" $isection
  177. fi
  178. if [ -n "$dtb" ]; then
  179. addsec $tmp "$dtb" .kernel:dtb
  180. if [ -n "$dts" ]; then
  181. rm $dtb
  182. fi
  183. fi
  184. if [ "$platform" != "miboot" ]; then
  185. ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
  186. $platformo $tmp $object/wrapper.a
  187. rm $tmp
  188. fi
  189. # Some platforms need the zImage's entry point and base address
  190. base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
  191. entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
  192. # post-processing needed for some platforms
  193. case "$platform" in
  194. pseries|chrp)
  195. $object/addnote "$ofile"
  196. ;;
  197. coff)
  198. ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
  199. $object/hack-coff "$ofile"
  200. ;;
  201. cuboot*)
  202. mv "$ofile" "$ofile".elf
  203. ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin
  204. gzip -f -9 "$ofile".bin
  205. mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
  206. $uboot_version -d "$ofile".bin.gz "$ofile"
  207. ;;
  208. treeboot*)
  209. mv "$ofile" "$ofile.elf"
  210. $object/mktree "$ofile.elf" "$ofile" "$base" "$entry"
  211. if [ -z "$cacheit" ]; then
  212. rm -f "$ofile.elf"
  213. fi
  214. exit 0
  215. ;;
  216. esac