xref: /openbmc/linux/arch/powerpc/boot/wrapper (revision c21b37f6)
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
25kernel=
26ofile=zImage
27platform=of
28initrd=
29dtb=
30dts=
31cacheit=
32gzip=.gz
33
34# cross-compilation prefix
35CROSS=
36
37# directory for object and other files used by this script
38object=arch/powerpc/boot
39
40# directory for working files
41tmpdir=.
42
43usage() {
44    echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2
45    echo '       [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2
46    echo '       [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2
47    exit 1
48}
49
50while [ "$#" -gt 0 ]; do
51    case "$1" in
52    -o)
53	shift
54	[ "$#" -gt 0 ] || usage
55	ofile="$1"
56	;;
57    -p)
58	shift
59	[ "$#" -gt 0 ] || usage
60	platform="$1"
61	;;
62    -i)
63	shift
64	[ "$#" -gt 0 ] || usage
65	initrd="$1"
66	;;
67    -d)
68	shift
69	[ "$#" -gt 0 ] || usage
70	dtb="$1"
71	;;
72    -s)
73	shift
74	[ "$#" -gt 0 ] || usage
75	dts="$1"
76	;;
77    -c)
78	cacheit=y
79	;;
80    -C)
81	shift
82	[ "$#" -gt 0 ] || usage
83	CROSS="$1"
84	;;
85    -D)
86	shift
87	[ "$#" -gt 0 ] || usage
88	object="$1"
89	;;
90    -W)
91	shift
92	[ "$#" -gt 0 ] || usage
93	tmpdir="$1"
94	;;
95    --no-gzip)
96        gzip=
97        ;;
98    -?)
99	usage
100	;;
101    *)
102	[ -z "$kernel" ] || usage
103	kernel="$1"
104	;;
105    esac
106    shift
107done
108
109if [ -n "$dts" ]; then
110    if [ -z "$dtb" ]; then
111	dtb="$platform.dtb"
112    fi
113    dtc -O dtb -o "$dtb" -b 0 -V 16 "$dts" || exit 1
114fi
115
116if [ -z "$kernel" ]; then
117    kernel=vmlinux
118fi
119
120platformo=$object/"$platform".o
121lds=$object/zImage.lds
122ext=strip
123objflags=-S
124tmp=$tmpdir/zImage.$$.o
125ksection=.kernel:vmlinux.strip
126isection=.kernel:initrd
127
128case "$platform" in
129pmac|pseries|chrp)
130    platformo=$object/of.o
131    ;;
132coff)
133    platformo=$object/of.o
134    lds=$object/zImage.coff.lds
135    ;;
136miboot|uboot)
137    # miboot and U-boot want just the bare bits, not an ELF binary
138    ext=bin
139    objflags="-O binary"
140    tmp="$ofile"
141    ksection=image
142    isection=initrd
143    ;;
144cuboot*)
145    gzip=
146    ;;
147ps3)
148    platformo="$object/ps3-head.o $object/ps3-hvcall.o $object/ps3.o"
149    lds=$object/zImage.ps3.lds
150    gzip=
151    ext=bin
152    objflags="-O binary --set-section-flags=.bss=contents,alloc,load,data"
153    ksection=.kernel:vmlinux.bin
154    isection=.kernel:initrd
155    ;;
156esac
157
158vmz="$tmpdir/`basename \"$kernel\"`.$ext"
159if [ -z "$cacheit" -o ! -f "$vmz$gzip" -o "$vmz$gzip" -ot "$kernel" ]; then
160    ${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
161
162    if [ -n "$gzip" ]; then
163        gzip -f -9 "$vmz.$$"
164    fi
165
166    if [ -n "$cacheit" ]; then
167	mv -f "$vmz.$$$gzip" "$vmz$gzip"
168    else
169	vmz="$vmz.$$"
170    fi
171fi
172
173vmz="$vmz$gzip"
174
175# Extract kernel version information, some platforms want to include
176# it in the image header
177version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \
178    cut -d' ' -f3`
179if [ -n "$version" ]; then
180    uboot_version="-n Linux-$version"
181fi
182
183case "$platform" in
184uboot)
185    rm -f "$ofile"
186    mkimage -A ppc -O linux -T kernel -C gzip -a 00000000 -e 00000000 \
187	$uboot_version -d "$vmz" "$ofile"
188    if [ -z "$cacheit" ]; then
189	rm -f "$vmz"
190    fi
191    exit 0
192    ;;
193esac
194
195addsec() {
196    ${CROSS}objcopy $4 $1 \
197	--add-section=$3="$2" \
198	--set-section-flags=$3=contents,alloc,load,readonly,data
199}
200
201addsec $tmp "$vmz" $ksection $object/empty.o
202if [ -z "$cacheit" ]; then
203    rm -f "$vmz"
204fi
205
206if [ -n "$initrd" ]; then
207    addsec $tmp "$initrd" $isection
208fi
209
210if [ -n "$dtb" ]; then
211    addsec $tmp "$dtb" .kernel:dtb
212    if [ -n "$dts" ]; then
213	rm $dtb
214    fi
215fi
216
217if [ "$platform" != "miboot" ]; then
218    ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \
219	$platformo $tmp $object/wrapper.a
220    rm $tmp
221fi
222
223# Some platforms need the zImage's entry point and base address
224base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1`
225entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3`
226
227# post-processing needed for some platforms
228case "$platform" in
229pseries|chrp)
230    $object/addnote "$ofile"
231    ;;
232coff)
233    ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile"
234    $object/hack-coff "$ofile"
235    ;;
236cuboot*)
237    mv "$ofile" "$ofile".elf
238    ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin
239    gzip -f -9 "$ofile".bin
240    mkimage -A ppc -O linux -T kernel -C gzip -a "$base" -e "$entry" \
241            $uboot_version -d "$ofile".bin.gz "$ofile"
242    ;;
243treeboot*)
244    mv "$ofile" "$ofile.elf"
245    $object/mktree "$ofile.elf" "$ofile" "$base" "$entry"
246    if [ -z "$cacheit" ]; then
247	rm -f "$ofile.elf"
248    fi
249    exit 0
250    ;;
251ps3)
252    # The ps3's loader supports loading gzipped binary images from flash
253    # rom to addr zero. The loader enters the image at addr 0x100.  A
254    # bootwrapper overlay is use to arrange for the kernel to be loaded
255    # to addr zero and to have a suitable bootwrapper entry at 0x100.
256    # To construct the rom image, 0x100 bytes from offset 0x100 in the
257    # kernel is copied to the bootwrapper symbol __system_reset_kernel.
258    # The 0x100 bytes at the bootwrapper symbol __system_reset_overlay is
259    # then copied to offset 0x100.  At runtime the bootwrapper program
260    # copies the 0x100 bytes at __system_reset_kernel to addr 0x100.
261
262    system_reset_overlay=0x`${CROSS}nm "$ofile" \
263        | grep ' __system_reset_overlay$'       \
264        | cut -d' ' -f1`
265    system_reset_overlay=`printf "%d" $system_reset_overlay`
266    system_reset_kernel=0x`${CROSS}nm "$ofile" \
267        | grep ' __system_reset_kernel$'       \
268        | cut -d' ' -f1`
269    system_reset_kernel=`printf "%d" $system_reset_kernel`
270    overlay_dest="256"
271    overlay_size="256"
272
273    rm -f "$object/otheros.bld"
274
275    ${CROSS}objcopy -O binary "$ofile" "$ofile.bin"
276
277    msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
278        skip=$overlay_dest seek=$system_reset_kernel      \
279        count=$overlay_size bs=1 2>&1)
280
281    if [ $? -ne "0" ]; then
282       echo $msg
283       exit 1
284    fi
285
286    msg=$(dd if="$ofile.bin" of="$ofile.bin" conv=notrunc \
287        skip=$system_reset_overlay seek=$overlay_dest     \
288        count=$overlay_size bs=1 2>&1)
289
290    if [ $? -ne "0" ]; then
291       echo $msg
292       exit 2
293    fi
294
295    gzip --force -9 --stdout "$ofile.bin" > "$object/otheros.bld"
296    ;;
297esac
298