Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 4 | * Copyright (c) 2009-2013, 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 | { |
travis geiselbrecht | 858b1ad | 2008-12-23 21:55:41 +0000 | [diff] [blame] | 68 | // get us into some sort of thread context |
| 69 | thread_init_early(); |
| 70 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 71 | // 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 Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 80 | dprintf(INFO, "welcome to lk\n\n"); |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 81 | bs_set_timestamp(BS_BL_START); |
| 82 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 83 | // deal with any static constructors |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 84 | dprintf(SPEW, "calling constructors\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 85 | call_constructors(); |
| 86 | |
| 87 | // bring up the kernel heap |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 88 | dprintf(SPEW, "initializing heap\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 89 | heap_init(); |
| 90 | |
| 91 | // initialize the threading system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 92 | dprintf(SPEW, "initializing threads\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 93 | thread_init(); |
| 94 | |
| 95 | // initialize the dpc system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 96 | dprintf(SPEW, "initializing dpc\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 97 | dpc_init(); |
| 98 | |
| 99 | // initialize kernel timers |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 100 | dprintf(SPEW, "initializing timers\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 101 | timer_init(); |
| 102 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 103 | #if (!ENABLE_NANDWRITE) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 104 | // create a thread to complete system initialization |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 105 | dprintf(SPEW, "creating bootstrap completion thread\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 106 | 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 Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 113 | #else |
| 114 | bootstrap_nandwrite(); |
| 115 | #endif |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int main(void); |
| 119 | |
| 120 | static int bootstrap2(void *arg) |
| 121 | { |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 122 | dprintf(SPEW, "top of bootstrap2()\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 123 | |
| 124 | arch_init(); |
| 125 | |
Travis Geiselbrecht | e856083 | 2010-05-06 14:34:26 -0700 | [diff] [blame] | 126 | // 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 Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 134 | // initialize the rest of the platform |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 135 | dprintf(SPEW, "initializing platform\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 136 | platform_init(); |
Amol Jadi | a55c3a6 | 2013-03-18 14:49:37 -0700 | [diff] [blame] | 137 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 138 | // initialize the target |
| 139 | dprintf(SPEW, "initializing target\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 140 | target_init(); |
| 141 | |
Travis Geiselbrecht | 6837223 | 2009-01-24 21:21:08 -0800 | [diff] [blame] | 142 | dprintf(SPEW, "calling apps_init()\n"); |
| 143 | apps_init(); |
| 144 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 148 | #if (ENABLE_NANDWRITE) |
| 149 | void 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 |