blob: 95a433951332038d80feebbb725f22e00af074ba [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
Maria Yue2e80352014-05-28 15:57:51 +08004 * Copyright (c) 2009-2014, 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
Maria Yue2e80352014-05-28 15:57:51 +080091 __stack_chk_guard_setup();
92
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070093 // initialize the threading system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070094 dprintf(SPEW, "initializing threads\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070095 thread_init();
96
97 // initialize the dpc system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070098 dprintf(SPEW, "initializing dpc\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070099 dpc_init();
100
101 // initialize kernel timers
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700102 dprintf(SPEW, "initializing timers\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700103 timer_init();
104
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800105#if (!ENABLE_NANDWRITE)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700106 // create a thread to complete system initialization
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700107 dprintf(SPEW, "creating bootstrap completion thread\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700108 thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE));
109
110 // enable interrupts
111 exit_critical_section();
112
113 // become the idle thread
114 thread_become_idle();
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800115#else
116 bootstrap_nandwrite();
117#endif
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700118}
119
120int main(void);
121
122static int bootstrap2(void *arg)
123{
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700124 dprintf(SPEW, "top of bootstrap2()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700125
126 arch_init();
127
Travis Geiselbrechte8560832010-05-06 14:34:26 -0700128 // XXX put this somewhere else
129#if WITH_LIB_BIO
130 bio_init();
131#endif
132#if WITH_LIB_FS
133 fs_init();
134#endif
135
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700136 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700137 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700138 platform_init();
Amol Jadia55c3a62013-03-18 14:49:37 -0700139
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700140 // initialize the target
141 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700142 target_init();
143
Travis Geiselbrecht68372232009-01-24 21:21:08 -0800144 dprintf(SPEW, "calling apps_init()\n");
145 apps_init();
146
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700147 return 0;
148}
149
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800150#if (ENABLE_NANDWRITE)
151void bootstrap_nandwrite(void)
152{
153 dprintf(SPEW, "top of bootstrap2()\n");
154
155 arch_init();
156
157 // initialize the rest of the platform
158 dprintf(SPEW, "initializing platform\n");
159 platform_init();
160
161 // initialize the target
162 dprintf(SPEW, "initializing target\n");
163 target_init();
164
165 dprintf(SPEW, "calling nandwrite_init()\n");
166 nandwrite_init();
167
168 return 0;
169}
170#endif