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 | ||
7f66c1fd GL |
24 | # Allow for verbose output |
25 | if [ "$V" = 1 ]; then | |
26 | set -x | |
27 | fi | |
28 | ||
2bf11819 PM |
29 | # defaults |
30 | kernel= | |
31 | ofile=zImage | |
32 | platform=of | |
33 | initrd= | |
34 | dtb= | |
35 | dts= | |
36 | cacheit= | |
11c146cc | 37 | binary= |
a9903811 | 38 | gzip=.gz |
2bf11819 PM |
39 | |
40 | # cross-compilation prefix | |
41 | CROSS= | |
42 | ||
43 | # directory for object and other files used by this script | |
44 | object=arch/powerpc/boot | |
45 | ||
46 | # directory for working files | |
47 | tmpdir=. | |
48 | ||
49 | usage() { | |
50 | echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2 | |
51 | echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2 | |
a9903811 | 52 | echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2 |
2bf11819 PM |
53 | exit 1 |
54 | } | |
55 | ||
56 | while [ "$#" -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 | ;; | |
a9903811 SW |
101 | --no-gzip) |
102 | gzip= | |
103 | ;; | |
2bf11819 PM |
104 | -?) |
105 | usage | |
106 | ;; | |
107 | *) | |
108 | [ -z "$kernel" ] || usage | |
109 | kernel="$1" | |
110 | ;; | |
111 | esac | |
112 | shift | |
113 | done | |
114 | ||
115 | if [ -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 | |
120 | fi | |
121 | ||
122 | if [ -z "$kernel" ]; then | |
123 | kernel=vmlinux | |
124 | fi | |
125 | ||
126 | platformo=$object/"$platform".o | |
127 | lds=$object/zImage.lds | |
128 | ext=strip | |
129 | objflags=-S | |
130 | tmp=$tmpdir/zImage.$$.o | |
131 | ksection=.kernel:vmlinux.strip | |
132 | isection=.kernel:initrd | |
133 | ||
134 | case "$platform" in | |
135 | pmac|pseries|chrp) | |
136 | platformo=$object/of.o | |
137 | ;; | |
627aa944 | 138 | coff) |
2bf11819 PM |
139 | platformo=$object/of.o |
140 | lds=$object/zImage.coff.lds | |
141 | ;; | |
142 | miboot|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 | ;; | |
0fdd717e | 150 | cuboot*) |
11c146cc | 151 | binary=y |
0fdd717e SW |
152 | gzip= |
153 | ;; | |
bafdb645 GL |
154 | ps3) |
155 | platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o" | |
156 | lds=$object/zImage.ps3.lds | |
11c146cc | 157 | binary=y |
bafdb645 GL |
158 | 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 | ;; | |
11c146cc SW |
164 | ep88xc) |
165 | platformo="$object/fixed-head.o $object/$platform.o" | |
166 | binary=y | |
167 | ;; | |
2bf11819 PM |
168 | esac |
169 | ||
170 | vmz="$tmpdir/`basename \"$kernel\"`.$ext" | |
1383a34f | 171 | if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then |
2bf11819 | 172 | ${CROSS}objcopy $objflags "$kernel" "$vmz.$$" |
a9903811 SW |
173 | |
174 | if [ -n "$gzip" ]; then | |
175 | gzip -f -9 "$vmz.$$" | |
176 | fi | |
177 | ||
2bf11819 | 178 | if [ -n "$cacheit" ]; then |
a9903811 | 179 | mv -f "$vmz.$$$gzip" "$vmz$gzip" |
2bf11819 PM |
180 | else |
181 | vmz="$vmz.$$" | |
182 | fi | |
183 | fi | |
184 | ||
a9903811 SW |
185 | vmz="$vmz$gzip" |
186 | ||
a6afacb6 DG |
187 | # Extract kernel version information, some platforms want to include |
188 | # it in the image header | |
189 | version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \ | |
190 | cut -d' ' -f3` | |
191 | if [ -n "$version" ]; then | |
192 | uboot_version="-n Linux-$version" | |
193 | fi | |
0fdd717e SW |
194 | |
195 | case "$platform" in | |
196 | uboot) | |
197 | rm -f "$ofile" | |
2bf11819 | 198 | mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \ |
a6afacb6 | 199 | $uboot_version -d "$vmz" "$ofile" |
2bf11819 | 200 | if [ -z "$cacheit" ]; then |
a9903811 | 201 | rm -f "$vmz" |
2bf11819 PM |
202 | fi |
203 | exit 0 | |
204 | ;; | |
205 | esac | |
206 | ||
207 | addsec() { | |
208 | ${CROSS}objcopy $4 $1 \ | |
209 | --add-section=$3="$2" \ | |
210 | --set-section-flags=$3=contents,alloc,load,readonly,data | |
211 | } | |
212 | ||
a9903811 | 213 | addsec $tmp "$vmz" $ksection $object/empty.o |
2bf11819 | 214 | if [ -z "$cacheit" ]; then |
a9903811 | 215 | rm -f "$vmz" |
2bf11819 PM |
216 | fi |
217 | ||
218 | if [ -n "$initrd" ]; then | |
c888554b | 219 | addsec $tmp "$initrd" $isection |
2bf11819 PM |
220 | fi |
221 | ||
222 | if [ -n "$dtb" ]; then | |
c888554b | 223 | addsec $tmp "$dtb" .kernel:dtb |
e9c4b4bd MG |
224 | if [ -n "$dts" ]; then |
225 | rm $dtb | |
226 | fi | |
2bf11819 PM |
227 | fi |
228 | ||
229 | if [ "$platform" != "miboot" ]; then | |
230 | ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \ | |
cd197ffc | 231 | $platformo $tmp $object/wrapper.a |
2bf11819 PM |
232 | rm $tmp |
233 | fi | |
234 | ||
a6afacb6 DG |
235 | # Some platforms need the zImage's entry point and base address |
236 | base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1` | |
237 | entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3` | |
238 | ||
11c146cc SW |
239 | if [ -n "$binary" ]; then |
240 | mv "$ofile" "$ofile".elf | |
241 | ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin | |
242 | fi | |
243 | ||
2bf11819 PM |
244 | # post-processing needed for some platforms |
245 | case "$platform" in | |
246 | pseries|chrp) | |
247 | $object/addnote "$ofile" | |
248 | ;; | |
627aa944 | 249 | coff) |
cd197ffc | 250 | ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile" |
2bf11819 PM |
251 | $object/hack-coff "$ofile" |
252 | ;; | |
0fdd717e | 253 | cuboot*) |
0fdd717e SW |
254 | gzip -f -9 "$ofile".bin |
255 | mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \ | |
a6afacb6 | 256 | $uboot_version -d "$ofile".bin.gz "$ofile" |
0fdd717e | 257 | ;; |
f6dfc805 DG |
258 | treeboot*) |
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 | ;; | |
bafdb645 GL |
266 | ps3) |
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 | ||
11c146cc | 277 | system_reset_overlay=0x`${CROSS}nm "$ofile".elf \ |
bafdb645 GL |
278 | | grep ' __system_reset_overlay$' \ |
279 | | cut -d' ' -f1` | |
280 | system_reset_overlay=`printf "%d" $system_reset_overlay` | |
11c146cc | 281 | system_reset_kernel=0x`${CROSS}nm "$ofile".elf \ |
bafdb645 GL |
282 | | 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 | ||
bafdb645 GL |
290 | 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 | ;; | |
2bf11819 | 310 | esac |