You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /**
  2. * Copyright (c) 2022 Brian Starkey <stark3y@gmail.com>
  3. *
  4. * Based on the Pico W tcp_server example:
  5. * Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
  6. *
  7. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "RP2040.h"
  12. #include "pico/critical_section.h"
  13. #include "pico/time.h"
  14. #include "pico/util/queue.h"
  15. #include "hardware/dma.h"
  16. #include "hardware/flash.h"
  17. #include "hardware/structs/dma.h"
  18. #include "hardware/structs/watchdog.h"
  19. #include "hardware/gpio.h"
  20. #include "hardware/resets.h"
  21. #include "hardware/uart.h"
  22. #include "hardware/watchdog.h"
  23. #include "pico/stdlib.h"
  24. #include "pico/cyw43_arch.h"
  25. #include "tcp_comm.h"
  26. #include "picowota/reboot.h"
  27. #ifdef DEBUG
  28. #include <stdio.h>
  29. #include "pico/stdio_usb.h"
  30. #define DBG_PRINTF_INIT() stdio_usb_init()
  31. #define DBG_PRINTF(...) printf(__VA_ARGS__)
  32. #else
  33. #define DBG_PRINTF_INIT() { }
  34. #define DBG_PRINTF(...) { }
  35. #endif
  36. #define QUOTE(name) #name
  37. #define STR(macro) QUOTE(macro)
  38. #ifndef PICOWOTA_WIFI_SSID
  39. #warning "PICOWOTA_WIFI_SSID not defined"
  40. #else
  41. const char *wifi_ssid = STR(PICOWOTA_WIFI_SSID);
  42. #endif
  43. #ifndef PICOWOTA_WIFI_PASS
  44. #warning "PICOWOTA_WIFI_PASS not defined"
  45. #else
  46. const char *wifi_pass = STR(PICOWOTA_WIFI_PASS);
  47. #endif
  48. critical_section_t critical_section;
  49. #define EVENT_QUEUE_LENGTH 8
  50. queue_t event_queue;
  51. enum event_type {
  52. EVENT_TYPE_REBOOT = 1,
  53. EVENT_TYPE_GO,
  54. EVENT_TYPE_SERVER_DONE,
  55. };
  56. struct event {
  57. enum event_type type;
  58. union {
  59. struct {
  60. bool to_bootloader;
  61. } reboot;
  62. struct {
  63. uint32_t vtor;
  64. } go;
  65. };
  66. };
  67. #define BOOTLOADER_ENTRY_PIN 15
  68. #define TCP_PORT 4242
  69. #define IMAGE_HEADER_OFFSET (360 * 1024)
  70. #define WRITE_ADDR_MIN (XIP_BASE + IMAGE_HEADER_OFFSET + FLASH_SECTOR_SIZE)
  71. #define ERASE_ADDR_MIN (XIP_BASE + IMAGE_HEADER_OFFSET)
  72. #define FLASH_ADDR_MAX (XIP_BASE + PICO_FLASH_SIZE_BYTES)
  73. #define CMD_SYNC (('S' << 0) | ('Y' << 8) | ('N' << 16) | ('C' << 24))
  74. #define RSP_SYNC (('W' << 0) | ('O' << 8) | ('T' << 16) | ('A' << 24))
  75. #define CMD_INFO (('I' << 0) | ('N' << 8) | ('F' << 16) | ('O' << 24))
  76. #define CMD_READ (('R' << 0) | ('E' << 8) | ('A' << 16) | ('D' << 24))
  77. #define CMD_CSUM (('C' << 0) | ('S' << 8) | ('U' << 16) | ('M' << 24))
  78. #define CMD_CRC (('C' << 0) | ('R' << 8) | ('C' << 16) | ('C' << 24))
  79. #define CMD_ERASE (('E' << 0) | ('R' << 8) | ('A' << 16) | ('S' << 24))
  80. #define CMD_WRITE (('W' << 0) | ('R' << 8) | ('I' << 16) | ('T' << 24))
  81. #define CMD_SEAL (('S' << 0) | ('E' << 8) | ('A' << 16) | ('L' << 24))
  82. #define CMD_GO (('G' << 0) | ('O' << 8) | ('G' << 16) | ('O' << 24))
  83. #define CMD_REBOOT (('B' << 0) | ('O' << 8) | ('O' << 16) | ('T' << 24))
  84. static uint32_t handle_sync(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  85. {
  86. return RSP_SYNC;
  87. }
  88. const struct comm_command sync_cmd = {
  89. .opcode = CMD_SYNC,
  90. .nargs = 0,
  91. .resp_nargs = 0,
  92. .size = NULL,
  93. .handle = &handle_sync,
  94. };
  95. static uint32_t size_read(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
  96. {
  97. uint32_t size = args_in[1];
  98. if (size > TCP_COMM_MAX_DATA_LEN) {
  99. return TCP_COMM_RSP_ERR;
  100. }
  101. // TODO: Validate address
  102. *data_len_out = 0;
  103. *resp_data_len_out = size;
  104. return TCP_COMM_RSP_OK;
  105. }
  106. static uint32_t handle_read(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  107. {
  108. uint32_t addr = args_in[0];
  109. uint32_t size = args_in[1];
  110. memcpy(resp_data_out, (void *)addr, size);
  111. return TCP_COMM_RSP_OK;
  112. }
  113. const struct comm_command read_cmd = {
  114. // READ addr len
  115. // OKOK [data]
  116. .opcode = CMD_READ,
  117. .nargs = 2,
  118. .resp_nargs = 0,
  119. .size = &size_read,
  120. .handle = &handle_read,
  121. };
  122. static uint32_t size_csum(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
  123. {
  124. uint32_t addr = args_in[0];
  125. uint32_t size = args_in[1];
  126. if ((addr & 0x3) || (size & 0x3)) {
  127. // Must be aligned
  128. return TCP_COMM_RSP_ERR;
  129. }
  130. // TODO: Validate address
  131. *data_len_out = 0;
  132. *resp_data_len_out = 0;
  133. return TCP_COMM_RSP_OK;
  134. }
  135. static uint32_t handle_csum(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  136. {
  137. uint32_t dummy_dest;
  138. uint32_t addr = args_in[0];
  139. uint32_t size = args_in[1];
  140. int channel = dma_claim_unused_channel(true);
  141. dma_channel_config c = dma_channel_get_default_config(channel);
  142. channel_config_set_transfer_data_size(&c, DMA_SIZE_32);
  143. channel_config_set_read_increment(&c, true);
  144. channel_config_set_write_increment(&c, false);
  145. channel_config_set_sniff_enable(&c, true);
  146. dma_hw->sniff_data = 0;
  147. dma_sniffer_enable(channel, 0xf, true);
  148. dma_channel_configure(channel, &c, &dummy_dest, (void *)addr, size / 4, true);
  149. dma_channel_wait_for_finish_blocking(channel);
  150. dma_sniffer_disable();
  151. dma_channel_unclaim(channel);
  152. *resp_args_out = dma_hw->sniff_data;
  153. return TCP_COMM_RSP_OK;
  154. }
  155. struct comm_command csum_cmd = {
  156. // CSUM addr len
  157. // OKOK csum
  158. .opcode = CMD_CSUM,
  159. .nargs = 2,
  160. .resp_nargs = 1,
  161. .size = &size_csum,
  162. .handle = &handle_csum,
  163. };
  164. static uint32_t size_crc(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
  165. {
  166. uint32_t addr = args_in[0];
  167. uint32_t size = args_in[1];
  168. if ((addr & 0x3) || (size & 0x3)) {
  169. // Must be aligned
  170. return TCP_COMM_RSP_ERR;
  171. }
  172. // TODO: Validate address
  173. *data_len_out = 0;
  174. *resp_data_len_out = 0;
  175. return TCP_COMM_RSP_OK;
  176. }
  177. // ptr must be 4-byte aligned and len must be a multiple of 4
  178. static uint32_t calc_crc32(void *ptr, uint32_t len)
  179. {
  180. uint32_t dummy_dest, crc;
  181. int channel = dma_claim_unused_channel(true);
  182. dma_channel_config c = dma_channel_get_default_config(channel);
  183. channel_config_set_transfer_data_size(&c, DMA_SIZE_32);
  184. channel_config_set_read_increment(&c, true);
  185. channel_config_set_write_increment(&c, false);
  186. channel_config_set_sniff_enable(&c, true);
  187. // Seed the CRC calculation
  188. dma_hw->sniff_data = 0xffffffff;
  189. // Mode 1, then bit-reverse the result gives the same result as
  190. // golang's IEEE802.3 implementation
  191. dma_sniffer_enable(channel, 0x1, true);
  192. dma_hw->sniff_ctrl |= DMA_SNIFF_CTRL_OUT_REV_BITS;
  193. dma_channel_configure(channel, &c, &dummy_dest, ptr, len / 4, true);
  194. dma_channel_wait_for_finish_blocking(channel);
  195. // Read the result before resetting
  196. crc = dma_hw->sniff_data ^ 0xffffffff;
  197. dma_sniffer_disable();
  198. dma_channel_unclaim(channel);
  199. return crc;
  200. }
  201. static uint32_t handle_crc(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  202. {
  203. uint32_t addr = args_in[0];
  204. uint32_t size = args_in[1];
  205. resp_args_out[0] = calc_crc32((void *)addr, size);
  206. return TCP_COMM_RSP_OK;
  207. }
  208. struct comm_command crc_cmd = {
  209. // CRCC addr len
  210. // OKOK crc
  211. .opcode = CMD_CRC,
  212. .nargs = 2,
  213. .resp_nargs = 1,
  214. .size = &size_crc,
  215. .handle = &handle_crc,
  216. };
  217. static uint32_t handle_erase(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  218. {
  219. uint32_t addr = args_in[0];
  220. uint32_t size = args_in[1];
  221. if ((addr < ERASE_ADDR_MIN) || (addr + size >= FLASH_ADDR_MAX)) {
  222. // Outside flash
  223. return TCP_COMM_RSP_ERR;
  224. }
  225. if ((addr & (FLASH_SECTOR_SIZE - 1)) || (size & (FLASH_SECTOR_SIZE - 1))) {
  226. // Must be aligned
  227. return TCP_COMM_RSP_ERR;
  228. }
  229. critical_section_enter_blocking(&critical_section);
  230. flash_range_erase(addr - XIP_BASE, size);
  231. critical_section_exit(&critical_section);
  232. return TCP_COMM_RSP_OK;
  233. }
  234. struct comm_command erase_cmd = {
  235. // ERAS addr len
  236. // OKOK
  237. .opcode = CMD_ERASE,
  238. .nargs = 2,
  239. .resp_nargs = 0,
  240. .size = NULL,
  241. .handle = &handle_erase,
  242. };
  243. static uint32_t size_write(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
  244. {
  245. uint32_t addr = args_in[0];
  246. uint32_t size = args_in[1];
  247. if ((addr < WRITE_ADDR_MIN) || (addr + size >= FLASH_ADDR_MAX)) {
  248. // Outside flash
  249. return TCP_COMM_RSP_ERR;
  250. }
  251. if ((addr & (FLASH_PAGE_SIZE - 1)) || (size & (FLASH_PAGE_SIZE -1))) {
  252. // Must be aligned
  253. return TCP_COMM_RSP_ERR;
  254. }
  255. if (size > TCP_COMM_MAX_DATA_LEN) {
  256. return TCP_COMM_RSP_ERR;
  257. }
  258. // TODO: Validate address
  259. *data_len_out = size;
  260. *resp_data_len_out = 0;
  261. return TCP_COMM_RSP_OK;
  262. }
  263. static uint32_t handle_write(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  264. {
  265. uint32_t addr = args_in[0];
  266. uint32_t size = args_in[1];
  267. critical_section_enter_blocking(&critical_section);
  268. flash_range_program(addr - XIP_BASE, data_in, size);
  269. critical_section_exit(&critical_section);
  270. resp_args_out[0] = calc_crc32((void *)addr, size);
  271. return TCP_COMM_RSP_OK;
  272. }
  273. struct comm_command write_cmd = {
  274. // WRIT addr len [data]
  275. // OKOK crc
  276. .opcode = CMD_WRITE,
  277. .nargs = 2,
  278. .resp_nargs = 1,
  279. .size = &size_write,
  280. .handle = &handle_write,
  281. };
  282. struct image_header {
  283. uint32_t vtor;
  284. uint32_t size;
  285. uint32_t crc;
  286. uint8_t pad[FLASH_PAGE_SIZE - (3 * 4)];
  287. };
  288. static_assert(sizeof(struct image_header) == FLASH_PAGE_SIZE, "image_header must be FLASH_PAGE_SIZE bytes");
  289. static bool image_header_ok(struct image_header *hdr)
  290. {
  291. uint32_t *vtor = (uint32_t *)hdr->vtor;
  292. uint32_t calc = calc_crc32((void *)hdr->vtor, hdr->size);
  293. // CRC has to match
  294. if (calc != hdr->crc) {
  295. return false;
  296. }
  297. // Stack pointer needs to be in RAM
  298. if (vtor[0] < SRAM_BASE) {
  299. return false;
  300. }
  301. // Reset vector should be in the image, and thumb (bit 0 set)
  302. if ((vtor[1] < hdr->vtor) || (vtor[1] > hdr->vtor + hdr->size) || !(vtor[1] & 1)) {
  303. return false;
  304. }
  305. // Looks OK.
  306. return true;
  307. }
  308. static uint32_t handle_seal(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  309. {
  310. struct image_header hdr = {
  311. .vtor = args_in[0],
  312. .size = args_in[1],
  313. .crc = args_in[2],
  314. };
  315. if ((hdr.vtor & 0xff) || (hdr.size & 0x3)) {
  316. // Must be aligned
  317. return TCP_COMM_RSP_ERR;
  318. }
  319. if (!image_header_ok(&hdr)) {
  320. return TCP_COMM_RSP_ERR;
  321. }
  322. critical_section_enter_blocking(&critical_section);
  323. flash_range_erase(IMAGE_HEADER_OFFSET, FLASH_SECTOR_SIZE);
  324. flash_range_program(IMAGE_HEADER_OFFSET, (const uint8_t *)&hdr, sizeof(hdr));
  325. critical_section_exit(&critical_section);
  326. struct image_header *check = (struct image_header *)(XIP_BASE + IMAGE_HEADER_OFFSET);
  327. if (memcmp(&hdr, check, sizeof(hdr))) {
  328. return TCP_COMM_RSP_ERR;
  329. }
  330. return TCP_COMM_RSP_OK;
  331. }
  332. struct comm_command seal_cmd = {
  333. // SEAL vtor len crc
  334. // OKOK
  335. .opcode = CMD_SEAL,
  336. .nargs = 3,
  337. .resp_nargs = 0,
  338. .size = NULL,
  339. .handle = &handle_seal,
  340. };
  341. static void disable_interrupts(void)
  342. {
  343. SysTick->CTRL &= ~1;
  344. NVIC->ICER[0] = 0xFFFFFFFF;
  345. NVIC->ICPR[0] = 0xFFFFFFFF;
  346. }
  347. static void reset_peripherals(void)
  348. {
  349. reset_block(~(
  350. RESETS_RESET_IO_QSPI_BITS |
  351. RESETS_RESET_PADS_QSPI_BITS |
  352. RESETS_RESET_SYSCFG_BITS |
  353. RESETS_RESET_PLL_SYS_BITS
  354. ));
  355. }
  356. static void jump_to_vtor(uint32_t vtor)
  357. {
  358. // Derived from the Leaf Labs Cortex-M3 bootloader.
  359. // Copyright (c) 2010 LeafLabs LLC.
  360. // Modified 2021 Brian Starkey <stark3y@gmail.com>
  361. // Originally under The MIT License
  362. uint32_t reset_vector = *(volatile uint32_t *)(vtor + 0x04);
  363. SCB->VTOR = (volatile uint32_t)(vtor);
  364. asm volatile("msr msp, %0"::"g"
  365. (*(volatile uint32_t *)vtor));
  366. asm volatile("bx %0"::"r" (reset_vector));
  367. }
  368. static uint32_t handle_go(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  369. {
  370. struct event ev = {
  371. .type = EVENT_TYPE_GO,
  372. .go = {
  373. .vtor = args_in[0],
  374. },
  375. };
  376. if (!queue_try_add(&event_queue, &ev)) {
  377. return TCP_COMM_RSP_ERR;
  378. }
  379. return TCP_COMM_RSP_OK;
  380. }
  381. struct comm_command go_cmd = {
  382. // GOGO vtor
  383. // NO RESPONSE
  384. .opcode = CMD_GO,
  385. .nargs = 1,
  386. .resp_nargs = 0,
  387. .size = NULL,
  388. .handle = &handle_go,
  389. };
  390. static uint32_t handle_info(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  391. {
  392. resp_args_out[0] = WRITE_ADDR_MIN;
  393. resp_args_out[1] = (XIP_BASE + PICO_FLASH_SIZE_BYTES) - WRITE_ADDR_MIN;
  394. resp_args_out[2] = FLASH_SECTOR_SIZE;
  395. resp_args_out[3] = FLASH_PAGE_SIZE;
  396. resp_args_out[4] = TCP_COMM_MAX_DATA_LEN;
  397. return TCP_COMM_RSP_OK;
  398. }
  399. const struct comm_command info_cmd = {
  400. // INFO
  401. // OKOK flash_start flash_size erase_size write_size max_data_len
  402. .opcode = CMD_INFO,
  403. .nargs = 0,
  404. .resp_nargs = 5,
  405. .size = NULL,
  406. .handle = &handle_info,
  407. };
  408. static uint32_t size_reboot(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
  409. {
  410. *data_len_out = 0;
  411. *resp_data_len_out = 0;
  412. return TCP_COMM_RSP_OK;
  413. }
  414. static uint32_t handle_reboot(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_args_out, uint8_t *resp_data_out)
  415. {
  416. struct event ev = {
  417. .type = EVENT_TYPE_REBOOT,
  418. .reboot = {
  419. .to_bootloader = !!args_in[0],
  420. },
  421. };
  422. if (!queue_try_add(&event_queue, &ev)) {
  423. return TCP_COMM_RSP_ERR;
  424. }
  425. return TCP_COMM_RSP_OK;
  426. }
  427. struct comm_command reboot_cmd = {
  428. // BOOT to_bootloader
  429. // NO RESPONSE
  430. .opcode = CMD_REBOOT,
  431. .nargs = 1,
  432. .resp_nargs = 0,
  433. .size = &size_reboot,
  434. .handle = &handle_reboot,
  435. };
  436. static bool should_stay_in_bootloader()
  437. {
  438. bool wd_says_so = (watchdog_hw->scratch[5] == PICOWOTA_BOOTLOADER_ENTRY_MAGIC) &&
  439. (watchdog_hw->scratch[6] == ~PICOWOTA_BOOTLOADER_ENTRY_MAGIC);
  440. return !gpio_get(BOOTLOADER_ENTRY_PIN) || wd_says_so;
  441. }
  442. int main()
  443. {
  444. err_t err;
  445. gpio_init(BOOTLOADER_ENTRY_PIN);
  446. gpio_pull_up(BOOTLOADER_ENTRY_PIN);
  447. gpio_set_dir(BOOTLOADER_ENTRY_PIN, 0);
  448. sleep_ms(10);
  449. struct image_header *hdr = (struct image_header *)(XIP_BASE + IMAGE_HEADER_OFFSET);
  450. if (!should_stay_in_bootloader() && image_header_ok(hdr)) {
  451. uint32_t vtor = *((uint32_t *)(XIP_BASE + IMAGE_HEADER_OFFSET));
  452. disable_interrupts();
  453. reset_peripherals();
  454. jump_to_vtor(vtor);
  455. }
  456. DBG_PRINTF_INIT();
  457. queue_init(&event_queue, sizeof(struct event), EVENT_QUEUE_LENGTH);
  458. if (cyw43_arch_init()) {
  459. DBG_PRINTF("failed to initialise\n");
  460. return 1;
  461. }
  462. cyw43_arch_enable_sta_mode();
  463. DBG_PRINTF("Connecting to WiFi...\n");
  464. if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
  465. DBG_PRINTF("failed to connect.\n");
  466. return 1;
  467. } else {
  468. DBG_PRINTF("Connected.\n");
  469. }
  470. critical_section_init(&critical_section);
  471. const struct comm_command *cmds[] = {
  472. &sync_cmd,
  473. &read_cmd,
  474. &csum_cmd,
  475. &crc_cmd,
  476. &erase_cmd,
  477. &write_cmd,
  478. &seal_cmd,
  479. &go_cmd,
  480. &info_cmd,
  481. &reboot_cmd,
  482. };
  483. struct tcp_comm_ctx *tcp = tcp_comm_new(cmds, sizeof(cmds) / sizeof(cmds[0]), CMD_SYNC);
  484. struct event ev = {
  485. .type = EVENT_TYPE_SERVER_DONE,
  486. };
  487. queue_add_blocking(&event_queue, &ev);
  488. for ( ; ; ) {
  489. while (queue_try_remove(&event_queue, &ev)) {
  490. switch (ev.type) {
  491. case EVENT_TYPE_SERVER_DONE:
  492. err = tcp_comm_listen(tcp, TCP_PORT);
  493. if (err != ERR_OK) {
  494. DBG_PRINTF("Failed to start server: %d\n", err);
  495. }
  496. break;
  497. case EVENT_TYPE_REBOOT:
  498. tcp_comm_server_close(tcp);
  499. cyw43_arch_deinit();
  500. picowota_reboot(ev.reboot.to_bootloader);
  501. /* Should never get here */
  502. break;
  503. case EVENT_TYPE_GO:
  504. tcp_comm_server_close(tcp);
  505. cyw43_arch_deinit();
  506. disable_interrupts();
  507. reset_peripherals();
  508. jump_to_vtor(ev.go.vtor);
  509. /* Should never get here */
  510. break;
  511. };
  512. }
  513. cyw43_arch_poll();
  514. sleep_ms(5);
  515. }
  516. cyw43_arch_deinit();
  517. return 0;
  518. }