wrapper 11 KB

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