diff --git a/src/boards.h b/src/boards.h index 595f232..4a658ec 100644 --- a/src/boards.h +++ b/src/boards.h @@ -33,6 +33,10 @@ #include "board.h" +#ifndef UF2_VOLUME_LABEL +#define UF2_VOLUME_LABEL "NRF52BOOT " +#endif + #ifndef BUTTON_DFU #define BUTTON_DFU BUTTON_1 #endif diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index de0f02c..1180214 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -102,7 +102,7 @@ STATIC_ASSERT(ARRAY_SIZE(indexFile) < 512); #define NUM_FILES (ARRAY_SIZE(info)) #define NUM_DIRENTRIES (NUM_FILES + 1) // Code adds volume label as first root directory entry -#define UF2_SIZE (current_flash_size() * 2) +#define UF2_SIZE (uf2current_flash_sz * 2) #define UF2_SECTORS (UF2_SIZE / 512) #define UF2_FIRST_SECTOR (NUM_FILES + 1) // WARNING -- code presumes each non-UF2 file content fits in single sector #define UF2_LAST_SECTOR (UF2_FIRST_SECTOR + UF2_SECTORS - 1) @@ -146,48 +146,48 @@ static FAT_BootBlock const BootBlock = { //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ +static uint32_t uf2current_flash_sz = 0; + // get current.uf2 flash size in bytes, round up to 256 bytes static uint32_t current_flash_size(void) { - static uint32_t flash_sz = 0; - uint32_t result = flash_sz; // presumes atomic 32-bit read/write and static result + uint32_t flash_sz = 0; - // only need to compute once - if ( result == 0 ) + // return 1 block of 256 bytes + if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) { - // return 1 block of 256 bytes - if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) + flash_sz = 256; + }else + { + bootloader_settings_t const * boot_setting; + bootloader_util_settings_get(&boot_setting); + + flash_sz = boot_setting->bank_0_size; + + // Copy size must be multiple of 256 bytes + // else we will got an issue copying current.uf2 + if (flash_sz & 0xff) { - result = 256; - }else - { - bootloader_settings_t const * boot_setting; - bootloader_util_settings_get(&boot_setting); - - result = boot_setting->bank_0_size; - - // Copy size must be multiple of 256 bytes - // else we will got an issue copying current.uf2 - if (result & 0xff) - { - result = (result & ~0xff) + 256; - } - - // if bank0 size is not valid, happens when flashed with jlink - // use maximum application size - if ( (result == 0) || (result == 0xFFFFFFFFUL) ) - { - result = FLASH_SIZE; - } + flash_sz = (flash_sz & ~0xff) + 256; } - flash_sz = result; // presumes atomic 32-bit read/write and static result + // if bank0 size is not valid, happens when flashed with jlink + // use maximum application size + if ( (flash_sz == 0) || (flash_sz == 0xFFFFFFFFUL) ) + { + flash_sz = FLASH_SIZE; + } } return flash_sz; } +void uf2_init(void) +{ + uf2current_flash_sz = current_flash_size(); +} + void padded_memcpy (char *dst, char const *src, int len) { for ( int i = 0; i < len; ++i ) @@ -282,7 +282,7 @@ void read_block(uint32_t block_no, uint8_t *data) { bl->magicStart1 = UF2_MAGIC_START1; bl->magicEnd = UF2_MAGIC_END; bl->blockNo = sectionIdx; - bl->numBlocks = current_flash_size() / 256; + bl->numBlocks = uf2current_flash_sz / 256; bl->targetAddr = addr; bl->payloadSize = 256; bl->flags = UF2_FLAG_FAMILYID; diff --git a/src/usb/uf2/uf2.h b/src/usb/uf2/uf2.h index eae5054..3b55ba0 100644 --- a/src/usb/uf2/uf2.h +++ b/src/usb/uf2/uf2.h @@ -36,12 +36,11 @@ SOFTWARE. #include #include "app_util.h" - #include "dfu_types.h" -#define SD_MAGIC_NUMBER 0x51b1e5db -#define SD_MAGIC_OK() (*((uint32_t*)(SOFTDEVICE_INFO_STRUCT_ADDRESS+4)) == 0x51b1e5db) -extern bool sdRunning; +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ // All entries are little endian. #define UF2_MAGIC_START0 0x0A324655UL // "UF2\n" @@ -77,77 +76,15 @@ typedef struct { uint32_t magicEnd; } UF2_Block; -typedef struct { - uint8_t version; - uint8_t ep_in; - uint8_t ep_out; - uint8_t reserved0; - uint32_t cbw_tag; - uint32_t blocks_remaining; - uint8_t *buffer; -} UF2_HandoverArgs; - -typedef void (*UF2_MSC_Handover_Handler)(UF2_HandoverArgs *handover); -typedef void (*UF2_HID_Handover_Handler)(int ep); - -// this is required to be exactly 16 bytes long by the linker script -typedef struct { - void *reserved0; - UF2_HID_Handover_Handler handoverHID; - UF2_MSC_Handover_Handler handoverMSC; - const char *info_uf2; -} UF2_BInfo; - -#define UF2_BINFO ((UF2_BInfo *)(APP_START_ADDRESS - sizeof(UF2_BInfo))) - -static inline bool is_uf2_block(void *data) { - UF2_Block *bl = (UF2_Block *)data; +static inline bool is_uf2_block(UF2_Block const *bl) { return bl->magicStart0 == UF2_MAGIC_START0 && bl->magicStart1 == UF2_MAGIC_START1 && bl->magicEnd == UF2_MAGIC_END; } static inline bool in_uf2_bootloader_space(const void *addr) { - return USER_FLASH_END <= (uint32_t)addr && (uint32_t)addr < FLASH_SIZE; + return USER_FLASH_END <= (uint32_t)addr /*&& (uint32_t)addr < FLASH_SIZE*/; } - -#ifdef UF2_DEFINE_HANDOVER -static inline const char *uf2_info(void) { - if (in_uf2_bootloader_space(UF2_BINFO->info_uf2)) - return UF2_BINFO->info_uf2; - return "N/A"; -} - -static inline void hf2_handover(uint8_t ep) { - const char *board_info = UF2_BINFO->info_uf2; - UF2_HID_Handover_Handler fn = UF2_BINFO->handoverHID; - - if (in_uf2_bootloader_space(board_info) && in_uf2_bootloader_space((const void *)fn) && - ((uint32_t)fn & 1)) { - // Pass control to bootloader; never returns - fn(ep & 0xf); - } -} - -// the ep_in/ep_out are without the 0x80 mask -// cbw_tag is in the same bit format as it came -static inline void check_uf2_handover(uint8_t *buffer, uint32_t blocks_remaining, uint8_t ep_in, - uint8_t ep_out, uint32_t cbw_tag) { - if (!is_uf2_block(buffer)) - return; - - const char *board_info = UF2_BINFO->info_uf2; - UF2_MSC_Handover_Handler fn = UF2_BINFO->handoverMSC; - - if (in_uf2_bootloader_space(board_info) && in_uf2_bootloader_space((const void *)fn) && - ((uint32_t)fn & 1)) { - UF2_HandoverArgs hand = { - 1, ep_in, ep_out, 0, cbw_tag, blocks_remaining, buffer, - }; - // Pass control to bootloader; never returns - fn(&hand); - } -} -#endif +void uf2_init(void); #endif diff --git a/src/usb/uf2/uf2cfg.h b/src/usb/uf2/uf2cfg.h index ec6dbcc..11de527 100644 --- a/src/usb/uf2/uf2cfg.h +++ b/src/usb/uf2/uf2cfg.h @@ -1,15 +1,10 @@ #include "boards.h" #define UF2_NUM_BLOCKS 8000 // at least 4,1 MB for FAT16 - -#ifndef UF2_VOLUME_LABEL -#define UF2_VOLUME_LABEL "NRF52BOOT " -#endif - #define FLASH_SIZE (USER_FLASH_END-USER_FLASH_START) // Max flash size // Only allow to write application TODO dynamic depending on SD size -#define USER_FLASH_START 0x26000 +#define USER_FLASH_START (SD_FLASH_SIZE + MBR_SIZE) #define USER_FLASH_END 0xAD000 // Fat Fs start here #define UF2_FAMILY_ID 0xADA52840 diff --git a/src/usb/usb.c b/src/usb/usb.c index 0115072..c0903ca 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -34,6 +34,7 @@ #include "tusb.h" #include "usb_desc.h" +#include "uf2/uf2.h" #include "boards.h" //--------------------------------------------------------------------+ @@ -89,6 +90,8 @@ void usb_init(bool cdc_only) usb_desc_init(cdc_only); + uf2_init(); + // Init TinyUSB stack tusb_init(); }