blob: 068d7e0628326577f70e6cb822dea618d5493736 [file] [log] [blame]
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +00001#!/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
30mkmi4=$0
31target=$1
32input=$2
33output=$3
34
35# scan the $PATH for the given command
36findtool(){
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
50help () {
Barry Wardelle4e42192006-08-30 20:20:52 +000051 echo "Usage: mi4fix.sh <e200/h10/h10_5gb/elio> <input> <output>"
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +000052 exit
53}
54
55if test -z "$output"; then
56 help
57fi
58
Daniel Stenbergea4ed492006-08-30 10:35:14 +000059# 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 Stenbergca4bd6f2006-08-03 08:08:40 +000063case $target in
Daniel Stenbergea4ed492006-08-30 10:35:14 +000064 # fake example)
65 # sign="yes"
66 # encrypt="yes"
67 # tea=targetkey
68 # ;;
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +000069 e200)
Daniel Stenberg99139132006-08-21 15:14:21 +000070 sign="yes"
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +000071 ;;
72 h10)
Daniel Stenberg99139132006-08-21 15:14:21 +000073 sign="yes"
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +000074 ;;
Barry Wardell99c2dc52006-08-19 19:21:17 +000075 h10_5gb)
Daniel Stenberg99139132006-08-21 15:14:21 +000076 buildopt="-2"
Barry Wardell99c2dc52006-08-19 19:21:17 +000077 ;;
Barry Wardelle4e42192006-08-30 20:20:52 +000078 elio)
79 buildopt="-2"
80 ;;
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +000081 *)
82 echo "unsupported target"
83 help
84 ;;
85esac
86
87if 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
97else
98 tool=$MI4CODE
99fi
100
Barry Wardellf3d5b512006-08-29 07:58:06 +0000101# Use full file plaintext length if not encrypting
102if test -z "$encrypt"; then
103 buildopt="$buildopt -pall"
104fi
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +0000105
Barry Wardellf3d5b512006-08-29 07:58:06 +0000106# build mi4
Daniel Stenberg7e258432006-08-03 20:11:40 +0000107#echo "$tool build $input $output.raw"
Daniel Stenberg99139132006-08-21 15:14:21 +0000108$tool build $buildopt $input $output.raw
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +0000109# encrypt
Barry Wardellf3d5b512006-08-29 07:58:06 +0000110if test -n "$encrypt"; then
111 #echo "$tool encrypt $output.raw $output.encrypt $tea"
112 $tool encrypt $output.raw $output.encrypt $tea
113else
Barry Wardelle4e42192006-08-30 20:20:52 +0000114 # 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 Wardellf3d5b512006-08-29 07:58:06 +0000116fi
Daniel Stenbergca4bd6f2006-08-03 08:08:40 +0000117# sign
Daniel Stenberg99139132006-08-21 15:14:21 +0000118if test -n "$sign"; then
119 #echo "$tool sign $output.encrypt $output"
120 $tool sign $output.encrypt $output
Barry Wardell2f2c2802006-08-21 15:43:14 +0000121else
122 mv $output.encrypt $output
Daniel Stenberg99139132006-08-21 15:14:21 +0000123fi