| 1 |
2 |
ZTEX |
/*!
|
| 2 |
5 |
ZTEX |
Java host software API of ZTEX EZ-USB FX2 SDK
|
| 3 |
|
|
Copyright (C) 2009-2011 ZTEX GmbH.
|
| 4 |
2 |
ZTEX |
http://www.ztex.de
|
| 5 |
|
|
|
| 6 |
|
|
This program is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
| 8 |
|
|
published by the Free Software Foundation.
|
| 9 |
|
|
|
| 10 |
|
|
This program is distributed in the hope that it will be useful, but
|
| 11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 13 |
|
|
General Public License for more details.
|
| 14 |
|
|
|
| 15 |
|
|
You should have received a copy of the GNU General Public License
|
| 16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
| 17 |
|
|
!*/
|
| 18 |
|
|
|
| 19 |
|
|
/*
|
| 20 |
|
|
Reads an ihx file
|
| 21 |
|
|
*/
|
| 22 |
|
|
package ztex;
|
| 23 |
|
|
|
| 24 |
|
|
import java.io.*;
|
| 25 |
|
|
import java.util.*;
|
| 26 |
|
|
import java.net.*;
|
| 27 |
|
|
/**
|
| 28 |
|
|
* A class representing a firmware image loaded from an ihx (Intel Hex format) file.
|
| 29 |
|
|
*/
|
| 30 |
|
|
public class IhxFile {
|
| 31 |
|
|
|
| 32 |
|
|
/**
|
| 33 |
|
|
* This array stores the firmware image.
|
| 34 |
|
|
* Values <0 and >255 mean that the data is undefined.
|
| 35 |
|
|
*/
|
| 36 |
|
|
public short ihxData[] = new short[65536];
|
| 37 |
|
|
|
| 38 |
|
|
// ******* readHexDigit ********************************************************
|
| 39 |
|
|
private static final int readHexDigit( InputStream in ) throws IOException, IhxParseException {
|
| 40 |
|
|
int b = in.read();
|
| 41 |
|
|
if ( b>=(byte) '0' && b<=(byte) '9' )
|
| 42 |
|
|
return b-(byte) '0';
|
| 43 |
|
|
if ( b>=(byte) 'a' && b<=(byte) 'f' )
|
| 44 |
|
|
return 10+b-(byte) 'a';
|
| 45 |
|
|
if ( b>=(byte) 'A' && b<=(byte) 'F' )
|
| 46 |
|
|
return 10+b-(byte) 'A';
|
| 47 |
|
|
if ( b == -1 )
|
| 48 |
|
|
throw new IhxParseException( "Inexpected end of file" );
|
| 49 |
|
|
throw new IhxParseException( "Hex digit expected: " + (char) b );
|
| 50 |
|
|
}
|
| 51 |
|
|
|
| 52 |
|
|
// ******* readHexByte *********************************************************
|
| 53 |
|
|
private static final int readHexByte(InputStream in) throws IOException, IhxParseException {
|
| 54 |
|
|
return (readHexDigit(in) << 4) | readHexDigit(in);
|
| 55 |
|
|
}
|
| 56 |
|
|
|
| 57 |
|
|
// ******* IhxFile *************************************************************
|
| 58 |
|
|
/**
|
| 59 |
|
|
* Constructs an instance from a given file name.
|
| 60 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
| 61 |
|
|
* @param fileName The file name.
|
| 62 |
|
|
* @throws IOException If an read error occurred.
|
| 63 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
| 64 |
|
|
*/
|
| 65 |
|
|
public IhxFile ( String fileName ) throws IOException, IhxFileDamagedException {
|
| 66 |
|
|
InputStream in = JInputStream.getInputStream( fileName );
|
| 67 |
|
|
int b, len, cs, addr;
|
| 68 |
|
|
byte buf[] = new byte[255];
|
| 69 |
|
|
boolean eof = false;
|
| 70 |
|
|
int line = 0;
|
| 71 |
|
|
|
| 72 |
|
|
for ( int i=0; i<ihxData.length; i++ )
|
| 73 |
|
|
ihxData[i] = -1;
|
| 74 |
|
|
|
| 75 |
|
|
try {
|
| 76 |
|
|
while ( ! eof ) {
|
| 77 |
|
|
do {
|
| 78 |
|
|
b = in.read();
|
| 79 |
|
|
if ( b<0 )
|
| 80 |
|
|
throw new IhxParseException( "Inexpected end of file" );
|
| 81 |
|
|
} while ( b != (byte) ':' );
|
| 82 |
|
|
|
| 83 |
|
|
line ++;
|
| 84 |
|
|
|
| 85 |
|
|
len = readHexByte(in); // length field
|
| 86 |
|
|
cs = len;
|
| 87 |
|
|
|
| 88 |
|
|
b = readHexByte(in); // address field
|
| 89 |
|
|
cs += b;
|
| 90 |
|
|
addr = b << 8;
|
| 91 |
|
|
b = readHexByte(in);
|
| 92 |
|
|
cs += b;
|
| 93 |
|
|
addr |= b;
|
| 94 |
|
|
|
| 95 |
|
|
b = readHexByte(in); // record type field
|
| 96 |
|
|
cs += b;
|
| 97 |
|
|
|
| 98 |
|
|
for ( int i=0; i<len; i++ ) { // data
|
| 99 |
|
|
buf[i] = (byte) readHexByte(in);
|
| 100 |
|
|
cs+=buf[i];
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
cs += readHexByte(in); // checksum
|
| 104 |
|
|
if ( (cs & 0xff) != 0 ) {
|
| 105 |
|
|
throw new IhxParseException( "Checksum error" );
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
if ( b == 0 ) { // data record
|
| 109 |
|
|
for (int i=0; i<len; i++ ) {
|
| 110 |
|
|
if ( ihxData[addr+i]>=0 ) System.err.println ( "Warning: Memory at position " + Integer.toHexString(i) + " overwritten" );
|
| 111 |
|
|
ihxData[addr+i] = (short) (buf[i] & 255);
|
| 112 |
|
|
}
|
| 113 |
|
|
}
|
| 114 |
|
|
else if (b == 1 ) { // eof record
|
| 115 |
|
|
eof = true;
|
| 116 |
|
|
}
|
| 117 |
|
|
else {
|
| 118 |
|
|
throw new IhxParseException( "Invalid record type: " + b );
|
| 119 |
|
|
}
|
| 120 |
|
|
}
|
| 121 |
|
|
}
|
| 122 |
|
|
catch ( IhxParseException e ) {
|
| 123 |
|
|
throw new IhxFileDamagedException ( fileName, line, e.getLocalizedMessage() );
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
try {
|
| 127 |
|
|
in.close();
|
| 128 |
|
|
}
|
| 129 |
|
|
catch ( Exception e ) {
|
| 130 |
|
|
System.err.println( "Warning: Error closing file " + fileName + ": " + e.getLocalizedMessage() );
|
| 131 |
|
|
}
|
| 132 |
|
|
}
|
| 133 |
|
|
|
| 134 |
|
|
// ******* dataInfo ************************************************************
|
| 135 |
|
|
/**
|
| 136 |
|
|
* Print out some information about the memory usage.
|
| 137 |
|
|
* @param out Where the data is printed out.
|
| 138 |
|
|
*/
|
| 139 |
|
|
public void dataInfo( PrintStream out ) {
|
| 140 |
|
|
int addr=-1;
|
| 141 |
|
|
for ( int i=0; i<=65536; i++ ) { // data
|
| 142 |
|
|
if ( (i==65536 || ihxData[i]<0) && addr>=0 ) {
|
| 143 |
|
|
System.out.println( i-addr + " Bytes from " + Integer.toHexString(addr) + " to " + Integer.toHexString(i-1) );
|
| 144 |
|
|
addr = -1;
|
| 145 |
|
|
}
|
| 146 |
|
|
if ( i<65536 && ihxData[i]>=0 && addr<0 )
|
| 147 |
|
|
addr = i;
|
| 148 |
|
|
}
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
}
|
| 152 |
|
|
|