blob: efbb9e989818108e89117f3db9e5938d1f5d98b2 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
Chandan Uddarajubbec2b02009-12-16 13:27:55 -08004 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
5 *
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25#include <compiler.h>
26#include <debug.h>
27#include <string.h>
Travis Geiselbrecht68372232009-01-24 21:21:08 -080028#include <app.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070029#include <arch.h>
30#include <platform.h>
31#include <target.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070032#include <lib/heap.h>
33#include <kernel/thread.h>
34#include <kernel/timer.h>
35#include <kernel/dpc.h>
36
37extern void *__ctor_list;
38extern void *__ctor_end;
39extern int __bss_start;
40extern int _end;
41
42static int bootstrap2(void *arg);
43
Chandan Uddarajubbec2b02009-12-16 13:27:55 -080044#if (ENABLE_NANDWRITE)
45void bootstrap_nandwrite(void);
46#endif
47
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070048static void call_constructors(void)
49{
50 void **ctor;
51
52 ctor = &__ctor_list;
53 while(ctor != &__ctor_end) {
54 void (*func)(void);
55
56 func = (void (*)())*ctor;
57
58 func();
59 ctor++;
60 }
61}
62
63/* called from crt0.S */
64void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE;
65void kmain(void)
66{
travis geiselbrecht858b1ad2008-12-23 21:55:41 +000067 // get us into some sort of thread context
68 thread_init_early();
69
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070070 // early arch stuff
71 arch_early_init();
72
73 // do any super early platform initialization
74 platform_early_init();
75
76 // do any super early target initialization
77 target_early_init();
78
Travis Geiselbrechteb946052008-09-07 22:32:49 -070079 dprintf(INFO, "welcome to lk\n\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070080
81 // deal with any static constructors
Travis Geiselbrechteb946052008-09-07 22:32:49 -070082 dprintf(SPEW, "calling constructors\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070083 call_constructors();
84
85 // bring up the kernel heap
Travis Geiselbrechteb946052008-09-07 22:32:49 -070086 dprintf(SPEW, "initializing heap\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070087 heap_init();
88
89 // initialize the threading system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070090 dprintf(SPEW, "initializing threads\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070091 thread_init();
92
93 // initialize the dpc system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070094 dprintf(SPEW, "initializing dpc\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070095 dpc_init();
96
97 // initialize kernel timers
Travis Geiselbrechteb946052008-09-07 22:32:49 -070098 dprintf(SPEW, "initializing timers\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070099 timer_init();
100
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800101#if (!ENABLE_NANDWRITE)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700102 // create a thread to complete system initialization
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700103 dprintf(SPEW, "creating bootstrap completion thread\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700104 thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE));
105
106 // enable interrupts
107 exit_critical_section();
108
109 // become the idle thread
110 thread_become_idle();
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800111#else
112 bootstrap_nandwrite();
113#endif
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700114}
115
116int main(void);
117
118static int bootstrap2(void *arg)
119{
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700120 dprintf(SPEW, "top of bootstrap2()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700121
122 arch_init();
123
Travis Geiselbrechte8560832010-05-06 14:34:26 -0700124 // XXX put this somewhere else
125#if WITH_LIB_BIO
126 bio_init();
127#endif
128#if WITH_LIB_FS
129 fs_init();
130#endif
131
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700132 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700133 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700134 platform_init();
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700135
136 // initialize the target
137 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700138 target_init();
139
Travis Geiselbrecht68372232009-01-24 21:21:08 -0800140 dprintf(SPEW, "calling apps_init()\n");
141 apps_init();
142
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700143 return 0;
144}
145
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800146#if (ENABLE_NANDWRITE)
147void bootstrap_nandwrite(void)
148{
149 dprintf(SPEW, "top of bootstrap2()\n");
150
151 arch_init();
152
153 // initialize the rest of the platform
154 dprintf(SPEW, "initializing platform\n");
155 platform_init();
156
157 // initialize the target
158 dprintf(SPEW, "initializing target\n");
159 target_init();
160
161 dprintf(SPEW, "calling nandwrite_init()\n");
162 nandwrite_init();
163
164 return 0;
165}
166#endif