source: rtems/c/src/lib/libbsp/shared/bootcard.c @ 4dc6a46

4.104.114.95
Last change on this file since 4dc6a46 was 18e6e824, checked in by Joel Sherrill <joel.sherrill@…>, on 07/15/08 at 13:32:12

Spacing.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 *  This is the C entry point for ALL RTEMS BSPs.  It is invoked
3 *  from the assembly language initialization file usually called
4 *  start.S.  It provides the framework for the BSP initialization
5 *  sequence.  The basic flow of initialization is:
6 *
7 *  + start.S: basic CPU setup (stack, zero BSS)
8 *    + boot_card
9 *      + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
10 *        - obtain information on BSP memory and allocate RTEMS Workspace
11 *      + bspstart.c: bsp_start - more advanced initialization
12 *      + rtems_initialize_data_structures
13 *      + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
14 *        - Allocate memory to C Program Heap
15 *        - initialize C Library and C Program Heap
16 *      + bsp_pretasking_hook
17 *      + if defined(RTEMS_DEBUG)
18 *        - rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
19 *      + rtems_initialize_before_drivers
20 *      + bsp_predriver_hook
21 *      + rtems_initialize_device_drivers
22 *        - all device drivers
23 *      + bsp_postdriver_hook
24 *      + rtems_initialize_start_multitasking
25 *        - 1st task executes C++ global constructors
26 *          .... appplication runs ...
27 *          - exit
28 *     + back to here eventually
29 *     + bspclean.c: bsp_cleanup
30 *
31 *  This style of initialization ensures that the C++ global
32 *  constructors are executed after RTEMS is initialized.
33 *  Thanks to Chris Johns <cjohns@plessey.com.au> for the idea
34 *  to move C++ global constructors into the first task.
35 *
36 *  COPYRIGHT (c) 1989-2008.
37 *  On-Line Applications Research Corporation (OAR).
38 *
39 *  The license and distribution terms for this file may be
40 *  found in the file LICENSE in this distribution or at
41 *  http://www.rtems.com/license/LICENSE.
42 *
43 *  $Id$
44 */
45
46#include <stddef.h>
47#include <stdint.h>
48
49#include <rtems.h>
50
51/*
52 *  Since there is a forward reference
53 */
54char *rtems_progname;
55
56
57/*
58 *  Prototypes of external routines
59 */
60extern void bsp_start( void );
61extern void bsp_cleanup( void );
62extern void bsp_pretasking_hook(void);
63extern void bsp_libc_init( void *, uint32_t, int );
64extern void bsp_predriver_hook(void);
65extern void bsp_postdriver_hook(void);
66
67/*
68 *  These are the prototypes and helper routines which are used
69 *  when the BSP lets the framework handle RAM allocation between
70 *  the RTEMS Workspace and C Program Heap.
71 */
72#if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
73  extern void bsp_get_workarea( void **, size_t *, size_t *);
74
75  void bootcard_bsp_libc_helper(
76    void   *workarea_base,
77    size_t  workarea_size,
78    size_t  requested_heap_size
79  )
80  {
81    uint32_t     heap_start;
82    uint32_t     heap_size;
83
84    heap_start = (uint32_t) workarea_base;
85    if (heap_start & (CPU_ALIGNMENT-1))
86      heap_start = (heap_start + CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1);
87
88    if ( requested_heap_size == 0 ) {
89      heap_size = Configuration.work_space_start - workarea_base;
90      heap_size &= 0xfffffff0;  /* keep it as a multiple of 16 bytes */
91    } else {
92      heap_size = requested_heap_size;
93    }
94
95    bsp_libc_init((void *) heap_start, heap_size, 0);
96  }
97#endif
98
99/*
100 *  This is the initialization framework routine that weaves together
101 *  calls to RTEMS and the BSP in the proper sequence to initialize
102 *  the system while maximizing shared code and keeping BSP code in C
103 *  as much as possible.
104 */
105int boot_card(
106  int    argc,
107  char **argv,
108  char **envp
109)
110{
111  static char  *argv_pointer = NULL;
112  static char  *envp_pointer = NULL;
113  char **argv_p = &argv_pointer;
114  char **envp_p = &envp_pointer;
115  rtems_interrupt_level bsp_isr_level;
116  #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
117    void   *workarea_base;
118    size_t  workarea_size;
119    size_t  heap_size;
120  #endif
121
122  /*
123   * Special case for PowerPC: The interrupt disable mask is stored in SPRG0.
124   * It must be valid before we can use rtems_interrupt_disable().
125   */
126  #ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT
127    ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT );
128  #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */
129
130  /*
131   *  Make sure interrupts are disabled.
132   */
133  rtems_interrupt_disable( bsp_isr_level );
134
135  /*
136   *  Set things up so we have real pointers for argv and envp.
137   *  If the BSP has passed us something useful, then pass it on.
138   *  Somehow we need to eventually make this available to
139   *  a real main() in user land. :)
140   */
141  if ( argv ) argv_p = argv;
142  if ( envp ) envp_p = envp;
143
144  /*
145   *  Set the program name in case some application cares.
146   */
147  if ((argc > 0) && argv && argv[0])
148    rtems_progname = argv[0];
149  else
150    rtems_progname = "RTEMS";
151
152  /*
153   *  Find out where the block of memory the BSP will use for
154   *  the RTEMS Workspace and the C Program Heap is.
155   */
156  #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
157  {
158    unsigned char     *work_space_start;
159
160    bsp_get_workarea( &workarea_base, &workarea_size, &heap_size );
161
162    work_space_start = workarea_base + workarea_size
163      - rtems_configuration_get_work_space_size();
164
165    if ( work_space_start <= (unsigned char *)workarea_base ) {
166      printk( "bootcard: Not enough RAM!!!\n" );
167      bsp_cleanup();
168      return -1;
169    }
170
171    Configuration.work_space_start = work_space_start;
172
173    #if (BSP_DIRTY_MEMORY == 1)
174      memset(workarea_base, 0xCF,  workarea_size);
175    #endif
176  }
177
178  #endif
179
180  /*
181   * Invoke Board Support Package initialization routine written in C.
182   */
183  bsp_start();
184
185  /*
186   *  Initialize RTEMS data structures
187   */
188  rtems_initialize_data_structures( &Configuration );
189
190  /*
191   *  Initialize the C library for those BSPs using the shared
192   *  framework.
193   */
194  #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
195    bootcard_bsp_libc_helper( workarea_base, workarea_size, heap_size );
196  #endif
197
198  /*
199   *  All BSP to do any required initialization now that RTEMS
200   *  data structures are initialized.  In older BSPs or those
201   *  which do not use the shared framework, this is the typical
202   *  time when the C Library is initialized so malloc()
203   *  can be called by device drivers.  For BSPs using the shared
204   *  framework, this routine can be empty.
205   */
206  bsp_pretasking_hook();
207
208  /*
209   *  If debug is enabled, then enable all dynamic RTEMS debug
210   *  capabilities.
211   *
212   *  NOTE: Most debug features are conditionally compiled in
213   *        or enabled via configure time plugins.
214   */
215  #ifdef RTEMS_DEBUG
216    rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
217  #endif
218
219  /*
220   *  Let RTEMS perform initialization it requires before drivers
221   *  are allowed to be initialized.
222   */
223  rtems_initialize_before_drivers();
224
225  /*
226   *  Execute BSP specific pre-driver hook. Drivers haven't gotten
227   *  to initialize yet so this is a good chance to initialize
228   *  buses, spurious interrupt handlers, etc..
229   *
230   *  NOTE: Many BSPs do not require this handler and use the
231   *        shared stub.
232   */
233  bsp_predriver_hook();
234
235  /*
236   *  Initialize all device drivers.
237   */
238  rtems_initialize_device_drivers();
239
240  /*
241   *  Invoke the postdriver hook.  This normally opens /dev/console
242   *  for use as stdin, stdout, and stderr.
243   */
244  bsp_postdriver_hook();
245
246  /*
247   *  Complete initialization of RTEMS and switch to the first task.
248   *  Global C++ constructors will be executed in the context of that task.
249   */
250  rtems_initialize_start_multitasking();
251
252  /***************************************************************
253   ***************************************************************
254   *  APPLICATION RUNS HERE!!!  When it shuts down, we return!!! *
255   ***************************************************************
256   ***************************************************************
257   */
258
259  /*
260   *  Perform any BSP specific shutdown actions which are written in C.
261   */
262  bsp_cleanup();
263
264  /*
265   *  Now return to the start code.
266   */
267
268  return 0;
269}
Note: See TracBrowser for help on using the repository browser.