alt_main.c


001: #include <stdio.h>
002: #include <stdarg.h>
003: #include <string.h>
004: #include <fcntl.h>
005: #include <stdlib.h>
006: 
007: #include "sys/alt_dev.h"
008: #include "sys/alt_sys_init.h"
009: #include "sys/alt_irq.h"
010: #include "sys/alt_dev.h"
011: 
012: #include "os/alt_hooks.h"
013: 
014: #include "priv/alt_file.h"
015: #include "alt_types.h"
016: 
017: #include "system.h"
018: 
019: #include "sys/alt_log_printf.h"
020: 
021: extern void _do_ctors(void);
022: extern void _do_dtors(void);
023: 
024: /*
025:  * Standard arguments for main. By default, no arguments are passed to main.
026:  * However a device driver may choose to configure these arguments by calling
027:  * alt_set_args(). The expectation is that this facility will only be used by
028:  * the iclient/ihost utility.
029:  */
030: 
031: int    alt_argc = 0;
032: char** alt_argv = {NULL};
033: char** alt_envp = {NULL};
034: 
035: /*
036:  * Prototype for the entry point to the users application.
037:  */
038: 
039: extern int main (int, char **, char **);
040: 
041: /*
042:  * alt_main is the C entry point for the HAL. It is called by the assembler
043:  * startup code in the processor specific crt0.S. It is responsible for:
044:  * completing the C runtime configuration; configuring all the
045:  * devices/filesystems/components in the system; and call the entry point for
046:  * the users application, i.e. main().
047:  */
048: 
049: void alt_main (void)
050: {
051: 
052:   /* ALT LOG - please see HAL/sys/alt_log_printf.h for details */
053:   ALT_LOG_PRINT_BOOT("[alt_main.c] Entering alt_main, calling alt_irq_init.\r\n");
054:   /* Initialize the interrupt controller. */
055:   alt_irq_init (ALT_IRQ_BASE);
056: 
057:   /* Initialize the operating system */
058:   ALT_LOG_PRINT_BOOT("[alt_main.c] Done alt_irq_init, calling alt_os_init.\r\n");
059:   ALT_OS_INIT();
060: 
061:   /*
062:    * Initialize the semaphore used to control access to the file descriptor
063:    * list.
064:    */
065: 
066:   ALT_LOG_PRINT_BOOT("[alt_main.c] Done OS Init, calling alt_sem_create.\r\n");
067:   ALT_SEM_CREATE (&alt_fd_list_lock, 1);
068: 
069:   /* Initialize the device drivers/software components. */
070:   ALT_LOG_PRINT_BOOT("[alt_main.c] Calling alt_sys_init.\r\n");
071:   alt_sys_init();
072:   ALT_LOG_PRINT_BOOT("[alt_main.c] Done alt_sys_init.\r\n");
073: 
074: #if !defined(ALT_USE_DIRECT_DRIVERS) && (defined(ALT_STDIN_PRESENT) || defined(ALT_STDOUT_PRESENT) || defined(ALT_STDERR_PRESENT))
075: 
076:   /*
077:    * Redirect stdio to the apropriate devices now that the devices have
078:    * been initialized. This is only done if the user has requested these
079:    * devices be present (not equal to /dev/null) and if direct drivers
080:    * aren't being used.
081:    */
082: 
083:     ALT_LOG_PRINT_BOOT("[alt_main.c] Redirecting IO.\r\n");
084:     alt_io_redirect(ALT_STDOUT, ALT_STDIN, ALT_STDERR);
085: #endif
086: 
087: #ifndef ALT_NO_C_PLUS_PLUS
088:   /* 
089:    * Call the C++ constructors 
090:    */
091: 
092:   ALT_LOG_PRINT_BOOT("[alt_main.c] Calling C++ constructors.\r\n");
093:   _do_ctors ();
094: #endif /* ALT_NO_C_PLUS_PLUS */
095: 
096: #if !defined(ALT_NO_C_PLUS_PLUS) && !defined(ALT_NO_CLEAN_EXIT) && !defined(ALT_NO_EXIT)
097:   /*
098:    * Set the C++ destructors to be called at system shutdown. This is only done
099:    * if a clean exit has been requested (i.e. the exit() function has not been
100:    * redefined as _exit()). This is in the interest of reducing code footprint,
101:    * in that the atexit() overhead is removed when it's not needed.
102:    */
103: 
104:   ALT_LOG_PRINT_BOOT("[alt_main.c] Calling atexit.\r\n");
105:   atexit (_do_dtors);
106: #endif
107: 
108:   /*
109:    * Finally, call main(). The return code is then passed to a subsequent
110:    * call to exit() unless the application is never supposed to exit.
111:    */
112: 
113:   ALT_LOG_PRINT_BOOT("[alt_main.c] Calling main.\r\n");
114: 
115: #ifdef ALT_NO_EXIT
116:   main (alt_argc, alt_argv, alt_envp);
117: #else
118:   exit (main (alt_argc, alt_argv, alt_envp));
119: #endif
120: 
121:   ALT_LOG_PRINT_BOOT("[alt_main.c] After main - we should not be here?.\r\n");
122: }
123: 


Maintained by John Loomis, updated Thu Nov 13 22:23:16 2008