root/branches/streaming-rework/src/freebob-downloader.cpp

Revision 413, 6.4 kB (checked in by pieterpalmers, 16 years ago)

- moved all generic IEEE1394 classes into libieee1394

src/libieee1394/ieee1394service.h
src/libieee1394/csr1212.h
src/libieee1394/configrom.cpp
src/libieee1394/configrom.h
src/libieee1394/ieee1394service.cpp
src/libieee1394/csr1212.c

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