root/trunk/libffado/support/firmware/bridgeco-downloader.cpp

Revision 864, 6.5 kB (checked in by ppalmers, 15 years ago)

update license to GPLv2 or GPLv3 instead of GPLv2 or any later version. Update copyrights to reflect the new year

Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "src/bebob/bebob_dl_mgr.h"
25 #include "src/bebob/bebob_dl_bcd.h"
26
27 #include "libieee1394/configrom.h"
28 #include "libieee1394/ieee1394service.h"
29
30 #include <argp.h>
31 #include <iostream>
32
33
34 ////////////////////////////////////////////////
35 // arg parsing
36 ////////////////////////////////////////////////
37 const char *argp_program_version = "bridgeco-downloader 0.1";
38 const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>";
39 static char doc[] = "bridgeco-downloader -- firmware downloader application for BridgeCo devices\n\n"
40                     "OPERATION: display\n"
41                     "           setguid GUID\n"
42                     "           firmware FILE\n"
43                     "           cne FILE\n"
44                     "           bcd FILE\n";
45 static char args_doc[] = "NODE_ID OPERATION";
46 static struct argp_option options[] = {
47     {"verbose",   'v', "level",     0,  "Produce verbose output" },
48     {"port",      'p', "PORT",      0,  "Set port" },
49     {"force",     'f', 0,           0,  "Force firmware download" },
50     {"noboot",    'b', 0,           0,  "Do no start bootloader (bootloader is already running)" },
51     { 0 }
52 };
53
54 struct arguments
55 {
56     arguments()
57         : verbose( 0 )
58         , port( 0 )
59         , force( 0 )
60         {
61             args[0] = 0;
62             args[1] = 0;
63             args[2] = 0;
64         }
65
66     char* args[3];
67     short verbose;
68     int   port;
69     int   force;
70     int   no_bootloader_restart;
71 } arguments;
72
73 // Parse a single option.
74 static error_t
75 parse_opt( int key, char* arg, struct argp_state* state )
76 {
77     // Get the input argument from `argp_parse', which we
78     // know is a pointer to our arguments structure.
79     struct arguments* arguments = ( struct arguments* ) state->input;
80
81     char* tail;
82     switch (key) {
83     case 'v':
84         if (arg) {
85             arguments->verbose = strtol( arg, &tail, 0 );
86             if ( errno ) {
87                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
88                 return ARGP_ERR_UNKNOWN;
89             }
90         }
91         break;
92     case 'p':
93         errno = 0;
94         arguments->port = strtol(arg, &tail, 0);
95         if (errno) {
96             perror("argument parsing failed:");
97             return errno;
98         }
99         break;
100     case 'f':
101         arguments->force = 1;
102         break;
103     case 'b':
104         arguments->no_bootloader_restart = 1;
105         break;
106     case ARGP_KEY_ARG:
107         if (state->arg_num >= 3) {
108             // Too many arguments.
109             argp_usage (state);
110         }
111         arguments->args[state->arg_num] = arg;
112         break;
113     case ARGP_KEY_END:
114         if (state->arg_num < 2) {
115             // Not enough arguments.
116             argp_usage (state);
117         }
118         break;
119     default:
120         return ARGP_ERR_UNKNOWN;
121     }
122     return 0;
123 }
124
125 static struct argp argp = { options, parse_opt, args_doc, doc };
126
127
128 int
129 main( int argc, char** argv )
130 {
131     using namespace std;
132
133     // arg parsing
134     argp_parse (&argp, argc, argv, 0, 0, &arguments);
135
136     errno = 0;
137     char* tail;
138     int node_id = strtol(arguments.args[0], &tail, 0);
139     if (errno) {
140     perror("argument parsing failed:");
141     return -1;
142     }
143
144     Ieee1394Service service;
145     if ( !service.initialize( arguments.port ) ) {
146         cerr << "Could not initialize IEEE 1394 service" << endl;
147         return -1;
148     }
149
150     service.setVerboseLevel( arguments.verbose );
151     BeBoB::BootloaderManager blMgr( service, node_id );
152     if ( arguments.force == 1 ) {
153         blMgr.setForceOperations( true );
154     }
155     blMgr.getConfigRom()->printConfigRom();
156     blMgr.printInfoRegisters();
157
158     if ( strcmp( arguments.args[1], "setguid" ) == 0 ) {
159         if (!arguments.args[2] ) {
160             cerr << "guid argument is missing" << endl;
161             return -1;
162         }
163
164         char* tail;
165         fb_octlet_t guid = strtoll(arguments.args[2], &tail, 0 );
166
167         if ( !blMgr.programGUID( guid ) ) {
168             cerr << "Failed to set GUID" << endl;
169             return -1;
170         } else {
171             cout << "new GUID programmed" << endl;
172         }
173     } else if ( strcmp( arguments.args[1], "firmware" ) == 0 ) {
174         if (!arguments.args[2] ) {
175             cerr << "FILE argument is missing" << endl;
176             return -1;
177         }
178         std::string str( arguments.args[2] );
179
180         blMgr.setStartBootloader( arguments.no_bootloader_restart != 1 );
181         if ( !blMgr.downloadFirmware( str ) ) {
182             cerr << "Failed to download firmware" << endl;
183             return -1;
184         } else {
185             cout << "Firmware download was successful" << endl;
186         }
187     } else if ( strcmp( arguments.args[1], "cne" ) == 0 ) {
188         if (!arguments.args[2] ) {
189             cerr << "FILE argument is missing" << endl;
190             return -1;
191         }
192         std::string str( arguments.args[2] );
193
194         if ( !blMgr.downloadCnE( str ) ) {
195             cerr << "Failed to download CnE" << endl;
196             return -1;
197         } else {
198             cout << "CnE download was successful" << endl;
199         }
200     } else if ( strcmp( arguments.args[1], "display" ) == 0 ) {
201         // nothing to do
202     } else if ( strcmp( arguments.args[1], "bcd" ) == 0 ) {
203         if ( !arguments.args[2] ) {
204             cerr << "FILE arguments is missing" << endl;
205             return -1;
206         }
207         BeBoB::BCD* bcd = new BeBoB::BCD::BCD( arguments.args[2] );
208         if ( !bcd ) {
209             cerr << "Could no open file " << arguments.args[2] << endl;
210             return -1;
211         }
212         if ( !bcd->parse() ) {
213             cerr << "Could not parse file " << arguments.args[2] << endl;
214             return -1;
215         }
216
217         bcd->displayInfo();
218         delete bcd;
219     } else {
220         cout << "Unknown operation" << endl;
221     }
222
223     return 0;
224 }
Note: See TracBrowser for help on using the browser.