summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/ui/ui_loadcmds.c
blob: a1df159f521c75d2c5dd2b6287a9474f9947c2b5 (plain)
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
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Program Loader commands			File: ui_loadcmds.c
    *  
    *  User interface for program loader
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_string.h"
#include "lib_queue.h"
#include "lib_malloc.h"
#include "lib_printf.h"

#include "cfe_iocb.h"
#include "cfe_device.h"
#include "cfe_console.h"
#include "cfe_devfuncs.h"

#include "ui_command.h"
#include "cfe.h"

#include "net_ebuf.h"
#include "net_ether.h"
#include "net_api.h"

#include "cfe_fileops.h"
#include "cfe_boot.h"

#include "bsp_config.h"
#include "cfe_loader.h"
#include "cfe_autoboot.h"

#include "url.h"


int ui_init_loadcmds(void);
static int ui_cmd_load(ui_cmdline_t *cmd,int argc,char *argv[]);

#if CFG_NETWORK
static int ui_cmd_save(ui_cmdline_t *cmd,int argc,char *argv[]);
#endif

static int ui_cmd_autoboot(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_boot(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_batch(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_go(ui_cmdline_t *cmd,int argc,char *argv[]);


extern cfe_loadargs_t cfe_loadargs;

static long getaddr(char *str)
{
    /* 
     * hold on to your lunch, this is really, really bad! 
     * Make 64-bit addresses expressed as 8-digit numbers
     * sign extend automagically.  Saves typing, but is very
     * gross.  Not very portable, either.
     */
    int longaddr = 0;
    long newaddr;

    longaddr = strlen(str);
    if (memcmp(str,"0x",2) == 0) longaddr -= 2;
    longaddr = (longaddr > 8) ? 1 : 0;

    if (longaddr) newaddr = (long) xtoq(str);
    else newaddr = (long) xtoi(str);

    return newaddr;
}



int ui_init_loadcmds(void)
{	

#if CFG_NETWORK
    cmd_addcmd("save",
	       ui_cmd_save,
	       NULL,
	       "Save a region of memory to a remote file via TFTP",
	       "save [-options] host:filename startaddr length\n\n",
	       "");
#endif

    cmd_addcmd("load",
	       ui_cmd_load,
	       NULL,
	       "Load an executable file into memory without executing it",
	       "load [-options] host:filename|dev:filename\n\n"
	       "This command loads an executable file into memory, but does not\n"
	       "execute it.  It can be used for loading data files, overlays or\n"
	       "other programs needed before the 'boot' command is used.  By\n"
	       "default, 'load' will load a raw binary at virtual address 0x20000000.",
	       "-elf;Load the file as an ELF executable|"
	       "-srec;Load the file as ASCII S-records|"
	       "-raw;Load the file as a raw binary|"
#if CFG_ZLIB
	       "-z;Load compessed file|"
#endif
	       "-loader=*;Specify CFE loader name|"
	       "-tftp;Load the file using the TFTP protocol|"
	       "-fatfs;Load the file from a FAT file system|"
	       "-rawfs;Load the file from an unformatted file system|"
#if (CFG_TCP) && (CFG_HTTPFS)
	       "-http;Load the file using the HTTP protocol|"
#endif
               "-fs=*;Specify CFE file system name|"
	       "-max=*;Specify the maximum number of bytes to load (raw only)|"
	       "-addr=*;Specify the load address (hex) (raw only)");

    cmd_addcmd("boot",
	       ui_cmd_boot,
	       NULL,
	       "Load an executable file into memory and execute it",
	       "boot [-options] host:filename|dev:filename\n\n"
	       "This command loads and executes a program from a boot device\n"
	       "By default, 'boot' will load a raw binary at virtual \n"
	       "address 0x20000000 and then jump to that address",
	       "-elf;Load the file as an ELF executable|"
	       "-srec;Load the file as ASCII S-records|"
	       "-raw;Load the file as a raw binary|"
#if CFG_ZLIB
	       "-z;Load compessed file|"
#endif
	       "-loader=*;Specify CFE loader name|"
	       "-tftp;Load the file using the TFTP protocol|"
	       "-fatfs;Load the file from a FAT file system|"
	       "-rawfs;Load the file from an unformatted file system|"
#if (CFG_TCP) && (CFG_HTTPFS)
	       "-http;Load the file using the HTTP protocol|"
#endif
               "-fs=*;Specify CFE file system name|"
	       "-max=*;Specify the maximum number of bytes to load (raw only)|"
	       "-addr=*;Specify the load address (hex) (raw only)|"
	       "-noclose;Don't close network link before executing program");

    cmd_addcmd("go",
	       ui_cmd_go,
	       NULL,
	       "Start a previously loaded program.",
	       "go [address]\n\n"
	       "The 'go' command will start a program previously loaded with \n"
	       "the 'load' command.  You can override the start address by"
	       "specifying it as a parameter to the 'go' command.",
	       "-noclose;Don't close network link before executing program");

    cmd_addcmd("batch",
	       ui_cmd_batch,
	       NULL,
	       "Load a batch file into memory and execute it",
	       "batch [-options] host:filename|dev:filename\n\n"
	       "This command loads and executes a batch file from a boot device",
#if CFG_ZLIB
	       "-z;Load compessed file|"
#endif
	       "-tftp;Load the file using the TFTP protocol|"
	       "-fatfs;Load the file from a FAT file system|"
	       "-rawfs;Load the file from an unformatted file system|"
               "-fs=*;Specify CFE file system name");


    cmd_addcmd("autoboot",
	       ui_cmd_autoboot,
	       NULL,
	       "Automatic system bootstrap.",
	       "autoboot [dev]\n\n"
	       "The 'autoboot' command causes an automatic system bootstrap from\n"
	       "a predefined list of devices and boot files.  This list is \n"
	       "specific to the board and port of CFE.  To try autobooting from\n"
	       "a specific device, you can specify the CFE device name on the command line.",
	       "-forever;Loop over devices until boot is successful|"
	       "-interruptible;Scan console between devices, drop to prompt if key pressed");

    return 0;
}


static int ui_cmd_autoboot(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int res;
    char *x;
    int flags = 0;

    if (cmd_sw_isset(cmd,"-forever")) flags |= CFE_AUTOFLG_TRYFOREVER;
    if (cmd_sw_isset(cmd,"-interruptible")) flags |= CFE_AUTOFLG_POLLCONSOLE;

    x = cmd_getarg(cmd,0);
    res = cfe_autoboot(x,flags);

    return res;
}

static int ui_cmd_go(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *arg;

    arg = cmd_getarg(cmd,0);
    if (arg) {
	cfe_loadargs.la_entrypt = getaddr(arg);
	}

    if (cmd_sw_isset(cmd,"-noclose")) {
	cfe_loadargs.la_flags |= LOADFLG_NOCLOSE;
	}

    cfe_go(&cfe_loadargs);

    return 0;
}



static int ui_cmd_bootcommon(ui_cmdline_t *cmd,int argc,char *argv[],int flags)
{
    int res;
    char *arg;
    cfe_loadargs_t *la = &cfe_loadargs;
    char copy[200];

    la->la_flags = flags;

    arg = cmd_getarg(cmd,0);	
    strncpy(copy,arg,sizeof(copy));

    if (!arg) {
	xprintf("No program name specified\n");
	return -1;
	}

    res = ui_process_url(arg,cmd,la);
    if (res < 0) return res;
	
    /*
     * Pick up the remaining command line parameters for use as
     * arguments to the loaded program.
     */

    la->la_options = cmd_getarg(cmd,1);

    /*
     * Note: we might not come back here if we really launch the program.
     */

    xprintf("Loader:%s Filesys:%s Dev:%s File:%s Options:%s\n",
	    la->la_loader,la->la_filesys,la->la_device,la->la_filename,la->la_options);

    res = cfe_boot(la->la_loader,la);

    /*	
     * Give the bad news.
     */

    if (res < 0) xprintf("Could not load %s: %s\n",copy,cfe_errortext(res));

    return res;
}


static int ui_cmd_load(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int flags = LOADFLG_NOISY;

    return ui_cmd_bootcommon(cmd,argc,argv,flags);
}




static int ui_cmd_boot(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int flags = LOADFLG_NOISY | LOADFLG_EXECUTE;

    return ui_cmd_bootcommon(cmd,argc,argv,flags);
}

static int ui_cmd_batch(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int flags = LOADFLG_NOISY | LOADFLG_EXECUTE | LOADFLG_BATCH;

    return ui_cmd_bootcommon(cmd,argc,argv,flags);
}

#if CFG_NETWORK
static int ui_cmd_save(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *x;
    uint8_t *start,*end;
    int len;
    char *fname;
    int res;

    fname = cmd_getarg(cmd,0);

    if ((x = cmd_getarg(cmd,1))) {
	start = (uint8_t *) getaddr(x);
	}
    else {
	return ui_showusage(cmd);
	}

    if ((x = cmd_getarg(cmd,2))) {
	len = xtoi(x);
	}
    else {
	return ui_showusage(cmd);
	}

    end = start+len;

    res = cfe_savedata("tftp","",fname,start,end);

    if (res < 0) {
	return ui_showerror(res,"Could not dump data to network");
	}
    else {
	xprintf("%d bytes written to %s\n",res,fname);
	}

    return 0;
}
#endif