Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 4 | * Copyright (c) 2009, Code Aurora Forum. All rights reserved. |
| 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> |
| 36 | |
| 37 | extern void *__ctor_list; |
| 38 | extern void *__ctor_end; |
| 39 | extern int __bss_start; |
| 40 | extern int _end; |
| 41 | |
| 42 | static int bootstrap2(void *arg); |
| 43 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 44 | #if (ENABLE_NANDWRITE) |
| 45 | void bootstrap_nandwrite(void); |
| 46 | #endif |
| 47 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 48 | static void call_constructors(void) |
| 49 | { |
| 50 | void **ctor; |
| 51 | |
| 52 | ctor = &__ctor_list; |
| 53 | while(ctor != &__ctor_end) { |
| 54 | void (*func)(void); |
| 55 | |
| 56 | func = (void (*)())*ctor; |
| 57 | |
| 58 | func(); |
| 59 | ctor++; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* called from crt0.S */ |
| 64 | void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE; |
| 65 | void kmain(void) |
| 66 | { |
travis geiselbrecht | 858b1ad | 2008-12-23 21:55:41 +0000 | [diff] [blame] | 67 | // get us into some sort of thread context |
| 68 | thread_init_early(); |
| 69 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 70 | // early arch stuff |
| 71 | arch_early_init(); |
| 72 | |
| 73 | // do any super early platform initialization |
| 74 | platform_early_init(); |
| 75 | |
| 76 | // do any super early target initialization |
| 77 | target_early_init(); |
| 78 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 79 | dprintf(INFO, "welcome to lk\n\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 80 | |
| 81 | // deal with any static constructors |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 82 | dprintf(SPEW, "calling constructors\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 83 | call_constructors(); |
| 84 | |
| 85 | // bring up the kernel heap |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 86 | dprintf(SPEW, "initializing heap\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 87 | heap_init(); |
| 88 | |
| 89 | // initialize the threading system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 90 | dprintf(SPEW, "initializing threads\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 91 | thread_init(); |
| 92 | |
| 93 | // initialize the dpc system |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 94 | dprintf(SPEW, "initializing dpc\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 95 | dpc_init(); |
| 96 | |
| 97 | // initialize kernel timers |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 98 | dprintf(SPEW, "initializing timers\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 99 | timer_init(); |
| 100 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 101 | #if (!ENABLE_NANDWRITE) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 102 | // create a thread to complete system initialization |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 103 | dprintf(SPEW, "creating bootstrap completion thread\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 104 | thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); |
| 105 | |
| 106 | // enable interrupts |
| 107 | exit_critical_section(); |
| 108 | |
| 109 | // become the idle thread |
| 110 | thread_become_idle(); |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 111 | #else |
| 112 | bootstrap_nandwrite(); |
| 113 | #endif |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int main(void); |
| 117 | |
| 118 | static int bootstrap2(void *arg) |
| 119 | { |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 120 | dprintf(SPEW, "top of bootstrap2()\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 121 | |
| 122 | arch_init(); |
| 123 | |
Travis Geiselbrecht | e856083 | 2010-05-06 14:34:26 -0700 | [diff] [blame] | 124 | // XXX put this somewhere else |
| 125 | #if WITH_LIB_BIO |
| 126 | bio_init(); |
| 127 | #endif |
| 128 | #if WITH_LIB_FS |
| 129 | fs_init(); |
| 130 | #endif |
| 131 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 132 | // initialize the rest of the platform |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 133 | dprintf(SPEW, "initializing platform\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 134 | platform_init(); |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 135 | |
| 136 | // initialize the target |
| 137 | dprintf(SPEW, "initializing target\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 138 | target_init(); |
| 139 | |
Travis Geiselbrecht | 6837223 | 2009-01-24 21:21:08 -0800 | [diff] [blame] | 140 | dprintf(SPEW, "calling apps_init()\n"); |
| 141 | apps_init(); |
| 142 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Chandan Uddaraju | bbec2b0 | 2009-12-16 13:27:55 -0800 | [diff] [blame] | 146 | #if (ENABLE_NANDWRITE) |
| 147 | void bootstrap_nandwrite(void) |
| 148 | { |
| 149 | dprintf(SPEW, "top of bootstrap2()\n"); |
| 150 | |
| 151 | arch_init(); |
| 152 | |
| 153 | // initialize the rest of the platform |
| 154 | dprintf(SPEW, "initializing platform\n"); |
| 155 | platform_init(); |
| 156 | |
| 157 | // initialize the target |
| 158 | dprintf(SPEW, "initializing target\n"); |
| 159 | target_init(); |
| 160 | |
| 161 | dprintf(SPEW, "calling nandwrite_init()\n"); |
| 162 | nandwrite_init(); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | #endif |