diff --git a/bootrom/bootrom.c b/bootrom/bootrom.c index ddd4f731c..3583adbd9 100644 --- a/bootrom/bootrom.c +++ b/bootrom/bootrom.c @@ -17,21 +17,42 @@ // Main code for the bootloader //----------------------------------------------------------------------------- -#include "clocks.h" -#include "usb_cdc.h" +#include "commonutil.h" +#include "flash_code_apis.h" +#include "usb_cdc_apis.h" +#include "gpio_apis.h" +#include "sys_apis.h" +#include "ticks_apis.h" +#include "proxmark3_arm.h" #ifdef WITH_FLASH #include "flashmem.h" #endif -#include "proxmark3_arm.h" #define DEBUG 0 +// At present, in the case of at32 with a flash size of 4m byte, a sector is 4096 bytes. +// If there is a larger size sector in the future, remember to modify it here. +#define FLASH_MIN_UNIT_DATA_SIZE 4096 +typedef struct { + uint32_t count; + uint32_t data[FLASH_MIN_UNIT_DATA_SIZE / sizeof(uint32_t)]; +} flash_min_unit_data_t; + +// An information segment memory shared between bootrom and osimage. common_area_t g_common_area __attribute__((section(".commonarea"))); +// The start address & end address of flash for writing. uint32_t start_addr, end_addr; +// Is bootrom unlocked? if true, the bootrom can be overwritten. bool bootrom_unlocked; -extern uint32_t _bootrom_start[], _bootrom_end[], _flash_start[], _flash_end[], _osimage_entry[], __bss_start__[], __bss_end__[]; +// Buffer the firmware block data from USB, and write it to FLASH once when the minimum write unit is reached. +flash_min_unit_data_t flash_min_unit_data; +// Define in link script(ld) +extern uint32_t _bootrom_start[], _bootrom_end[], _flash_start[], _flash_end[], __bss_start__[], __bss_end__[]; +extern uint32_t _osimage_entry[], _stack_start[], _stack_end[]; + +// Send an old frame response packet. static int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) { PacketResponseOLD txcmd; @@ -56,6 +77,18 @@ static int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, return usb_write((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); } +// Check the table to see if the magic is valid. +// TODO DXL Reuse functions similar to CheckValidInformationMagic? +static bool is_valid_magic(int magic) { + int magics[] = { VERSION_INFORMATION_MAGIC_PM3V, VERSION_INFORMATION_MAGIC_PM5V }; + for (int i = 0; i < ARRAYLEN(magics); i++) { + if (magics[i] == magic) { + return true; + } + } + return false; +} + #if DEBUG static void DbpString(char *str) { uint8_t len = 0; @@ -66,60 +99,10 @@ static void DbpString(char *str) { } #endif -static void ConfigClocks(void) { - // we are using a 16 MHz crystal as the basis for everything - // slow clock runs at 32kHz typical regardless of crystal - - // enable system clock and USB clock - AT91C_BASE_PMC->PMC_SCER |= AT91C_PMC_PCK | AT91C_PMC_UDP; - - // enable the clock to the following peripherals - AT91C_BASE_PMC->PMC_PCER = - (1 << AT91C_ID_PIOA) | - (1 << AT91C_ID_ADC) | - (1 << AT91C_ID_SPI) | - (1 << AT91C_ID_SSC) | - (1 << AT91C_ID_PWMC) | - (1 << AT91C_ID_UDP); - - mck_from_slck_to_pll(); -} - static void Fatal(void) { for (;;) {}; } -static uint32_t flash_size_from_cidr(uint32_t cidr) { - uint8_t nvpsiz = (cidr & 0xF00) >> 8; - switch (nvpsiz) { - case 0: - return 0; - case 1: - return 8 * 1024; - case 2: - return 16 * 1024; - case 3: - return 32 * 1024; - case 5: - return 64 * 1024; - case 7: - return 128 * 1024; - case 9: - return 256 * 1024; - case 10: - return 512 * 1024; - case 12: - return 1024 * 1024; - case 14: - default: // for 'reserved' values, guess 2MB - return 2048 * 1024; - } -} - -static uint32_t get_flash_size(void) { - return flash_size_from_cidr(*AT91C_DBGU_CIDR); -} - static void UsbPacketReceived(uint8_t *packet) { bool ack = true; PacketCommandOLD *c = (PacketCommandOLD *)packet; @@ -137,7 +120,8 @@ static void UsbPacketReceived(uint8_t *packet) { DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH | DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO | DEVICE_INFO_FLAG_UNDERSTANDS_VERSION | - DEVICE_INFO_FLAG_UNDERSTANDS_READ_MEM; + DEVICE_INFO_FLAG_UNDERSTANDS_READ_MEM | + DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_TYPE; if (g_common_area.flags.osimage_present) { arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT; @@ -149,11 +133,18 @@ static void UsbPacketReceived(uint8_t *packet) { case CMD_CHIP_INFO: { ack = false; - arg0 = *(AT91C_DBGU_CIDR); + arg0 = GetChipId(); reply_old(CMD_CHIP_INFO, arg0, 0, 0, 0, 0); } break; + case CMD_CHIP_TYPE: { + ack = false; + arg0 = GetChipType(); + reply_old(CMD_CHIP_TYPE, arg0, 0, 0, 0, 0); + } + break; + case CMD_BL_VERSION: { ack = false; arg0 = BL_VERSION_1_0_0; @@ -177,7 +168,7 @@ static void UsbPacketReceived(uint8_t *packet) { base = (uint8_t *) _flash_start; - size_t flash_size = get_flash_size(); + size_t flash_size = GetChipFlashSize(); // Boundary check the offset. if (offset > flash_size) { @@ -217,53 +208,85 @@ static void UsbPacketReceived(uint8_t *packet) { } case CMD_FINISH_WRITE: { -#if defined ICOPYX - if (c->arg[1] == 0xff && c->arg[2] == 0x1fd) { -#endif - for (int j = 0; j < 2; j++) { - uint32_t flash_address = arg0 + (0x100 * j); - AT91PS_EFC efc_bank = AT91C_BASE_EFC0; - int offset = 0; - uint32_t page_n = (flash_address - (uint32_t)_flash_start) / AT91C_IFLASH_PAGE_SIZE; - if (page_n >= AT91C_IFLASH_NB_OF_PAGES / 2) { - page_n -= AT91C_IFLASH_NB_OF_PAGES / 2; - efc_bank = AT91C_BASE_EFC1; - // We need to offset the writes or it will not fill the correct bank write buffer. - offset = (AT91C_IFLASH_NB_OF_PAGES / 2) * AT91C_IFLASH_PAGE_SIZE / sizeof(uint32_t); - } - for (int i = 0 + (64 * j); i < 64 + (64 * j); i++) { - _flash_start[offset + i] = c->d.asDwords[i]; - } - /* Check that the address that we are supposed to write to is within our allowed region */ - if (((flash_address + AT91C_IFLASH_PAGE_SIZE - 1) >= end_addr) || (flash_address < start_addr)) { - /* Disallow write */ - ack = false; - reply_old(CMD_NACK, 0, 0, 0, 0, 0); - } else { + // For this COMMAND Note + // --- + // 20260604: In older versions, the response arg1 was 0x00; in newer versions, it will be changed to PM3_E* error codes. + // These codes are used to transmit specific error information to the client in cases such as out-of-bounds access; + // they are unrelated to the FLASH error status. + // --- - efc_bank->EFC_FCR = MC_FLASH_COMMAND_KEY | - MC_FLASH_COMMAND_PAGEN(page_n) | - AT91C_MC_FCMD_START_PROG; - } - - // Wait until flashing of page finishes - uint32_t sr; - while (!((sr = efc_bank->EFC_FSR) & AT91C_MC_FRDY)); - if (sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) { - ack = false; - reply_old(CMD_NACK, sr, 0, 0, 0, 0); - } - } -#if defined ICOPYX +#if defined ICOPYX // ICopyX needs special parameters to unlock boot write. + if (c->arg[1] != 0xff || c->arg[2] != 0x1fd) { + // arg[1] must be 0xff, arg[2] must be 0x1fd + // The reason why icopyx locks the boot is that the device cannot be used + // due to the possibility of incorrect firmware flash. Because fpga and other hardware features are different. + // If there is a better way to prevent the firmware from entering an inoperable state, this check is theoretically unnecessary. + break; } #endif + // If a valid magic is passed in, we need to check if the magic is the same as the current firmware. + if (is_valid_magic((int)c->arg[1]) && g_version_information.magic != c->arg[1]) { + ack = false; + reply_old(CMD_NACK, 0, PM3_EINVARG, 0, 0, 0); + break; + } + // Get current flash min erase/write unit of platform in bytes(not u32). + const uint16_t flash_ew_unit = FlashCodeGetEWMinUnit(); + const uint16_t flash_ew_unit_u32 = flash_ew_unit / sizeof(uint32_t); // count of min erase/write unit(u32) + // The fixed data payload is 512 bytes, which is 128 u32. + const uint16_t usb_payload_u32_len = sizeof(c->d) / sizeof(uint32_t); + // Copy data from usb to flash_min_unit_data buffer. A single usb payload may hold more + // than one erase/write unit (e.g. AT91 pages of 256 bytes), so always copy it in and flush + // the whole units below instead of assuming the payload is no larger than one unit. + bool copy_overflow = false; + for (int i = 0; i < usb_payload_u32_len; i++) { + // Check data buffer is no overflow. + if (flash_min_unit_data.count >= ARRAYLEN(flash_min_unit_data.data)) { + copy_overflow = true; + break; + } + flash_min_unit_data.data[flash_min_unit_data.count++] = c->d.asDwords[i]; + } + if (copy_overflow) { + ack = false; + flash_min_unit_data.count = 0; + reply_old(CMD_NACK, 0, PM3_EOVFLOW, 0, 0, 0); + break; + } + // How many min unit are stored in the data buffer? + const uint16_t flash_unit_num_u32 = flash_min_unit_data.count / flash_ew_unit_u32; + for (int idx_unit = 0; idx_unit < flash_unit_num_u32; idx_unit++) { + // Calculate the write start address of the new flash unit. + uint32_t flash_address = arg0 + idx_unit * flash_ew_unit; + // Check that the address that we are supposed to write to is within our allowed region + if (((flash_address + flash_ew_unit - 1) >= end_addr) || (flash_address < start_addr)) { + ack = false; // Disallow write + reply_old(CMD_NACK, 0, PM3_EOUTOFBOUND, 0, 0, 0); + break; + } + uint32_t *flash_min_unit_addr = &flash_min_unit_data.data[idx_unit * flash_ew_unit_u32]; + // Call the cross-platform flash api to write firmware to flash. + uint32_t status = 0x00; + bool isok = FlashCodeEWriteMinUnit(flash_address, flash_min_unit_addr, _flash_start, &status); + if (!isok) { + ack = false; + reply_old(CMD_NACK, status, 0, 0, 0, 0); + break; + } + } + if (ack) { + // After flushing whole units, keep the remaining partial unit for the next transfer. + flash_min_unit_data.count %= flash_ew_unit_u32; + } else { + flash_min_unit_data.count = 0; // Discard buffered data after a failed write. + } } break; case CMD_HARDWARE_RESET: { usb_disable(); - AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST; + ResetChip(); } break; @@ -273,12 +296,13 @@ static void UsbPacketReceived(uint8_t *packet) { else bootrom_unlocked = false; - uint32_t cmd_start = c->arg[0]; - uint32_t cmd_end = c->arg[1]; + uint32_t cmd_start = c->arg[0]; // code flash start address + uint32_t cmd_end = c->arg[1]; // code flash end address - /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected - * bootrom area. In any case they must be within the flash area. - */ + /* + * Only allow command if the bootrom is unlocked, or the parameters are outside of the protected + * bootrom area. In any case they must be within the flash area. + */ if ((bootrom_unlocked || ((cmd_start >= (uint32_t)_bootrom_end) || (cmd_end < (uint32_t)_bootrom_start))) && (cmd_start >= (uint32_t)_flash_start) && (cmd_end <= (uint32_t)_flash_end)) { @@ -286,6 +310,9 @@ static void UsbPacketReceived(uint8_t *packet) { end_addr = cmd_end; } else { start_addr = end_addr = 0; + flash_min_unit_data.count = 0; + // In this command, flasher.c does not care what arg0 is; + // it considers the process to have failed as long as a NACK response is received. ack = false; reply_old(CMD_NACK, 0, 0, 0, 0, 0); } @@ -303,16 +330,11 @@ static void UsbPacketReceived(uint8_t *packet) { } } -// delay_loop(1) = 3.07us -static volatile uint32_t ccc; -static void __attribute__((optimize("O0"))) delay_loop(uint32_t delay) { - for (ccc = delay * 2; ccc; ccc--) {}; -} - static void flash_mode(void) { start_addr = 0; end_addr = 0; bootrom_unlocked = false; + flash_min_unit_data.count = 0; uint8_t rx[sizeof(PacketCommandOLD)]; g_common_area.command = COMMON_AREA_COMMAND_NONE; if (!g_common_area.flags.button_pressed && BUTTON_PRESS()) { @@ -331,7 +353,7 @@ static void flash_mode(void) { usb_enable(); // wait for reset to be complete? - delay_loop(100000); + SpinDelayUs(300 * 1000); // Wait for 300ms for (;;) { WDT_HIT(); @@ -344,8 +366,7 @@ static void flash_mode(void) { } bool button_state = BUTTON_PRESS(); - // ~10ms, prevent jitter - delay_loop(3333); + SpinDelayUs(10000); // ~10ms, prevent jitter if (button_state != BUTTON_PRESS()) { // in jitter state, ignore continue; @@ -358,93 +379,25 @@ static void flash_mode(void) { g_common_area.flags.button_pressed = 1; usb_disable(); LED_B_ON(); - AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST; + ResetChip(); for (;;) {}; } } } -void BootROM(void); -void BootROM(void) { - /* Set up (that is: clear) BSS. */ - uint32_t *bss_dst = __bss_start__; - while (bss_dst < __bss_end__) *bss_dst++ = 0; - - //------------ - // First set up all the I/O pins; GPIOs configured directly, other ones - // just need to be assigned to the appropriate peripheral. - - // Kill all the pullups, especially the one on USB D+; leave them for - // the unused pins, though. - AT91C_BASE_PIOA->PIO_PPUDR = - GPIO_USB_PU | - GPIO_LED_A | - GPIO_LED_B | - GPIO_LED_C | - GPIO_LED_D | - GPIO_FPGA_DIN | - GPIO_FPGA_DOUT | - GPIO_FPGA_CCLK | - GPIO_FPGA_NINIT | - GPIO_FPGA_NPROGRAM | - GPIO_FPGA_DONE | - GPIO_MUXSEL_HIPKD | - GPIO_MUXSEL_HIRAW | - GPIO_MUXSEL_LOPKD | - GPIO_MUXSEL_LORAW | - GPIO_RELAY | - GPIO_NVDD_ON; - // (and add GPIO_FPGA_ON) - // These pins are outputs - AT91C_BASE_PIOA->PIO_OER = - GPIO_LED_A | - GPIO_LED_B | - GPIO_LED_C | - GPIO_LED_D | - GPIO_RELAY | - GPIO_NVDD_ON; - // PIO controls the following pins - AT91C_BASE_PIOA->PIO_PER = - GPIO_USB_PU | - GPIO_LED_A | - GPIO_LED_B | - GPIO_LED_C | - GPIO_LED_D; - - // USB_D_PLUS_PULLUP_OFF(); - usb_disable(); - LED_D_OFF(); - LED_C_ON(); - LED_B_OFF(); - LED_A_OFF(); - - // Set the first 256KB memory flashspeed - AT91C_BASE_EFC0->EFC_FMR = AT91C_MC_FWS_1FWS | MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48); - - // 9 = 256, 10+ is 512KB - uint8_t id = (*(AT91C_DBGU_CIDR) & 0xF00) >> 8; - if (id > 9) - AT91C_BASE_EFC1->EFC_FMR = AT91C_MC_FWS_1FWS | MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48); - - // Initialize all system clocks - ConfigClocks(); - - LED_A_ON(); - - int g_common_area_present = 0; - switch (AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_RSTTYP) { - case AT91C_RSTC_RSTTYP_WATCHDOG: - case AT91C_RSTC_RSTTYP_SOFTWARE: - case AT91C_RSTC_RSTTYP_USER: - /* In these cases the g_common_area in RAM should be ok, retain it if it's there */ - if (g_common_area.magic == COMMON_AREA_MAGIC && g_common_area.version == 1) - g_common_area_present = 1; - break; - default: /* Otherwise, initialize it from scratch */ - break; +// Detect whether to enter flash mode. If the button is pressed for more than 2s, +// or if the command is set to enter flash mode, or if the OS image entry point is invalid, then enter flash mode. +static bool check_goto_flash_mode(void) { + int common_area_present = 0; + // Check if RESET is SRAM retention? if not, the content of g_common_area in RAM is not reliable, must to init. + if (CheckRSTWithSRAMRetention()) { + // In these cases the g_common_area in RAM should be ok, retain it if it's there + if (g_common_area.magic == COMMON_AREA_MAGIC && g_common_area.version == 1) { + common_area_present = 1; + } } - if (!g_common_area_present) { + if (!common_area_present) { /* Common area not ok, initialize it */ size_t i; /* Makeshift memset, no need to drag util.c into this */ @@ -456,14 +409,66 @@ void BootROM(void) { } g_common_area.flags.bootrom_present = 1; - if ((g_common_area.command == COMMON_AREA_COMMAND_ENTER_FLASH_MODE) || - (!g_common_area.flags.button_pressed && BUTTON_PRESS()) || - (*_osimage_entry == 0xffffffffU)) { + // Handle the event of button startup separately. (Pressing the button is no longer considered as necessary to enter BOOT.) + bool to_flash_mode = false; + if (!g_common_area.flags.button_pressed && BUTTON_PRESS()) { + uint32_t time_counter = 0; + to_flash_mode = true; + while (time_counter++ < 3000) { // It is necessary to press and hold the button for more than 2s before entering BOOT. + if (!BUTTON_PRESS()) { + to_flash_mode = false; // If the button is not pressed for more than 2s, exit BOOT. + break; + } + SpinDelayUs(1000); // 1ms + } + } else if ((g_common_area.command == COMMON_AREA_COMMAND_ENTER_FLASH_MODE) || (*_osimage_entry == 0xffffffffU)) { + to_flash_mode = true; + } + + return to_flash_mode; +} + +void BootROM(void); +void BootROM(void) { + // __BKPT(0); // For debug + + /* Set up (that is: clear) BSS. */ + uint32_t *bss_dst = __bss_start__; + while (bss_dst < __bss_end__) *bss_dst++ = 0; + + //------------ + // First set up all the I/O pins; GPIOs configured directly, other ones + // just need to be assigned to the appropriate peripheral. + gpio_sysboot_setup(); + + // Turn off all leds + LED_A_OFF(); + LED_B_OFF(); + LED_C_OFF(); + LED_D_OFF(); + + // USB_D_PLUS_PULLUP_OFF(); + usb_disable(); + + // Initialize the FLASH area for firmware/code. + FlashCodeInit(); + + // Initialize all system clocks + ConfigSystemClocks(); + + // Check whether to enter the FLASH mode. + const bool to_flash_mode = check_goto_flash_mode(); + + LED_C_ON(); + LED_A_ON(); + + // Keep running in BOOT or jump to App image? + if (to_flash_mode) { flash_mode(); } else { // clear button status, even if button still pressed g_common_area.flags.button_pressed = 0; - // jump to Flash address of the osimage entry point (LSBit set for thumb mode) - __asm("bx %0\n" : : "r"(((uint32_t)_osimage_entry) | 0x1)); + // jump to OS image + JumpToAnyImage((uint32_t)_stack_end, (uint32_t)_osimage_entry); } } diff --git a/bootrom/flash-reset-at32.s b/bootrom/flash-reset-at32.s new file mode 100644 index 000000000..7977817d2 --- /dev/null +++ b/bootrom/flash-reset-at32.s @@ -0,0 +1,70 @@ +.section .startup,"ax" +.syntax unified +.cpu cortex-m4 +.fpu softvfp +.thumb + +/* ------------------------------------------------------------------- */ + +.global flashstart +.type flashstart, %object +flashstart: + + .word _stack_end + .word reset + .word default_handler + .word default_handler + .word default_handler + .word default_handler + .word default_handler + .word 0 + .word 0 + .word 0 + .word 0 + .word default_handler + .word default_handler + .word 0 + .word default_handler + .word default_handler + +/* ------------------------------------------------------------------- */ + +.thumb_func +.type reset, %function +reset: + /* Copy the bootrom to sram */ + ldr r0, =__bootphase2_src_start__ + ldr r1, =__bootphase2_start__ + ldr r2, =__bootphase2_end__ +1: + ldr r3, [r0], #4 + str r3, [r1], #4 + cmp r1, r2 + blo 1b + + /* + * DXL Note: + * It is very important to ensure that the memory expansion configuration is correct before accessing the memory! + * Do not use _stack_end, because this memory area may be temporarily inaccessible. + * If you adjust from small sram to large sram, using _stack_end and function call will result in hardware fault. + */ + ldr sp, =__stack_boot_end__ + bl Extend_SRAM + + /* Load the stack pointer(Such as _stack_end?) from the first word of the bootphase2. */ + ldr sp, =_stack_end /* Reset the stack pointer once to call JumpToAnyImage. */ + ldr r0, =__bootphase2_start__ + ldr r0, [r0] + ldr r1, =__bootphase2_start__ /* Vector table base / entry point. */ + ldr r2, =JumpToAnyImage + blx r2 + +/* ------------------------------------------------------------------- */ + +.thumb_func +.type default_handler, %function +default_handler: +1: + b 1b + + .ltorg diff --git a/bootrom/flash-reset.s b/bootrom/flash-reset-at91.s similarity index 100% rename from bootrom/flash-reset.s rename to bootrom/flash-reset-at91.s diff --git a/bootrom/ldscript-flash-at32 b/bootrom/ldscript-flash-at32 new file mode 100644 index 000000000..bdb526ff4 --- /dev/null +++ b/bootrom/ldscript-flash-at32 @@ -0,0 +1,40 @@ +/* +----------------------------------------------------------------------------- + Copyright (C) Jonathan Westhues, Mar 2006 + Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + See LICENSE.txt for the text of the license. +----------------------------------------------------------------------------- + Bootrom linker script for AT32 microcontrollers + By DXL +----------------------------------------------------------------------------- +*/ + +INCLUDE ../common_arm/ldscript.defs.at32 +INCLUDE ../common_arm/ldscript.common +INCLUDE ./ldscript-flash-common + +PHDRS +{ + stack_boot PT_LOAD; +} + +SECTIONS +{ + /* It is used in flash-reset-at32.s. To called some c functions for init/config. */ + .stack_boot : { + __stack_boot_start__ = .; + . += stacksize; + __stack_boot_end__ = .; + } >ram AT>ram :stack_boot +} diff --git a/bootrom/ldscript-flash-at91 b/bootrom/ldscript-flash-at91 new file mode 100644 index 000000000..3af530029 --- /dev/null +++ b/bootrom/ldscript-flash-at91 @@ -0,0 +1,24 @@ +/* +----------------------------------------------------------------------------- + Copyright (C) Jonathan Westhues, Mar 2006 + Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + See LICENSE.txt for the text of the license. +----------------------------------------------------------------------------- + Bootrom linker script for AT91 microcontrollers +----------------------------------------------------------------------------- +*/ + +INCLUDE ../common_arm/ldscript.defs.at91 +INCLUDE ../common_arm/ldscript.common +INCLUDE ./ldscript-flash-common \ No newline at end of file diff --git a/bootrom/ldscript-flash b/bootrom/ldscript-flash-common similarity index 95% rename from bootrom/ldscript-flash rename to bootrom/ldscript-flash-common index 374c2d6c7..50578a204 100644 --- a/bootrom/ldscript-flash +++ b/bootrom/ldscript-flash-common @@ -15,12 +15,10 @@ See LICENSE.txt for the text of the license. ----------------------------------------------------------------------------- - Bootrom linker script + Bootrom linker script common to all flash-based targets. ----------------------------------------------------------------------------- */ -INCLUDE ../common_arm/ldscript.common - PHDRS { phase1 PT_LOAD; @@ -43,7 +41,7 @@ SECTIONS } >bootphase1 :phase1 .bootphase2 : { - *(.startphase2) + KEEP(*(.startphase2)) *(.text) *(.text.*) *(.eh_frame) @@ -72,4 +70,4 @@ SECTIONS .commonarea (NOLOAD) : { *(.commonarea) } >commonarea -} +} \ No newline at end of file diff --git a/bootrom/ram-reset-at32.s b/bootrom/ram-reset-at32.s new file mode 100644 index 000000000..784ba3b13 --- /dev/null +++ b/bootrom/ram-reset-at32.s @@ -0,0 +1,486 @@ +@----------------------------------------------------------------------------- +@ RAM reset vector for relaunching the bootloader of AT32F435 +@ By DXL +@----------------------------------------------------------------------------- + +.syntax unified +.cpu cortex-m4 +.thumb + +.extern BootROM +.global g_pfnVectors +.global Default_Handler + +/* ----------------------------------------------------------------------------- */ + +@ This is the code that gets called when the processor receives an +@ unexpected interrupt. This simply enters an infinite loop, preserving +@ the system state for examination by a debugger. + +.section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop +.size Default_Handler, .-Default_Handler + +/* ----------------------------------------------------------------------------- */ + +.section .startphase2,"ax",%progbits +.type g_pfnVectors, %object +.size g_pfnVectors, .-g_pfnVectors + +g_pfnVectors: + .word _stack_end + .word BootROM /* Start from BootRom (reset_handler) */ + .word NMI_Handler + .word HardFault_Handler + .word MemManage_Handler + .word BusFault_Handler + .word UsageFault_Handler + .word 0 + .word 0 + .word 0 + .word 0 + .word SVC_Handler + .word DebugMon_Handler + .word 0 + .word PendSV_Handler + .word SysTick_Handler + + /* External Interrupts */ + .word WWDT_IRQHandler /* Window Watchdog Timer */ + .word PVM_IRQHandler /* PVM through EXINT Line detect */ + .word TAMP_STAMP_IRQHandler /* Tamper and TimeStamps through the EXINT line */ + .word ERTC_WKUP_IRQHandler /* ERTC Wakeup through the EXINT line */ + .word FLASH_IRQHandler /* Flash */ + .word CRM_IRQHandler /* CRM */ + .word EXINT0_IRQHandler /* EXINT Line 0 */ + .word EXINT1_IRQHandler /* EXINT Line 1 */ + .word EXINT2_IRQHandler /* EXINT Line 2 */ + .word EXINT3_IRQHandler /* EXINT Line 3 */ + .word EXINT4_IRQHandler /* EXINT Line 4 */ + .word EDMA_Stream1_IRQHandler /* EDMA Stream 1 */ + .word EDMA_Stream2_IRQHandler /* EDMA Stream 2 */ + .word EDMA_Stream3_IRQHandler /* EDMA Stream 3 */ + .word EDMA_Stream4_IRQHandler /* EDMA Stream 4 */ + .word EDMA_Stream5_IRQHandler /* EDMA Stream 5 */ + .word EDMA_Stream6_IRQHandler /* EDMA Stream 6 */ + .word EDMA_Stream7_IRQHandler /* EDMA Stream 7 */ + .word ADC1_2_3_IRQHandler /* ADC1 & ADC2 & ADC3 */ + .word CAN1_TX_IRQHandler /* CAN1 TX */ + .word CAN1_RX0_IRQHandler /* CAN1 RX0 */ + .word CAN1_RX1_IRQHandler /* CAN1 RX1 */ + .word CAN1_SE_IRQHandler /* CAN1 SE */ + .word EXINT9_5_IRQHandler /* EXINT Line [9:5] */ + .word TMR1_BRK_TMR9_IRQHandler /* TMR1 Brake and TMR9 */ + .word TMR1_OVF_TMR10_IRQHandler /* TMR1 Overflow and TMR10 */ + .word TMR1_TRG_HALL_TMR11_IRQHandler /* TMR1 Trigger and hall and TMR11 */ + .word TMR1_CH_IRQHandler /* TMR1 Channel */ + .word TMR2_GLOBAL_IRQHandler /* TMR2 */ + .word TMR3_GLOBAL_IRQHandler /* TMR3 */ + .word TMR4_GLOBAL_IRQHandler /* TMR4 */ + .word I2C1_EVT_IRQHandler /* I2C1 Event */ + .word I2C1_ERR_IRQHandler /* I2C1 Error */ + .word I2C2_EVT_IRQHandler /* I2C2 Event */ + .word I2C2_ERR_IRQHandler /* I2C2 Error */ + .word SPI1_IRQHandler /* SPI1 */ + .word SPI2_I2S2EXT_IRQHandler /* SPI2 */ + .word USART1_IRQHandler /* USART1 */ + .word USART2_IRQHandler /* USART2 */ + .word USART3_IRQHandler /* USART3 */ + .word EXINT15_10_IRQHandler /* EXINT Line [15:10] */ + .word ERTCAlarm_IRQHandler /* RTC Alarm through EXINT Line */ + .word OTGFS1_WKUP_IRQHandler /* OTGFS1 Wakeup from suspend */ + .word TMR8_BRK_TMR12_IRQHandler /* TMR8 Brake and TMR12 */ + .word TMR8_OVF_TMR13_IRQHandler /* TMR8 Overflow and TMR13 */ + .word TMR8_TRG_HALL_TMR14_IRQHandler /* TMR8 Trigger and hall and TMR14 */ + .word TMR8_CH_IRQHandler /* TMR8 Channel */ + .word EDMA_Stream8_IRQHandler /* EDMA Stream 8 */ + .word XMC_IRQHandler /* XMC */ + .word SDIO1_IRQHandler /* SDIO1 */ + .word TMR5_GLOBAL_IRQHandler /* TMR5 */ + .word SPI3_I2S3EXT_IRQHandler /* SPI3 */ + .word UART4_IRQHandler /* UART4 */ + .word UART5_IRQHandler /* UART5 */ + .word TMR6_DAC_GLOBAL_IRQHandler /* TMR6 & DAC */ + .word TMR7_GLOBAL_IRQHandler /* TMR7 */ + .word DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */ + .word DMA1_Channel2_IRQHandler /* DMA1 Channel 2 */ + .word DMA1_Channel3_IRQHandler /* DMA1 Channel 3 */ + .word DMA1_Channel4_IRQHandler /* DMA1 Channel 4 */ + .word DMA1_Channel5_IRQHandler /* DMA1 Channel 5 */ + .word EMAC_IRQHandler /* EMAC */ + .word EMAC_WKUP_IRQHandler /* EMAC Wakeup */ + .word CAN2_TX_IRQHandler /* CAN2 TX */ + .word CAN2_RX0_IRQHandler /* CAN2 RX0 */ + .word CAN2_RX1_IRQHandler /* CAN2 RX1 */ + .word CAN2_SE_IRQHandler /* CAN2 SE */ + .word OTGFS1_IRQHandler /* OTGFS1 */ + .word DMA1_Channel6_IRQHandler /* DMA1 Channel 6 */ + .word DMA1_Channel7_IRQHandler /* DMA1 Channel 7 */ + .word 0 /* Reserved */ + .word USART6_IRQHandler /* USART6 */ + .word I2C3_EVT_IRQHandler /* I2C3 Event */ + .word I2C3_ERR_IRQHandler /* I2C3 Error */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word OTGFS2_WKUP_IRQHandler /* OTGFS2 Wakeup from suspend */ + .word OTGFS2_IRQHandler /* OTGFS2 */ + .word DVP_IRQHandler /* DVP */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word FPU_IRQHandler /* FPU */ + .word UART7_IRQHandler /* UART7 */ + .word UART8_IRQHandler /* UART8 */ + .word SPI4_IRQHandler /* SPI4 */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word QSPI2_IRQHandler /* QSPI2 */ + .word QSPI1_IRQHandler /* QSPI1 */ + .word 0 /* Reserved */ + .word DMAMUX_IRQHandler /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word 0 /* Reserved */ + .word SDIO2_IRQHandler /* SDIO2 */ + .word ACC_IRQHandler /* ACC */ + .word TMR20_BRK_IRQHandler /* TMR20 Brake */ + .word TMR20_OVF_IRQHandler /* TMR20 Overflow */ + .word TMR20_TRG_HALL_IRQHandler /* TMR20 Trigger and hall */ + .word TMR20_CH_IRQHandler /* TMR20 Channel */ + .word DMA2_Channel1_IRQHandler /* DMA2 Channel 1 */ + .word DMA2_Channel2_IRQHandler /* DMA2 Channel 2 */ + .word DMA2_Channel3_IRQHandler /* DMA2 Channel 3 */ + .word DMA2_Channel4_IRQHandler /* DMA2 Channel 4 */ + .word DMA2_Channel5_IRQHandler /* DMA2 Channel 5 */ + .word DMA2_Channel6_IRQHandler /* DMA2 Channel 6 */ + .word DMA2_Channel7_IRQHandler /* DMA2 Channel 7 */ + +@----------------------------------------------------------------------------- +@ Provide weak aliases for each Exception handler to the Default_Handler. +@ As they are weak aliases, any function with the same name will override +@ this definition. +@----------------------------------------------------------------------------- + + .weak NMI_Handler + .thumb_set NMI_Handler,Default_Handler + + .weak HardFault_Handler + .thumb_set HardFault_Handler,Default_Handler + + .weak MemManage_Handler + .thumb_set MemManage_Handler,Default_Handler + + .weak BusFault_Handler + .thumb_set BusFault_Handler,Default_Handler + + .weak UsageFault_Handler + .thumb_set UsageFault_Handler,Default_Handler + + .weak SVC_Handler + .thumb_set SVC_Handler,Default_Handler + + .weak DebugMon_Handler + .thumb_set DebugMon_Handler,Default_Handler + + .weak PendSV_Handler + .thumb_set PendSV_Handler,Default_Handler + + .weak SysTick_Handler + .thumb_set SysTick_Handler,Default_Handler + + .weak WWDT_IRQHandler + .thumb_set WWDT_IRQHandler,Default_Handler + + .weak PVM_IRQHandler + .thumb_set PVM_IRQHandler,Default_Handler + + .weak TAMP_STAMP_IRQHandler + .thumb_set TAMP_STAMP_IRQHandler,Default_Handler + + .weak ERTC_WKUP_IRQHandler + .thumb_set ERTC_WKUP_IRQHandler,Default_Handler + + .weak FLASH_IRQHandler + .thumb_set FLASH_IRQHandler,Default_Handler + + .weak CRM_IRQHandler + .thumb_set CRM_IRQHandler,Default_Handler + + .weak EXINT0_IRQHandler + .thumb_set EXINT0_IRQHandler,Default_Handler + + .weak EXINT1_IRQHandler + .thumb_set EXINT1_IRQHandler,Default_Handler + + .weak EXINT2_IRQHandler + .thumb_set EXINT2_IRQHandler,Default_Handler + + .weak EXINT3_IRQHandler + .thumb_set EXINT3_IRQHandler,Default_Handler + + .weak EXINT4_IRQHandler + .thumb_set EXINT4_IRQHandler,Default_Handler + + .weak EDMA_Stream1_IRQHandler + .thumb_set EDMA_Stream1_IRQHandler,Default_Handler + + .weak EDMA_Stream2_IRQHandler + .thumb_set EDMA_Stream2_IRQHandler,Default_Handler + + .weak EDMA_Stream3_IRQHandler + .thumb_set EDMA_Stream3_IRQHandler,Default_Handler + + .weak EDMA_Stream4_IRQHandler + .thumb_set EDMA_Stream4_IRQHandler,Default_Handler + + .weak EDMA_Stream5_IRQHandler + .thumb_set EDMA_Stream5_IRQHandler,Default_Handler + + .weak EDMA_Stream6_IRQHandler + .thumb_set EDMA_Stream6_IRQHandler,Default_Handler + + .weak EDMA_Stream7_IRQHandler + .thumb_set EDMA_Stream7_IRQHandler,Default_Handler + + .weak ADC1_2_3_IRQHandler + .thumb_set ADC1_2_3_IRQHandler,Default_Handler + + .weak CAN1_TX_IRQHandler + .thumb_set CAN1_TX_IRQHandler,Default_Handler + + .weak CAN1_RX0_IRQHandler + .thumb_set CAN1_RX0_IRQHandler,Default_Handler + + .weak CAN1_RX1_IRQHandler + .thumb_set CAN1_RX1_IRQHandler,Default_Handler + + .weak CAN1_SE_IRQHandler + .thumb_set CAN1_SE_IRQHandler,Default_Handler + + .weak EXINT9_5_IRQHandler + .thumb_set EXINT9_5_IRQHandler,Default_Handler + + .weak TMR1_BRK_TMR9_IRQHandler + .thumb_set TMR1_BRK_TMR9_IRQHandler,Default_Handler + + .weak TMR1_OVF_TMR10_IRQHandler + .thumb_set TMR1_OVF_TMR10_IRQHandler,Default_Handler + + .weak TMR1_TRG_HALL_TMR11_IRQHandler + .thumb_set TMR1_TRG_HALL_TMR11_IRQHandler,Default_Handler + + .weak TMR1_CH_IRQHandler + .thumb_set TMR1_CH_IRQHandler,Default_Handler + + .weak TMR2_GLOBAL_IRQHandler + .thumb_set TMR2_GLOBAL_IRQHandler,Default_Handler + + .weak TMR3_GLOBAL_IRQHandler + .thumb_set TMR3_GLOBAL_IRQHandler,Default_Handler + + .weak TMR4_GLOBAL_IRQHandler + .thumb_set TMR4_GLOBAL_IRQHandler,Default_Handler + + .weak I2C1_EVT_IRQHandler + .thumb_set I2C1_EVT_IRQHandler,Default_Handler + + .weak I2C1_ERR_IRQHandler + .thumb_set I2C1_ERR_IRQHandler,Default_Handler + + .weak I2C2_EVT_IRQHandler + .thumb_set I2C2_EVT_IRQHandler,Default_Handler + + .weak I2C2_ERR_IRQHandler + .thumb_set I2C2_ERR_IRQHandler,Default_Handler + + .weak SPI1_IRQHandler + .thumb_set SPI1_IRQHandler,Default_Handler + + .weak SPI2_I2S2EXT_IRQHandler + .thumb_set SPI2_I2S2EXT_IRQHandler,Default_Handler + + .weak USART1_IRQHandler + .thumb_set USART1_IRQHandler,Default_Handler + + .weak USART2_IRQHandler + .thumb_set USART2_IRQHandler,Default_Handler + + .weak USART3_IRQHandler + .thumb_set USART3_IRQHandler,Default_Handler + + .weak EXINT15_10_IRQHandler + .thumb_set EXINT15_10_IRQHandler,Default_Handler + + .weak ERTCAlarm_IRQHandler + .thumb_set ERTCAlarm_IRQHandler,Default_Handler + + .weak OTGFS1_WKUP_IRQHandler + .thumb_set OTGFS1_WKUP_IRQHandler,Default_Handler + + .weak TMR8_BRK_TMR12_IRQHandler + .thumb_set TMR8_BRK_TMR12_IRQHandler,Default_Handler + + .weak TMR8_OVF_TMR13_IRQHandler + .thumb_set TMR8_OVF_TMR13_IRQHandler,Default_Handler + + .weak TMR8_TRG_HALL_TMR14_IRQHandler + .thumb_set TMR8_TRG_HALL_TMR14_IRQHandler,Default_Handler + + .weak TMR8_CH_IRQHandler + .thumb_set TMR8_CH_IRQHandler,Default_Handler + + .weak EDMA_Stream8_IRQHandler + .thumb_set EDMA_Stream8_IRQHandler,Default_Handler + + .weak XMC_IRQHandler + .thumb_set XMC_IRQHandler,Default_Handler + + .weak SDIO1_IRQHandler + .thumb_set SDIO1_IRQHandler,Default_Handler + + .weak TMR5_GLOBAL_IRQHandler + .thumb_set TMR5_GLOBAL_IRQHandler,Default_Handler + + .weak SPI3_I2S3EXT_IRQHandler + .thumb_set SPI3_I2S3EXT_IRQHandler,Default_Handler + + .weak UART4_IRQHandler + .thumb_set UART4_IRQHandler,Default_Handler + + .weak UART5_IRQHandler + .thumb_set UART5_IRQHandler,Default_Handler + + .weak TMR6_DAC_GLOBAL_IRQHandler + .thumb_set TMR6_DAC_GLOBAL_IRQHandler,Default_Handler + + .weak TMR7_GLOBAL_IRQHandler + .thumb_set TMR7_GLOBAL_IRQHandler,Default_Handler + + .weak DMA1_Channel1_IRQHandler + .thumb_set DMA1_Channel1_IRQHandler,Default_Handler + + .weak DMA1_Channel2_IRQHandler + .thumb_set DMA1_Channel2_IRQHandler,Default_Handler + + .weak DMA1_Channel3_IRQHandler + .thumb_set DMA1_Channel3_IRQHandler,Default_Handler + + .weak DMA1_Channel4_IRQHandler + .thumb_set DMA1_Channel4_IRQHandler,Default_Handler + + .weak DMA1_Channel5_IRQHandler + .thumb_set DMA1_Channel5_IRQHandler,Default_Handler + + .weak EMAC_IRQHandler + .thumb_set EMAC_IRQHandler,Default_Handler + + .weak EMAC_WKUP_IRQHandler + .thumb_set EMAC_WKUP_IRQHandler,Default_Handler + + .weak CAN2_TX_IRQHandler + .thumb_set CAN2_TX_IRQHandler,Default_Handler + + .weak CAN2_RX0_IRQHandler + .thumb_set CAN2_RX0_IRQHandler ,Default_Handler + + .weak CAN2_RX1_IRQHandler + .thumb_set CAN2_RX1_IRQHandler ,Default_Handler + + .weak CAN2_SE_IRQHandler + .thumb_set CAN2_SE_IRQHandler,Default_Handler + + .weak OTGFS1_IRQHandler + .thumb_set OTGFS1_IRQHandler,Default_Handler + + .weak DMA1_Channel6_IRQHandler + .thumb_set DMA1_Channel6_IRQHandler,Default_Handler + + .weak DMA1_Channel7_IRQHandler + .thumb_set DMA1_Channel7_IRQHandler,Default_Handler + + .weak USART6_IRQHandler + .thumb_set USART6_IRQHandler,Default_Handler + + .weak I2C3_EVT_IRQHandler + .thumb_set I2C3_EVT_IRQHandler,Default_Handler + + .weak I2C3_ERR_IRQHandler + .thumb_set I2C3_ERR_IRQHandler,Default_Handler + + .weak OTGFS2_WKUP_IRQHandler + .thumb_set OTGFS2_WKUP_IRQHandler,Default_Handler + + .weak OTGFS2_IRQHandler + .thumb_set OTGFS2_IRQHandler,Default_Handler + + .weak DVP_IRQHandler + .thumb_set DVP_IRQHandler,Default_Handler + + .weak FPU_IRQHandler + .thumb_set FPU_IRQHandler,Default_Handler + + .weak UART7_IRQHandler + .thumb_set UART7_IRQHandler,Default_Handler + + .weak UART8_IRQHandler + .thumb_set UART8_IRQHandler,Default_Handler + + .weak SPI4_IRQHandler + .thumb_set SPI4_IRQHandler,Default_Handler + + .weak QSPI2_IRQHandler + .thumb_set QSPI2_IRQHandler,Default_Handler + + .weak QSPI1_IRQHandler + .thumb_set QSPI1_IRQHandler,Default_Handler + + .weak DMAMUX_IRQHandler + .thumb_set DMAMUX_IRQHandler ,Default_Handler + + .weak SDIO2_IRQHandler + .thumb_set SDIO2_IRQHandler ,Default_Handler + + .weak ACC_IRQHandler + .thumb_set ACC_IRQHandler,Default_Handler + + .weak TMR20_BRK_IRQHandler + .thumb_set TMR20_BRK_IRQHandler,Default_Handler + + .weak TMR20_OVF_IRQHandler + .thumb_set TMR20_OVF_IRQHandler,Default_Handler + + .weak TMR20_TRG_HALL_IRQHandler + .thumb_set TMR20_TRG_HALL_IRQHandler,Default_Handler + + .weak TMR20_CH_IRQHandler + .thumb_set TMR20_CH_IRQHandler,Default_Handler + + .weak DMA2_Channel1_IRQHandler + .thumb_set DMA2_Channel1_IRQHandler,Default_Handler + + .weak DMA2_Channel2_IRQHandler + .thumb_set DMA2_Channel2_IRQHandler,Default_Handler + + .weak DMA2_Channel3_IRQHandler + .thumb_set DMA2_Channel3_IRQHandler,Default_Handler + + .weak DMA2_Channel4_IRQHandler + .thumb_set DMA2_Channel4_IRQHandler,Default_Handler + + .weak DMA2_Channel5_IRQHandler + .thumb_set DMA2_Channel5_IRQHandler,Default_Handler + + .weak DMA2_Channel6_IRQHandler + .thumb_set DMA2_Channel6_IRQHandler,Default_Handler + + .weak DMA2_Channel7_IRQHandler + .thumb_set DMA2_Channel7_IRQHandler,Default_Handler diff --git a/bootrom/ram-reset.s b/bootrom/ram-reset-at91.s similarity index 100% rename from bootrom/ram-reset.s rename to bootrom/ram-reset-at91.s