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.

main.c 13KB

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