-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheflash.c
474 lines (435 loc) · 14.3 KB
/
eflash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//*****************************************************************************
//
// eflash.c - This file holds the main routine for downloading an image to a
// Stellaris Device via Ethernet.
//
// Copyright (c) 2009-2013 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 10636 of the Stellaris Firmware Development Package.
//
//*****************************************************************************
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>
#include <ifaddrs.h>
#include "eflash.h"
#include "bootp_server.h"
//*****************************************************************************
//
// The version of the application.
//
//*****************************************************************************
const unsigned short g_usApplicationVersion = 10636;
//*****************************************************************************
//
// Program strings used in various display routines.
//
//*****************************************************************************
static const char g_pcProgramName[] =
"EFLASH Ethernet Boot Loader Download Utility";
static const char g_pcProgramCopyright[] =
"Copyright (c) 2009-2013 Texas Instruments Incorporated. All rights reserved.";
static const char g_pcProgramHelp[] =
"usage: eflash [options] file\n"
"\n"
"Download a file to a remote device, using the Ethernet Boot Loader.\n"
"The file should be a binary image, and the IP and MAC address of the\n"
"target device must be specified.\n"
"Example: eflash -i 169.254.19.63 --mac=00.1a.b6.00.12.04 enet_lwip.bin\n"
"\n"
"Required options:\n"
" -i addr, --ip=addr IP address of remote device to program,\n"
" in dotted-decimal notation\n"
" (e.g. 169.254.19.63)\n"
" -l addr, --myip=addr IP address of this local host,\n"
" in dotted-decimal notation\n"
" (e.g. 169.254.19.63)\n"
" -m addr, --mac=addr MAC address of remote device to program,\n"
" specified as a series of hexadecimal numbers\n"
" delimited with '-', ':', or '.'.\n"
" (e.g. 00.1a.b6.00.12.04)\n"
" file binary file to be downloaded to the remote device.\n"
" (e.g. enet_lwip.bin)\n"
"\n"
"Output control:\n"
" --quiet, --silent suppress all normal output\n"
" --verbose display additional status information\n"
" --debug display additional diagnostic information\n"
"\n"
"Miscellaneous:\n"
" --version display program version information, then exit\n"
" --help display this help text, then exit\n"
"\n"
"Support Information:\n"
"Report any bugs to <[email protected]>\n";
//*****************************************************************************
//
// Command line option to set "printf" output display level.
//
//*****************************************************************************
int g_iOptVerbose = 1;
//*****************************************************************************
//
// MAC address of target (remote) device.
//
//*****************************************************************************
unsigned char g_pucRemoteMAC[6] = {0, 0, 0, 0, 0, 0};
//*****************************************************************************
//
// IP address of target (remote) device.
//
//*****************************************************************************
unsigned long g_ulRemoteAddress = 0;
unsigned long g_ulLocalAddress = 0;
//*****************************************************************************
//
// File name to download to target (remote) device.
//
//*****************************************************************************
char *g_pcFileName = NULL;
//*****************************************************************************
//
// A flag that is true if the application should terminate.
//
//*****************************************************************************
static unsigned long g_bAbortMain = 0;
//*****************************************************************************
//
// This function will convert a delimited address string into an array of
// unsigned bytes. The numbers in the address string can be delimited by
// ".", "-", or ":". The value for each "token" in the string will be stored
// into subsequent elements in the array. The function will return the total
// number of tokens converted.
//
//*****************************************************************************
static int
AddressToBytes(char *pcString, void *pValue, int iCount, int iBase)
{
int iConverted = 0;
char *token = NULL;
char *delimit = ".-:";
char *tail = NULL;
//
// Extract the first token from the string to get the loop started. Then,
// For each token (up to iCount), convert it and find the next token in
// the string. Exit the loop when iCount has been reached, or when there
// are no more tokens to convert.
//
token = strtok(pcString, delimit);
while((iConverted < iCount) && (NULL != token))
{
//
// Convert the token into a number. If the conversion fails, the
// input value of "tail" will match "token", and that means that
// the input string has been formatted incorrectly, so break out of
// the process loop and simply return the number of bytes that have
// been converted thus far.
//
((unsigned char *)pValue)[iConverted] =
(strtoul(token, &tail, iBase) & 0xFF);
if(tail == token)
{
break;
}
//
// Get the next token and setup for the next iteration in the loop.
//
token = strtok(NULL, delimit);
iConverted++;
}
//
// Return the number
return(iConverted);
}
//******************************************************************************
//
// This function will display help text in conformance with the GNU coding
// standard.
//
//******************************************************************************
static void
DisplayHelp(void)
{
puts(g_pcProgramHelp);
}
//******************************************************************************
//
// This function will display version information in conformance with the GNU
// coding standard.
//
//******************************************************************************
static void
DisplayVersion(void)
{
printf("%s (Version: %u)\n", g_pcProgramName, g_usApplicationVersion);
printf("\n");
printf("%s\n", g_pcProgramCopyright);
printf("\n");
}
//******************************************************************************
//
// This function will parse the command line arguments, storing any needed
// information in the appropriate variables for later reference. The
// "getopts" command line processing library functions are used to parse
// the command line options. The options are defined in accordance with
// the GNU coding standard.
//
//******************************************************************************
static void
ParseOptions(int argc, char **argv)
{
struct option sLongOptions[] =
{
//
// GNU Standard Options that set a flag for program operation.
//
{"quiet", no_argument, &g_iOptVerbose, 0},
{"silent", no_argument, &g_iOptVerbose, 0},
{"verbose", no_argument, &g_iOptVerbose, 2},
//
// GNU Standard options that simply display information and exit.
//
{"help", no_argument, 0, 0x100},
{"version", no_argument, 0, 0x101},
//
// Program specific options that set variables and/or require arguments.
//
{"mac", required_argument, 0, 'm'},
{"ip", required_argument, 0, 'i'},
{"myip", required_argument, 0, 'l'},
//
// Terminating Element of the array
//
{0, 0, 0, 0}
};
int iOptionIndex = 0;
int iOption;
int iReturnCode;
//
// Continue parsing options till there are no more to parse.
// Note: The "m:i" allows the short and long options to be
// used for the file, mac and ip parameters and will be processed
// below by the same case statement.
//
while((iOption = getopt_long(argc, argv, "m:i:l:", sLongOptions,
&iOptionIndex)) != -1)
{
//
// Process the current option.
//
switch(iOption)
{
//
// Long option with flag set.
//
case 0:
break;
//
// --help
//
case 0x100:
DisplayHelp();
exit(0);
break;
//
// --version
//
case 0x101:
DisplayVersion();
exit(0);
break;
//
// --mac=string, -m string
//
case 'm':
iReturnCode = AddressToBytes(optarg, g_pucRemoteMAC, 6, 16);
if(iReturnCode != 6)
{
EPRINTF(("Error Processing MAC (%d)\n", iReturnCode));
DisplayHelp();
exit(-(__LINE__));
}
break;
//
// --ip=string, -i string
//
case 'i':
iReturnCode = AddressToBytes(optarg, &g_ulRemoteAddress, 4, 10);
if(iReturnCode != 4)
{
EPRINTF(("Error Processing Remote IP (%d)\n", iReturnCode));
DisplayHelp();
exit(-(__LINE__));
}
break;
//
// --myip=string, -l string
//
case 'l':
iReturnCode = AddressToBytes(optarg, &g_ulLocalAddress, 4, 10);
if(iReturnCode != 4)
{
EPRINTF(("Error Processing Local IP (%d)\n", iReturnCode));
DisplayHelp();
exit(-(__LINE__));
}
break;
//
// Unrecognized option.
//
default:
DisplayVersion();
DisplayHelp();
exit(-(__LINE__));
break;
}
}
//
// Extract filename from the last argument on the command line (if
// provided).
//
if(optind == argc)
{
EPRINTF(("No File Name Specified\n"));
DisplayHelp();
exit(-(__LINE__));
}
else if(optind > (argc -1))
{
EPRINTF(("Too Many Command Line Options\n"));
DisplayHelp();
exit(-(__LINE__));
}
else
{
g_pcFileName = argv[optind];
}
//
// Check for non-zero MAC address.
//
if((0 == g_pucRemoteMAC[0]) && (0 == g_pucRemoteMAC[1]) &&
(0 == g_pucRemoteMAC[2]) && (0 == g_pucRemoteMAC[3]) &&
(0 == g_pucRemoteMAC[4]) && (0 == g_pucRemoteMAC[5]))
{
EPRINTF(("No MAC Address Specified\n"));
DisplayHelp();
exit(-(__LINE__));
}
//
// Check for non-zero IP address.
//
if(0 == g_ulRemoteAddress)
{
EPRINTF(("No IP Address Specified\n"));
DisplayHelp();
exit(-(__LINE__));
}
}
//*****************************************************************************
//
// A callback function to monitor the progress.
//
//*****************************************************************************
static void
StatusCallback(unsigned long ulPercent)
{
//
// Print out the percentage.
//
if(g_iOptVerbose == 1)
{
printf("%% Complete: %3lu%%\r", ulPercent);
}
else if(g_iOptVerbose > 1)
{
printf("%% Complete: %3lu%%\n", ulPercent);
}
}
//*****************************************************************************
//
// A callback function to monitor the progress.
//
//*****************************************************************************
static void
SignalIntHandler(int iSignal)
{
//
// Display a diagnostic message.
//
fprintf(stderr, "Abort Received (%d)... cleaning up\n", iSignal);
//
// Abort the BOOTP process (if already running).
//
AbortBOOTPUpdate();
//
// Flag main to abort any processes that are running.
//
g_bAbortMain = 1;
}
//*****************************************************************************
//
// Main entry. Process command line options, and start the bootp_server.
//
//*****************************************************************************
int
main(int argc, char **argv)
{
//
// Parse the command line options.
//
if(argc > 1)
{
ParseOptions(argc, argv);
}
else
{
DisplayVersion();
DisplayHelp();
return(0);
}
//
// Display (if needed) verbose function entry.
//
if(g_iOptVerbose > 1)
{
DisplayVersion();
}
//
// Install an abort handler.
//
signal(SIGINT, SignalIntHandler);
//
// Start the BOOTP/TFTP server to perform an update.
//
QPRINTF(("Starting BOOTP/TFTP Server ...\n"));
StatusCallback(0);
StartBOOTPUpdate(g_pucRemoteMAC, g_ulLocalAddress, g_ulRemoteAddress,
g_pcFileName, StatusCallback);
//
// Clean up and return.
//
if(g_bAbortMain)
{
return(2);
}
return(0);
}