ymodem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * @file ymodem.c
  3. * @brief YMODEM-CRC 接收器(阻塞轮询式,无动态内存)
  4. *
  5. * 时序: [文件名块 block0] ACK C [数据块 1..N] C EOT ACK C [结束块00] ACK
  6. * - 1024/128 块自适应(逐块以块头 SOH/STX 判定);
  7. * - 块号 + 反码 + CRC16(XMODEM) 三重校验,重复块静默吞掉;
  8. * - 写 NOR 通过回调注入,本模块不感知存储介质;
  9. * - idle_ms 为无进展超时:握手阶段到点仍无起始块返回 YM_ERR_NO_START
  10. * (调用方据此放行跳 APP),半途超时返回 YM_ERR_TIMEOUT。
  11. * - 握手阶段(等文件名块)一旦看到 App 协议帧头 AA 55 就立刻返回 NO_START:
  12. * 那说明对面在讲 App 协议,本次不是升级;数据阶段不采信(固件里必然有 AA 55)。
  13. */
  14. #include "ymodem.h"
  15. #include "boot_crc.h"
  16. #include "boot_layout.h"
  17. #include "usart.h"
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #define YM_SOH 0x01U
  21. #define YM_STX 0x02U
  22. #define YM_EOT 0x04U
  23. #define YM_ACK 0x06U
  24. #define YM_NAK 0x15U
  25. #define YM_CAN 0x18U
  26. #define YM_C 0x43U
  27. /* 上位机 App 协议帧头(AA 55)。与 YMODEM 共用一根线,握手阶段看到它即说明
  28. * 对面在讲 App 协议,本次不可能是升级 —— 由 ym_recv_frame 的返回码 4 上报。 */
  29. #define YM_APP_HDR0 0xAAU
  30. #define YM_APP_HDR1 0x55U
  31. #define YM_BLOCK_TIMEOUT_MS 1500U /* 单块到达等待(含发送方处理 ACK 时间) */
  32. #define YM_CHAR_TIMEOUT_MS 50U /* 块内字节间隔 */
  33. #define YM_C_INTERVAL_MS 1000U /* 'C' 广播周期 */
  34. #define YM_IDLE_SESSION_MS 30000U /* 数据流阶段的无进展超时 */
  35. static UART_HandleTypeDef *s_hu;
  36. /* ---------------- 底层 ---------------- */
  37. static void ym_send(uint8_t b)
  38. {
  39. HAL_UART_Transmit(s_hu, &b, 1, 50);
  40. }
  41. /* 轮询收 1 字节;返回 0=超时。ORE 溢出标志即时清除不丢后续数据。 */
  42. static uint8_t ym_recv_byte(uint32_t timeout_ms, uint8_t *out)
  43. {
  44. uint32_t start = HAL_GetTick();
  45. while ((uint32_t)(HAL_GetTick() - start) < timeout_ms)
  46. {
  47. if (__HAL_UART_GET_FLAG(s_hu, UART_FLAG_ORE))
  48. {
  49. /* 注意!H7 的 ORE 必须写 ICR.ORECF 清(HAL 全程如此)。原先靠"读 RDR"清,
  50. * 一旦锁死,本函数就停在这条分支里把每个字节都丢掉 ⇒ 表现为"只发不收"
  51. * (RXNE 永远看不到)。2026-10-26 实测:BL 收不到任何字节,App 同线正常。 */
  52. __HAL_UART_CLEAR_FLAG(s_hu, UART_CLEAR_OREF);
  53. continue;
  54. }
  55. if (__HAL_UART_GET_FLAG(s_hu, UART_FLAG_RXNE))
  56. {
  57. *out = (uint8_t)(s_hu->Instance->RDR & 0xFFU);
  58. return 1;
  59. }
  60. }
  61. return 0;
  62. }
  63. void ymodem_init(UART_HandleTypeDef *huart)
  64. {
  65. s_hu = huart;
  66. }
  67. void ymodem_flush(void)
  68. {
  69. uint8_t d;
  70. uint32_t last = HAL_GetTick();
  71. while ((uint32_t)(HAL_GetTick() - last) < 20U)
  72. {
  73. if (ym_recv_byte(5, &d)) last = HAL_GetTick();
  74. }
  75. }
  76. /* ---------------- 帧接收 ---------------- */
  77. /* 返回: 0=收到完整帧(字段已填, 有效性待定) 1=EOT 2=CAN 3=超时/杂字节/帧结构错
  78. * 4=疑似上位机协议帧头(AA 55) —— 由调用方按所处阶段决定是否放行 */
  79. static uint8_t ym_recv_frame(ym_frame_t *fr)
  80. {
  81. uint8_t b, b2, h, l;
  82. uint32_t i;
  83. if (!ym_recv_byte(YM_BLOCK_TIMEOUT_MS, &b)) return 3;
  84. if (b == YM_EOT) return 1;
  85. if (b == YM_CAN) return 2;
  86. if (b == YM_APP_HDR0)
  87. {
  88. /* 多等一个字节确认是不是 55;不是就只丢这 1 字节(本阶段本来就是噪声) */
  89. if (ym_recv_byte(YM_CHAR_TIMEOUT_MS, &b2) && b2 == YM_APP_HDR1) return 4;
  90. return 3;
  91. }
  92. if (b != YM_SOH && b != YM_STX) return 3;
  93. fr->size = (b == YM_SOH) ? 128U : 1024U;
  94. if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &fr->blk)) return 3;
  95. if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &fr->blk_inv)) return 3;
  96. for (i = 0; i < fr->size; i++)
  97. if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &fr->data[i])) return 3;
  98. if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &h)) return 3;
  99. if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &l)) return 3;
  100. fr->crc = ((uint16_t)h << 8) | l;
  101. return 0;
  102. }
  103. static uint8_t ym_frame_valid(const ym_frame_t *fr, uint8_t expect_blk)
  104. {
  105. if (fr->blk != expect_blk) return 0;
  106. if ((uint8_t)(fr->blk ^ 0xFFU) != fr->blk_inv) return 0;
  107. if (boot_ymodem_crc16(fr->data, fr->size) != fr->crc) return 0;
  108. return 1;
  109. }
  110. /* ---------------- 会话 ---------------- */
  111. ym_result_t ymodem_receive(uint32_t idle_ms,
  112. void (*on_start_cb)(void),
  113. uint8_t (*write_cb)(uint32_t off, const uint8_t *d, uint32_t n))
  114. {
  115. ym_result_t res;
  116. ym_frame_t fr;
  117. uint8_t state = 0; /* 0=等文件名块 1=收数据块 2=等结束块 */
  118. uint32_t blk_exp = 0; /* 绝对块序号, 不回绕; 与线上块号比对时取低 8 位 */
  119. uint8_t retries = 0;
  120. uint32_t t_last_c;
  121. uint32_t now;
  122. uint32_t last_progress;
  123. memset(&res, 0, sizeof(res));
  124. t_last_c = HAL_GetTick() - YM_C_INTERVAL_MS; /* 立即广播首个 'C' */
  125. last_progress = HAL_GetTick();
  126. for (;;)
  127. {
  128. now = HAL_GetTick();
  129. /* 空闲时周期性广播 'C'(state 1 由 ACK 后补发驱动) */
  130. if ((state == 0 || state == 2) &&
  131. (uint32_t)(now - t_last_c) >= YM_C_INTERVAL_MS)
  132. {
  133. ym_send(YM_C);
  134. t_last_c = now;
  135. }
  136. {
  137. uint8_t r = ym_recv_frame(&fr);
  138. if (r == 1) /* EOT */
  139. {
  140. if (state == 1)
  141. {
  142. ym_send(YM_ACK);
  143. state = 2;
  144. blk_exp = 0;
  145. ym_send(YM_C);
  146. t_last_c = HAL_GetTick();
  147. continue;
  148. }
  149. if (state == 2)
  150. {
  151. ym_send(YM_ACK); /* 结束块后又来 EOT → 完成 */
  152. res.result = YM_ERR_NONE;
  153. return res;
  154. }
  155. /* state==0:还没收到文件名块,此时的 0x04 只能是噪声 —— 上位机
  156. * 0x04 查询帧 "AA 55 04 ..." 的第 3 字节就是 0x04(=EOT)。若在这里
  157. * 当"接收完成",调用方会拿 NOR staging 里的残留包重烧一遍(现场表现:
  158. * 发一条查询就自动升级,版本号不变)。 */
  159. continue;
  160. }
  161. if (r == 2) { res.result = YM_ERR_ABORT; return res; }
  162. if (r == 4)
  163. {
  164. /* 只在"等文件名块"阶段采信:此时对面若在讲 App 协议,本次就不可能是
  165. * 升级,直接放行跳 APP。数据阶段绝不能采信 —— 固件二进制里必然有 AA 55。 */
  166. if (state == 0)
  167. {
  168. res.result = YM_ERR_NO_START;
  169. return res;
  170. }
  171. continue;
  172. }
  173. if (r == 3)
  174. {
  175. /* 无进展超时: 握手阶段用 idle_ms,
  176. * 数据流阶段放宽到 YM_IDLE_SESSION_MS(发送方写盘/换块可能停顿) */
  177. uint32_t idle_eff = (state == 0) ? idle_ms : YM_IDLE_SESSION_MS;
  178. if ((uint32_t)(HAL_GetTick() - last_progress) >= idle_eff)
  179. {
  180. /* 死在哪一块: state 1 说明数据块阶段, blk_exp-1 = 已接受块数 */
  181. res.blocks_done = (state == 1U) ? (uint32_t)(blk_exp - 1U) : 0U;
  182. if (state == 0 && retries == 0)
  183. res.result = YM_ERR_NO_START; /* 探测窗口放行 */
  184. else
  185. {
  186. ym_send(YM_CAN); ym_send(YM_CAN);
  187. res.result = YM_ERR_TIMEOUT;
  188. }
  189. return res;
  190. }
  191. continue;
  192. }
  193. last_progress = HAL_GetTick();
  194. }
  195. if (state == 0)
  196. {
  197. /* ---- 文件名块 ---- */
  198. if (ym_frame_valid(&fr, 0))
  199. {
  200. char *q;
  201. fr.data[fr.size - 1] = '\0';
  202. q = strchr((char *)fr.data, ',');
  203. if (q)
  204. {
  205. uint32_t sz = strtoul(q + 1, 0, 10);
  206. if (sz > 64U && sz <= NOR_IMG_STAGING_SIZE)
  207. {
  208. res.image_size = sz;
  209. res.size_known = 1;
  210. }
  211. else if (sz > NOR_IMG_STAGING_SIZE)
  212. {
  213. ym_send(YM_CAN); ym_send(YM_CAN);
  214. res.result = YM_ERR_TOOLARGE;
  215. return res;
  216. }
  217. }
  218. /* on_start_cb 必须在 ACK 之前执行: 它写 EEPROM 期间 CPU 不读 RDR,
  219. * 若放在 ACK 之后, 对端收到 ACK 立刻发第 1 个数据块, 块首若干字节
  220. * 被 ORE 吞掉->帧边界自此永久错位, 之后每次重传都拼不出完整帧。 */
  221. if (on_start_cb) on_start_cb();
  222. ym_send(YM_ACK);
  223. ym_send(YM_C);
  224. t_last_c = HAL_GetTick();
  225. state = 1;
  226. blk_exp = 1;
  227. retries = 0;
  228. continue;
  229. }
  230. ym_send(YM_NAK);
  231. if (++retries > BL_YMODEM_MAX_RETRIES) goto fail_retries;
  232. continue;
  233. }
  234. if (state == 2)
  235. {
  236. /* ---- 结束块: blk 0, 全 0 填充, CRC 完整校验 ---- */
  237. if (ym_frame_valid(&fr, 0))
  238. {
  239. ym_send(YM_ACK);
  240. /* 一个字节都没落盘就"完成",数据只可能来自 NOR 残留包 —— 宁可判失败
  241. * 也不能烧。已解析出大小时,还要求实收不少于声明大小。 */
  242. if (res.recv_bytes == 0U ||
  243. (res.size_known && res.recv_bytes < res.image_size))
  244. {
  245. ym_send(YM_CAN); ym_send(YM_CAN);
  246. res.result = YM_ERR_ABORT;
  247. return res;
  248. }
  249. res.result = YM_ERR_NONE;
  250. return res;
  251. }
  252. ym_send(YM_NAK);
  253. if (++retries > BL_YMODEM_MAX_RETRIES) goto fail_retries;
  254. continue;
  255. }
  256. /* state==1 数据块 */
  257. if (!ym_frame_valid(&fr, (uint8_t)blk_exp))
  258. {
  259. if (blk_exp > 1 && fr.blk == (uint8_t)(blk_exp - 1U) &&
  260. boot_ymodem_crc16(fr.data, fr.size) == fr.crc)
  261. {
  262. /* 上一块的重发(ACK 丢失): 重发 ACK, 不重复写 */
  263. ym_send(YM_ACK);
  264. continue;
  265. }
  266. ym_send(YM_NAK);
  267. if (++retries > BL_YMODEM_MAX_RETRIES) goto fail_retries;
  268. continue;
  269. }
  270. retries = 0;
  271. {
  272. uint32_t offset = (blk_exp - 1U) * fr.size;
  273. /* 容量检查必须用 64 位: 若 offset 被算成回绕值(如 0xFFFFFC00),
  274. * 32 位加法 offset+size 会溢出成 0 从而"看起来"合法, 于是越界地址
  275. * 被静默送给 NOR (24 位地址还会被截断) —— 2026-09-17 现场故障
  276. * (卡在第 257 块, 设备 30s 不理人) 正是这条路。 */
  277. if ((uint64_t)offset + fr.size > NOR_IMG_STAGING_SIZE)
  278. {
  279. res.blocks_done = (uint32_t)(blk_exp - 1U);
  280. ym_send(YM_CAN); ym_send(YM_CAN);
  281. res.result = YM_ERR_OVERSIZE;
  282. return res;
  283. }
  284. if (write_cb(offset, fr.data, fr.size) != 0)
  285. {
  286. res.blocks_done = (uint32_t)(blk_exp - 1U);
  287. ym_send(YM_CAN); ym_send(YM_CAN);
  288. res.result = YM_ERR_WRITE;
  289. return res;
  290. }
  291. if (offset + fr.size > res.recv_bytes)
  292. res.recv_bytes = offset + fr.size;
  293. res.blocks_done = blk_exp; /* 已接受块数 = 绝对序号 */
  294. }
  295. ym_send(YM_ACK);
  296. ym_send(YM_C);
  297. t_last_c = HAL_GetTick();
  298. blk_exp++;
  299. }
  300. fail_retries:
  301. ym_send(YM_CAN); ym_send(YM_CAN);
  302. res.result = YM_ERR_RETRIES;
  303. return res;
  304. }