Merge branch 'master' of git://oak/home/sfr/kernels/iseries/work
[sfrench/cifs-2.6.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 # defaults
25 kernel=
26 ofile=zImage
27 platform=of
28 initrd=
29 dtb=
30 dts=
31 cacheit=
32
33 # cross-compilation prefix
34 CROSS=
35
36 # directory for object and other files used by this script
37 object=arch/powerpc/boot
38
39 # directory for working files
40 tmpdir=.
41
42 usage() {
43     echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
44     echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
45     echo '       [-D datadir] [-W workingdir] [vmlinux]' >&2
46     exit 1
47 }
48
49 while [ "$#" -gt 0 ]; do
50     case "$1" in
51     -o)
52         shift
53         [ "$#" -gt 0 ] || usage
54         ofile="$1"
55         ;;
56     -p)
57         shift
58         [ "$#" -gt 0 ] || usage
59         platform="$1"
60         ;;
61     -i)
62         shift
63         [ "$#" -gt 0 ] || usage
64         initrd="$1"
65         ;;
66     -d)
67         shift
68         [ "$#" -gt 0 ] || usage
69         dtb="$1"
70         ;;
71     -s)
72         shift
73         [ "$#" -gt 0 ] || usage
74         dts="$1"
75         ;;
76     -c)
77         cacheit=y
78         ;;
79     -C)
80         shift
81         [ "$#" -gt 0 ] || usage
82         CROSS="$1"
83         ;;
84     -D)
85         shift
86         [ "$#" -gt 0 ] || usage
87         object="$1"
88         ;;
89     -W)
90         shift
91         [ "$#" -gt 0 ] || usage
92         tmpdir="$1"
93         ;;
94     -?)
95         usage
96         ;;
97     *)
98         [ -z "$kernel" ] || usage
99         kernel="$1"
100         ;;
101     esac
102     shift
103 done
104
105 if [ -n "$dts" ]; then
106     if [ -z "$dtb" ]; then
107         dtb="$platform.dtb"
108     fi
109     dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts" || exit 1
110 fi
111
112 if [ -z "$kernel" ]; then
113     kernel=vmlinux
114 fi
115
116 platformo=$object/"$platform".o
117 lds=$object/zImage.lds
118 ext=strip
119 objflags=-S
120 tmp=$tmpdir/zImage.$$.o
121 ksection=.kernel:vmlinux.strip
122 isection=.kernel:initrd
123
124 case "$platform" in
125 pmac|pseries|chrp)
126     platformo=$object/of.o
127     ;;
128 pmaccoff)
129     platformo=$object/of.o
130     lds=$object/zImage.coff.lds
131     ;;
132 miboot|uboot)
133     # miboot and U-boot want just the bare bits, not an ELF binary
134     ext=bin
135     objflags="-O binary"
136     tmp="$ofile"
137     ksection=image
138     isection=initrd
139     ;;
140 esac
141
142 vmz="$tmpdir/`basename \"$kernel\"`.$ext"
143 if [ -z "$cacheit" -o ! -f "$vmz.gz" -o "$vmz.gz" -ot "$kernel" ]; then
144     ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
145     gzip -f -9 "$vmz.$$"
146     if [ -n "$cacheit" ]; then
147         mv -f "$vmz.$$.gz" "$vmz.gz"
148     else
149         vmz="$vmz.$$"
150     fi
151 fi
152
153 case "$platform" in
154 uboot)
155     rm -f "$ofile"
156     version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
157         cut -d' ' -f3`
158     if [ -n "$version" ]; then
159         version="-n Linux-$version"
160     fi
161     mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
162         $version -d "$vmz.gz" "$ofile"
163     if [ -z "$cacheit" ]; then
164         rm -f $vmz.gz
165     fi
166     exit 0
167     ;;
168 esac
169
170 addsec() {
171     ${CROSS}objcopy $4 $1 \
172         --add-section=$3="$2" \
173         --set-section-flags=$3=contents,alloc,load,readonly,data
174 }
175
176 addsec $tmp "$vmz.gz" $ksection $object/empty.o
177 if [ -z "$cacheit" ]; then
178     rm -f "$vmz.gz"
179 fi
180
181 if [ -n "$initrd" ]; then
182     addsec $tmp "$initrd" initrd
183 fi
184
185 if [ -n "$dtb" ]; then
186     addsec $tmp "$dtb" dtb
187 fi
188
189 if [ "$platform" != "miboot" ]; then
190     ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
191         $object/crt0.o $platformo $tmp $object/wrapper.a
192     rm $tmp
193 fi
194
195 # post-processing needed for some platforms
196 case "$platform" in
197 pseries|chrp)
198     $object/addnote "$ofile"
199     ;;
200 pmaccoff)
201     ${CROSS}objcopy -O aixcoff-rs6000 --set-start 0x500000 "$ofile"
202     $object/hack-coff "$ofile"
203     ;;
204 esac