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 an firmware image file with ZTEX descriptor 1
|
28 |
|
|
*/
|
29 |
|
|
package ztex;
|
30 |
|
|
|
31 |
|
|
import java.io.*;
|
32 |
|
|
import java.util.*;
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* Represents a firmware image with ZTEX descriptor 1. <br>
|
36 |
|
|
* The ZTEX descriptor is usually located at the position 0x6x of the FX2 firmware image.
|
37 |
|
|
* In FX3 firmwares the descriptor is located by searching for the signature. <br>
|
38 |
|
|
* A description of the ZTEX descriptor 1 can be found in {@link ZtexDevice1}.
|
39 |
|
|
* @see ZtexDevice1
|
40 |
|
|
* @see Ztex1
|
41 |
|
|
*/
|
42 |
|
|
public class ZtexImgFile1 extends ImgFile {
|
43 |
|
|
private static final int defaultZtexDescriptorOffs = 0x6c;
|
44 |
|
|
|
45 |
|
|
private int ztexDescriptorOffs = defaultZtexDescriptorOffs;
|
46 |
|
|
|
47 |
|
|
private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descriptor, not the USB product ID
|
48 |
|
|
private byte fwVersion = 0;
|
49 |
|
|
private byte interfaceVersion = 0;
|
50 |
|
|
private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
|
51 |
|
|
private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
|
52 |
|
|
private char snString[] = new char[10];
|
53 |
|
|
|
54 |
|
|
// ******* ZtexImgFile1 ********************************************************
|
55 |
|
|
/**
|
56 |
|
|
* Constructs an instance from a given file name and descriptor position.<br>
|
57 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
58 |
|
|
* @param in Input stream from which the img file is read.
|
59 |
|
|
* @param name Name of the input.
|
60 |
|
|
* @throws IOException If an read error occurred.
|
61 |
|
|
* @throws ImgFileDamagedException If the firmware file is damaged.
|
62 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
63 |
|
|
*/
|
64 |
|
|
public ZtexImgFile1( InputStream in, String name ) throws IOException, ImgFileDamagedException, IncompatibleFirmwareException {
|
65 |
|
|
super( in, name );
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
ztexDescriptorOffs = -1;
|
69 |
|
|
for (int i=0; i<data.length-40; i++) {
|
70 |
|
|
if ( data[i]==40 && data[i+1]==1 && data[i+2]=='Z' && data[i+3]=='T' && data[i+4]=='E' && data[i+5]=='X'
|
71 |
|
|
&& ( (i==defaultZtexDescriptorOffs) || ((data[i+29])==207) )
|
72 |
|
|
)
|
73 |
|
|
ztexDescriptorOffs = i;
|
74 |
|
|
}
|
75 |
|
|
if ( ztexDescriptorOffs < 0 ) throw new IncompatibleFirmwareException( "No valid ZTEX descriptor found" );
|
76 |
|
|
|
77 |
|
|
/* for (int i=0; i<40; i++)
|
78 |
|
|
System.out.println(i + ": " + data[ztexDescriptorOffs+i] + " " + (char)data[ztexDescriptorOffs+i] );*/
|
79 |
|
|
|
80 |
|
|
productId[0] = (byte) data[ztexDescriptorOffs+6];
|
81 |
|
|
productId[1] = (byte) data[ztexDescriptorOffs+7];
|
82 |
|
|
productId[2] = (byte) data[ztexDescriptorOffs+8];
|
83 |
|
|
productId[3] = (byte) data[ztexDescriptorOffs+9];
|
84 |
|
|
fwVersion = (byte) data[ztexDescriptorOffs+10];
|
85 |
|
|
interfaceVersion = (byte) data[ztexDescriptorOffs+11];
|
86 |
|
|
interfaceCapabilities[0] = (byte) data[ztexDescriptorOffs+12];
|
87 |
|
|
interfaceCapabilities[1] = (byte) data[ztexDescriptorOffs+13];
|
88 |
|
|
interfaceCapabilities[2] = (byte) data[ztexDescriptorOffs+14];
|
89 |
|
|
interfaceCapabilities[3] = (byte) data[ztexDescriptorOffs+15];
|
90 |
|
|
interfaceCapabilities[4] = (byte) data[ztexDescriptorOffs+16];
|
91 |
|
|
interfaceCapabilities[5] = (byte) data[ztexDescriptorOffs+17];
|
92 |
|
|
moduleReserved[0] = (byte) data[ztexDescriptorOffs+18];
|
93 |
|
|
moduleReserved[1] = (byte) data[ztexDescriptorOffs+19];
|
94 |
|
|
moduleReserved[2] = (byte) data[ztexDescriptorOffs+20];
|
95 |
|
|
moduleReserved[3] = (byte) data[ztexDescriptorOffs+21];
|
96 |
|
|
moduleReserved[4] = (byte) data[ztexDescriptorOffs+22];
|
97 |
|
|
moduleReserved[5] = (byte) data[ztexDescriptorOffs+23];
|
98 |
|
|
moduleReserved[6] = (byte) data[ztexDescriptorOffs+24];
|
99 |
|
|
moduleReserved[7] = (byte) data[ztexDescriptorOffs+25];
|
100 |
|
|
moduleReserved[8] = (byte) data[ztexDescriptorOffs+26];
|
101 |
|
|
moduleReserved[9] = (byte) data[ztexDescriptorOffs+27];
|
102 |
|
|
moduleReserved[10] = (byte) data[ztexDescriptorOffs+28];
|
103 |
|
|
moduleReserved[11] = (byte) data[ztexDescriptorOffs+29];
|
104 |
|
|
|
105 |
|
|
isFx3 = (interfaceCapabilities[1] & 4) != 0;
|
106 |
|
|
|
107 |
|
|
for (int i=0; i<10; i++ ) {
|
108 |
|
|
int b = data[ztexDescriptorOffs+30+i];
|
109 |
|
|
if ( b>=0 && b<=255 ) {
|
110 |
|
|
snString[i] = (char) b;
|
111 |
|
|
}
|
112 |
|
|
else {
|
113 |
|
|
throw new IncompatibleFirmwareException( "Invalid serial number string" );
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
// ensure word aligned upload data
|
118 |
|
|
for ( int i=0; i+1<data.length; i+=2 )
|
119 |
|
|
if ( data[i]<0 && data[i+1]>=0 )
|
120 |
|
|
data[i] = 0;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
/**
|
124 |
|
|
* Constructs an instance from a given file name.
|
125 |
|
|
* The ZTEX descriptor 1 is expected to be at the position 0x6c of the firmware image.<br>
|
126 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
127 |
|
|
* @param fileName The file name.
|
128 |
|
|
* @throws IOException If an read error occurred.
|
129 |
|
|
* @throws ImgFileDamagedException If the firmware file is damaged.
|
130 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
131 |
|
|
*/
|
132 |
|
|
public ZtexImgFile1( String fileName ) throws IOException, ImgFileDamagedException, IncompatibleFirmwareException {
|
133 |
|
|
this( JInputStream.getInputStream(fileName), fileName);
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
// ******* productId ***********************************************************
|
137 |
|
|
/**
|
138 |
|
|
* Returns the product ID (all 4 bytes).
|
139 |
|
|
* @return PRODUCT_ID, see {@link ZtexDevice1}.
|
140 |
|
|
*/
|
141 |
|
|
public final byte[] productId() {
|
142 |
|
|
return productId;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* Returns byte i of the product ID.
|
147 |
|
|
* @return PRODUCT_ID[i], see {@link ZtexDevice1}.
|
148 |
|
|
* @param i index
|
149 |
|
|
*/
|
150 |
|
|
public int productId( int i ) {
|
151 |
|
|
return productId[i] & 255;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
// ******* fwVersion ***********************************************************
|
155 |
|
|
/**
|
156 |
|
|
* Returns the firmware version.
|
157 |
|
|
* @return FW_VERSION, see {@link ZtexDevice1}.
|
158 |
|
|
*/
|
159 |
|
|
public final int fwVersion() {
|
160 |
|
|
return fwVersion & 255;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
// ******* interfaceVersion *****************************************************
|
164 |
|
|
/**
|
165 |
|
|
* Returns the interface version.
|
166 |
|
|
* @return INTERFACE_VERSION, see {@link ZtexDevice1}.
|
167 |
|
|
*/
|
168 |
|
|
public final int interfaceVersion() {
|
169 |
|
|
return interfaceVersion & 255;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
// ******* interfaceCapabilities ************************************************
|
173 |
|
|
/**
|
174 |
|
|
* Returns the interface capabilities (all 6 bytes).
|
175 |
|
|
* @return INTERFACE_CAPABILITIES, see {@link ZtexDevice1}.
|
176 |
|
|
*/
|
177 |
|
|
public final byte[] interfaceCapabilities() {
|
178 |
|
|
return interfaceCapabilities;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
/**
|
182 |
|
|
* Returns byte i of the interface capabilities.
|
183 |
|
|
* @return INTERFACE_CAPABILITIES[i], see {@link ZtexDevice1}.
|
184 |
|
|
* @param i index
|
185 |
|
|
*/
|
186 |
|
|
public final int interfaceCapabilities( int i ) {
|
187 |
|
|
return interfaceCapabilities[i] & 255;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// ******* moduleReserved ******************************************************
|
191 |
|
|
/**
|
192 |
|
|
* Returns the application specific information (all 12 bytes).
|
193 |
|
|
* @return MODULE_RESERVED, see {@link ZtexDevice1}.
|
194 |
|
|
*/
|
195 |
|
|
public final byte[] moduleReserved() {
|
196 |
|
|
return moduleReserved;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
/**
|
200 |
|
|
* Returns byte i of the application specific information.
|
201 |
|
|
* @return MODULE_RESERVED[i], see {@link ZtexDevice1}.
|
202 |
|
|
* @param i index
|
203 |
|
|
*/
|
204 |
|
|
public final int moduleReserved( int i ) {
|
205 |
|
|
return moduleReserved[i] & 255;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
// ******* snString ************************************************************
|
209 |
|
|
/**
|
210 |
|
|
* Returns the serial number string.
|
211 |
|
|
* @return SN_STRING, see {@link ZtexDevice1}.
|
212 |
|
|
*/
|
213 |
|
|
public final String snString() {
|
214 |
|
|
return new String( snString );
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
// ******* setSnString **********************************************************
|
218 |
|
|
/**
|
219 |
|
|
* Modifies the serial number string.
|
220 |
|
|
* @param s The new serial number string which must not be longer then 10 characters.
|
221 |
|
|
*/
|
222 |
|
|
public final void setSnString( String s ) throws IncompatibleFirmwareException {
|
223 |
|
|
if ( s.length()>10 )
|
224 |
|
|
throw new IncompatibleFirmwareException( "Serial number too long (max. 10 characters)" );
|
225 |
|
|
|
226 |
|
|
int i=0;
|
227 |
|
|
for (; i<s.length(); i++ ) {
|
228 |
|
|
data[ztexDescriptorOffs+30+i] = (byte) s.charAt(i);
|
229 |
|
|
}
|
230 |
|
|
for (; i<10; i++ ) {
|
231 |
|
|
data[ztexDescriptorOffs+30+i] = 0;
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
// ******* toString ************************************************************
|
236 |
|
|
/**
|
237 |
|
|
* Returns a string representation if the instance.
|
238 |
|
|
* @return a string representation if the instance.
|
239 |
|
|
*/
|
240 |
|
|
public String toString () {
|
241 |
|
|
return "productID=" + ZtexDevice1.byteArrayString(productId) + " fwVer="+(fwVersion & 255) + " ifVer="+(interfaceVersion & 255)+ " snString=\"" + snString() + "\"";
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
}
|