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