/** * @file firmware_map.h * @brief Flash memory map definition for Donghui OTA project * * Memory map (STM32G474RET6 - 512KB Flash, 128KB RAM): * * +-----------------------+ 0x08000000 * | Bootloader | 28KB * | - Vector table | * | - Crypto context | * | - SPI Flash driver | * +-----------------------+ 0x08007000 * | Parameter Sector | 4KB * | - Boot flags | * | - Version info | * | - Upgrade status | * +-----------------------+ 0x08008000 * | App A (Active Bank) | 240KB * +-----------------------+ 0x08044000 * | App B (Backup Bank) | 240KB * +-----------------------+ 0x08080000 (End of Flash) * * External SPI Flash (W25Q64 - 8MB): * +-----------------------+ 0x00000000 * | OTA Download Buffer | 1MB (for incoming encrypted firmware) * | (Multiple versions) | * +-----------------------+ 0x00100000 * | Factory Backup | 512KB (golden firmware copy) * +-----------------------+ 0x00180000 * | Log / Config Data | 512KB * +-----------------------+ 0x00200000 * | Reserved | 6MB * +-----------------------+ 0x00800000 (End) */ #ifndef _FIRMWARE_MAP_H_ #define _FIRMWARE_MAP_H_ #include /* ========== Internal Flash Map ========== */ #define FLASH_BASE_ADDR 0x08000000UL #define FLASH_TOTAL_SIZE (512 * 1024) /* 512KB */ /* Bootloader region */ #define BOOTLOADER_ADDR 0x08000000UL #define BOOTLOADER_SIZE (28 * 1024) /* 28KB */ #define BOOTLOADER_SECTORS 7 /* 4KB per sector */ /* Parameter / Flag sector */ #define PARAM_SECTOR_ADDR 0x08007000UL #define PARAM_SECTOR_SIZE (4 * 1024) /* 4KB, single sector */ /* App A (active bank) */ #define APP_A_ADDR 0x08008000UL #define APP_A_SIZE (240 * 1024) /* 240KB */ #define APP_A_SECTORS 60 /* App B (backup bank) */ #define APP_B_ADDR 0x08044000UL #define APP_B_SIZE (240 * 1024) /* 240KB */ #define APP_B_SECTORS 60 /* ========== External SPI Flash Map (W25Q64) ========== */ #define SPI_FLASH_TOTAL_SIZE (8 * 1024 * 1024) /* 8MB */ #define OTA_BUF_ADDR 0x00000000UL #define OTA_BUF_SIZE (1 * 1024 * 1024) /* 1MB */ #define FACTORY_BACKUP_ADDR 0x00100000UL #define FACTORY_BACKUP_SIZE (512 * 1024) /* 512KB */ #define LOG_CFG_ADDR 0x00180000UL #define LOG_CFG_SIZE (512 * 1024) /* 512KB */ /* ========== Parameter Sector Structure ========== */ /* Stored at PARAM_SECTOR_ADDR in internal flash */ typedef struct __attribute__((packed)) { /* Boot configuration */ uint32_t magic; /* 0xA5A5A5A5 for valid sector */ uint32_t active_bank; /* 0 = App A, 1 = App B */ uint32_t upgrade_status; /* See UPGRADE_STATUS_* */ uint32_t boot_count; /* Number of boots (for health check) */ /* Firmware version info */ uint32_t app_a_version; /* Version code (e.g. 0x0201 = v2.1) */ uint32_t app_b_version; uint32_t app_a_crc32; /* CRC32 of App A firmware */ uint32_t app_b_crc32; /* CRC32 of App B firmware */ /* OTA state */ uint32_t ota_file_size; /* Total size of OTA file in SPI flash */ uint32_t ota_file_crc32; /* CRC32 of OTA file content */ uint32_t ota_downloaded; /* 1 = fully downloaded, 0 = incomplete */ uint32_t reserve[8]; /* Future use */ /* End marker */ uint32_t tail_magic; /* 0x5A5A5A5A */ } BootParam_t; /* ========== Upgrade Status Definitions ========== */ #define UPGRADE_STATUS_IDLE 0 #define UPGRADE_STATUS_DOWNLOADING 1 #define UPGRADE_STATUS_DOWNLOADED 2 #define UPGRADE_STATUS_VERIFIED 3 #define UPGRADE_STATUS_FLASHING 4 #define UPGRADE_STATUS_DONE 5 #define UPGRADE_STATUS_FAILED 0xFFFFFFFF /* ========== Bootloader Version ========== */ #define BOOTLOADER_VERSION_MAJOR 1 #define BOOTLOADER_VERSION_MINOR 0 #define BOOTLOADER_VERSION_PATCH 0 #define BOOTLOADER_VERSION_HEX \ ((BOOTLOADER_VERSION_MAJOR << 16) | \ (BOOTLOADER_VERSION_MINOR << 8) | \ (BOOTLOADER_VERSION_PATCH)) /* ========== OTA Protocol Constants ========== */ #define OTA_PACKET_MAX_DATA 256 /* Max payload bytes per packet */ #define OTA_HEADER_SIZE 10 /* Sync(2) + Len(2) + Seq(2) + Type(2) + CRC16(2) */ #define OTA_TYPE_HELLO 0x0001 /* Handshake request */ #define OTA_TYPE_HELLO_ACK 0x0002 /* Handshake response */ #define OTA_TYPE_DATA 0x0010 /* Firmware data packet */ #define OTA_TYPE_DATA_ACK 0x0011 /* Data ACK */ #define OTA_TYPE_END 0x00FF /* Transfer complete */ #define OTA_TYPE_ABORT 0x00FE /* Abort transfer */ /* ========== Flash Protection ========== */ #define FLASH_OPT_RDP_LEVEL0 0xAA /* No read protection */ #define FLASH_OPT_RDP_LEVEL1 0xBB /* Read protection Level 1 */ #define FLASH_OPT_RDP_LEVEL2 0xCC /* Read protection Level 2 (permanent) */ #endif /* _FIRMWARE_MAP_H_ */