blob: f48ea555417d33ae511568be0d81d662deb9d4f0 [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
124 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700125 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700126 platform_init();
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700127
128 // initialize the target
129 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700130 target_init();
131
Travis Geiselbrecht68372232009-01-24 21:21:08 -0800132 dprintf(SPEW, "calling apps_init()\n");
133 apps_init();
134
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700135 return 0;
136}
137
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800138#if (ENABLE_NANDWRITE)
139void bootstrap_nandwrite(void)
140{
141 dprintf(SPEW, "top of bootstrap2()\n");
142
143 arch_init();
144
145 // initialize the rest of the platform
146 dprintf(SPEW, "initializing platform\n");
147 platform_init();
148
149 // initialize the target
150 dprintf(SPEW, "initializing target\n");
151 target_init();
152
153 dprintf(SPEW, "calling nandwrite_init()\n");
154 nandwrite_init();
155
156 return 0;
157}
158#endif