blob: 39b27e5ef6c19a840d4b2a9fc0f87d621e72d789 [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
24# defaults
25kernel=
26ofile=zImage
27platform=of
28initrd=
29dtb=
30dts=
31cacheit=
Scott Wood11c146c2007-09-14 14:58:25 -050032binary=
Scott Wooda9903812007-03-16 12:27:59 -050033gzip=.gz
Paul Mackerras2bf11812006-09-27 22:47:03 +100034
35# cross-compilation prefix
36CROSS=
37
38# directory for object and other files used by this script
39object=arch/powerpc/boot
40
41# directory for working files
42tmpdir=.
43
44usage() {
45 echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
46 echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
Scott Wooda9903812007-03-16 12:27:59 -050047 echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
Paul Mackerras2bf11812006-09-27 22:47:03 +100048 exit 1
49}
50
51while [ "$#" -gt 0 ]; do
52 case "$1" in
53 -o)
54 shift
55 [ "$#" -gt 0 ] || usage
56 ofile="$1"
57 ;;
58 -p)
59 shift
60 [ "$#" -gt 0 ] || usage
61 platform="$1"
62 ;;
63 -i)
64 shift
65 [ "$#" -gt 0 ] || usage
66 initrd="$1"
67 ;;
68 -d)
69 shift
70 [ "$#" -gt 0 ] || usage
71 dtb="$1"
72 ;;
73 -s)
74 shift
75 [ "$#" -gt 0 ] || usage
76 dts="$1"
77 ;;
78 -c)
79 cacheit=y
80 ;;
81 -C)
82 shift
83 [ "$#" -gt 0 ] || usage
84 CROSS="$1"
85 ;;
86 -D)
87 shift
88 [ "$#" -gt 0 ] || usage
89 object="$1"
90 ;;
91 -W)
92 shift
93 [ "$#" -gt 0 ] || usage
94 tmpdir="$1"
95 ;;
Scott Wooda9903812007-03-16 12:27:59 -050096 --no-gzip)
97 gzip=
98 ;;
Paul Mackerras2bf11812006-09-27 22:47:03 +100099 -?)
100 usage
101 ;;
102 *)
103 [ -z "$kernel" ] || usage
104 kernel="$1"
105 ;;
106 esac
107 shift
108done
109
110if [ -n "$dts" ]; then
111 if [ -z "$dtb" ]; then
112 dtb="$platform.dtb"
113 fi
114 dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts" || exit 1
115fi
116
117if [ -z "$kernel" ]; then
118 kernel=vmlinux
119fi
120
121platformo=$object/"$platform".o
122lds=$object/zImage.lds
123ext=strip
124objflags=-S
125tmp=$tmpdir/zImage.$$.o
126ksection=.kernel:vmlinux.strip
127isection=.kernel:initrd
128
129case "$platform" in
130pmac|pseries|chrp)
131 platformo=$object/of.o
132 ;;
Milton Miller627aa942007-05-31 01:29:01 +1000133coff)
Paul Mackerras2bf11812006-09-27 22:47:03 +1000134 platformo=$object/of.o
135 lds=$object/zImage.coff.lds
136 ;;
137miboot|uboot)
138 # miboot and U-boot want just the bare bits, not an ELF binary
139 ext=bin
140 objflags="-O binary"
141 tmp="$ofile"
142 ksection=image
143 isection=initrd
144 ;;
Scott Wood0fdd7172007-04-17 09:25:50 +1000145cuboot*)
Scott Wood11c146c2007-09-14 14:58:25 -0500146 binary=y
Scott Wood0fdd7172007-04-17 09:25:50 +1000147 gzip=
148 ;;
Geoff Levandbafdb642007-07-04 09:07:18 +1000149ps3)
150 platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
151 lds=$object/zImage.ps3.lds
Scott Wood11c146c2007-09-14 14:58:25 -0500152 binary=y
Geoff Levandbafdb642007-07-04 09:07:18 +1000153 gzip=
154 ext=bin
155 objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
156 ksection=.kernel:vmlinux.bin
157 isection=.kernel:initrd
158 ;;
Scott Wood11c146c2007-09-14 14:58:25 -0500159ep88xc)
160 platformo="$object/fixed-head.o $object/$platform.o"
161 binary=y
162 ;;
Paul Mackerras2bf11812006-09-27 22:47:03 +1000163esac
164
165vmz="$tmpdir/`basename \"$kernel\"`.$ext"
Milton Miller1383a342007-03-28 02:21:04 -0600166if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
Paul Mackerras2bf11812006-09-27 22:47:03 +1000167 ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
Scott Wooda9903812007-03-16 12:27:59 -0500168
169 if [ -n "$gzip" ]; then
170 gzip -f -9 "$vmz.$$"
171 fi
172
Paul Mackerras2bf11812006-09-27 22:47:03 +1000173 if [ -n "$cacheit" ]; then
Scott Wooda9903812007-03-16 12:27:59 -0500174 mv -f "$vmz.$$$gzip" "$vmz$gzip"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000175 else
176 vmz="$vmz.$$"
177 fi
178fi
179
Scott Wooda9903812007-03-16 12:27:59 -0500180vmz="$vmz$gzip"
181
David Gibsona6afacb2007-05-01 10:20:20 +1000182# Extract kernel version information, some platforms want to include
183# it in the image header
184version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
185 cut -d' ' -f3`
186if [ -n "$version" ]; then
187 uboot_version="-n Linux-$version"
188fi
Scott Wood0fdd7172007-04-17 09:25:50 +1000189
190case "$platform" in
191uboot)
192 rm -f "$ofile"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000193 mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
David Gibsona6afacb2007-05-01 10:20:20 +1000194 $uboot_version -d "$vmz" "$ofile"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000195 if [ -z "$cacheit" ]; then
Scott Wooda9903812007-03-16 12:27:59 -0500196 rm -f "$vmz"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000197 fi
198 exit 0
199 ;;
200esac
201
202addsec() {
203 ${CROSS}objcopy $4 $1 \
204 --add-section=$3="$2" \
205 --set-section-flags=$3=contents,alloc,load,readonly,data
206}
207
Scott Wooda9903812007-03-16 12:27:59 -0500208addsec $tmp "$vmz" $ksection $object/empty.o
Paul Mackerras2bf11812006-09-27 22:47:03 +1000209if [ -z "$cacheit" ]; then
Scott Wooda9903812007-03-16 12:27:59 -0500210 rm -f "$vmz"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000211fi
212
213if [ -n "$initrd" ]; then
Mark A. Greerc8885542006-10-16 13:49:27 -0700214 addsec $tmp "$initrd" $isection
Paul Mackerras2bf11812006-09-27 22:47:03 +1000215fi
216
217if [ -n "$dtb" ]; then
Mark A. Greerc8885542006-10-16 13:49:27 -0700218 addsec $tmp "$dtb" .kernel:dtb
Mark A. Greere9c4b4b2006-11-08 17:50:44 -0700219 if [ -n "$dts" ]; then
220 rm $dtb
221 fi
Paul Mackerras2bf11812006-09-27 22:47:03 +1000222fi
223
224if [ "$platform" != "miboot" ]; then
225 ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
David Gibsoncd197ff2007-03-05 14:24:52 +1100226 $platformo $tmp $object/wrapper.a
Paul Mackerras2bf11812006-09-27 22:47:03 +1000227 rm $tmp
228fi
229
David Gibsona6afacb2007-05-01 10:20:20 +1000230# Some platforms need the zImage's entry point and base address
231base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
232entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
233
Scott Wood11c146c2007-09-14 14:58:25 -0500234if [ -n "$binary" ]; then
235 mv "$ofile" "$ofile".elf
236 ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin
237fi
238
Paul Mackerras2bf11812006-09-27 22:47:03 +1000239# post-processing needed for some platforms
240case "$platform" in
241pseries|chrp)
242 $object/addnote "$ofile"
243 ;;
Milton Miller627aa942007-05-31 01:29:01 +1000244coff)
David Gibsoncd197ff2007-03-05 14:24:52 +1100245 ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
Paul Mackerras2bf11812006-09-27 22:47:03 +1000246 $object/hack-coff "$ofile"
247 ;;
Scott Wood0fdd7172007-04-17 09:25:50 +1000248cuboot*)
Scott Wood0fdd7172007-04-17 09:25:50 +1000249 gzip -f -9 "$ofile".bin
250 mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
David Gibsona6afacb2007-05-01 10:20:20 +1000251 $uboot_version -d "$ofile".bin.gz "$ofile"
Scott Wood0fdd7172007-04-17 09:25:50 +1000252 ;;
David Gibsonf6dfc802007-05-08 14:10:01 +1000253treeboot*)
254 mv "$ofile" "$ofile.elf"
255 $object/mktree "$ofile.elf" "$ofile" "$base" "$entry"
256 if [ -z "$cacheit" ]; then
257 rm -f "$ofile.elf"
258 fi
259 exit 0
260 ;;
Geoff Levandbafdb642007-07-04 09:07:18 +1000261ps3)
262 # The ps3's loader supports loading gzipped binary images from flash
263 # rom to addr zero. The loader enters the image at addr 0x100. A
264 # bootwrapper overlay is use to arrange for the kernel to be loaded
265 # to addr zero and to have a suitable bootwrapper entry at 0x100.
266 # To construct the rom image, 0x100 bytes from offset 0x100 in the
267 # kernel is copied to the bootwrapper symbol __system_reset_kernel.
268 # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is
269 # then copied to offset 0x100. At runtime the bootwrapper program
270 # copies the 0x100 bytes at __system_reset_kernel to addr 0x100.
271
Scott Wood11c146c2007-09-14 14:58:25 -0500272 system_reset_overlay=0x`${CROSS}nm "$ofile".elf \
Geoff Levandbafdb642007-07-04 09:07:18 +1000273 | grep ' __system_reset_overlay$' \
274 | cut -d' ' -f1`
275 system_reset_overlay=`printf "%d" $system_reset_overlay`
Scott Wood11c146c2007-09-14 14:58:25 -0500276 system_reset_kernel=0x`${CROSS}nm "$ofile".elf \
Geoff Levandbafdb642007-07-04 09:07:18 +1000277 | grep ' __system_reset_kernel$' \
278 | cut -d' ' -f1`
279 system_reset_kernel=`printf "%d" $system_reset_kernel`
280 overlay_dest="256"
281 overlay_size="256"
282
283 rm -f "$object/otheros.bld"
284
Geoff Levandbafdb642007-07-04 09:07:18 +1000285 msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
286 skip=$overlay_dest seek=$system_reset_kernel \
287 count=$overlay_size bs=1 2>&1)
288
289 if [ $? -ne "0" ]; then
290 echo $msg
291 exit 1
292 fi
293
294 msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
295 skip=$system_reset_overlay seek=$overlay_dest \
296 count=$overlay_size bs=1 2>&1)
297
298 if [ $? -ne "0" ]; then
299 echo $msg
300 exit 2
301 fi
302
303 gzip --force -9 --stdout "$ofile.bin" > "$object/otheros.bld"
304 ;;
Paul Mackerras2bf11812006-09-27 22:47:03 +1000305esac