blob: a40aa7d8fea4f9cf7050673b1ab8d87956b0f7d1 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
Maria Yud3200512014-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{
Parth Dixit826f5752016-08-16 17:23:32 +053068 thread_t *thr;
69
travis geiselbrecht858b1ad2008-12-23 21:55:41 +000070 // get us into some sort of thread context
71 thread_init_early();
72
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070073 // early arch stuff
74 arch_early_init();
75
76 // do any super early platform initialization
77 platform_early_init();
78
79 // do any super early target initialization
80 target_early_init();
81
Travis Geiselbrechteb946052008-09-07 22:32:49 -070082 dprintf(INFO, "welcome to lk\n\n");
Amol Jadia55c3a62013-03-18 14:49:37 -070083 bs_set_timestamp(BS_BL_START);
84
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070085 // deal with any static constructors
Travis Geiselbrechteb946052008-09-07 22:32:49 -070086 dprintf(SPEW, "calling constructors\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070087 call_constructors();
88
89 // bring up the kernel heap
Travis Geiselbrechteb946052008-09-07 22:32:49 -070090 dprintf(SPEW, "initializing heap\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070091 heap_init();
92
Maria Yud3200512014-05-28 15:57:51 +080093 __stack_chk_guard_setup();
94
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070095 // initialize the threading system
Travis Geiselbrechteb946052008-09-07 22:32:49 -070096 dprintf(SPEW, "initializing threads\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070097 thread_init();
98
99 // initialize the dpc system
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700100 dprintf(SPEW, "initializing dpc\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700101 dpc_init();
102
103 // initialize kernel timers
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700104 dprintf(SPEW, "initializing timers\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700105 timer_init();
106
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800107#if (!ENABLE_NANDWRITE)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700108 // create a thread to complete system initialization
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700109 dprintf(SPEW, "creating bootstrap completion thread\n");
Parth Dixit826f5752016-08-16 17:23:32 +0530110 thr = thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
111 if (!thr)
112 {
113 panic("failed to create thread bootstrap2\n");
114 }
115 thread_resume(thr);
116
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700117
118 // enable interrupts
119 exit_critical_section();
120
121 // become the idle thread
122 thread_become_idle();
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800123#else
124 bootstrap_nandwrite();
125#endif
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700126}
127
128int main(void);
129
130static int bootstrap2(void *arg)
131{
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700132 dprintf(SPEW, "top of bootstrap2()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700133
134 arch_init();
135
Travis Geiselbrechte8560832010-05-06 14:34:26 -0700136 // XXX put this somewhere else
137#if WITH_LIB_BIO
138 bio_init();
139#endif
140#if WITH_LIB_FS
141 fs_init();
142#endif
143
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700144 // initialize the rest of the platform
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700145 dprintf(SPEW, "initializing platform\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700146 platform_init();
Amol Jadia55c3a62013-03-18 14:49:37 -0700147
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700148 // initialize the target
149 dprintf(SPEW, "initializing target\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700150 target_init();
151
Travis Geiselbrecht68372232009-01-24 21:21:08 -0800152 dprintf(SPEW, "calling apps_init()\n");
153 apps_init();
154
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700155 return 0;
156}
157
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800158#if (ENABLE_NANDWRITE)
159void bootstrap_nandwrite(void)
160{
161 dprintf(SPEW, "top of bootstrap2()\n");
162
163 arch_init();
164
165 // initialize the rest of the platform
166 dprintf(SPEW, "initializing platform\n");
167 platform_init();
168
169 // initialize the target
170 dprintf(SPEW, "initializing target\n");
171 target_init();
172
173 dprintf(SPEW, "calling nandwrite_init()\n");
174 nandwrite_init();
175
176 return 0;
177}
178#endif