1 //==========================================================================
5 // Standard interfaces for RedBoot
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Red Hat, Inc.
12 // Copyright (C) 2002, 2003, 2004, 2005 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: gthomas, tkoeller
50 // This code is part of RedBoot (tm).
52 //####DESCRIPTIONEND####
54 //==========================================================================
59 #include <pkgconf/redboot.h>
60 #include <pkgconf/hal.h>
61 #include <cyg/hal/hal_if.h>
62 #include <cyg/hal/hal_tables.h>
63 #include <cyg/hal/hal_endian.h>
64 #include <cyg/infra/diag.h>
65 #include <cyg/crc/crc.h>
68 #ifdef CYGPKG_REDBOOT_NETWORKING
70 #include <net/bootp.h>
71 // Determine an IP address for this node, using BOOTP
72 extern int __bootp_find_local_ip(bootp_header_t *info);
84 EXTERN char *argv[MAX_ARGV];
85 EXTERN unsigned char *ram_start, *ram_end;
86 EXTERN struct _mem_segment {
87 unsigned char *start, *end;
88 } mem_segments[CYGBLD_REDBOOT_MAX_MEM_SEGMENTS];
89 #define NO_MEMORY (unsigned char *)0xFFFFFFFF
90 EXTERN bool valid_address(unsigned char *addr);
91 EXTERN void cyg_plf_memory_segment(int seg, unsigned char **start, unsigned char **end);
92 EXTERN unsigned char *workspace_start, *workspace_end;
94 // Data squirreled away after a load operation
95 EXTERN unsigned long entry_address;
96 EXTERN unsigned long load_address;
97 EXTERN unsigned long load_address_end;
100 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE
101 EXTERN bool console_selected;
103 EXTERN bool console_echo;
104 EXTERN bool gdb_active;
105 #if CYGNUM_REDBOOT_CMD_LINE_EDITING != 0
106 EXTERN bool cmd_history;
109 #ifdef CYGPKG_REDBOOT_NETWORKING
110 EXTERN bool have_net, use_bootp;
111 EXTERN bootp_header_t my_bootp_info;
113 EXTERN bool net_debug;
116 #ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
117 EXTERN unsigned char *script;
118 EXTERN int script_timeout;
119 #ifdef CYGSEM_REDBOOT_VARIABLE_BAUD_RATE
120 EXTERN int console_baud_rate;
124 #ifdef CYGOPT_REDBOOT_FIS_ZLIB_COMMON_BUFFER
125 EXTERN unsigned char *fis_zlib_common_buffer;
128 #ifdef CYGSEM_REDBOOT_PLF_STARTUP
129 EXTERN void cyg_plf_redboot_startup(void);
133 typedef int _printf_fun(const char *fmt, ...);
134 externC int strcasecmp(const char *s1, const char *s2);
135 externC int strncasecmp(const char *s1, const char *s2, size_t len);
137 externC void mon_write_char(char c);
138 externC bool mon_read_char_with_timeout(char *c);
139 externC void mon_set_read_char_timeout(int ms);
140 externC bool verify_action(char *fmt, ...);
141 externC bool verify_action_with_timeout(int timeout, char *fmt, ...);
143 // Read a single line of input from the console, possibly with timeout
144 externC int _rb_gets(char *line, int len, int timeout);
145 // Just like _rb_gets(), except that the line buffer is assumed to contain
146 // valid input data. This provides an easy mechanism for edit-in-place.
147 externC int _rb_gets_preloaded(char *line, int len, int timeout);
148 // Result codes from 'gets()'
149 #define _GETS_TIMEOUT -1
150 #define _GETS_CTRLC -2
153 // Test for ^C on the console. This function should only be used if any
154 // other console input can be discarded, e.g. while performing some long
155 // computation, waiting for the network, etc. Returns 'true' if ^C typed.
156 externC bool _rb_break(int timeout);
158 // "console" selection
159 externC int start_console(void);
160 externC void end_console(int old_console);
163 #ifdef CYGSEM_REDBOOT_FLASH_ALIASES
164 externC char *flash_lookup_alias(char *alias, char *alias_buf);
166 externC void expand_aliases(char *line, int len);
169 // Stream I/O support
176 #ifdef CYGPKG_REDBOOT_NETWORKING
177 struct sockaddr_in *server;
182 int (*open)(connection_info_t *info, int *err);
183 void (*close)(int *err);
184 void (*terminate)(bool abort, int (*getc)(void));
185 int (*read)(char *buf, int size, int *err);
186 char *(*error)(int err);
189 #define GETC_IO_FUNCS(_label_, _open_, _close_, _terminate_, _read_, _error_) \
190 getc_io_funcs_t _label_ = { \
191 _open_, _close_, _terminate_, _read_, _error_ \
194 struct load_io_entry {
196 getc_io_funcs_t *funcs;
200 } CYG_HAL_TABLE_TYPE;
201 #define _RedBoot_load(_name_,_funcs_,_verbose_,_filename_,_mode_) \
202 struct load_io_entry _load_tab_##_funcs_##_name_ \
203 CYG_HAL_TABLE_QUALIFIED_ENTRY(RedBoot_load,_funcs_##_name) = \
204 { #_name_, &_funcs_, _verbose_, _filename_, _mode_ };
205 #define RedBoot_load(_name_,_funcs_,_verbose_,_filename_, _mode_) \
206 _RedBoot_load(_name_,_funcs_,_verbose_,_filename_,_mode_)
208 #ifdef CYGPKG_COMPRESS_ZLIB
209 // Decompression support
210 typedef struct _pipe {
211 unsigned char* in_buf; // only changed by producer
212 int in_avail; // only changed by producer
213 unsigned char* out_buf; // only changed by consumer (init by producer)
214 int out_size; // only changed by consumer (init by producer)
215 int out_max; // set by producer
216 const char* msg; // message from consumer
217 void* priv; // handler's data
220 typedef int _decompress_fun_init(_pipe_t*);
221 typedef int _decompress_fun_inflate(_pipe_t*);
222 typedef int _decompress_fun_close(_pipe_t*, int);
224 externC _decompress_fun_init* _dc_init;
225 externC _decompress_fun_inflate* _dc_inflate;
226 externC _decompress_fun_close* _dc_close;
227 #endif // CYGPKG_COMPRESS_ZLIB
229 // CLI support functions
230 externC bool parse_num(char *s, unsigned long *val, char **es, char *delim);
231 externC bool parse_bool(char *s, bool *val);
233 typedef void cmd_fun(int argc, char *argv[]);
239 struct cmd *sub_cmds, *sub_cmds_end;
240 } CYG_HAL_TABLE_TYPE;
241 externC struct cmd *cmd_search(struct cmd *tab, struct cmd *tabend, char *arg);
242 externC void cmd_usage(struct cmd *tab, struct cmd *tabend, char *prefix);
243 #define RedBoot_cmd(_s_,_h_,_u_,_f_) cmd_entry(_s_,_h_,_u_,_f_,0,0,RedBoot_commands)
244 #define RedBoot_nested_cmd(_s_,_h_,_u_,_f_,_subs_,_sube_) cmd_entry(_s_,_h_,_u_,_f_,_subs_,_sube_,RedBoot_commands)
245 #define _cmd_entry(_s_,_h_,_u_,_f_,_subs_,_sube_,_n_) \
247 struct cmd _cmd_tab_##_f_ CYG_HAL_TABLE_QUALIFIED_ENTRY(_n_,_f_) = {_s_, _h_, _u_, _f_, _subs_, _sube_};
248 #define cmd_entry(_s_,_h_,_u_,_f_,_subs_,_sube_,_n_) \
249 extern _cmd_entry(_s_,_h_,_u_,_f_,_subs_,_sube_,_n_)
250 #define local_cmd_entry(_s_,_h_,_u_,_f_,_n_) \
251 static _cmd_entry(_s_,_h_,_u_,_f_,0,0,_n_)
253 // Initialization functions
254 #define RedBoot_INIT_FIRST 0000
255 #define RedBoot_INIT_SECOND 0100
256 // Specify a 3 digit numeric value for proper prioritizing
257 #define RedBoot_INIT_PRIO(_n_) 1##_n_
258 #define RedBoot_INIT_LAST 9999
259 typedef void void_fun(void);
260 typedef void_fun *void_fun_ptr;
261 struct init_tab_entry {
263 } CYG_HAL_TABLE_TYPE;
264 #define _RedBoot_init(_f_,_p_) \
265 struct init_tab_entry _init_tab_##_p_##_f_ \
266 CYG_HAL_TABLE_QUALIFIED_ENTRY(RedBoot_inits,_p_##_f_) = { _f_ };
267 #define RedBoot_init(_f_,_p_) _RedBoot_init(_f_,_p_)
269 // Main loop [idle] call-back functions
270 #define RedBoot_IDLE_FIRST 0000
271 #define RedBoot_IDLE_BEFORE_NETIO 3000
272 #define RedBoot_IDLE_NETIO 5000
273 #define RedBoot_IDLE_AFTER_NETIO 7000
274 #define RedBoot_IDLE_LAST 9999
275 typedef void idle_fun(bool);
276 typedef idle_fun *idle_fun_ptr;
277 struct idle_tab_entry {
279 } CYG_HAL_TABLE_TYPE;
280 #define _RedBoot_idle(_f_,_p_) \
281 struct idle_tab_entry _idle_tab_##_p_##_f_ \
282 CYG_HAL_TABLE_QUALIFIED_ENTRY(RedBoot_idle,_p_##_f_) = { _f_ };
283 #define RedBoot_idle(_f_,_p_) _RedBoot_idle(_f_,_p_)
285 // This function called when changing idle/not - mostly used by I/O
286 // to support idle when timeout, etc.
287 void do_idle(bool state);
289 // Option processing support
300 #define NUM_ELEMS(s) (sizeof(s)/sizeof(s[0]))
302 #define OPTION_ARG_TYPE_NUM 0 // Numeric data
303 #define OPTION_ARG_TYPE_STR 1 // Generic string
304 #define OPTION_ARG_TYPE_FLG 2 // Flag only
306 // Command line parsing
307 externC struct cmd *parse(char **line, int *argc, char **argv);
309 externC void init_opts(struct option_info *opts, char flag, bool takes_arg,
310 int arg_type, void *arg, bool *arg_set, char *name);
311 externC bool scan_opts(int argc, char *argv[], int first,
312 struct option_info *opts, int num_opts,
313 void *def_arg, int def_arg_type, char *def_descr);
315 #ifdef CYGNUM_HAL_VIRTUAL_VECTOR_AUX_CHANNELS
316 #define CYGNUM_HAL_VIRTUAL_VECTOR_NUM_CHANNELS \
317 (CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS+CYGNUM_HAL_VIRTUAL_VECTOR_AUX_CHANNELS)
319 #define CYGNUM_HAL_VIRTUAL_VECTOR_NUM_CHANNELS \
320 CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS
323 #ifdef CYGPKG_REDBOOT_NETWORKING
324 //-----------------------------------------------------------------------------
326 #ifdef CYGPKG_REDBOOT_NETWORKING_DNS
328 // I would really like if we could just pull in cyg/ns/dns/dns.h, but
329 // that would require adding dummy <network.h> and <netinet/in.h> files.
331 // Host name / IP mapping
333 char *h_name; /* official name of host */
334 char **h_aliases; /* alias list */
335 int h_addrtype; /* host address type */
336 int h_length; /* length of address */
337 char **h_addr_list; /* list of addresses */
339 #define h_addr h_addr_list[0] /* for backward compatibility */
341 externC int redboot_dns_res_init(void);
342 externC void set_dns(char* new_ip);
343 externC void show_dns(void);
344 externC struct hostent *gethostbyname(const char *host);
349 #define DNS_SUCCESS 0
350 #define HOST_NOT_FOUND 1
352 #define NO_RECOVERY 3
356 _gethostbyname(const char* name, in_addr_t* host)
358 struct hostent* hent = gethostbyname(name);
360 memcpy(host, hent->h_addr_list[0], sizeof(in_addr_t));
363 // Fall back to inet_aton - gethostbyname may already have tried
364 // it, but we can't know for sure (the DNS IP may not have been
365 // valid, preventing the inet_aton).
366 return inet_aton(name, host);
370 _gethostbyname(const char* name, in_addr_t* host)
372 return inet_aton(name, host);
374 #endif // CYGPKG_REDBOOT_NETWORKING_DNS
375 #endif // CYGPKG_REDBOOT_NETWORKING
377 //-----------------------------------------------------------------------------
378 // String functions. Some of these are duplicates of the same functions in
381 // Validate a hex character
382 __inline__ static bool
385 return (((c >= '0') && (c <= '9')) ||
386 ((c >= 'A') && (c <= 'F')) ||
387 ((c >= 'a') && (c <= 'f')));
390 // Convert a single hex nibble
391 __inline__ static int
396 if ((c >= '0') && (c <= '9')) {
398 } else if ((c >= 'a') && (c <= 'f')) {
399 ret = (c - 'a' + 0x0a);
400 } else if ((c >= 'A') && (c <= 'F')) {
401 ret = (c - 'A' + 0x0A);
406 // Convert a character to lower case
407 __inline__ static char
410 if ((c >= 'A') && (c <= 'Z')) {
417 __inline__ static bool
420 return (((c >= 'a') && (c <= 'z')) ||
421 ((c >= 'A') && (c <= 'Z')));
425 __inline__ static bool
428 return ((c >= '0') && (c <= '9'));
432 __inline__ static bool
435 return (isalpha(c) || isdigit(c));
438 //----------------------------------------------------------------------------
440 #if defined(CYGSEM_REDBOOT_BSP_SYSCALLS)
442 // These are required by the ANSI C part of newlib (excluding system() of
454 //#define SYS_sbrk 11 - not currently a system call, but reserved.
457 #define SYS_argvlen 12
460 // These are extras added for one reason or another.
466 #define SYS_gettimeofday 19
469 #define SYS_interrupt 1000
470 #define SYS_meminfo 1001
472 #define __GET_SHARED 0xbaad // 47789 decimal
474 #ifdef CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
476 #define SYS_timer_call_back 2001
477 #define SYS_timer_frequency 2002
478 #define SYS_timer_reset 2003
480 #endif // CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
481 #define SYS_rename 3001
482 #define SYS_isatty 3002
483 #define SYS_system 3003
485 #endif // CYGSEM_REDBOOT_BSP_SYSCALLS
488 //----------------------------------------------------------------------------
489 // Allow HAL to override RedBoot flash read/program operations.
490 #ifdef HAL_FLASH_READ
491 #define FLASH_READ(f, r, l, e) HAL_FLASH_READ((f),(r),(l),(e))
493 #define FLASH_READ(f, r, l, e) flash_read((f), (r), (l), (e))
496 #ifdef HAL_FLASH_PROGRAM
497 #define FLASH_PROGRAM(f, r, l, e) HAL_FLASH_PROGRAM((f),(r),(l),(e))
499 #define FLASH_PROGRAM(f, r, l, e) flash_program((f), (r), (l), (e))
503 // Define REDBOOT_FLASH_REVERSE_BYTEORDER if config and fis info is stored in flash
504 // with byte ordering opposite from CYG_BYTEORDER.
505 #if (defined(CYGOPT_REDBOOT_FLASH_BYTEORDER_MSBFIRST) && (CYG_BYTEORDER != CYG_MSBFIRST)) || \
506 (defined(CYGOPT_REDBOOT_FLASH_BYTEORDER_LSBFIRST) && (CYG_BYTEORDER != CYG_LSBFIRST))
507 #define REDBOOT_FLASH_REVERSE_BYTEORDER
510 #endif // _REDBOOT_H_