Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
Maria Yu | d320051 | 2014-05-28 15:57:51 +0800 | [diff] [blame] | 4 | * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved. |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 5 | * |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 6 | * 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 Geiselbrecht | 6837223 | 2009-01-24 21:21:08 -0800 | [diff] [blame] | 28 | #include <app.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 29 | #include <arch.h> |
| 30 | #include <platform.h> |
| 31 | #include <target.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 32 | #include <lib/heap.h> |
| 33 | #include <kernel/thread.h> |
| 34 | #include <kernel/timer.h> |
| 35 | #include <kernel/dpc.h> |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 36 | #include <boot_stats.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 37 | |
| 38 | extern void *__ctor_list; |
| 39 | extern void *__ctor_end; |
| 40 | extern int __bss_start; |
| 41 | extern int _end; |
| 42 | |
| 43 | static int bootstrap2(void *arg); |
| 44 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 45 | #if (ENABLE_NANDWRITE) |
| 46 | void bootstrap_nandwrite(void); |
| 47 | #endif |
| 48 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 49 | static void call_constructors(void) |
| 50 | { |
| 51 | void **ctor; |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 52 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 53 | 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 */ |
| 65 | void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE; |
| 66 | void kmain(void) |
| 67 | { |
Parth Dixit | 826f575 | 2016-08-16 17:23:32 +0530 | [diff] [blame] | 68 | thread_t *thr; |
| 69 | |
travis geiselbrecht | 858b1ad | 2008-12-23 21:55:41 +0000 | [diff] [blame] | 70 | // get us into some sort of thread context |
| 71 | thread_init_early(); |
| 72 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 73 | // 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 Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 82 | dprintf(INFO, "welcome to lk\n\n"); |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 83 | bs_set_timestamp(BS_BL_START); |
| 84 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 85 | // deal with any static constructors |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 86 | dprintf(SPEW, "calling constructors\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 87 | call_constructors(); |
| 88 | |
| 89 | // bring up the kernel heap |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 90 | dprintf(SPEW, "initializing heap\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 91 | heap_init(); |
| 92 | |
Maria Yu | d320051 | 2014-05-28 15:57:51 +0800 | [diff] [blame] | 93 | __stack_chk_guard_setup(); |
| 94 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 95 | // initialize the threading system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 96 | dprintf(SPEW, "initializing threads\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 97 | thread_init(); |
| 98 | |
| 99 | // initialize the dpc system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 100 | dprintf(SPEW, "initializing dpc\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 101 | dpc_init(); |
| 102 | |
| 103 | // initialize kernel timers |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 104 | dprintf(SPEW, "initializing timers\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 105 | timer_init(); |
| 106 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 107 | #if (!ENABLE_NANDWRITE) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 108 | // create a thread to complete system initialization |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 109 | dprintf(SPEW, "creating bootstrap completion thread\n"); |
Parth Dixit | 826f575 | 2016-08-16 17:23:32 +0530 | [diff] [blame] | 110 | 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 Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 117 | |
| 118 | // enable interrupts |
| 119 | exit_critical_section(); |
| 120 | |
| 121 | // become the idle thread |
| 122 | thread_become_idle(); |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 123 | #else |
| 124 | bootstrap_nandwrite(); |
| 125 | #endif |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int main(void); |
| 129 | |
| 130 | static int bootstrap2(void *arg) |
| 131 | { |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 132 | dprintf(SPEW, "top of bootstrap2()\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 133 | |
| 134 | arch_init(); |
| 135 | |
Travis Geiselbrecht | e856083 | 2010-05-06 14:34:26 -0700 | [diff] [blame] | 136 | // 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 Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 144 | // initialize the rest of the platform |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 145 | dprintf(SPEW, "initializing platform\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 146 | platform_init(); |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 147 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 148 | // initialize the target |
| 149 | dprintf(SPEW, "initializing target\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 150 | target_init(); |
| 151 | |
Travis Geiselbrecht | 6837223 | 2009-01-24 21:21:08 -0800 | [diff] [blame] | 152 | dprintf(SPEW, "calling apps_init()\n"); |
| 153 | apps_init(); |
| 154 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 158 | #if (ENABLE_NANDWRITE) |
| 159 | void 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 |