Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # __________ __ ___. |
| 3 | # Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | # \/ \/ \/ \/ \/ |
| 8 | # $Id$ |
| 9 | # |
| 10 | # Purpose of this script: |
| 11 | # |
| 12 | # Inputs: music player model name and a file name |
| 13 | # |
| 14 | # Action: Build a valid mi4 file (prepending and appending magic) |
| 15 | # Encrypt the file with TEA encryption so that the model's own |
| 16 | # bootloader accepts this file. |
| 17 | # Sign the file with a DSA signature the bootloader accepts |
| 18 | # |
| 19 | # Output: A built, encrypted and signed mi4 file image. |
| 20 | # |
| 21 | # Requirement: |
| 22 | # |
| 23 | # This script assumes that you have the mi4code tool in your path, that |
| 24 | # you have the environment variable MI4CODE pointing to the tool or that you |
| 25 | # have it in the same dir that you invoke this script with. |
| 26 | # |
| 27 | # mi4 info and tool are here: http://daniel.haxx.se/sansa/mi4.html |
| 28 | # |
| 29 | |
| 30 | mkmi4=$0 |
| 31 | target=$1 |
| 32 | input=$2 |
| 33 | output=$3 |
| 34 | |
| 35 | # scan the $PATH for the given command |
| 36 | findtool(){ |
| 37 | file="$1" |
| 38 | |
| 39 | IFS=":" |
| 40 | for path in $PATH |
| 41 | do |
| 42 | # echo "checks for $file in $path" >&2 |
| 43 | if test -f "$path/$file"; then |
| 44 | echo "$path/$file" |
| 45 | return |
| 46 | fi |
| 47 | done |
| 48 | } |
| 49 | |
| 50 | help () { |
Barry Wardell | e4e4219 | 2006-08-30 20:20:52 +0000 | [diff] [blame] | 51 | echo "Usage: mi4fix.sh <e200/h10/h10_5gb/elio> <input> <output>" |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 52 | exit |
| 53 | } |
| 54 | |
| 55 | if test -z "$output"; then |
| 56 | help |
| 57 | fi |
| 58 | |
Daniel Stenberg | ea4ed49 | 2006-08-30 10:35:14 +0000 | [diff] [blame] | 59 | # sign - if the firmware should be DSA signed with a dummy (only 010301 |
| 60 | # firmwares) |
| 61 | # tea - name of the TEA crypt key to use for encrypting, but only if ... |
| 62 | # encrypt - is set to "yes" for encrypting the firmware |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 63 | case $target in |
Daniel Stenberg | ea4ed49 | 2006-08-30 10:35:14 +0000 | [diff] [blame] | 64 | # fake example) |
| 65 | # sign="yes" |
| 66 | # encrypt="yes" |
| 67 | # tea=targetkey |
| 68 | # ;; |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 69 | e200) |
Daniel Stenberg | 9913913 | 2006-08-21 15:14:21 +0000 | [diff] [blame] | 70 | sign="yes" |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 71 | ;; |
| 72 | h10) |
Daniel Stenberg | 9913913 | 2006-08-21 15:14:21 +0000 | [diff] [blame] | 73 | sign="yes" |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 74 | ;; |
Barry Wardell | 99c2dc5 | 2006-08-19 19:21:17 +0000 | [diff] [blame] | 75 | h10_5gb) |
Daniel Stenberg | 9913913 | 2006-08-21 15:14:21 +0000 | [diff] [blame] | 76 | buildopt="-2" |
Barry Wardell | 99c2dc5 | 2006-08-19 19:21:17 +0000 | [diff] [blame] | 77 | ;; |
Barry Wardell | e4e4219 | 2006-08-30 20:20:52 +0000 | [diff] [blame] | 78 | elio) |
| 79 | buildopt="-2" |
| 80 | ;; |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 81 | *) |
| 82 | echo "unsupported target" |
| 83 | help |
| 84 | ;; |
| 85 | esac |
| 86 | |
| 87 | if test -z "$MI4CODE"; then |
| 88 | tool=`findtool mi4code` |
| 89 | if test -z "$tool"; then |
| 90 | # not in path |
| 91 | tool=`dirname $mkmi4`/mi4code |
| 92 | if ! test -f $tool; then |
| 93 | echo "Couldn't find mi4code" |
| 94 | exit |
| 95 | fi |
| 96 | fi |
| 97 | else |
| 98 | tool=$MI4CODE |
| 99 | fi |
| 100 | |
Barry Wardell | f3d5b51 | 2006-08-29 07:58:06 +0000 | [diff] [blame] | 101 | # Use full file plaintext length if not encrypting |
| 102 | if test -z "$encrypt"; then |
| 103 | buildopt="$buildopt -pall" |
| 104 | fi |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 105 | |
Barry Wardell | f3d5b51 | 2006-08-29 07:58:06 +0000 | [diff] [blame] | 106 | # build mi4 |
Daniel Stenberg | 7e25843 | 2006-08-03 20:11:40 +0000 | [diff] [blame] | 107 | #echo "$tool build $input $output.raw" |
Daniel Stenberg | 9913913 | 2006-08-21 15:14:21 +0000 | [diff] [blame] | 108 | $tool build $buildopt $input $output.raw |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 109 | # encrypt |
Barry Wardell | f3d5b51 | 2006-08-29 07:58:06 +0000 | [diff] [blame] | 110 | if test -n "$encrypt"; then |
| 111 | #echo "$tool encrypt $output.raw $output.encrypt $tea" |
| 112 | $tool encrypt $output.raw $output.encrypt $tea |
| 113 | else |
Barry Wardell | e4e4219 | 2006-08-30 20:20:52 +0000 | [diff] [blame] | 114 | # Even if we don't encrypt we need to do this to ensure the crc gets fixed |
| 115 | $tool encrypt -pall $output.raw $output.encrypt default |
Barry Wardell | f3d5b51 | 2006-08-29 07:58:06 +0000 | [diff] [blame] | 116 | fi |
Daniel Stenberg | ca4bd6f | 2006-08-03 08:08:40 +0000 | [diff] [blame] | 117 | # sign |
Daniel Stenberg | 9913913 | 2006-08-21 15:14:21 +0000 | [diff] [blame] | 118 | if test -n "$sign"; then |
| 119 | #echo "$tool sign $output.encrypt $output" |
| 120 | $tool sign $output.encrypt $output |
Barry Wardell | 2f2c280 | 2006-08-21 15:43:14 +0000 | [diff] [blame] | 121 | else |
| 122 | mv $output.encrypt $output |
Daniel Stenberg | 9913913 | 2006-08-21 15:14:21 +0000 | [diff] [blame] | 123 | fi |