blob: 9999f854ed123ed49dc33b711fd010cab22dffa9 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
Amol Jadia55c3a62013-03-18 14:49:37 -07004 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Chandan Uddarajubbec2b02009-12-16 13:27:55 -08005 *
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>
Amol Jadia55c3a62013-03-18 14:49:37 -070036#include <boot_stats.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070037
38extern void *__ctor_list;
39extern void *__ctor_end;
40extern int __bss_start;
41extern int _end;
42
43static int bootstrap2(void *arg);
44
Chandan Uddarajubbec2b02009-12-16 13:27:55 -080045#if (ENABLE_NANDWRITE)
46void bootstrap_nandwrite(void);
47#endif
48
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070049static void call_constructors(void)
50{
51 void **ctor;
Amol Jadia55c3a62013-03-18 14:49:37 -070052
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070053 ctor = &__ctor_list;
54 while(ctor != &__ctor_end) {
55 void (*func)(void);
56
57 func = (void (*)())*ctor;
58
59 func();
60 ctor++;
61 }
62}
63
64/* called from crt0.S */
65void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE;
66void kmain(void)
67{
travis geiselbrecht858b1ad2008-12-23 21:55:41 +000068 // get us into some sort of thread context
69 thread_init_early();
70
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070071 // early arch stuff
72 arch_early_init();
73
74 // do any super early platform initialization
75 platform_early_init();
76
77 // do any super early target initialization
78 target_early_init();
79
Travis Geiselbrechteb946052008-09-07 22:32:49 -070080 dprintf(INFO, "welcome to lk\n\n");
Amol Jadia55c3a62013-03-18 14:49:37 -070081 bs_set_timestamp(BS_BL_START);
82
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070083 // deal with any static constructors
Travis Geiselbrechteb946052008-09-07 22:32:49 -070084 dprintf(SPEW, "calling constructors\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070085 call_constructors();
86
87 // bring up the kernel heap
Travis Geiselbrechteb946052008-09-07 22:32:49 -070088 dprintf(SPEW, "initializing heap\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070089 heap_init();
90
91 // initialize the threading system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070092 dprintf(SPEW, "initializing threads\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070093 thread_init();
94
95 // initialize the dpc system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070096 dprintf(SPEW, "initializing dpc\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070097 dpc_init();
98
99 // initialize kernel timers
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700100 dprintf(SPEW, "initializing timers\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700101 timer_init();
102
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800103#if (!ENABLE_NANDWRITE)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700104 // create a thread to complete system initialization
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700105 dprintf(SPEW, "creating bootstrap completion thread\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700106 thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE));
107
108 // enable interrupts
109 exit_critical_section();
110
111 // become the idle thread
112 thread_become_idle();
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800113#else
114 bootstrap_nandwrite();
115#endif
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700116}
117
118int main(void);
119
120static int bootstrap2(void *arg)
121{
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700122 dprintf(SPEW, "top of bootstrap2()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700123
124 arch_init();
125
Travis Geiselbrechte8560832010-05-06 14:34:26 -0700126 // XXX put this somewhere else
127#if WITH_LIB_BIO
128 bio_init();
129#endif
130#if WITH_LIB_FS
131 fs_init();
132#endif
133
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700134 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700135 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700136 platform_init();
Amol Jadia55c3a62013-03-18 14:49:37 -0700137
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700138 // initialize the target
139 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700140 target_init();
141
Travis Geiselbrecht68372232009-01-24 21:21:08 -0800142 dprintf(SPEW, "calling apps_init()\n");
143 apps_init();
144
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700145 return 0;
146}
147
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800148#if (ENABLE_NANDWRITE)
149void bootstrap_nandwrite(void)
150{
151 dprintf(SPEW, "top of bootstrap2()\n");
152
153 arch_init();
154
155 // initialize the rest of the platform
156 dprintf(SPEW, "initializing platform\n");
157 platform_init();
158
159 // initialize the target
160 dprintf(SPEW, "initializing target\n");
161 target_init();
162
163 dprintf(SPEW, "calling nandwrite_init()\n");
164 nandwrite_init();
165
166 return 0;
167}
168#endif