wrapper 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. pie=
  37. # cross-compilation prefix
  38. CROSS=
  39. # mkimage wrapper script
  40. MKIMAGE=$srctree/scripts/mkuboot.sh
  41. # directory for object and other files used by this script
  42. object=arch/powerpc/boot
  43. objbin=$object
  44. dtc=scripts/dtc/dtc
  45. # directory for working files
  46. tmpdir=.
  47. usage() {
  48. echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
  49. echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
  50. echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
  51. exit 1
  52. }
  53. while [ "$#" -gt 0 ]; do
  54. case "$1" in
  55. -o)
  56. shift
  57. [ "$#" -gt 0 ] || usage
  58. ofile="$1"
  59. ;;
  60. -p)
  61. shift
  62. [ "$#" -gt 0 ] || usage
  63. platform="$1"
  64. ;;
  65. -i)
  66. shift
  67. [ "$#" -gt 0 ] || usage
  68. initrd="$1"
  69. ;;
  70. -d)
  71. shift
  72. [ "$#" -gt 0 ] || usage
  73. dtb="$1"
  74. ;;
  75. -s)
  76. shift
  77. [ "$#" -gt 0 ] || usage
  78. dts="$1"
  79. ;;
  80. -c)
  81. cacheit=y
  82. ;;
  83. -C)
  84. shift
  85. [ "$#" -gt 0 ] || usage
  86. CROSS="$1"
  87. ;;
  88. -D)
  89. shift
  90. [ "$#" -gt 0 ] || usage
  91. object="$1"
  92. objbin="$1"
  93. ;;
  94. -W)
  95. shift
  96. [ "$#" -gt 0 ] || usage
  97. tmpdir="$1"
  98. ;;
  99. --no-gzip)
  100. gzip=
  101. ;;
  102. -?)
  103. usage
  104. ;;
  105. *)
  106. [ -z "$kernel" ] || usage
  107. kernel="$1"
  108. ;;
  109. esac
  110. shift
  111. done
  112. if [ -n "$dts" ]; then
  113. if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
  114. dts="$object/dts/$dts"
  115. fi
  116. if [ -z "$dtb" ]; then
  117. dtb="$platform.dtb"
  118. fi
  119. $dtc -O dtb -o "$dtb" -b 0 "$dts"
  120. fi
  121. if [ -z "$kernel" ]; then
  122. kernel=vmlinux
  123. fi
  124. platformo=$object/"$platform".o
  125. lds=$object/zImage.lds
  126. ext=strip
  127. objflags=-S
  128. tmp=$tmpdir/zImage.$$.o
  129. ksection=.kernel:vmlinux.strip
  130. isection=.kernel:initrd
  131. link_address='0x400000'
  132. make_space=y
  133. case "$platform" in
  134. pseries)
  135. platformo=$object/of.o
  136. link_address='0x4000000'
  137. ;;
  138. maple)
  139. platformo=$object/of.o
  140. link_address='0x400000'
  141. ;;
  142. pmac|chrp)
  143. platformo=$object/of.o
  144. ;;
  145. coff)
  146. platformo="$object/crt0.o $object/of.o"
  147. lds=$object/zImage.coff.lds
  148. link_address='0x500000'
  149. pie=
  150. ;;
  151. miboot|uboot*)
  152. # miboot and U-boot want just the bare bits, not an ELF binary
  153. ext=bin
  154. objflags="-O binary"
  155. tmp="$ofile"
  156. ksection=image
  157. isection=initrd
  158. ;;
  159. cuboot*)
  160. binary=y
  161. gzip=
  162. case "$platform" in
  163. *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
  164. platformo=$object/cuboot-8xx.o
  165. ;;
  166. *5200*|*-motionpro)
  167. platformo=$object/cuboot-52xx.o
  168. ;;
  169. *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
  170. platformo=$object/cuboot-pq2.o
  171. ;;
  172. *-mpc824*)
  173. platformo=$object/cuboot-824x.o
  174. ;;
  175. *-mpc83*|*-asp834x*)
  176. platformo=$object/cuboot-83xx.o
  177. ;;
  178. *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
  179. platformo=$object/cuboot-85xx-cpm2.o
  180. ;;
  181. *-mpc85*|*-tqm85*|*-sbc85*)
  182. platformo=$object/cuboot-85xx.o
  183. ;;
  184. *-amigaone)
  185. link_address='0x800000'
  186. ;;
  187. esac
  188. ;;
  189. ps3)
  190. platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
  191. lds=$object/zImage.ps3.lds
  192. gzip=
  193. ext=bin
  194. objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
  195. ksection=.kernel:vmlinux.bin
  196. isection=.kernel:initrd
  197. link_address=''
  198. make_space=n
  199. pie=
  200. ;;
  201. ep88xc|ep405|ep8248e)
  202. platformo="$object/fixed-head.o $object/$platform.o"
  203. binary=y
  204. ;;
  205. adder875-redboot)
  206. platformo="$object/fixed-head.o $object/redboot-8xx.o"
  207. binary=y
  208. ;;
  209. simpleboot-virtex405-*)
  210. platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o"
  211. binary=y
  212. ;;
  213. simpleboot-virtex440-*)
  214. platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o"
  215. binary=y
  216. ;;
  217. simpleboot-*)
  218. platformo="$object/fixed-head.o $object/simpleboot.o"
  219. binary=y
  220. ;;
  221. asp834x-redboot)
  222. platformo="$object/fixed-head.o $object/redboot-83xx.o"
  223. binary=y
  224. ;;
  225. xpedite52*)
  226. link_address='0x1400000'
  227. platformo=$object/cuboot-85xx.o
  228. ;;
  229. gamecube|wii)
  230. link_address='0x600000'
  231. platformo="$object/$platform-head.o $object/$platform.o"
  232. ;;
  233. treeboot-currituck)
  234. link_address='0x1000000'
  235. ;;
  236. treeboot-iss4xx-mpic)
  237. platformo="$object/treeboot-iss4xx.o"
  238. ;;
  239. epapr)
  240. link_address='0x20000000'
  241. pie=-pie
  242. ;;
  243. esac
  244. vmz="$tmpdir/`basename \"$kernel\"`.$ext"
  245. if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
  246. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  247. strip_size=$(stat -c %s $vmz.$$)
  248. if [ -n "$gzip" ]; then
  249. gzip -n -f -9 "$vmz.$$"
  250. fi
  251. if [ -n "$cacheit" ]; then
  252. mv -f "$vmz.$$$gzip" "$vmz$gzip"
  253. else
  254. vmz="$vmz.$$"
  255. fi
  256. else
  257. # Calculate the vmlinux.strip size
  258. ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
  259. strip_size=$(stat -c %s $vmz.$$)
  260. rm -f $vmz.$$
  261. fi
  262. if [ "$make_space" = "y" ]; then
  263. # Round the size to next higher MB limit
  264. round_size=$(((strip_size + 0xfffff) & 0xfff00000))
  265. round_size=0x$(printf "%x" $round_size)
  266. link_addr=$(printf "%d" $link_address)
  267. if [ $link_addr -lt $strip_size ]; then
  268. echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
  269. "overlaps the address of the wrapper($link_address)"
  270. echo "INFO: Fixing the link_address of wrapper to ($round_size)"
  271. link_address=$round_size
  272. fi
  273. fi
  274. vmz="$vmz$gzip"
  275. # Extract kernel version information, some platforms want to include
  276. # it in the image header
  277. version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
  278. cut -d' ' -f3`
  279. if [ -n "$version" ]; then
  280. uboot_version="-n Linux-$version"
  281. fi
  282. # physical offset of kernel image
  283. membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
  284. case "$platform" in
  285. uboot)
  286. rm -f "$ofile"
  287. ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
  288. $uboot_version -d "$vmz" "$ofile"
  289. if [ -z "$cacheit" ]; then
  290. rm -f "$vmz"
  291. fi
  292. exit 0
  293. ;;
  294. uboot-obs600)
  295. rm -f "$ofile"
  296. # obs600 wants a multi image with an initrd, so we need to put a fake
  297. # one in even when building a "normal" image.
  298. if [ -n "$initrd" ]; then
  299. real_rd="$initrd"
  300. else
  301. real_rd=`mktemp`
  302. echo "\0" >>"$real_rd"
  303. fi
  304. ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
  305. $uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
  306. if [ -z "$initrd" ]; then
  307. rm -f "$real_rd"
  308. fi
  309. if [ -z "$cacheit" ]; then
  310. rm -f "$vmz"
  311. fi
  312. exit 0
  313. ;;
  314. esac
  315. addsec() {
  316. ${CROSS}objcopy $4 $1 \
  317. --add-section=$3="$2" \
  318. --set-section-flags=$3=contents,alloc,load,readonly,data
  319. }
  320. addsec $tmp "$vmz" $ksection $object/empty.o
  321. if [ -z "$cacheit" ]; then
  322. rm -f "$vmz"
  323. fi
  324. if [ -n "$initrd" ]; then
  325. addsec $tmp "$initrd" $isection
  326. fi
  327. if [ -n "$dtb" ]; then
  328. addsec $tmp "$dtb" .kernel:dtb
  329. if [ -n "$dts" ]; then
  330. rm $dtb
  331. fi
  332. fi
  333. if [ "$platform" != "miboot" ]; then
  334. if [ -n "$link_address" ] ; then
  335. text_start="-Ttext $link_address"
  336. fi
  337. ${CROSS}ld -m elf32ppc -T $lds $text_start $pie -o "$ofile" \
  338. $platformo $tmp $object/wrapper.a
  339. rm $tmp
  340. fi
  341. # Some platforms need the zImage's entry point and base address
  342. base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
  343. entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
  344. if [ -n "$binary" ]; then
  345. mv "$ofile" "$ofile".elf
  346. ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
  347. fi
  348. # post-processing needed for some platforms
  349. case "$platform" in
  350. pseries|chrp|maple)
  351. $objbin/addnote "$ofile"
  352. ;;
  353. coff)
  354. ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
  355. $objbin/hack-coff "$ofile"
  356. ;;
  357. cuboot*)
  358. gzip -n -f -9 "$ofile"
  359. ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
  360. $uboot_version -d "$ofile".gz "$ofile"
  361. ;;
  362. treeboot*)
  363. mv "$ofile" "$ofile.elf"
  364. $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
  365. if [ -z "$cacheit" ]; then
  366. rm -f "$ofile.elf"
  367. fi
  368. exit 0
  369. ;;
  370. ps3)
  371. # The ps3's loader supports loading a gzipped binary image from flash
  372. # rom to ram addr zero. The loader then enters the system reset
  373. # vector at addr 0x100. A bootwrapper overlay is used to arrange for
  374. # a binary image of the kernel to be at addr zero, and yet have a
  375. # suitable bootwrapper entry at 0x100. To construct the final rom
  376. # image 512 bytes from offset 0x100 is copied to the bootwrapper
  377. # place holder at symbol __system_reset_kernel. The 512 bytes of the
  378. # bootwrapper entry code at symbol __system_reset_overlay is then
  379. # copied to offset 0x100. At runtime the bootwrapper program copies
  380. # the data at __system_reset_kernel back to addr 0x100.
  381. system_reset_overlay=0x`${CROSS}nm "$ofile" \
  382. | grep ' __system_reset_overlay$' \
  383. | cut -d' ' -f1`
  384. system_reset_overlay=`printf "%d" $system_reset_overlay`
  385. system_reset_kernel=0x`${CROSS}nm "$ofile" \
  386. | grep ' __system_reset_kernel$' \
  387. | cut -d' ' -f1`
  388. system_reset_kernel=`printf "%d" $system_reset_kernel`
  389. overlay_dest="256"
  390. overlay_size="512"
  391. ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
  392. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  393. skip=$overlay_dest seek=$system_reset_kernel \
  394. count=$overlay_size bs=1
  395. dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
  396. skip=$system_reset_overlay seek=$overlay_dest \
  397. count=$overlay_size bs=1
  398. odir="$(dirname "$ofile.bin")"
  399. rm -f "$odir/otheros.bld"
  400. gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
  401. ;;
  402. esac