1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| #include "bootloader.h" #include <stdio.h> #include <string.h> char *hostname = BL_SERVER_HOST; uint16_t hostport = BL_SERVER_PORT;
static uint32_t crc32_update(uint32_t crc, uint8_t data) { crc ^= data; for (int i = 0; i < 8; i++) crc = (crc & 1) ? (crc >> 1) ^ 0xEDB88320UL : (crc >> 1); return crc; } static uint32_t crc32(const uint8_t *buf, uint32_t len) { uint32_t crc = 0xFFFFFFFFUL; for (uint32_t i = 0; i < len; i++) crc = crc32_update(crc, buf[i]); return ~crc; }
static void build_request(char *buf, size_t size, const char *path){ snprintf(buf, size, "GET %s HTTP/1.1\r\nHost: %s:%d\r\nConnection: close\r\n\r\n", path, BL_SERVER_HOST, BL_SERVER_PORT); }
static int skip_http_header(void){ uint8_t tmp[4]; while (1) { if (BL_WIFI_RECV(tmp, 1) != 1) return -1; if (tmp[0] == '\r') { if (BL_WIFI_RECV(tmp, 3) != 3) return -1; if (tmp[0] == '\n' && tmp[1] == '\r' && tmp[2] == '\n') return 0; } } }
static int flash_erase_block(BLOCK_TYPE type){ FLASH_EraseInitTypeDef erase = {0}; uint32_t sector_error = 0; if (HAL_FLASH_Unlock() != HAL_OK) return -1;
erase.TypeErase = FLASH_TYPEERASE_SECTORS; erase.Banks = FLASH_BANK_1; erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
if (type == OTA_BLOCK) { erase.Sector = BL_OTA_INFO_SECTOR; erase.NbSectors = 1; } else if (type == APP_BLOCK) { erase.Sector = BL_APP_FIRST_SECTOR; erase.NbSectors = BL_APP_SECTOR_COUNT; } else { HAL_FLASH_Lock(); return -1; }
if (HAL_FLASHEx_Erase(&erase, &sector_error) != HAL_OK) { HAL_FLASH_Lock(); bl_printf("[FLASH] Erase failed, error=0x%lX\r\n", sector_error); return -1; }
HAL_FLASH_Lock(); bl_printf("[FLASH] Erase %s success\r\n", type == OTA_BLOCK ? "OTA" : "APP"); return 0;
}
static int flash_write(uint32_t addr, const uint8_t *data, uint32_t len){ if (HAL_FLASH_Unlock() != HAL_OK) return -1; for (uint32_t i = 0; i < len; i += 4) { uint32_t word = 0xFFFFFFFFUL; uint32_t remain = len - i; memcpy(&word, data + i, remain > 4 ? 4 : remain);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr + i, word) != HAL_OK) { HAL_FLASH_Lock(); bl_printf("[FLASH] Program failed @0x%08lX\r\n", addr + i); return -1; } } HAL_FLASH_Lock(); return 0;
}
static void read_ota_info(bl_ota_info_t p){ memcpy(p, (const void)BL_OTA_INFO_START, sizeof(*p)); }
static int is_app_empty(void){ const uint32_t p = (const uint32_t)BL_APP_START; for (int i = 0; i < 16; i++) { if (p[i] != 0xFFFFFFFFUL) return 0; } return 1; }
static void jump_to_app(void){ uint32_t msp = ((__IO uint32_t)BL_APP_START); uint32_t reset = ((__IO uint32_t)(BL_APP_START + 4)); if ((msp & 0x2FFE0000) != 0x20000000) { bl_printf("[BOOT] Invalid stack pointer\r\n"); return; }
HAL_RCC_DeInit(); HAL_DeInit();
for (int i = 0; i < 8; i++) { NVIC->ICER[i] = 0xFFFFFFFF; NVIC->ICPR[i] = 0xFFFFFFFF; }
__set_MSP(msp); SCB->VTOR = BL_APP_START;
((void (*)(void))reset)();
while (1);
}
static void perform_ota(void){ char req[256]; uint8_t block[260]; bl_firmware_header_t hdr = {0}; uint32_t total_crc = 0xFFFFFFFFUL; uint32_t write_addr = BL_APP_START;
build_request(req, sizeof(req), BL_HEADER_PATH); BL_WIFI_SEND(req, strlen(req)); if (skip_http_header() != 0 || BL_WIFI_RECV(&hdr, sizeof(hdr)) != sizeof(hdr)) { goto fail; }
bl_ota_info_t current; read_ota_info(&current); if (hdr.version <= current.version) { bl_printf("[OTA] Already the latest version\r\n"); bl_ota_info_t new_info = { .version = hdr.version, .update = 0, .effective = 1 }; flash_erase_block(OTA_BLOCK); flash_write(BL_OTA_INFO_START, (uint8_t*)&new_info, sizeof(new_info)); jump_to_app(); return; }
if (flash_erase_block(APP_BLOCK) != 0) goto fail;
for (uint32_t i = 0; i < hdr.blocknum; i++) { char path[64]; snprintf(path, sizeof(path), BL_BLOCK_PATH_FMT, i);
build_request(req, sizeof(req), path); BL_WIFI_SEND(req, strlen(req)); BL_WIFI_WAIT(8000);
if (skip_http_header() != 0) goto fail;
uint32_t recv = 0; while (recv < 260) { int r = BL_WIFI_RECV(block + recv, 260 - recv); if (r <= 0) goto fail; recv += r; }
if (crc32(block, 256) != ((uint32_t*)(block + 256))[0]) { bl_printf("[OTA] Block %lu CRC error\r\n", i); goto fail; }
uint32_t this_len = 256; if (i == hdr.blocknum - 1) { uint32_t remain = hdr.binsize % 256; if (remain) this_len = remain; }
for (uint32_t j = 0; j < this_len; j++) total_crc = crc32_update(total_crc, block[j]);
if (flash_write(write_addr, block, this_len) != 0) goto fail;
write_addr += this_len; bl_printf("[OTA] Block %lu/%lu OK\r\n", i + 1, hdr.blocknum); }
if (~total_crc != hdr.checksum) { bl_printf("[OTA] Total CRC failed\r\n"); goto fail; }
bl_ota_info_t new_info = { .version = hdr.version, .update = 0, .effective = 1 }; flash_erase_block(OTA_BLOCK); flash_write(BL_OTA_INFO_START, (uint8_t*)&new_info, sizeof(new_info));
bl_printf("[OTA] UPDATE SUCCESS! Jumping to APP...\r\n"); HAL_Delay(200); jump_to_app();
fail: bl_printf("[OTA] UPDATE FAILED!\r\n"); while (1) HAL_Delay(1000); }
void Bootloader_Main(void){ delay_Init(); HAL_Delay(200); #if BL_FIRST_BOOT_INIT { bl_ota_info_t init = { .version = 0, .update = 0, .effective = 0 }; flash_erase_block(OTA_BLOCK); flash_write(BL_OTA_INFO_START, (uint8_t*)&init, sizeof(init)); } #endif bl_ota_info_t ota_info; read_ota_info(&ota_info);
if (is_app_empty() || ota_info.effective != 1 || ota_info.update == 1) { bl_printf("[BOOT] Enter OTA mode\r\n"); BL_WIFI_CONNECT(); perform_ota();
while (1) { HAL_Delay(2000); bl_printf("[BOOT] Update failed, please reset device\r\n"); } }
bl_printf("[BOOT] Start APP v0x%08lX\r\n", ota_info.version); jump_to_app();
}
|