1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 // This module is similar to a traditional assembly startup file paired with a 17 // linker script. It turns out that everything typically done in ARMv7-M 18 // assembly startup can be done straight from C code. This makes startup code 19 // easier to maintain, modify, and read. 20 // 21 // Core initialization is comprised of two primary parts: 22 // 23 // 1. Load boot information from ARMv7-M Vector Table: The ARMv7-M vector table 24 // (See ARMv7-M Architecture Reference Manual DDI 0403E.b section B1.5) 25 // dictates the starting Program Counter (PC) and Stack Pointer (SP) when the 26 // SoC powers on. The vector table also contains a number of other vectors to 27 // handle different exceptions. This module does not provide a vector table, 28 // but it does account for it in the linker script. 29 // 30 // 2. Initialize static memory: When execution begins due to SoC power-on (or 31 // the device is reset), static memory regions must be initialized to ensure 32 // they contains the expected values when code begins to run. The SoC doesn't 33 // inherently have a notion of how to do this, so before ANYTHING else the 34 // memory must be initialized. This is done at the beginning of 35 // pw_boot_Entry(). 36 // 37 // 38 // The simple flow is as follows: 39 // Power on -> PC and SP set (from vector_table by SoC) -> pw_boot_Entry() 40 // 41 // In pw_boot_Entry(): 42 // Initialize memory -> pw_PreMainInit() -> main() 43 44 #include <stdint.h> 45 46 #include "pw_preprocessor/compiler.h" 47 #include "pw_preprocessor/util.h" 48 49 PW_EXTERN_C_START 50 51 // The following extern symbols are provided by the linker script, and their 52 // values are accessible via the reference of the symbol. 53 // 54 // Example: 55 // if (stack_pointer < &pw_boot_stack_low_addr) { 56 // PW_LOG_ERROR("Main stack overflowed!") 57 // } 58 59 // pw_boot_stack_[low/high]_addr indicate the range of the main stack. Note that 60 // this might not be the only stack in the system. 61 // 62 // The main stack pointer (sp_main) should be initialized to 63 // pw_boot_stack_high_addr. This can be done by inserting the address into index 64 // 0 of the ARMv7-M vector table. (See ARMv7-M Architecture Reference Manual DDI 65 // 0403E.b section B1.5.3) 66 extern uint8_t pw_boot_stack_low_addr; 67 extern uint8_t pw_boot_stack_high_addr; 68 69 // pw_boot_heap_[low/high]_addr indicate the address range reserved for the 70 // heap. 71 extern uint8_t pw_boot_heap_low_addr; 72 extern uint8_t pw_boot_heap_high_addr; 73 74 // The address that denotes the beginning of the .vector_table section. This 75 // can be used to set VTOR (vector table offset register) by the bootloader. 76 extern uint8_t pw_boot_vector_table_addr; 77 78 // Forward declaration of main. Pigweed applications are expected to implement 79 // this function. An implementation of main() is NOT provided by this module. 80 int main(void); 81 82 // Reset handler or boot entry point. 83 // 84 // For this module to work as expected, index 1 of the ARMv7-M vector table 85 // (which usually points to Reset_Handler) must be set to point to this 86 // function. This function is implemented by pw_boot_armv7m, and does early 87 // memory initialization. 88 PW_NO_RETURN void pw_boot_Entry(void); 89 90 // pw_boot hook: Before static memory is initialized (user supplied) 91 // 92 // This is a hook function that users of pw_boot must supply. It is called 93 // immediately upon entry to pw_boot_Entry() and before zero initialization of 94 // RAM (.bss) and loading values into static memory (commonly labeled as the 95 // .data section in an ELF file). 96 // WARNING: Be EXTREMELY careful when in the context of this function as it 97 // violates the C spec in several ways as .bss has not yet been zero-initialized 98 // and static values have not yet been loaded into memory. This function is NOT 99 // implemented by pw_boot_armv7m. 100 void pw_boot_PreStaticMemoryInit(void); 101 102 // pw_boot hook: Before C++ static constructors are invoked (user supplied). 103 // 104 // This is a hook function that users of pw_boot must supply. It is called just 105 // after zero initialization of RAM and loading values into static memory 106 // (commonly labeled as the .data section in an ELF file). Per the naming, this 107 // function is called just before C++ static constructors are invoked. It is 108 // safe to run C code, but NOT safe to call out to any C++ code. This function 109 // is NOT implemented by pw_boot_armv7m. 110 void pw_boot_PreStaticConstructorInit(void); 111 112 // pw_boot hook: Before main is invoked (user supplied). 113 // 114 // This is a hook function that users of pw_boot must supply. It is called by 115 // pw_boot_Entry() after memory initialization but before main. This allows 116 // targets to have pre-main initialization of the device and seamlessly swap out 117 // the main() implementation. This function is NOT implemented by 118 // pw_boot_armv7m. 119 void pw_boot_PreMainInit(void); 120 121 // pw_boot hook: After main returned (user supplied). 122 // 123 // This is a hook function that users of pw_boot must supply. It is called by 124 // pw_boot_Entry() after main() has returned. This function must not return! 125 // This function is NOT implemented by pw_boot_armv7m. 126 PW_NO_RETURN void pw_boot_PostMain(void); 127 128 PW_EXTERN_C_END 129