stm32cubeprogrammer_uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2021-2023, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <endian.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.h>
  12. #include <drivers/delay_timer.h>
  13. #include <drivers/st/stm32_iwdg.h>
  14. #include <drivers/st/stm32_uart.h>
  15. #include <drivers/st/stm32_uart_regs.h>
  16. #include <lib/mmio.h>
  17. #include <tools_share/firmware_image_package.h>
  18. #include <platform_def.h>
  19. #include <stm32cubeprogrammer.h>
  20. /* USART bootloader protocol version V4.0 */
  21. #define USART_BL_VERSION 0x40U
  22. /* Command definition */
  23. #define GET_CMD_COMMAND 0x00U
  24. #define GET_VER_COMMAND 0x01U
  25. #define GET_ID_COMMAND 0x02U
  26. #define PHASE_COMMAND 0x03U
  27. #define READ_PART_COMMAND 0x12U
  28. #define START_COMMAND 0x21U
  29. #define DOWNLOAD_COMMAND 0x31U
  30. /* Answer defines */
  31. #define INIT_BYTE 0x7FU
  32. #define ACK_BYTE 0x79U
  33. #define NACK_BYTE 0x1FU
  34. #define ABORT 0x5FU
  35. #define UNDEFINED_DOWN_ADDR U(0xFFFFFFFF)
  36. #define PROGRAMMER_TIMEOUT_US 20000U
  37. static const uint8_t command_tab[] = {
  38. GET_CMD_COMMAND,
  39. GET_VER_COMMAND,
  40. GET_ID_COMMAND,
  41. PHASE_COMMAND,
  42. START_COMMAND,
  43. DOWNLOAD_COMMAND
  44. };
  45. /* STM32CubeProgrammer over UART handle */
  46. struct stm32prog_uart_handle_s {
  47. struct stm32_uart_handle_s uart;
  48. uint32_t packet;
  49. uint8_t *addr;
  50. uint32_t len;
  51. uint8_t phase;
  52. /* Error msg buffer: max 255 in UART protocol, reduced in TF-A */
  53. uint8_t error[64];
  54. } handle;
  55. /* Trace and handle unrecoverable UART protocol error */
  56. #define STM32PROG_ERROR(...) \
  57. { \
  58. ERROR(__VA_ARGS__); \
  59. if (handle.phase != PHASE_RESET) { \
  60. snprintf((char *)&handle.error, sizeof(handle.error), __VA_ARGS__); \
  61. handle.phase = PHASE_RESET; \
  62. handle.addr = (uint8_t *)UNDEFINED_DOWN_ADDR; \
  63. handle.len = 0U; \
  64. handle.packet = 0U; \
  65. } \
  66. }
  67. static int uart_write(const uint8_t *addr, uint16_t size)
  68. {
  69. while (size != 0U) {
  70. if (stm32_uart_putc(&handle.uart, *addr) != 0) {
  71. return -EIO;
  72. }
  73. size--;
  74. addr++;
  75. }
  76. return 0;
  77. }
  78. static int uart_write_8(uint8_t byte)
  79. {
  80. return stm32_uart_putc(&handle.uart, byte);
  81. }
  82. static int uart_write_32(uint32_t value)
  83. {
  84. return uart_write((uint8_t *)&value, 4U);
  85. }
  86. static int uart_read_8(uint8_t *byte)
  87. {
  88. int ret;
  89. uint64_t timeout_ref = timeout_init_us(PROGRAMMER_TIMEOUT_US);
  90. do {
  91. ret = stm32_uart_getc(&handle.uart);
  92. if (ret == -EAGAIN) {
  93. if (timeout_elapsed(timeout_ref)) {
  94. return -ETIMEDOUT;
  95. }
  96. } else if (ret < 0) {
  97. return ret;
  98. }
  99. } while (ret == -EAGAIN);
  100. *byte = (uint8_t)ret;
  101. return 0;
  102. }
  103. static int uart_send_result(uint8_t byte)
  104. {
  105. int ret;
  106. /* Always flush fifo before to send result = read all pending data */
  107. do {
  108. ret = stm32_uart_getc(&handle.uart);
  109. } while (ret >= 0);
  110. return uart_write_8(byte);
  111. }
  112. static bool is_valid_header(fip_toc_header_t *header)
  113. {
  114. return (header->name == TOC_HEADER_NAME) &&
  115. (header->serial_number != 0U);
  116. }
  117. static int uart_receive_command(uint8_t *command)
  118. {
  119. uint8_t byte = 0U;
  120. uint8_t xor = 0U;
  121. unsigned int count;
  122. bool found = false;
  123. int ret;
  124. /* Repeat read until something is received */
  125. do {
  126. stm32_iwdg_refresh();
  127. ret = uart_read_8(&byte);
  128. } while (ret == -ETIMEDOUT);
  129. if (ret != 0) {
  130. return ret;
  131. }
  132. /* Handle reconnection request */
  133. if (byte == INIT_BYTE) {
  134. *command = byte;
  135. return 0;
  136. }
  137. for (count = 0U; count < ARRAY_SIZE(command_tab); count++) {
  138. if (command_tab[count] == byte) {
  139. found = true;
  140. break;
  141. }
  142. }
  143. if (!found) {
  144. VERBOSE("UART: Command unknown (byte=0x%x)\n", byte);
  145. return -EPROTO;
  146. }
  147. ret = uart_read_8(&xor);
  148. if (ret != 0) {
  149. return ret;
  150. }
  151. if ((byte ^ xor) != 0xFF) {
  152. VERBOSE("UART: Command XOR check fail (byte=0x%x, xor=0x%x)\n",
  153. byte, xor);
  154. return -EPROTO;
  155. }
  156. *command = byte;
  157. return 0;
  158. }
  159. static int get_cmd_command(void)
  160. {
  161. const uint8_t msg[2] = {
  162. sizeof(command_tab), /* Length of data - 1 */
  163. USART_BL_VERSION
  164. };
  165. int ret;
  166. ret = uart_write(msg, sizeof(msg));
  167. if (ret != 0) {
  168. return ret;
  169. }
  170. return uart_write(command_tab, sizeof(command_tab));
  171. }
  172. static int get_version_command(void)
  173. {
  174. return uart_write_8(STM32_TF_VERSION);
  175. }
  176. static int get_id_command(void)
  177. {
  178. uint8_t msg[3] = {
  179. sizeof(msg) - 1 /* Length of data - 1 */
  180. };
  181. uint32_t chip_id = stm32mp_get_chip_dev_id();
  182. be16enc(&msg[1], chip_id);
  183. return uart_write(msg, sizeof(msg));
  184. }
  185. static int uart_send_phase(uint32_t address)
  186. {
  187. int ret;
  188. uint8_t msg_size = 5U; /* Length of data - 1 */
  189. uint8_t error_size = 0U;
  190. /* Additional information only for RESET phase */
  191. if (handle.phase == PHASE_RESET) {
  192. error_size = strnlen((char *)&handle.error, sizeof(handle.error));
  193. }
  194. ret = uart_write_8(msg_size + error_size);
  195. if (ret != 0) {
  196. return ret;
  197. }
  198. /* Send the ID of next partition */
  199. ret = uart_write_8(handle.phase);
  200. if (ret != 0) {
  201. return ret;
  202. }
  203. /* Destination address */
  204. ret = uart_write_32(address);
  205. if (ret != 0) {
  206. return ret;
  207. }
  208. ret = uart_write_8(error_size);
  209. if (ret != 0) {
  210. return ret;
  211. }
  212. /* Additional information: message error */
  213. if (error_size > 0U) {
  214. ret = uart_write(handle.error, error_size);
  215. }
  216. return ret;
  217. }
  218. static int uart_download_part(void)
  219. {
  220. uint8_t operation = 0U;
  221. uint8_t xor;
  222. uint8_t byte = 0U;
  223. uint32_t packet_number = 0U;
  224. uint32_t packet_size = 0U;
  225. uint32_t i = 0U;
  226. int ret;
  227. /* Get operation number */
  228. ret = uart_read_8(&operation);
  229. if (ret != 0) {
  230. return ret;
  231. }
  232. xor = operation;
  233. /* Get packet number */
  234. for (i = 3U; i != 0U; i--) {
  235. ret = uart_read_8(&byte);
  236. if (ret != 0) {
  237. return ret;
  238. }
  239. xor ^= byte;
  240. packet_number = (packet_number << 8) | byte;
  241. }
  242. if (packet_number != handle.packet) {
  243. WARN("UART: Bad packet number receive: %u, expected %u\n",
  244. packet_number, handle.packet);
  245. return -EPROTO;
  246. }
  247. /* Checksum */
  248. ret = uart_read_8(&byte);
  249. if (ret != 0) {
  250. return ret;
  251. }
  252. if (xor != byte) {
  253. VERBOSE("UART: Download Command checksum xor: %x, received %x\n",
  254. xor, byte);
  255. return -EPROTO;
  256. }
  257. ret = uart_send_result(ACK_BYTE);
  258. if (ret != 0) {
  259. return ret;
  260. }
  261. ret = uart_read_8(&byte);
  262. if (ret != 0) {
  263. return ret;
  264. }
  265. xor = byte;
  266. packet_size = byte + 1U;
  267. if (handle.len < packet_size) {
  268. STM32PROG_ERROR("Download overflow at %p\n", handle.addr + packet_size);
  269. return 0;
  270. }
  271. for (i = 0U; i < packet_size; i++) {
  272. ret = uart_read_8(&byte);
  273. if (ret != 0) {
  274. return ret;
  275. }
  276. *(handle.addr + i) = byte;
  277. xor ^= byte;
  278. }
  279. /* Checksum */
  280. ret = uart_read_8(&byte) != 0;
  281. if (ret != 0) {
  282. return ret;
  283. }
  284. if (xor != byte) {
  285. VERBOSE("UART: Download Data checksum xor: %x, received %x\n",
  286. xor, byte);
  287. return -EPROTO;
  288. }
  289. /* Packet treated */
  290. handle.packet++;
  291. handle.addr += packet_size;
  292. handle.len -= packet_size;
  293. return 0;
  294. }
  295. static int uart_start_cmd(uintptr_t buffer)
  296. {
  297. uint8_t byte = 0U;
  298. uint8_t xor = 0U;
  299. uint32_t i;
  300. uint32_t start_address = 0U;
  301. int ret;
  302. /* Get address */
  303. for (i = 4U; i != 0U; i--) {
  304. ret = uart_read_8(&byte);
  305. if (ret != 0U) {
  306. return ret;
  307. }
  308. xor ^= byte;
  309. start_address = (start_address << 8) | byte;
  310. }
  311. /* Checksum */
  312. ret = uart_read_8(&byte);
  313. if (ret != 0) {
  314. return ret;
  315. }
  316. if (xor != byte) {
  317. VERBOSE("UART: Start Command checksum xor: %x, received %x\n",
  318. xor, byte);
  319. return -EPROTO;
  320. }
  321. if (start_address != UNDEFINED_DOWN_ADDR) {
  322. STM32PROG_ERROR("Invalid start at %x, for phase %u\n",
  323. start_address, handle.phase);
  324. return 0;
  325. }
  326. if (!is_valid_header((fip_toc_header_t *)buffer)) {
  327. STM32PROG_ERROR("FIP Header check failed %lx, for phase %u\n",
  328. buffer, handle.phase);
  329. return -EIO;
  330. }
  331. VERBOSE("FIP header looks OK.\n");
  332. return 0;
  333. }
  334. static int uart_read(uint8_t id, uintptr_t buffer, size_t length)
  335. {
  336. bool start_done = false;
  337. int ret;
  338. uint8_t command = 0U;
  339. handle.phase = id;
  340. handle.packet = 0U;
  341. handle.addr = (uint8_t *)buffer;
  342. handle.len = length;
  343. INFO("UART: read phase %u at 0x%lx size 0x%zx\n",
  344. id, buffer, length);
  345. while (!start_done) {
  346. ret = uart_receive_command(&command);
  347. if (ret != 0) {
  348. /* Delay to wait STM32CubeProgrammer end of transmission */
  349. mdelay(3);
  350. ret = uart_send_result(NACK_BYTE);
  351. if (ret != 0U) {
  352. return ret;
  353. }
  354. continue;
  355. }
  356. uart_send_result(ACK_BYTE);
  357. switch (command) {
  358. case INIT_BYTE:
  359. INFO("UART: Connected\n");
  360. /* Nothing to do */
  361. continue;
  362. case GET_CMD_COMMAND:
  363. ret = get_cmd_command();
  364. break;
  365. case GET_VER_COMMAND:
  366. ret = get_version_command();
  367. break;
  368. case GET_ID_COMMAND:
  369. ret = get_id_command();
  370. break;
  371. case PHASE_COMMAND:
  372. ret = uart_send_phase((uint32_t)buffer);
  373. if ((ret == 0) && (handle.phase == PHASE_RESET)) {
  374. start_done = true;
  375. INFO("UART: Reset\n");
  376. }
  377. break;
  378. case DOWNLOAD_COMMAND:
  379. ret = uart_download_part();
  380. break;
  381. case START_COMMAND:
  382. ret = uart_start_cmd(buffer);
  383. if ((ret == 0) && (handle.phase == id)) {
  384. INFO("UART: Start phase %u\n", handle.phase);
  385. start_done = true;
  386. }
  387. break;
  388. default:
  389. WARN("UART: Unknown command\n");
  390. ret = -EINVAL;
  391. break;
  392. }
  393. if (ret == 0) {
  394. ret = uart_send_result(ACK_BYTE);
  395. } else {
  396. ret = uart_send_result(NACK_BYTE);
  397. }
  398. if (ret != 0) {
  399. return ret;
  400. }
  401. }
  402. stm32_uart_flush(&handle.uart);
  403. return 0;
  404. }
  405. /* Init UART: 115200, 8bit 1stop parity even and enable FIFO mode */
  406. const struct stm32_uart_init_s init = {
  407. .baud_rate = STM32MP_UART_BAUDRATE,
  408. .word_length = STM32_UART_WORDLENGTH_9B,
  409. .stop_bits = STM32_UART_STOPBITS_1,
  410. .parity = STM32_UART_PARITY_EVEN,
  411. .hw_flow_control = STM32_UART_HWCONTROL_NONE,
  412. .mode = STM32_UART_MODE_TX_RX,
  413. .fifo_mode = STM32_UART_FIFOMODE_EN,
  414. };
  415. int stm32cubeprog_uart_load(uintptr_t instance, uintptr_t base, size_t len)
  416. {
  417. int ret;
  418. if (stm32_uart_init(&handle.uart, instance, &init) != 0) {
  419. return -EIO;
  420. }
  421. /*
  422. * The following NACK_BYTE is written because STM32CubeProgrammer has
  423. * already sent its command before TF-A has reached this point, and
  424. * because FIFO was not configured by BootROM.
  425. * The byte in the UART_RX register is then the checksum and not the
  426. * command. NACK_BYTE has to be written, so that the programmer will
  427. * re-send the good command.
  428. */
  429. ret = uart_send_result(NACK_BYTE);
  430. if (ret != 0) {
  431. return ret;
  432. }
  433. return uart_read(PHASE_SSBL, base, len);
  434. }