ota_file_patch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief OTA firmware file patch
  9. */
  10. #include <kernel.h>
  11. #include <string.h>
  12. #include <soc.h>
  13. #include <mem_manager.h>
  14. #include <ota_backend.h>
  15. #include <ota_storage.h>
  16. #include "ota_image.h"
  17. #include "ota_file_patch.h"
  18. #include "hpatch.h"
  19. #include <os_common_api.h>
  20. static int g_patch_data_offs = 0;
  21. static const unsigned short crc16_table[256] = {
  22. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  23. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  24. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  25. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  26. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  27. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  28. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  29. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  30. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  31. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  32. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  33. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  34. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  35. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  36. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  37. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  38. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  39. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  40. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  41. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  42. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  43. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  44. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  45. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  46. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  47. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  48. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  49. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  50. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  51. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  52. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  53. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78,
  54. };
  55. static unsigned short reflect16(unsigned short b) {
  56. unsigned short v = 0;
  57. v |= (b & 0x8000) >> 15;
  58. v |= (b & 0x4000) >> 13;
  59. v |= (b & 0x2000) >> 11;
  60. v |= (b & 0x1000) >> 9;
  61. v |= (b & 0x0800) >> 7;
  62. v |= (b & 0x0400) >> 5;
  63. v |= (b & 0x0200) >> 3;
  64. v |= (b & 0x0100) >> 1;
  65. v |= (b & 0x0080) << 1;
  66. v |= (b & 0x0040) << 3;
  67. v |= (b & 0x0020) << 5;
  68. v |= (b & 0x0010) << 7;
  69. v |= (b & 0x0008) << 9;
  70. v |= (b & 0x0004) << 11;
  71. v |= (b & 0x0002) << 13;
  72. v |= (b & 0x0001) << 15;
  73. return v;
  74. }
  75. static unsigned short rom_crc16(const unsigned char *buf, int len, unsigned short initial_crc)
  76. {
  77. unsigned short crc = initial_crc;
  78. crc = reflect16(crc);
  79. while (len--) {
  80. crc = ((crc >> 8) & 0xff) ^ crc16_table[(crc & 0xff) ^ *buf++];
  81. }
  82. crc = reflect16(crc);
  83. crc = ((crc >> 8) & 0xff) | ((crc & 0xff) << 8);
  84. return crc;
  85. }
  86. static int ota_patch_read_old_data(hpatch_TStreamInputHandle streamHandle, const hpatch_StreamPos_t readFromPos,
  87. unsigned char* out_data, unsigned char* out_data_end)
  88. {
  89. struct ota_file_patch_info *ota_patch;
  90. int read_len;
  91. ota_patch = (struct ota_file_patch_info *)streamHandle;
  92. if (!ota_patch || !ota_patch->storage)
  93. return -1;
  94. read_len = ((int)out_data_end - (int)out_data);
  95. memcpy(out_data, ota_patch->old_file_mapping_addr + readFromPos, read_len);
  96. return read_len;
  97. }
  98. static int ota_patch_read_patch_data(hpatch_TStreamInputHandle streamHandle, const hpatch_StreamPos_t readFromPos,
  99. unsigned char* out_data, unsigned char* out_data_end)
  100. {
  101. struct ota_file_patch_info *ota_patch;
  102. int read_len;
  103. int err;
  104. ota_patch = (struct ota_file_patch_info *)streamHandle;
  105. if (!ota_patch || !ota_patch->img)
  106. return -1;
  107. read_len = ((int)out_data_end - (int)out_data);
  108. err = ota_image_read(ota_patch->img,
  109. ota_patch->patch_file_offset + g_patch_data_offs + readFromPos,
  110. out_data, read_len);
  111. if (err) {
  112. SYS_LOG_ERR("cannot read data, offs 0x%x", readFromPos);
  113. return -EIO;
  114. }
  115. return read_len;
  116. }
  117. static int write_cache_data(struct ota_file_patch_info *ota_patch, int write_pos)
  118. {
  119. uint32_t addr;
  120. uint16_t data_crc;
  121. int seg_size;
  122. if (write_pos % 0x20) {
  123. SYS_LOG_ERR("BUG: write_pos 0x%x\n", write_pos);
  124. return -EINVAL;
  125. }
  126. if (ota_patch->flag_use_crc) {
  127. addr = ota_patch->new_file_offset + (write_pos / 0x20) * 0x22;
  128. data_crc = rom_crc16(ota_patch->write_cache, 0x20, 0xffff);
  129. ota_patch->write_cache[0x20] = data_crc & 0xff;
  130. ota_patch->write_cache[0x21] = (data_crc >> 8) & 0xff;
  131. seg_size = 0x22;
  132. } else {
  133. addr = ota_patch->new_file_offset + write_pos;
  134. seg_size = 0x20;
  135. }
  136. if (ota_patch->flag_use_encrypt)
  137. addr |= 0x80000000;
  138. ota_storage_write(ota_patch->storage, addr,
  139. ota_patch->write_cache, seg_size);
  140. ota_patch->write_cache_pos = 0;
  141. ota_patch->write_cache_offs = write_pos + 0x20;
  142. return 0;
  143. }
  144. static int flush_cache_data(struct ota_file_patch_info *ota_patch)
  145. {
  146. SYS_LOG_INF("flush ota cache write_cache_offs 0x%x, write_cache_pos 0x%x",
  147. ota_patch->write_cache_offs,
  148. ota_patch->write_cache_pos);
  149. if (ota_patch->write_cache_pos == 0)
  150. return 0;
  151. memset(ota_patch->write_cache + ota_patch->write_cache_pos, 0x0, 0x20 - ota_patch->write_cache_pos);
  152. return write_cache_data(ota_patch, ota_patch->write_cache_offs);
  153. }
  154. int write_data_with_crc_rand(struct ota_file_patch_info *ota_patch, int write_pos,
  155. const unsigned char* data, int write_len)
  156. {
  157. int wlen, write_seg, unaligned_pos;
  158. wlen = write_len;
  159. if ((ota_patch->write_cache_offs + ota_patch->write_cache_pos) != write_pos) {
  160. SYS_LOG_ERR("BUG: write_cache_offs 0x%x, cur write_pos 0x%x",
  161. ota_patch->write_cache_offs, write_pos);
  162. return 0;
  163. }
  164. unaligned_pos = write_pos % 0x20;
  165. if (unaligned_pos) {
  166. write_seg = 0x20 - unaligned_pos;
  167. if (wlen < write_seg) {
  168. write_seg = wlen;
  169. }
  170. memcpy(ota_patch->write_cache + ota_patch->write_cache_pos, data, write_seg);
  171. ota_patch->write_cache_pos += write_seg;
  172. data += write_seg;
  173. wlen -= write_seg;
  174. write_pos += write_seg;
  175. if (ota_patch->write_cache_pos == 0x20) {
  176. write_cache_data(ota_patch, ota_patch->write_cache_offs);
  177. } else {
  178. return write_seg;
  179. }
  180. }
  181. while (wlen >= 0x20) {
  182. memcpy(ota_patch->write_cache, data, 0x20);
  183. write_cache_data(ota_patch, write_pos);
  184. write_pos += 0x20;
  185. data += 0x20;
  186. wlen -= 0x20;
  187. }
  188. if (wlen > 0) {
  189. SYS_LOG_INF("write to cache wlen 0x%x, write_cache_pos 0x%x",
  190. wlen, ota_patch->write_cache_pos);
  191. memcpy(ota_patch->write_cache, data, wlen);
  192. ota_patch->write_cache_offs = write_pos;
  193. ota_patch->write_cache_pos = wlen;
  194. }
  195. return write_len;
  196. }
  197. static int write_new_fw_file(hpatch_TStreamInputHandle streamHandle, const hpatch_StreamPos_t write_pos,
  198. const unsigned char* data, const unsigned char* data_end)
  199. {
  200. struct ota_file_patch_info *ota_patch;
  201. int write_len = ((int)data_end - (int)data);
  202. ota_patch = (struct ota_file_patch_info *)streamHandle;
  203. if (!ota_patch || !ota_patch->storage)
  204. return -1;
  205. SYS_LOG_INF("write pos 0x%x len 0x%x", write_pos, write_len);
  206. if (ota_patch->flag_use_crc || ota_patch->flag_use_encrypt) {
  207. write_data_with_crc_rand(ota_patch, write_pos, data, write_len);
  208. } else {
  209. ota_storage_write(ota_patch->storage, ota_patch->new_file_offset + write_pos,
  210. (uint8_t *)data, write_len);
  211. }
  212. return write_len;
  213. }
  214. static void open_old_fw_file(struct ota_file_patch_info *ota_patch, hpatch_TStreamInput *fw_stream)
  215. {
  216. fw_stream->streamHandle = ota_patch;
  217. fw_stream->streamSize = ota_patch->old_file_size;
  218. fw_stream->read = ota_patch_read_old_data;
  219. }
  220. static void open_patch_file(struct ota_file_patch_info *ota_patch, hpatch_TStreamInput *patch_stream)
  221. {
  222. patch_stream->streamHandle = ota_patch;
  223. patch_stream->streamSize = ota_patch->patch_file_size;
  224. patch_stream->read = ota_patch_read_patch_data;
  225. g_patch_data_offs = 0;
  226. }
  227. static void open_new_fw_file(struct ota_file_patch_info *ota_patch, unsigned int size, hpatch_TStreamOutput *stream_out)
  228. {
  229. stream_out->streamHandle = ota_patch;
  230. stream_out->streamSize = size;
  231. stream_out->write = write_new_fw_file;
  232. }
  233. static hpatch_StreamPos_t readSavedSize(unsigned char * diffData, unsigned int diffDataSize, int* out_sizeCodeLength)
  234. {
  235. hpatch_StreamPos_t highSize;
  236. unsigned int diffFileDataSize = diffDataSize;
  237. int sizeCodeLength = -1;
  238. hpatch_StreamPos_t newDataSize = diffData[0] | (diffData[1] << 8) | (diffData[2] << 16);
  239. if (diffData[3] != 0xFF) {
  240. sizeCodeLength = 4;
  241. newDataSize |= (diffData[3] << 24);
  242. } else {
  243. sizeCodeLength = 9;
  244. if ((sizeof(int) <= 4) || (diffFileDataSize < 9)) {
  245. SYS_LOG_ERR("diffFileDataSize error!");
  246. return 0;
  247. }
  248. newDataSize |= (diffData[4] << 24);
  249. highSize = diffData[5] | (diffData[6] << 8) | (diffData[7] << 16) | (diffData[8] << 24);
  250. newDataSize |= ((highSize << 16) << 16);
  251. }
  252. *out_sizeCodeLength = sizeCodeLength;
  253. return newDataSize;
  254. }
  255. static hpatch_StreamPos_t readNewDataSize(hpatch_TStreamInput *diffFileData)
  256. {
  257. char diffData[64];
  258. int readLength = 9;
  259. unsigned char diffData_begin = 0;
  260. int kNewDataSizeSavedSize = -1;
  261. hpatch_StreamPos_t newDataSize;
  262. if (readLength > diffFileData->streamSize) {
  263. readLength = (int)diffFileData->streamSize;
  264. }
  265. diffFileData->read(diffFileData->streamHandle, 0, &diffData[diffData_begin],
  266. &diffData[diffData_begin + readLength]);
  267. //print_buffer(diffData, 1, readLength, 16, 0);
  268. newDataSize = readSavedSize(diffData, sizeof(diffData), &kNewDataSizeSavedSize);
  269. diffFileData->streamSize -= kNewDataSizeSavedSize;
  270. g_patch_data_offs = kNewDataSizeSavedSize;
  271. return newDataSize;
  272. }
  273. int ota_file_patch_write(struct ota_file_patch_info *ota_patch)
  274. {
  275. hpatch_StreamPos_t newDataSize;
  276. hpatch_TStreamInput old_fw_stream;
  277. hpatch_TStreamInput patch_fw_stream;
  278. hpatch_TStreamOutput new_fw_stream;
  279. SYS_LOG_INF("new fw: offs 0x%x size 0x%x, old fw: mapping addr %p offs 0x%x size 0x%x, patch fw: offs 0x%x size 0x%x",
  280. ota_patch->new_file_offset, ota_patch->new_file_size,
  281. ota_patch->old_file_mapping_addr, ota_patch->old_file_offset, ota_patch->old_file_offset,
  282. ota_patch->patch_file_offset, ota_patch->patch_file_size);
  283. SYS_LOG_INF("flag_use_crc %d, flag_use_encrypt %d",
  284. ota_patch->flag_use_crc, ota_patch->flag_use_encrypt);
  285. open_old_fw_file(ota_patch, &old_fw_stream);
  286. open_patch_file(ota_patch, &patch_fw_stream);
  287. newDataSize = readNewDataSize(&patch_fw_stream);
  288. SYS_LOG_INF("new fw: file size 0x%x", newDataSize);
  289. open_new_fw_file(ota_patch, newDataSize, &new_fw_stream);
  290. if (!patch_stream(&new_fw_stream, &old_fw_stream, &patch_fw_stream)) {
  291. SYS_LOG_ERR(" patch_stream run error!!!");
  292. return -1;
  293. }
  294. flush_cache_data(ota_patch);
  295. return 0;
  296. }