blob: 347639c63936f592329314a95ddeb77b0211830d [file] [log] [blame]
Paul Mackerras2bf11812006-09-27 22:47:03 +10001#!/bin/sh
2
3# Copyright (C) 2006 Paul Mackerras, IBM Corporation <paulus@samba.org>
4# This program may be used under the terms of version 2 of the GNU
5# General Public License.
6
7# This script takes a kernel binary and optionally an initrd image
8# and/or a device-tree blob, and creates a bootable zImage for a
9# given platform.
10
11# Options:
12# -o zImage specify output file
13# -p platform specify platform (links in $platform.o)
14# -i initrd specify initrd file
15# -d devtree specify device-tree blob
16# -s tree.dts specify device-tree source file (needs dtc installed)
17# -c cache $kernel.strip.gz (use if present & newer, else make)
18# -C prefix specify command prefix for cross-building tools
19# (strip, objcopy, ld)
20# -D dir specify directory containing data files used by script
21# (default ./arch/powerpc/boot)
22# -W dir specify working directory for temporary files (default .)
23
Grant Likely7f66c1f2007-10-23 14:27:31 +100024# Allow for verbose output
25if [ "$V" = 1 ]; then
26 set -x
27fi
28
Paul Mackerras2bf11812006-09-27 22:47:03 +100029# defaults
30kernel=
31ofile=zImage
32platform=of
33initrd=
34dtb=
35dts=
36cacheit=
Scott Wood11c146c2007-09-14 14:58:25 -050037binary=
Scott Wooda9903812007-03-16 12:27:59 -050038gzip=.gz
Paul Mackerras2bf11812006-09-27 22:47:03 +100039
40# cross-compilation prefix
41CROSS=
42
43# directory for object and other files used by this script
44object=arch/powerpc/boot
45
46# directory for working files
47tmpdir=.
48
49usage() {
50 echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
51 echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
Scott Wooda9903812007-03-16 12:27:59 -050052 echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
Paul Mackerras2bf11812006-09-27 22:47:03 +100053 exit 1
54}
55
56while [ "$#" -gt 0 ]; do
57 case "$1" in
58 -o)
59 shift
60 [ "$#" -gt 0 ] || usage
61 ofile="$1"
62 ;;
63 -p)
64 shift
65 [ "$#" -gt 0 ] || usage
66 platform="$1"
67 ;;
68 -i)
69 shift
70 [ "$#" -gt 0 ] || usage
71 initrd="$1"
72 ;;
73 -d)
74 shift
75 [ "$#" -gt 0 ] || usage
76 dtb="$1"
77 ;;
78 -s)
79 shift
80 [ "$#" -gt 0 ] || usage
81 dts="$1"
82 ;;
83 -c)
84 cacheit=y
85 ;;
86 -C)
87 shift
88 [ "$#" -gt 0 ] || usage
89 CROSS="$1"
90 ;;
91 -D)
92 shift
93 [ "$#" -gt 0 ] || usage
94 object="$1"
95 ;;
96 -W)
97 shift
98 [ "$#" -gt 0 ] || usage
99 tmpdir="$1"
100 ;;
Scott Wooda9903812007-03-16 12:27:59 -0500101 --no-gzip)
102 gzip=
103 ;;
Paul Mackerras2bf11812006-09-27 22:47:03 +1000104 -?)
105 usage
106 ;;
107 *)
108 [ -z "$kernel" ] || usage
109 kernel="$1"
110 ;;
111 esac
112 shift
113done
114
115if [ -n "$dts" ]; then
116 if [ -z "$dtb" ]; then
117 dtb="$platform.dtb"
118 fi
119 dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts" || exit 1
120fi
121
122if [ -z "$kernel" ]; then
123 kernel=vmlinux
124fi
125
126platformo=$object/"$platform".o
127lds=$object/zImage.lds
128ext=strip
129objflags=-S
130tmp=$tmpdir/zImage.$$.o
131ksection=.kernel:vmlinux.strip
132isection=.kernel:initrd
133
134case "$platform" in
135pmac|pseries|chrp)
136 platformo=$object/of.o
137 ;;
Milton Miller627aa942007-05-31 01:29:01 +1000138coff)
Paul Mackerras2bf11812006-09-27 22:47:03 +1000139 platformo=$object/of.o
140 lds=$object/zImage.coff.lds
141 ;;
142miboot|uboot)
143 # miboot and U-boot want just the bare bits, not an ELF binary
144 ext=bin
145 objflags="-O binary"
146 tmp="$ofile"
147 ksection=image
148 isection=initrd
149 ;;
Scott Wood0fdd7172007-04-17 09:25:50 +1000150cuboot*)
Scott Wood11c146c2007-09-14 14:58:25 -0500151 binary=y
Scott Wood0fdd7172007-04-17 09:25:50 +1000152 gzip=
153 ;;
Geoff Levandbafdb642007-07-04 09:07:18 +1000154ps3)
155 platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
156 lds=$object/zImage.ps3.lds
Scott Wood11c146c2007-09-14 14:58:25 -0500157 binary=y
Geoff Levandbafdb642007-07-04 09:07:18 +1000158 gzip=
159 ext=bin
160 objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
161 ksection=.kernel:vmlinux.bin
162 isection=.kernel:initrd
163 ;;
Scott Wood11c146c2007-09-14 14:58:25 -0500164ep88xc)
165 platformo="$object/fixed-head.o $object/$platform.o"
166 binary=y
167 ;;
Paul Mackerras2bf11812006-09-27 22:47:03 +1000168esac
169
170vmz="$tmpdir/`basename \"$kernel\"`.$ext"
Milton Miller1383a342007-03-28 02:21:04 -0600171if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
Paul Mackerras2bf11812006-09-27 22:47:03 +1000172 ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
Scott Wooda9903812007-03-16 12:27:59 -0500173
174 if [ -n "$gzip" ]; then
175 gzip -f -9 "$vmz.$$"
176 fi
177
Paul Mackerras2bf11812006-09-27 22:47:03 +1000178 if [ -n "$cacheit" ]; then
Scott Wooda9903812007-03-16 12:27:59 -0500179 mv -f "$vmz.$$$gzip" "$vmz$gzip"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000180 else
181 vmz="$vmz.$$"
182 fi
183fi
184
Scott Wooda9903812007-03-16 12:27:59 -0500185vmz="$vmz$gzip"
186
David Gibsona6afacb2007-05-01 10:20:20 +1000187# Extract kernel version information, some platforms want to include
188# it in the image header
189version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
190 cut -d' ' -f3`
191if [ -n "$version" ]; then
192 uboot_version="-n Linux-$version"
193fi
Scott Wood0fdd7172007-04-17 09:25:50 +1000194
195case "$platform" in
196uboot)
197 rm -f "$ofile"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000198 mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
David Gibsona6afacb2007-05-01 10:20:20 +1000199 $uboot_version -d "$vmz" "$ofile"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000200 if [ -z "$cacheit" ]; then
Scott Wooda9903812007-03-16 12:27:59 -0500201 rm -f "$vmz"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000202 fi
203 exit 0
204 ;;
205esac
206
207addsec() {
208 ${CROSS}objcopy $4 $1 \
209 --add-section=$3="$2" \
210 --set-section-flags=$3=contents,alloc,load,readonly,data
211}
212
Scott Wooda9903812007-03-16 12:27:59 -0500213addsec $tmp "$vmz" $ksection $object/empty.o
Paul Mackerras2bf11812006-09-27 22:47:03 +1000214if [ -z "$cacheit" ]; then
Scott Wooda9903812007-03-16 12:27:59 -0500215 rm -f "$vmz"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000216fi
217
218if [ -n "$initrd" ]; then
Mark A. Greerc8885542006-10-16 13:49:27 -0700219 addsec $tmp "$initrd" $isection
Paul Mackerras2bf11812006-09-27 22:47:03 +1000220fi
221
222if [ -n "$dtb" ]; then
Mark A. Greerc8885542006-10-16 13:49:27 -0700223 addsec $tmp "$dtb" .kernel:dtb
Mark A. Greere9c4b4b2006-11-08 17:50:44 -0700224 if [ -n "$dts" ]; then
225 rm $dtb
226 fi
Paul Mackerras2bf11812006-09-27 22:47:03 +1000227fi
228
229if [ "$platform" != "miboot" ]; then
230 ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
David Gibsoncd197ff2007-03-05 14:24:52 +1100231 $platformo $tmp $object/wrapper.a
Paul Mackerras2bf11812006-09-27 22:47:03 +1000232 rm $tmp
233fi
234
David Gibsona6afacb2007-05-01 10:20:20 +1000235# Some platforms need the zImage's entry point and base address
236base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
237entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
238
Scott Wood11c146c2007-09-14 14:58:25 -0500239if [ -n "$binary" ]; then
240 mv "$ofile" "$ofile".elf
241 ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin
242fi
243
Paul Mackerras2bf11812006-09-27 22:47:03 +1000244# post-processing needed for some platforms
245case "$platform" in
246pseries|chrp)
247 $object/addnote "$ofile"
248 ;;
Milton Miller627aa942007-05-31 01:29:01 +1000249coff)
David Gibsoncd197ff2007-03-05 14:24:52 +1100250 ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000251 $object/hack-coff "$ofile"
252 ;;
Scott Wood0fdd7172007-04-17 09:25:50 +1000253cuboot*)
Scott Wood0fdd7172007-04-17 09:25:50 +1000254 gzip -f -9 "$ofile".bin
255 mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
David Gibsona6afacb2007-05-01 10:20:20 +1000256 $uboot_version -d "$ofile".bin.gz "$ofile"
Scott Wood0fdd7172007-04-17 09:25:50 +1000257 ;;
David Gibsonf6dfc802007-05-08 14:10:01 +1000258treeboot*)
259 mv "$ofile" "$ofile.elf"
260 $object/mktree "$ofile.elf" "$ofile" "$base" "$entry"
261 if [ -z "$cacheit" ]; then
262 rm -f "$ofile.elf"
263 fi
264 exit 0
265 ;;
Geoff Levandbafdb642007-07-04 09:07:18 +1000266ps3)
267 # The ps3's loader supports loading gzipped binary images from flash
268 # rom to addr zero. The loader enters the image at addr 0x100. A
269 # bootwrapper overlay is use to arrange for the kernel to be loaded
270 # to addr zero and to have a suitable bootwrapper entry at 0x100.
271 # To construct the rom image, 0x100 bytes from offset 0x100 in the
272 # kernel is copied to the bootwrapper symbol __system_reset_kernel.
273 # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is
274 # then copied to offset 0x100. At runtime the bootwrapper program
275 # copies the 0x100 bytes at __system_reset_kernel to addr 0x100.
276
Scott Wood11c146c2007-09-14 14:58:25 -0500277 system_reset_overlay=0x`${CROSS}nm "$ofile".elf \
Geoff Levandbafdb642007-07-04 09:07:18 +1000278 | grep ' __system_reset_overlay$' \
279 | cut -d' ' -f1`
280 system_reset_overlay=`printf "%d" $system_reset_overlay`
Scott Wood11c146c2007-09-14 14:58:25 -0500281 system_reset_kernel=0x`${CROSS}nm "$ofile".elf \
Geoff Levandbafdb642007-07-04 09:07:18 +1000282 | grep ' __system_reset_kernel$' \
283 | cut -d' ' -f1`
284 system_reset_kernel=`printf "%d" $system_reset_kernel`
285 overlay_dest="256"
286 overlay_size="256"
287
288 rm -f "$object/otheros.bld"
289
Geoff Levandbafdb642007-07-04 09:07:18 +1000290 msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
291 skip=$overlay_dest seek=$system_reset_kernel \
292 count=$overlay_size bs=1 2>&1)
293
294 if [ $? -ne "0" ]; then
295 echo $msg
296 exit 1
297 fi
298
299 msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
300 skip=$system_reset_overlay seek=$overlay_dest \
301 count=$overlay_size bs=1 2>&1)
302
303 if [ $? -ne "0" ]; then
304 echo $msg
305 exit 2
306 fi
307
308 gzip --force -9 --stdout "$ofile.bin" > "$object/otheros.bld"
309 ;;
Paul Mackerras2bf11812006-09-27 22:47:03 +1000310esac