ac16e9984ecb39e805a0b9e93fb47727db6c1379
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / powerpc / boot / wrapper
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
24 # Stop execution if any command fails
25 set -e
26
27 # Allow for verbose output
28 if [ "$V" = 1 ]; then
29     set -x
30 fi
31
32 # defaults
33 kernel=
34 ofile=zImage
35 platform=of
36 initrd=
37 dtb=
38 dts=
39 cacheit=
40 binary=
41 gzip=.gz
42 pie=
43
44 # cross-compilation prefix
45 CROSS=
46
47 # mkimage wrapper script
48 MKIMAGE=$srctree/scripts/mkuboot.sh
49
50 # directory for object and other files used by this script
51 object=arch/powerpc/boot
52 objbin=$object
53 dtc=scripts/dtc/dtc
54
55 # directory for working files
56 tmpdir=.
57
58 usage() {
59     echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
60     echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
61     echo '       [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
62     exit 1
63 }
64
65 while [ "$#" -gt 0 ]; do
66     case "$1" in
67     -o)
68         shift
69         [ "$#" -gt 0 ] || usage
70         ofile="$1"
71         ;;
72     -p)
73         shift
74         [ "$#" -gt 0 ] || usage
75         platform="$1"
76         ;;
77     -i)
78         shift
79         [ "$#" -gt 0 ] || usage
80         initrd="$1"
81         ;;
82     -d)
83         shift
84         [ "$#" -gt 0 ] || usage
85         dtb="$1"
86         ;;
87     -s)
88         shift
89         [ "$#" -gt 0 ] || usage
90         dts="$1"
91         ;;
92     -c)
93         cacheit=y
94         ;;
95     -C)
96         shift
97         [ "$#" -gt 0 ] || usage
98         CROSS="$1"
99         ;;
100     -D)
101         shift
102         [ "$#" -gt 0 ] || usage
103         object="$1"
104         objbin="$1"
105         ;;
106     -W)
107         shift
108         [ "$#" -gt 0 ] || usage
109         tmpdir="$1"
110         ;;
111     --no-gzip)
112         gzip=
113         ;;
114     -?)
115         usage
116         ;;
117     *)
118         [ -z "$kernel" ] || usage
119         kernel="$1"
120         ;;
121     esac
122     shift
123 done
124
125 if [ -n "$dts" ]; then
126     if [ ! -r "$dts" -a -r "$object/dts/$dts" ]; then
127         dts="$object/dts/$dts"
128     fi
129     if [ -z "$dtb" ]; then
130         dtb="$platform.dtb"
131     fi
132     $dtc -O dtb -o "$dtb" -b 0 "$dts"
133 fi
134
135 if [ -z "$kernel" ]; then
136     kernel=vmlinux
137 fi
138
139 platformo=$object/"$platform".o
140 lds=$object/zImage.lds
141 ext=strip
142 objflags=-S
143 tmp=$tmpdir/zImage.$$.o
144 ksection=.kernel:vmlinux.strip
145 isection=.kernel:initrd
146 link_address='0x400000'
147 make_space=y
148
149 case "$platform" in
150 pseries)
151     platformo="$object/of.o $object/epapr.o"
152     link_address='0x4000000'
153     make_space=n
154     ;;
155 maple)
156     platformo="$object/of.o $object/epapr.o"
157     link_address='0x400000'
158     make_space=n
159     ;;
160 pmac|chrp)
161     platformo="$object/of.o $object/epapr.o"
162     make_space=n
163     ;;
164 coff)
165     platformo="$object/crt0.o $object/of.o $object/epapr.o"
166     lds=$object/zImage.coff.lds
167     link_address='0x500000'
168     make_space=n
169     pie=
170     ;;
171 miboot|uboot*)
172     # miboot and U-boot want just the bare bits, not an ELF binary
173     ext=bin
174     objflags="-O binary"
175     tmp="$ofile"
176     ksection=image
177     isection=initrd
178     ;;
179 cuboot*)
180     binary=y
181     gzip=
182     case "$platform" in
183     *-mpc866ads|*-mpc885ads|*-adder875*|*-ep88xc)
184         platformo=$object/cuboot-8xx.o
185         ;;
186     *5200*|*-motionpro)
187         platformo=$object/cuboot-52xx.o
188         ;;
189     *-pq2fads|*-ep8248e|*-mpc8272*|*-storcenter)
190         platformo=$object/cuboot-pq2.o
191         ;;
192     *-mpc824*)
193         platformo=$object/cuboot-824x.o
194         ;;
195     *-mpc83*|*-asp834x*)
196         platformo=$object/cuboot-83xx.o
197         ;;
198     *-tqm8541|*-mpc8560*|*-tqm8560|*-tqm8555|*-ksi8560*)
199         platformo=$object/cuboot-85xx-cpm2.o
200         ;;
201     *-mpc85*|*-tqm85*|*-sbc85*)
202         platformo=$object/cuboot-85xx.o
203         ;;
204     *-amigaone)
205         link_address='0x800000'
206         ;;
207     esac
208     ;;
209 ps3)
210     platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
211     lds=$object/zImage.ps3.lds
212     gzip=
213     ext=bin
214     objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
215     ksection=.kernel:vmlinux.bin
216     isection=.kernel:initrd
217     link_address=''
218     make_space=n
219     pie=
220     ;;
221 ep88xc|ep405|ep8248e)
222     platformo="$object/fixed-head.o $object/$platform.o"
223     binary=y
224     ;;
225 adder875-redboot)
226     platformo="$object/fixed-head.o $object/redboot-8xx.o"
227     binary=y
228     ;;
229 simpleboot-virtex405-*)
230     platformo="$object/virtex405-head.o $object/simpleboot.o $object/virtex.o"
231     binary=y
232     ;;
233 simpleboot-virtex440-*)
234     platformo="$object/fixed-head.o $object/simpleboot.o $object/virtex.o"
235     binary=y
236     ;;
237 simpleboot-*)
238     platformo="$object/fixed-head.o $object/simpleboot.o"
239     binary=y
240     ;;
241 asp834x-redboot)
242     platformo="$object/fixed-head.o $object/redboot-83xx.o"
243     binary=y
244     ;;
245 xpedite52*)
246     link_address='0x1400000'
247     platformo=$object/cuboot-85xx.o
248     ;;
249 gamecube|wii)
250     link_address='0x600000'
251     platformo="$object/$platform-head.o $object/$platform.o"
252     ;;
253 treeboot-currituck)
254     link_address='0x1000000'
255     ;;
256 treeboot-iss4xx-mpic)
257     platformo="$object/treeboot-iss4xx.o"
258     ;;
259 epapr)
260     platformo="$object/epapr.o $object/epapr-wrapper.o"
261     link_address='0x20000000'
262     pie=-pie
263     ;;
264 esac
265
266 vmz="$tmpdir/`basename \"$kernel\"`.$ext"
267 if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
268     ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
269
270     strip_size=$(stat -c %s $vmz.$$)
271
272     if [ -n "$gzip" ]; then
273         gzip -n -f -9 "$vmz.$$"
274     fi
275
276     if [ -n "$cacheit" ]; then
277         mv -f "$vmz.$$$gzip" "$vmz$gzip"
278     else
279         vmz="$vmz.$$"
280     fi
281 else
282     # Calculate the vmlinux.strip size
283     ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
284     strip_size=$(stat -c %s $vmz.$$)
285     rm -f $vmz.$$
286 fi
287
288 if [ "$make_space" = "y" ]; then
289         # Round the size to next higher MB limit
290         round_size=$(((strip_size + 0xfffff) & 0xfff00000))
291
292         round_size=0x$(printf "%x" $round_size)
293         link_addr=$(printf "%d" $link_address)
294
295         if [ $link_addr -lt $strip_size ]; then
296             echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \
297                         "overlaps the address of the wrapper($link_address)"
298             echo "INFO: Fixing the link_address of wrapper to ($round_size)"
299             link_address=$round_size
300         fi
301 fi
302
303 vmz="$vmz$gzip"
304
305 # Extract kernel version information, some platforms want to include
306 # it in the image header
307 version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
308     cut -d' ' -f3`
309 if [ -n "$version" ]; then
310     uboot_version="-n Linux-$version"
311 fi
312
313 # physical offset of kernel image
314 membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'`
315
316 case "$platform" in
317 uboot)
318     rm -f "$ofile"
319     ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a $membase -e $membase \
320         $uboot_version -d "$vmz" "$ofile"
321     if [ -z "$cacheit" ]; then
322         rm -f "$vmz"
323     fi
324     exit 0
325     ;;
326 uboot-obs600)
327     rm -f "$ofile"
328     # obs600 wants a multi image with an initrd, so we need to put a fake
329     # one in even when building a "normal" image.
330     if [ -n "$initrd" ]; then
331         real_rd="$initrd"
332     else
333         real_rd=`mktemp`
334         echo "\0" >>"$real_rd"
335     fi
336     ${MKIMAGE} -A ppc -O linux -T multi -C gzip -a $membase -e $membase \
337         $uboot_version -d "$vmz":"$real_rd":"$dtb" "$ofile"
338     if [ -z "$initrd" ]; then
339         rm -f "$real_rd"
340     fi
341     if [ -z "$cacheit" ]; then
342         rm -f "$vmz"
343     fi
344     exit 0
345     ;;
346 esac
347
348 addsec() {
349     ${CROSS}objcopy $4 $1 \
350         --add-section=$3="$2" \
351         --set-section-flags=$3=contents,alloc,load,readonly,data
352 }
353
354 addsec $tmp "$vmz" $ksection $object/empty.o
355 if [ -z "$cacheit" ]; then
356     rm -f "$vmz"
357 fi
358
359 if [ -n "$initrd" ]; then
360     addsec $tmp "$initrd" $isection
361 fi
362
363 if [ -n "$dtb" ]; then
364     addsec $tmp "$dtb" .kernel:dtb
365     if [ -n "$dts" ]; then
366         rm $dtb
367     fi
368 fi
369
370 if [ "$platform" != "miboot" ]; then
371     if [ -n "$link_address" ] ; then
372         text_start="-Ttext $link_address"
373     fi
374     ${CROSS}ld -m elf32ppc -T $lds $text_start $pie -o "$ofile" \
375         $platformo $tmp $object/wrapper.a
376     rm $tmp
377 fi
378
379 # Some platforms need the zImage's entry point and base address
380 base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
381 entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
382
383 if [ -n "$binary" ]; then
384     mv "$ofile" "$ofile".elf
385     ${CROSS}objcopy -O binary "$ofile".elf "$ofile"
386 fi
387
388 # post-processing needed for some platforms
389 case "$platform" in
390 pseries|chrp|maple)
391     $objbin/addnote "$ofile"
392     ;;
393 coff)
394     ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
395     $objbin/hack-coff "$ofile"
396     ;;
397 cuboot*)
398     gzip -n -f -9 "$ofile"
399     ${MKIMAGE} -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
400             $uboot_version -d "$ofile".gz "$ofile"
401     ;;
402 treeboot*)
403     mv "$ofile" "$ofile.elf"
404     $objbin/mktree "$ofile.elf" "$ofile" "$base" "$entry"
405     if [ -z "$cacheit" ]; then
406         rm -f "$ofile.elf"
407     fi
408     exit 0
409     ;;
410 ps3)
411     # The ps3's loader supports loading a gzipped binary image from flash
412     # rom to ram addr zero. The loader then enters the system reset
413     # vector at addr 0x100.  A bootwrapper overlay is used to arrange for
414     # a binary image of the kernel to be at addr zero, and yet have a
415     # suitable bootwrapper entry at 0x100.  To construct the final rom
416     # image 512 bytes from offset 0x100 is copied to the bootwrapper
417     # place holder at symbol __system_reset_kernel.  The 512 bytes of the
418     # bootwrapper entry code at symbol __system_reset_overlay is then
419     # copied to offset 0x100.  At runtime the bootwrapper program copies
420     # the data at __system_reset_kernel back to addr 0x100.
421
422     system_reset_overlay=0x`${CROSS}nm "$ofile" \
423         | grep ' __system_reset_overlay$'       \
424         | cut -d' ' -f1`
425     system_reset_overlay=`printf "%d" $system_reset_overlay`
426     system_reset_kernel=0x`${CROSS}nm "$ofile" \
427         | grep ' __system_reset_kernel$'       \
428         | cut -d' ' -f1`
429     system_reset_kernel=`printf "%d" $system_reset_kernel`
430     overlay_dest="256"
431     overlay_size="512"
432
433     ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
434
435     dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
436         skip=$overlay_dest seek=$system_reset_kernel  \
437         count=$overlay_size bs=1
438
439     dd if="$ofile.bin" of="$ofile.bin" conv=notrunc   \
440         skip=$system_reset_overlay seek=$overlay_dest \
441         count=$overlay_size bs=1
442
443     odir="$(dirname "$ofile.bin")"
444     rm -f "$odir/otheros.bld"
445     gzip -n --force -9 --stdout "$ofile.bin" > "$odir/otheros.bld"
446     ;;
447 esac