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/] [default/] [DefaultUpdater/] [DefaultUpdater.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   Utility for automatic updating default firmware
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   Licensed under the Apache License, Version 2.0 (the "License");
7
   you may not use this file except in compliance with the License.
8
   You may obtain a copy of the License at
9
 
10
       http://www.apache.org/licenses/LICENSE-2.0
11
 
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
%*/
18
 
19
import java.io.*;
20
import java.util.*;
21
import java.nio.*;
22
 
23
import org.usb4java.*;
24
 
25
import ztex.*;
26
 
27
// *****************************************************************************
28
// ******* ParameterException **************************************************
29
// *****************************************************************************
30
// Exception the prints a help message
31
class ParameterException extends Exception {
32
    public final static String helpMsg = new String (
33
                "Update default firmware in non-volatile memory.\n\n"+
34
                "Parameters:\n"+
35
                "    -d <number>  Device Number (default: 0)\n" +
36
                "    -p           Print bus info\n" +
37
                "    -i           Print firmware information only\n"+
38
                "    -u           Update only\n" +
39
                "    -f           Overwrite firmware in any case\n" +
40
                "    -h           This help\n\n" +
41
                "If neither -u nor -f is given and a non-default firmware if found overwriting must be confirmed by user.");
42
 
43
    public ParameterException (String msg) {
44
        super( msg + "\n" + helpMsg );
45
    }
46
}
47
 
48
// *****************************************************************************
49
// ******* DefaultUpdater ******************************************************
50
// *****************************************************************************
51
class DefaultUpdater {
52
 
53
// ******* main ****************************************************************
54
    public static void main (String args[]) {
55
 
56
        int devNum = 0;
57
        boolean force = false;
58
        boolean info = false;
59
        boolean updateonly = false;
60
        boolean ask = false;
61
 
62
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
63
            Runtime.getRuntime().addShutdownHook(new Thread() {
64
                public void run() {
65
                    Scanner s=new Scanner(System.in);
66
                    System.out.println("Press <enter> to continue ...");
67
                    s.nextLine();
68
                }
69
            });
70
        }
71
 
72
        try {
73
// Scan the USB. This also creates and initializes a new USB context.
74
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
75
            if ( bus.numberOfDevices() <= 0) {
76
                System.err.println("No devices found");
77
                System.exit(0);
78
            }
79
 
80
// scan the command line arguments
81
            for (int i=0; i<args.length; i++ ) {
82
                if ( args[i].equals("-d") ) {
83
                    i++;
84
                    try {
85
                        if (i>=args.length) throw new Exception();
86
                        devNum = Integer.parseInt( args[i] );
87
                    }
88
                    catch (Exception e) {
89
                        throw new ParameterException("Device number expected after -d");
90
                    }
91
                }
92
                else if ( args[i].equals("-p") ) {
93
                    bus.printBus(System.out);
94
                    System.exit(0);
95
                }
96
                else if ( args[i].equals("-i") ) {
97
                    info = true;
98
                }
99
                else if ( args[i].equals("-u") ) {
100
                    updateonly = true;
101
                }
102
                else if ( args[i].equals("-f") ) {
103
                    force = true;
104
                }
105
                else if ( args[i].equals("-h") ) {
106
                    System.err.println(ParameterException.helpMsg);
107
                    System.exit(0);
108
                }
109
                else if ( !args[i].equals("-re") && !args[i].equals("-ue") )
110
                    throw new ParameterException("Invalid Parameter: "+args[i]);
111
            }
112
            if ( updateonly && force ) throw new ParameterException("Parameters -u and -f must not be specified at the same time");
113
 
114
// create the main class            
115
            Ztex1v1 ztex = new Ztex1v1 ( bus.device(devNum) );
116
            bus.unref();
117
            ConfigData config = ztex.config;
118
            ztex.defaultDisableWarnings = true;
119
 
120
// evaluate current firmware(s)
121
            System.out.println("Reset EZ-USB in order to load and evaluate firmware installed in non-volatile memory");
122
            ztex.resetEzUsb();
123
 
124
            if ( ztex.config != null ) config = ztex.config;
125
            if ( config == null ) throw new Exception("No configuration data, can't evaluate type of FPGA Board");
126
            byte majorVersion = config.getMajorVersion();
127
            byte minorVersion = config.getMinorVersion();
128
            String defaultFN = "../usb-fpga-" + majorVersion + "." + ( minorVersion / 10) + ( minorVersion % 10) + "/default." + ( ztex.dev().fx3() ? "img" : "ihx" );
129
            System.out.println("Found " + config.getName()+",  using firmware image `"+defaultFN+"'");
130
 
131
            if ( !ztex.valid() ) {
132
                System.out.println("No Firmware installed: Installing the latest default firmware");
133
                force = true;
134
            } else if ( ! ztex.InterfaceCapabilities(ztex.CAPABILITY_DEFAULT ) ) {
135
                System.out.println("Non-default firmware (firmware without default interface) installed");
136
                ask = !updateonly && !force;
137
            } else {
138
                ztex.defaultVersion();
139
                boolean b = (ztex.defaultVersion() < ztex.defaultLatestVersion) || ((ztex.defaultVersion()==ztex.defaultLatestVersion) && (ztex.defaultSubVersion()<ztex.defaultLatestSubVersion));
140
                System.out.println("Default Firmware version " + ztex.defaultVersion() + "." + ztex.defaultSubVersion() + " found, latest version: " + ztex.defaultLatestVersion + "." + ztex.defaultLatestSubVersion +
141
                    ( b ? ": update recommended" : ": no update required" ) );
142
                force = force || b;
143
            }
144
 
145
            if ( info ) {
146
                ztex.dispose();
147
                System.exit(0);
148
            }
149
 
150
            if ( !(new File(defaultFN)).exists() ) throw new Exception("Firmware image does not exist: "+defaultFN);
151
 
152
            if ( ask ) {
153
                BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
154
                System.out.print("Overwriting current firmware (y|n) ? ");
155
                String str = reader.readLine();
156
                force = str.equals("y") || str.equals("Y");
157
            }
158
 
159
            if ( ! force ) {
160
                ztex.dispose();
161
                System.exit(0);
162
            }
163
 
164
// upload the firmware
165
            System.out.println("Firmware upload time: " + ztex.uploadFirmware( defaultFN, force ) + " ms");
166
 
167
            System.out.println("Firmware to NV memory upload time: " + ztex.nvUploadFirmware( defaultFN, force ) + " ms");
168
 
169
// release resources
170
            ztex.dispose();
171
        }
172
        catch (Exception e) {
173
            System.out.println("Error: "+e.getLocalizedMessage() );
174
        }
175
        catch (Error e) {
176
            System.out.println("Error: "+e.getLocalizedMessage() );
177
        }
178
    }
179
 
180
}
181
 

powered by: WebSVN 2.1.0

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