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 | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 32 | gzip=.gz |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 33 | |
| 34 | # cross-compilation prefix |
| 35 | CROSS= |
| 36 | |
| 37 | # directory for object and other files used by this script |
| 38 | object=arch/powerpc/boot |
| 39 | |
| 40 | # directory for working files |
| 41 | tmpdir=. |
| 42 | |
| 43 | usage() { |
| 44 | echo 'Usage: wrapper [-o output] [-p platform] [-i initrd]' >&2 |
| 45 | echo ' [-d devtree] [-s tree.dts] [-c] [-C cross-prefix]' >&2 |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 46 | echo ' [-D datadir] [-W workingdir] [--no-gzip] [vmlinux]' >&2 |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 47 | exit 1 |
| 48 | } |
| 49 | |
| 50 | while [ "$#" -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 | ;; |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 95 | --no-gzip) |
| 96 | gzip= |
| 97 | ;; |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 98 | -?) |
| 99 | usage |
| 100 | ;; |
| 101 | *) |
| 102 | [ -z "$kernel" ] || usage |
| 103 | kernel="$1" |
| 104 | ;; |
| 105 | esac |
| 106 | shift |
| 107 | done |
| 108 | |
| 109 | if [ -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 |
| 114 | fi |
| 115 | |
| 116 | if [ -z "$kernel" ]; then |
| 117 | kernel=vmlinux |
| 118 | fi |
| 119 | |
| 120 | platformo=$object/"$platform".o |
| 121 | lds=$object/zImage.lds |
| 122 | ext=strip |
| 123 | objflags=-S |
| 124 | tmp=$tmpdir/zImage.$$.o |
| 125 | ksection=.kernel:vmlinux.strip |
| 126 | isection=.kernel:initrd |
| 127 | |
| 128 | case "$platform" in |
| 129 | pmac|pseries|chrp) |
| 130 | platformo=$object/of.o |
| 131 | ;; |
| 132 | pmaccoff) |
| 133 | platformo=$object/of.o |
| 134 | lds=$object/zImage.coff.lds |
| 135 | ;; |
| 136 | miboot|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 | ;; |
Scott Wood | 0fdd717 | 2007-04-17 09:25:50 +1000 | [diff] [blame] | 144 | cuboot*) |
| 145 | gzip= |
| 146 | ;; |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 147 | esac |
| 148 | |
| 149 | vmz="$tmpdir/`basename \"$kernel\"`.$ext" |
Milton Miller | 1383a34 | 2007-03-28 02:21:04 -0600 | [diff] [blame] | 150 | 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] | 151 | ${CROSS}objcopy $objflags "$kernel" "$vmz.$$" |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 152 | |
| 153 | if [ -n "$gzip" ]; then |
| 154 | gzip -f -9 "$vmz.$$" |
| 155 | fi |
| 156 | |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 157 | if [ -n "$cacheit" ]; then |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 158 | mv -f "$vmz.$$$gzip" "$vmz$gzip" |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 159 | else |
| 160 | vmz="$vmz.$$" |
| 161 | fi |
| 162 | fi |
| 163 | |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 164 | vmz="$vmz$gzip" |
| 165 | |
David Gibson | a6afacb | 2007-05-01 10:20:20 +1000 | [diff] [blame] | 166 | # Extract kernel version information, some platforms want to include |
| 167 | # it in the image header |
| 168 | version=`${CROSS}strings "$kernel" | grep '^Linux version [-0-9.]' | \ |
| 169 | cut -d' ' -f3` |
| 170 | if [ -n "$version" ]; then |
| 171 | uboot_version="-n Linux-$version" |
| 172 | fi |
Scott Wood | 0fdd717 | 2007-04-17 09:25:50 +1000 | [diff] [blame] | 173 | |
| 174 | case "$platform" in |
| 175 | uboot) |
| 176 | rm -f "$ofile" |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 177 | 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] | 178 | $uboot_version -d "$vmz" "$ofile" |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 179 | if [ -z "$cacheit" ]; then |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 180 | rm -f "$vmz" |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 181 | fi |
| 182 | exit 0 |
| 183 | ;; |
| 184 | esac |
| 185 | |
| 186 | addsec() { |
| 187 | ${CROSS}objcopy $4 $1 \ |
| 188 | --add-section=$3="$2" \ |
| 189 | --set-section-flags=$3=contents,alloc,load,readonly,data |
| 190 | } |
| 191 | |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 192 | addsec $tmp "$vmz" $ksection $object/empty.o |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 193 | if [ -z "$cacheit" ]; then |
Scott Wood | a990381 | 2007-03-16 12:27:59 -0500 | [diff] [blame] | 194 | rm -f "$vmz" |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 195 | fi |
| 196 | |
| 197 | if [ -n "$initrd" ]; then |
Mark A. Greer | c888554 | 2006-10-16 13:49:27 -0700 | [diff] [blame] | 198 | addsec $tmp "$initrd" $isection |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 199 | fi |
| 200 | |
| 201 | if [ -n "$dtb" ]; then |
Mark A. Greer | c888554 | 2006-10-16 13:49:27 -0700 | [diff] [blame] | 202 | addsec $tmp "$dtb" .kernel:dtb |
Mark A. Greer | e9c4b4b | 2006-11-08 17:50:44 -0700 | [diff] [blame] | 203 | if [ -n "$dts" ]; then |
| 204 | rm $dtb |
| 205 | fi |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 206 | fi |
| 207 | |
| 208 | if [ "$platform" != "miboot" ]; then |
| 209 | ${CROSS}ld -m elf32ppc -T $lds -o "$ofile" \ |
David Gibson | cd197ff | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 210 | $platformo $tmp $object/wrapper.a |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 211 | rm $tmp |
| 212 | fi |
| 213 | |
David Gibson | a6afacb | 2007-05-01 10:20:20 +1000 | [diff] [blame] | 214 | # Some platforms need the zImage's entry point and base address |
| 215 | base=0x`${CROSS}nm "$ofile" | grep ' _start$' | cut -d' ' -f1` |
| 216 | entry=`${CROSS}objdump -f "$ofile" | grep '^start address ' | cut -d' ' -f3` |
| 217 | |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 218 | # post-processing needed for some platforms |
| 219 | case "$platform" in |
| 220 | pseries|chrp) |
| 221 | $object/addnote "$ofile" |
| 222 | ;; |
| 223 | pmaccoff) |
David Gibson | cd197ff | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 224 | ${CROSS}objcopy -O aixcoff-rs6000 --set-start "$entry" "$ofile" |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 225 | $object/hack-coff "$ofile" |
| 226 | ;; |
Scott Wood | 0fdd717 | 2007-04-17 09:25:50 +1000 | [diff] [blame] | 227 | cuboot*) |
Scott Wood | 0fdd717 | 2007-04-17 09:25:50 +1000 | [diff] [blame] | 228 | mv "$ofile" "$ofile".elf |
| 229 | ${CROSS}objcopy -O binary "$ofile".elf "$ofile".bin |
| 230 | gzip -f -9 "$ofile".bin |
| 231 | 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] | 232 | $uboot_version -d "$ofile".bin.gz "$ofile" |
Scott Wood | 0fdd717 | 2007-04-17 09:25:50 +1000 | [diff] [blame] | 233 | ;; |
David Gibson | f6dfc80 | 2007-05-08 14:10:01 +1000 | [diff] [blame] | 234 | treeboot*) |
| 235 | mv "$ofile" "$ofile.elf" |
| 236 | $object/mktree "$ofile.elf" "$ofile" "$base" "$entry" |
| 237 | if [ -z "$cacheit" ]; then |
| 238 | rm -f "$ofile.elf" |
| 239 | fi |
| 240 | exit 0 |
| 241 | ;; |
Paul Mackerras | 2bf1181 | 2006-09-27 22:47:03 +1000 | [diff] [blame] | 242 | esac |