OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_14/usb_fpga_2_14/trunk

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [java/] [ztex/] [ImgFile.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   Java host software API of ZTEX SDK
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
/*
27
    Reads a firmware image. .ihx and .img format is supported supported
28
*/
29
package ztex;
30
 
31
import java.io.*;
32
import java.util.*;
33
import java.net.*;
34
/**
35
  * A class representing a firmware image.
36
  */
37
public class ImgFile {
38
 
39
/**
40
  * This array stores the firmware image.
41
  * Values <0 and >255 mean that the data is undefined.
42
  */
43
    public short data[] = null;
44
 
45
/**
46
  * Start vector.
47
  * Values <0 mean that no start vector is defined.
48
  */
49
    public long startVector = -1;
50
 
51
/**
52
  * Assumed to be an FX3 firmware.
53
  * This value is also set by {@link ZtexImgFile1}.
54
  */
55
    public boolean isFx3 = false;
56
 
57
 
58
// ******* compressAddr ********************************************************
59
/**
60
  * Compresses the FX3 address space.
61
  * @param addr Uncompressed address
62
  * @param len Length of the data packet (used for address validity check).
63
  * @return Compressed address.
64
  * @throws ImgParseException If an invalid address occurs.
65
  */
66
    private static final int compressAddr( long addr, int len ) throws ImgParseException {
67
        int sector = (int) (addr >> 28);
68
        addr = addr & 0xfffffff;
69
        int addr_max = 64*1024;
70
        int addr_offs = 0;
71
        if ( sector >= 1 ) { addr_offs+=addr_max+1; addr_max=8*1024; }      // 64*1024+1
72
        if ( sector >= 4 ) { addr_offs+=addr_max+1; addr_max=512*1024; }    // 72*1024+2
73
        if ( sector >= 0xe ) { addr_offs+=addr_max+1; addr_max=256*1024; }  // 584*1024+3
74
        if ( sector >= 0xf ) { addr_offs+=addr_max+1; addr_max=32*1024; }   // 840*1024+4 .. 872*1024+3
75
        if ( addr<0 || addr+len>addr_max ) throw new ImgParseException( "Address out of range: " + Integer.toHexString((int)addr) );
76
        return (int)addr + addr_offs;
77
    }
78
 
79
// ******* uncompressAddr ******************************************************
80
/**
81
  * Uncompresses the FX3 address space.
82
  * @param addr Compressed address
83
  * @return Uncompressed address.
84
  */
85
    public static final long uncompressAddr( int addr ) {
86
        if ( addr >= 840*1024+4 ) return addr - (840*1024+4) + 0xf0000000;
87
        if ( addr >= 584*1024+3 ) return addr - (584*1024+3) + 0xe0000000;
88
        if ( addr >=  72*1024+2 ) return addr - ( 72*1024+2) + 0x40000000;
89
        if ( addr >=  64*1024+1 ) return addr - ( 64*1024+1) + 0x10000000;
90
        return addr;
91
    }
92
 
93
// ******* read ****************************************************************
94
    private static final int read( InputStream in ) throws IOException, ImgParseException {
95
        int b = in.read();
96
        if ( b<0 ) throw new ImgParseException( "Unexpected end of file" );
97
        return b;
98
    }
99
 
100
// ******* readHexDigit ********************************************************
101
    private static final int readHexDigit( InputStream in ) throws IOException, ImgParseException {
102
        int b = in.read();
103
        if ( b>=(byte) '0' && b<=(byte) '9' )
104
            return b-(byte) '0';
105
        if ( b>=(byte) 'a' && b<=(byte) 'f' )
106
            return 10+b-(byte) 'a';
107
        if ( b>=(byte) 'A' && b<=(byte) 'F' )
108
            return 10+b-(byte) 'A';
109
        if ( b == -1 )
110
            throw new ImgParseException( "Unexpected end of file" );
111
        throw new ImgParseException( "Hex digit expected: " + (char) b );
112
    }
113
 
114
// ******* readHexByte *********************************************************
115
    private static final int readHexByte(InputStream in) throws IOException, ImgParseException {
116
        return (readHexDigit(in) << 4) | readHexDigit(in);
117
    }
118
 
119
// ******* ImgFile *************************************************************
120
/**
121
  * Constructs an instance from a given file name.
122
  * This method can also read system resources, e.g. files from the current jar archive.
123
  * @param in Input stream from which the firmware file is read.
124
  * @param name Name of the input.
125
  * @throws IOException If an read error occurred.
126
  * @throws ImgFileDamagedException If the firmware file is damaged.
127
  */
128
    public ImgFile ( InputStream in, String name ) throws IOException, ImgFileDamagedException {
129
        int b, len, cs, addr;
130
        byte buf[] = new byte[256];
131
        boolean eof = false;
132
        int line = 0;
133
 
134
        boolean isImg = false;
135
 
136
        cs = 0;
137
        try {
138
            b = read(in);
139
            if ( b == (byte) 'C' ) {
140
                b = read(in);
141
                isFx3 = isImg = b == (byte) 'Y';
142
            }
143
 
144
            data = isImg ? new short[873*1024] : new short[64*1024];
145
 
146
            for ( int i=0; i<data.length; i++ )
147
                data[i] = -1;
148
 
149
            if ( isImg ) {                      // img file
150
                startVector = ( (read(in) & 1)==0 ) ? 0 : -1;
151
                if ( (b=read(in)) != 0xb0 ) throw new ImgParseException( "Invalid image type: " + Integer.toHexString(b) );
152
                while ( true ) {
153
                    len = read(in) | (read(in) << 8) | (read(in) << 16) | (read(in) << 24);
154
                    if ( len==0 ) break;
155
                    addr = compressAddr(read(in) | (read(in) << 8) | (read(in) << 16) | (read(in) << 24), len*4);
156
//                  System.out.println("ImgFile: " + len*4 + " bytes at 0x" + Integer.toHexString(addr));
157
                    for ( int i=0; i<len; i++ ) {
158
                        data[addr+i*4] = (short) read(in);
159
                        data[addr+i*4+1] = (short) read(in);
160
                        data[addr+i*4+2] = (short) read(in);
161
                        data[addr+i*4+3] = (short) read(in);
162
                        cs += data[addr+i*4] | (data[addr+i*4+1]<<8) | (data[addr+i*4+2]<<16) | (data[addr+i*4+3] << 24);
163
                    }
164
                }
165
                if ( startVector==0 ) {
166
                    startVector=read(in) | (read(in) << 8) | (read(in) << 16) | (read(in) << 24);
167
                }
168
                else {
169
                    System.err.println("Warning: No program entry defined");
170
                }
171
                b = read(in) | (read(in) << 8) | (read(in) << 16) | (read(in) << 24);
172
                if ( b != cs )
173
                    throw new ImgParseException( "Checksum error" );
174
            }
175
            else {                              // ihx file
176
                while ( ! eof ) {
177
                    while ( b != (byte) ':' ) {
178
                        b = read(in);
179
                    }
180
 
181
                    line ++;
182
 
183
                    len = readHexByte(in);              // length field 
184
                    cs = len;
185
 
186
                    b = readHexByte(in);                // address field 
187
                    cs += b;
188
                    addr = b << 8;
189
                    b = readHexByte(in);
190
                    cs += b;
191
                    addr |= b;
192
 
193
                    b = readHexByte(in);                // record type field
194
                    cs += b;
195
 
196
                    for ( int i=0; i<len; i++ ) {        // data
197
                        buf[i] = (byte) readHexByte(in);
198
                        cs+=buf[i];
199
                    }
200
 
201
                    cs += readHexByte(in);              // checksum
202
                    if ( (cs & 0xff) != 0 ) {
203
                        throw new ImgParseException( "Checksum error" );
204
                    }
205
 
206
                    if ( b == 0 ) {                      // data record
207
                        for (int i=0; i<len; i++ ) {
208
                            if ( data[addr+i]>=0 ) System.err.println ( "Warning: Memory at position " + Integer.toHexString(i) + " overwritten" );
209
                            data[addr+i] = (short) (buf[i] & 255);
210
                        }
211
                    }
212
                    else if (b == 1 ) {         // eof record
213
                        eof = true;
214
                    }
215
                    else {
216
                        throw new ImgParseException( "Invalid record type: " + b );
217
                    }
218
                }
219
            }
220
        }
221
        catch ( ImgParseException e ) {
222
            throw new ImgFileDamagedException ( name, line, e.getLocalizedMessage() );
223
        }
224
 
225
        try {
226
            in.close();
227
        }
228
        catch ( Exception e ) {
229
            System.err.println( "Warning: Error closing file " + name + ": " + e.getLocalizedMessage() );
230
        }
231
    }
232
 
233
/**
234
  * Constructs an instance from a given file name.
235
  * This method can also read system resources, e.g. files from the current jar archive.
236
  * @param fileName The file name.
237
  * @throws IOException If an read error occurred.
238
  * @throws ImgFileDamagedException If the firmware image file is damaged.
239
  */
240
    public ImgFile ( String fileName ) throws IOException, ImgFileDamagedException {
241
        this( JInputStream.getInputStream( fileName ), fileName );
242
    }
243
 
244
// ******* dataInfo ************************************************************
245
 /**
246
  * Print out some information about the memory usage.
247
  * @param out Where the data is printed out.
248
  */
249
   public void dataInfo( PrintStream out ) {
250
        int addr=-1;
251
        for ( int i=0; i<=data.length; i++ ) {   // data
252
            if ( (i==data.length || data[i]<0) && addr>=0 ) {
253
                System.out.println( i-addr + " Bytes from " + Integer.toHexString(addr) + " to " + Integer.toHexString(i-1) );
254
                addr = -1;
255
            }
256
            if ( i<data.length && data[i]>=0 && addr<0 )
257
                addr = i;
258
        }
259
    }
260
 
261
}
262
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.