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