]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/redboot/v2_0/include/redboot.h
74f55ecb993b5d6111dde73c2e066da571ac8ad1
[karo-tx-redboot.git] / packages / redboot / v2_0 / include / redboot.h
1 //==========================================================================
2 //
3 //      redboot.h
4 //
5 //      Standard interfaces for RedBoot
6 //
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
13 //
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.
17 //
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
21 // for more details.
22 //
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.
26 //
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.
33 //
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.
36 //
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####
43 //
44 // Author(s):    gthomas
45 // Contributors: gthomas, tkoeller
46 // Date:         2000-07-14
47 // Purpose:      
48 // Description:  
49 //              
50 // This code is part of RedBoot (tm).
51 //
52 //####DESCRIPTIONEND####
53 //
54 //==========================================================================
55
56 #ifndef _REDBOOT_H_
57 #define _REDBOOT_H_
58
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>
66 #include <string.h>
67
68 #ifdef CYGPKG_REDBOOT_NETWORKING
69 #include <net/net.h>
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);
73 #endif
74
75 #ifdef DEFINE_VARS
76 #define EXTERN
77 #else
78 #define EXTERN extern
79 #endif
80
81 // Global variables
82 EXTERN int argc;
83 #define MAX_ARGV 16
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;
93
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;
98
99
100 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE
101 EXTERN bool console_selected;
102 #endif
103 EXTERN bool console_echo;
104 EXTERN bool gdb_active;
105 #if CYGNUM_REDBOOT_CMD_LINE_EDITING != 0
106 EXTERN bool cmd_history;
107 #endif
108
109 #ifdef CYGPKG_REDBOOT_NETWORKING
110 EXTERN bool have_net, use_bootp;
111 EXTERN bootp_header_t my_bootp_info;
112 EXTERN int gdb_port;
113 EXTERN bool net_debug;
114 #endif
115
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;
121 #endif
122 #endif
123
124 #ifdef CYGOPT_REDBOOT_FIS_ZLIB_COMMON_BUFFER
125 EXTERN unsigned char *fis_zlib_common_buffer;
126 #endif
127
128 #ifdef CYGSEM_REDBOOT_PLF_STARTUP
129 EXTERN void cyg_plf_redboot_startup(void);
130 #endif
131
132 // Prototypes
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);
136
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, ...);
142
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
151 #define _GETS_GDB      0
152 #define _GETS_OK       1
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);
157
158 // "console" selection
159 externC int  start_console(void);
160 externC void end_console(int old_console);
161
162 // Alias functions
163 #ifdef CYGSEM_REDBOOT_FLASH_ALIASES
164 externC char *flash_lookup_alias(char *alias, char *alias_buf);
165 #endif
166 externC void expand_aliases(char *line, int len);
167
168 //
169 // Stream I/O support
170 //
171
172 typedef struct {
173     char *filename;
174     int   mode;
175     int   chan;
176 #ifdef CYGPKG_REDBOOT_NETWORKING
177     struct sockaddr_in *server;
178 #endif
179 } connection_info_t;
180
181 typedef struct {
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);
187 } getc_io_funcs_t;
188
189 #define GETC_IO_FUNCS(_label_, _open_, _close_, _terminate_, _read_, _error_)   \
190 getc_io_funcs_t _label_ = {                                                     \
191     _open_, _close_, _terminate_, _read_, _error_                               \
192 };
193
194 struct load_io_entry {
195     char            *name;
196     getc_io_funcs_t *funcs;    
197     bool             can_verbose;
198     bool             need_filename;
199     int              mode;
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_)
207  
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
218 } _pipe_t;
219
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);
223
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
228
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);
232
233 typedef void cmd_fun(int argc, char *argv[]);
234 struct cmd {
235     char    *str;
236     char    *help;
237     char    *usage;
238     cmd_fun *fun;
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_)                                   \
246 cmd_fun _f_;                                                      \
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_)
252
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 {
262     void_fun_ptr fun;
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_)
268
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 {
278     idle_fun_ptr fun;
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_)
284
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);
288
289 // Option processing support
290
291 struct option_info {
292     char flag;
293     bool takes_arg;
294     int  arg_type;
295     void *arg;
296     bool *arg_set;
297     char *name;
298 };
299
300 #define NUM_ELEMS(s) (sizeof(s)/sizeof(s[0]))
301
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
305
306 // Command line parsing
307 externC struct cmd *parse(char **line, int *argc, char **argv);
308
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);
314
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)
318 #else
319 #define CYGNUM_HAL_VIRTUAL_VECTOR_NUM_CHANNELS \
320   CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS
321 #endif
322
323 #ifdef CYGPKG_REDBOOT_NETWORKING
324 //-----------------------------------------------------------------------------
325 // DNS wrapper
326 #ifdef CYGPKG_REDBOOT_NETWORKING_DNS
327
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.
330
331 // Host name / IP mapping
332 struct hostent {
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 */
338 };
339 #define h_addr  h_addr_list[0]  /* for backward compatibility */
340
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);
345
346 // Error reporting
347 externC int h_errno;
348
349 #define DNS_SUCCESS  0
350 #define HOST_NOT_FOUND 1
351 #define TRY_AGAIN      2
352 #define NO_RECOVERY    3
353 #define NO_DATA        4
354
355 static inline bool
356 _gethostbyname(const char* name, in_addr_t* host)
357
358     struct hostent* hent = gethostbyname(name);
359     if (hent) {
360         memcpy(host, hent->h_addr_list[0], sizeof(in_addr_t));
361         return true;
362     }
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);
367 }
368 #else
369 static inline bool
370 _gethostbyname(const char* name, in_addr_t* host)
371
372     return inet_aton(name, host);
373 }
374 #endif // CYGPKG_REDBOOT_NETWORKING_DNS
375 #endif // CYGPKG_REDBOOT_NETWORKING
376
377 //-----------------------------------------------------------------------------
378 // String functions. Some of these are duplicates of the same functions in
379 // the I18N package.
380
381 // Validate a hex character
382 __inline__ static bool
383 _is_hex(char c)
384 {
385     return (((c >= '0') && (c <= '9')) ||
386             ((c >= 'A') && (c <= 'F')) ||            
387             ((c >= 'a') && (c <= 'f')));
388 }
389
390 // Convert a single hex nibble
391 __inline__ static int
392 _from_hex(char c) 
393 {
394     int ret = 0;
395
396     if ((c >= '0') && (c <= '9')) {
397         ret = (c - '0');
398     } else if ((c >= 'a') && (c <= 'f')) {
399         ret = (c - 'a' + 0x0a);
400     } else if ((c >= 'A') && (c <= 'F')) {
401         ret = (c - 'A' + 0x0A);
402     }
403     return ret;
404 }
405
406 // Convert a character to lower case
407 __inline__ static char
408 _tolower(char c)
409 {
410     if ((c >= 'A') && (c <= 'Z')) {
411         c = (c - 'A') + 'a';
412     }
413     return c;
414 }
415
416 // Validate alpha
417 __inline__ static bool
418 isalpha(int c)
419 {
420     return (((c >= 'a') && (c <= 'z')) || 
421             ((c >= 'A') && (c <= 'Z')));
422 }
423
424 // Validate digit
425 __inline__ static bool
426 isdigit(int c)
427 {
428     return ((c >= '0') && (c <= '9'));
429 }
430
431 // Validate alphanum
432 __inline__ static bool
433 isalnum(int c)
434 {
435     return (isalpha(c) || isdigit(c));
436 }
437
438 //----------------------------------------------------------------------------
439 // syscall values
440 #if defined(CYGSEM_REDBOOT_BSP_SYSCALLS)
441
442 // These are required by the ANSI C part of newlib (excluding system() of
443 // course).
444 #define SYS_exit         1
445 #define SYS_open         2
446 #define SYS_close        3
447 #define SYS_read         4
448 #define SYS_write        5
449 #define SYS_lseek        6
450 #define SYS_unlink       7
451 #define SYS_getpid       8
452 #define SYS_kill         9
453 #define SYS_fstat        10
454 //#define SYS_sbrk       11 - not currently a system call, but reserved.
455
456 // ARGV support.
457 #define SYS_argvlen      12
458 #define SYS_argv         13
459
460 // These are extras added for one reason or another.
461 #define SYS_chdir        14
462 #define SYS_stat         15
463 #define SYS_chmod        16
464 #define SYS_utime        17
465 #define SYS_time         18
466 #define SYS_gettimeofday 19
467 #define SYS_times        20
468
469 #define SYS_interrupt   1000
470 #define SYS_meminfo     1001
471
472 #define __GET_SHARED  0xbaad // 47789 decimal
473
474 #ifdef CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
475
476 #define SYS_timer_call_back 2001
477 #define SYS_timer_frequency 2002
478 #define SYS_timer_reset     2003
479
480 #endif // CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
481 #define SYS_rename          3001
482 #define SYS_isatty          3002
483 #define SYS_system          3003
484
485 #endif // CYGSEM_REDBOOT_BSP_SYSCALLS
486
487
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))
492 #else
493 #define FLASH_READ(f, r, l, e) flash_read((f), (r), (l), (e))
494 #endif
495
496 #ifdef HAL_FLASH_PROGRAM
497 #define FLASH_PROGRAM(f, r, l, e) HAL_FLASH_PROGRAM((f),(r),(l),(e))
498 #else
499 #define FLASH_PROGRAM(f, r, l, e) flash_program((f), (r), (l), (e))
500 #endif
501
502
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
508 #endif
509
510 #endif // _REDBOOT_H_