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