summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/ui/url.c
blob: 80dc7b06fbf86229b99ecf36eb0248d1f2b91b69 (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
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Program and file loading URLs		File: url.c
    *  
    *  Functions to process URLs for loading software.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001
    *  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. Neither the "Broadcom 
    *     Corporation" name nor any trademark or logo of Broadcom 
    *     Corporation may 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 "bsp_config.h"
#include "cfe_loader.h"
#include "cfe_autoboot.h"
#include "cfe_iocb.h"
#include "cfe_devfuncs.h"
#include "cfe_fileops.h"
#include "cfe_error.h"

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

#include "ui_command.h"

#include "url.h"

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;
}

static int process_oldstyle(char *str,
			    ui_cmdline_t *cmd,
			    cfe_loadargs_t *la)
{
    char *file;
    char *devname;
    char *filesys = NULL;
    char *loader = la->la_loader;
    char *x;
    int info;
    char *colon;

    colon = strchr(str,':');

    if (!colon) {
	return CFE_ERR_DEVNOTFOUND;
	}

    devname = str;	/* will be used to check protocol later */
    *colon = '\0';
    file = colon + 1;	/* Ugly, we might put the colon back! */

    /*
     * Try to determine the load protocol ("filesystem")
     * first by using the command line, and
     * if not that try to figure it out automagically
     */

    if (cmd_sw_isset(cmd,"-fatfs")) filesys = "fat";
    if (cmd_sw_isset(cmd,"-tftp"))  filesys = "tftp";
    if (cmd_sw_isset(cmd,"-rawfs")) filesys = "raw";
#if (CFG_TCP) && (CFG_HTTPFS)
    if (cmd_sw_isset(cmd,"-http"))  filesys = "http";
#endif
    if (cmd_sw_value(cmd,"-fs",&x)) filesys = x;

    /*
     * Automagic configuration
     */

    /*
     * Determine the device type from the "host" name.  If we look
     * up the host name and it appears to be an invalid CFE device
     * name, then it's probably a TFTP host name.
     *
     * This is where we guess based on the device type what
     * sort of load method we're going to use.
     */

    info = devname ? cfe_getdevinfo(devname) : -1;
    if (info >= 0) {
	switch (info & CFE_DEV_MASK) {
	    case CFE_DEV_NETWORK:
		if (!filesys) filesys = "tftp";
		if (!loader)  loader  = "raw";
		break;
	    case CFE_DEV_DISK:
		if (!filesys) filesys = "raw";
		if (!loader)  loader  = "raw";
		break;
	    case CFE_DEV_FLASH:
		if (!filesys) filesys = "raw";
		if (!loader)  loader  = "raw";
		break;
	    case CFE_DEV_SERIAL:
		if (!filesys) filesys = "raw";
		if (!loader)  loader  = "srec";
		break;
	    default:
		break;
	    }
	la->la_device   = devname;
	la->la_filename = file;
	}
    else {
	/*
	 * It's probably a network boot.  Default to TFTP
	 * if not overridden
	 */
#if CFG_NETWORK
	la->la_device = (char *) net_getparam(NET_DEVNAME);
#else
	la->la_device = NULL;
#endif
	*colon = ':';			/* put the colon back */
	la->la_filename = devname;
	if (!filesys) filesys = "tftp";
	if (!loader)  loader  = "raw";
	}

    /*
     * Remember our file system and loader.
     */

    la->la_filesys = filesys;
    la->la_loader = loader;

    return 0;
}


#if CFG_URLS
static int process_url(char *str,
		       ui_cmdline_t *cmd,
		       cfe_loadargs_t *la)
{
    char *p;
    char *protocol;
    int idx,len;
    int network = 0;
    const fileio_dispatch_t *fdisp;

    /*
     * All URLs have the string "://" in them somewhere
     * If that's not there, try the old syntax.
     */

    len = strlen(str);
    p = str;

    for (idx = 0; idx < len-3; idx++) {
	if (memcmp(p,"://",3) == 0) break;
	p++;
	}

    if (idx == (len-3)) {
	return process_oldstyle(str,cmd,la);
	}

    /* 
     * Break the string apart into protocol, host, file
     */

    protocol = str;
    *p = '\0';
    p += 3;

    /*
     * Determine if this is a network loader.  If that is true,
     * the meaning of the "device" field is different.  Ugh.
     */

    fdisp = cfe_findfilesys(protocol);
    if (fdisp && (fdisp->loadflags & FSYS_TYPE_NETWORK)) network = 1;

    /*
     * Depending on the protocol we parse the file name one of two ways:
     *
     *      protocol://hostname/filename
     *
     * For network devices:
     *
     *      the "device" is the current Ethernet device.
     *      The filename is the //hostname/filename from the URL.
     *
     * For non-network devices:
     *
     *      The "device" is the CFE device name from the URL 'hostname' field
     *      The filename is from the URL filename field.
     */   

    la->la_filesys = protocol;

    if (network) {
#if CFG_NETWORK
	la->la_device = (char *) net_getparam(NET_DEVNAME);
#else
	la->la_device = NULL;
#endif
	la->la_filename = p;
	}
    else {
	la->la_device = p;
	p = strchr(p,'/');
	if (p) {
	    *p++ = '\0';
	    la->la_filename = p;
	    }
	else {
	    la->la_filename = NULL;
	    }
	}

    if (!la->la_loader) la->la_loader = "raw";

    return 0;
}
#endif



int ui_process_url(char *url,ui_cmdline_t *cmd,cfe_loadargs_t *la)
{
    int res;
    char *x;

    /*
     * Skip leading whitespace
     */

    while (*url && ((*url == ' ') || (*url == '\t'))) url++;

    /*
     * Process command-line switches to determine the loader stack
     */

    la->la_loader = NULL;
    if (cmd_sw_isset(cmd,"-elf"))  la->la_loader = "elf";
    if (cmd_sw_isset(cmd,"-srec")) la->la_loader = "srec";
    if (cmd_sw_isset(cmd,"-raw"))  la->la_loader = "raw";
    if (cmd_sw_value(cmd,"-loader",&x)) la->la_loader = x;

#if CFG_ZLIB
    if (cmd_sw_isset(cmd,"-z")) {
	la->la_flags |= LOADFLG_COMPRESSED;
	}
#endif

    /*
     * Parse the file name into its pieces.
     */

#if CFG_URLS
    res = process_url(url,cmd,la);
    if (res < 0) return res;
#else
    res = process_oldstyle(url,cmd,la);
    if (res < 0) return res;
#endif


    /*
     * This is used only by "boot" and "load" - to avoid this code
     * don't include these switches in the command table.
     */

    if (cmd_sw_value(cmd,"-max",&x)) {
	la->la_maxsize = atoi(x);
	}

    if (cmd_sw_value(cmd,"-addr",&x)) {
	la->la_address = getaddr(x);
	la->la_flags |= LOADFLG_SPECADDR;
	}

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

#if 0
    printf("--- Loader parameters:\n");
    printf("  Filename   = %s\n",la->la_filename);
    printf("  Filesys    = %s\n",la->la_filesys);
    printf("  Device     = %s\n",la->la_device);
    printf("  Options    = %s\n",la->la_options);
    printf("  Loader     = %s\n",la->la_loader);
    printf("  Flags      = %08X\n",la->la_flags);
    printf("  address    = %08X\n",la->la_address);
    printf("  maxsize    = %08X\n",la->la_maxsize);
    printf("  entrypt    = %08X\n",la->la_entrypt);
#endif

    return 0;    
}