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