|
| 1 | +/* SIMtrace 2 firmware USB DFU bootloader |
| 2 | + * |
| 3 | + * (C) 2015-2017 by Harald Welte <[email protected]> |
| 4 | + * (C) 2018-2019 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <[email protected]> |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License |
| 8 | + * as published by the Free Software Foundation; either version 2 |
| 9 | + * of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +#include "board.h" |
| 21 | +#include "core_cm3.h" |
| 22 | +#include "flashd.h" |
| 23 | +#include "utils.h" |
| 24 | +#include "usb/device/dfu/dfu.h" |
| 25 | +#include "usb/common/dfu/usb_dfu.h" |
| 26 | +#include "manifest.h" |
| 27 | +#include "USBD_HAL.h" |
| 28 | + |
| 29 | +#include <osmocom/core/timer.h> |
| 30 | + |
| 31 | +/* actual section content must be replaced with the padded bootloader by running objcopy! */ |
| 32 | +const uint32_t bl_update_data[BOARD_DFU_BOOT_SIZE / sizeof(uint32_t)] __attribute__((section(".fwupdate"))) = { 0xFF }; |
| 33 | + |
| 34 | +unsigned int g_unique_id[4]; |
| 35 | +/* remember if the watchdog has been configured in the main loop so we can kick it in the ISR */ |
| 36 | +static bool watchdog_configured = false; |
| 37 | + |
| 38 | +extern uint32_t _end; |
| 39 | +extern uint32_t _srelocate; |
| 40 | +extern uint32_t _etext; |
| 41 | + |
| 42 | +void DFURT_SwitchToDFU(void) |
| 43 | +{ |
| 44 | +} |
| 45 | +void USBDFU_Runtime_RequestHandler(const USBGenericRequest *request) |
| 46 | +{ |
| 47 | +} |
| 48 | +int USBDFU_handle_dnload(uint8_t altif, unsigned int offset, uint8_t *data, unsigned int len) |
| 49 | +{ |
| 50 | + return 0; |
| 51 | +} |
| 52 | +int USBDFU_handle_upload(uint8_t altif, unsigned int offset, uint8_t *data, unsigned int req_len) |
| 53 | +{ |
| 54 | + return 0; |
| 55 | +} |
| 56 | +int USBDFU_OverrideEnterDFU(void) |
| 57 | +{ |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +__attribute__((section(".ramfunc"), noinline)) static uint32_t flash_wait_ready() |
| 62 | +{ |
| 63 | + Efc *efc = EFC; |
| 64 | + uint32_t dwStatus; |
| 65 | + |
| 66 | + do { |
| 67 | + dwStatus = efc->EEFC_FSR; |
| 68 | + } while ((dwStatus & EEFC_FSR_FRDY) != EEFC_FSR_FRDY); |
| 69 | + return (dwStatus & (EEFC_FSR_FLOCKE | EEFC_FSR_FCMDE)); |
| 70 | +} |
| 71 | + |
| 72 | +__attribute__((section(".ramfunc"), noinline)) static void flash_cmd(uint32_t dwCommand, uint32_t dwArgument) |
| 73 | +{ |
| 74 | + Efc *efc = EFC; |
| 75 | + uint32_t dwStatus; |
| 76 | + efc->EEFC_FCR = EEFC_FCR_FKEY(0x5A) | EEFC_FCR_FARG(dwArgument) | EEFC_FCR_FCMD(dwCommand); |
| 77 | +} |
| 78 | + |
| 79 | +__attribute__((section(".ramfunc"), noinline, noreturn)) static void erase_first_app_sector() |
| 80 | +{ |
| 81 | + /* page 64 */ |
| 82 | + uint32_t first_app_page = (BOARD_DFU_BOOT_SIZE / IFLASH_PAGE_SIZE); |
| 83 | + uint32_t *first_app_address = (uint32_t *)(IFLASH_ADDR + first_app_page * IFLASH_PAGE_SIZE + 0); |
| 84 | + |
| 85 | +#if 1 |
| 86 | + /* overwrite first app sector so we don't keep booting this */ |
| 87 | + for (int i = 0; i < IFLASH_PAGE_SIZE / 4; i++) |
| 88 | + first_app_address[i] = 0xffffffff; |
| 89 | + |
| 90 | + flash_cmd(EFC_FCMD_EWP, first_app_page); |
| 91 | +#else |
| 92 | + /* why does erasing the whole flash with a protected bootloader not work at all? */ |
| 93 | + flash_cmd(EFC_FCMD_EA, 0); |
| 94 | +#endif |
| 95 | + flash_wait_ready(); |
| 96 | + for (;;) |
| 97 | + NVIC_SystemReset(); |
| 98 | +} |
| 99 | + |
| 100 | +#define MAX_USB_ITER BOARD_MCK / 72 // This should be around a second |
| 101 | +extern int main(void) |
| 102 | +{ |
| 103 | + uint8_t isUsbConnected = 0; |
| 104 | + unsigned int i = 0; |
| 105 | + uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos; |
| 106 | + |
| 107 | + /* Enable watchdog for 2000ms, with no window */ |
| 108 | + WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT | (WDT_GetPeriod(2000) << 16) | |
| 109 | + WDT_GetPeriod(2000)); |
| 110 | + watchdog_configured = true; |
| 111 | + |
| 112 | + EEFC_ReadUniqueID(g_unique_id); |
| 113 | + |
| 114 | + printf("\n\r\n\r"); |
| 115 | + printf("bootloader updater %s for board %s\n\r" |
| 116 | + "(C) 2010-2017 by Harald Welte, 2018-2019 by Kevin Redon\n\r", |
| 117 | + manifest_revision, manifest_board); |
| 118 | + |
| 119 | + /* clear g_dfu on power-up reset */ |
| 120 | + memset(g_dfu, 0, sizeof(*g_dfu)); |
| 121 | + |
| 122 | + TRACE_INFO("USB init...\n\r"); |
| 123 | + /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */ |
| 124 | + USBD_Disconnect(); |
| 125 | + |
| 126 | + /* Initialize the flash to be able to write it, using the IAP ROM code */ |
| 127 | + FLASHD_Initialize(BOARD_MCK, 1); |
| 128 | + |
| 129 | + __disable_irq(); |
| 130 | + FLASHD_Unlock(IFLASH_ADDR, IFLASH_ADDR + IFLASH_SIZE - 1, 0, 0); |
| 131 | + FLASHD_Write(IFLASH_ADDR, bl_update_data, BOARD_DFU_BOOT_SIZE); |
| 132 | + |
| 133 | + erase_first_app_sector(); |
| 134 | +} |
0 commit comments