Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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