ota_storage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief OTA storage interface
  9. */
  10. #include <kernel.h>
  11. #include <drivers/flash.h>
  12. #include <string.h>
  13. #include <mem_manager.h>
  14. #include <ota_storage.h>
  15. #include <os_common_api.h>
  16. #ifdef CONFIG_WATCHDOG
  17. #include <watchdog_hal.h>
  18. #endif
  19. #define CONFIG_XSPI_NOR_ACTS_DEV_NAME "spi_flash"
  20. #define CONFIG_XSPI_EXT_NOR_ACTS_DEV_NAME "spi_flash_2"
  21. #define OTA_STORAGE_EXT_DEVICE_NAME "spinand"
  22. #define OTA_STORAGE_SD_DEVICE_NAME "sd"
  23. #define XIP_DEV_NAME CONFIG_XSPI_NOR_ACTS_DEV_NAME
  24. #define OTA_STORAGE_SD_SECTOR_SIZE (512)
  25. enum ota_storage_type {
  26. OTA_STORAGE_SPINOR = 0,
  27. OTA_STORAGE_SD,
  28. OTA_STORAGE_NAND,
  29. OTA_STORAGE_EXT_NOR,
  30. OTA_STORAGE_BOOTNAND,
  31. OTA_STORAGE_MAX_TYPE
  32. };
  33. struct ota_storage
  34. {
  35. const struct device *dev;
  36. const char *dev_name;
  37. int max_write_seg;
  38. int max_erase_seg;
  39. int storage_id; /* code run on this device? */
  40. int storage_type; /* storage type and refer to enum ota_storage_type */
  41. };
  42. #define IS_STORAGE_TYPE_SD_NAND(x) ((x)->storage_type == OTA_STORAGE_SD || (x)->storage_type == OTA_STORAGE_NAND || (x)->storage_type == OTA_STORAGE_BOOTNAND)
  43. static struct ota_storage global_ota_storage[OTA_STORAGE_MAX_TYPE];
  44. int ota_storage_sync(struct ota_storage *storage)
  45. {
  46. if (storage && storage->dev && (storage->storage_type == OTA_STORAGE_NAND || storage->storage_type == OTA_STORAGE_BOOTNAND))
  47. flash_flush(storage->dev, true);
  48. return 0;
  49. }
  50. void ota_storage_set_max_write_seg(struct ota_storage *storage, int max_write_seg)
  51. {
  52. if (max_write_seg <= 0)
  53. return;
  54. storage->max_write_seg = max_write_seg;
  55. }
  56. void ota_storage_set_max_erase_seg(struct ota_storage *storage, int max_erase_seg)
  57. {
  58. if (max_erase_seg <= 0)
  59. return;
  60. storage->max_erase_seg = max_erase_seg;
  61. }
  62. int ota_storage_get_storage_id(struct ota_storage *storage)
  63. {
  64. return storage->storage_id;
  65. }
  66. static int ota_storage_write_sd_nand(struct ota_storage *storage, int offs,
  67. uint8_t *buf, int size)
  68. {
  69. int err = 0;
  70. uint32_t count, left;
  71. uint8_t *buffer;
  72. SYS_LOG_DBG("offs 0x%x, buf %p, size %d", offs, buf, size);
  73. if (storage == NULL || storage->dev == NULL)
  74. return -EINVAL;
  75. if (offs % OTA_STORAGE_SD_SECTOR_SIZE) {
  76. SYS_LOG_ERR("invalid offset %d", offs);
  77. return -EINVAL;
  78. }
  79. count = size / OTA_STORAGE_SD_SECTOR_SIZE;
  80. left = size % OTA_STORAGE_SD_SECTOR_SIZE;
  81. offs /= OTA_STORAGE_SD_SECTOR_SIZE;
  82. if (count) {
  83. err = flash_write(storage->dev, offs<<9, buf, count<<9);
  84. if (err) {
  85. SYS_LOG_ERR("write error %d, offs 0x%x, count %d", err, offs, count);
  86. return -EIO;
  87. }
  88. }
  89. if (left) {
  90. SYS_LOG_INF("write residual data %d", left);
  91. buffer = (uint8_t *)mem_malloc(OTA_STORAGE_SD_SECTOR_SIZE);
  92. if (!buffer) {
  93. SYS_LOG_ERR("can not malloc %d size", OTA_STORAGE_SD_SECTOR_SIZE);
  94. return -ENOMEM;
  95. }
  96. memset(buffer, 0, OTA_STORAGE_SD_SECTOR_SIZE);
  97. buf += (count * OTA_STORAGE_SD_SECTOR_SIZE);
  98. offs += count;
  99. memcpy(buffer, buf, left);
  100. err = flash_write(storage->dev, offs<<9, buffer, 1<<9);
  101. mem_free(buffer);
  102. }
  103. return err;
  104. }
  105. static int ota_storage_write_default(struct ota_storage *storage, int offs,
  106. uint8_t *buf, int size)
  107. {
  108. int wlen, err;
  109. SYS_LOG_DBG("offs 0x%x, buf %p, size %d", offs, buf, size);
  110. if (storage == NULL || storage->dev == NULL)
  111. return -EINVAL;
  112. wlen = storage->max_write_seg;
  113. while (size > 0) {
  114. if (size < storage->max_write_seg)
  115. wlen = size;
  116. err = flash_write(storage->dev, offs, buf, wlen);
  117. if (err < 0) {
  118. SYS_LOG_ERR("write error %d, offs 0x%x, buf %p, size %d", err, offs, buf, size);
  119. return -EIO;
  120. }
  121. offs += wlen;
  122. buf += wlen;
  123. size -= wlen;
  124. }
  125. return 0;
  126. }
  127. int ota_storage_write(struct ota_storage *storage, int offs,
  128. uint8_t *buf, int size)
  129. {
  130. if (storage == NULL || storage->dev == NULL)
  131. return -EINVAL;
  132. if (IS_STORAGE_TYPE_SD_NAND(storage))
  133. return ota_storage_write_sd_nand(storage, offs, buf, size);
  134. else
  135. return ota_storage_write_default(storage, offs, buf, size);
  136. }
  137. static int ota_storage_read_sd_nand(struct ota_storage *storage, int offs,
  138. uint8_t *buf, int size)
  139. {
  140. int err = 0;
  141. uint32_t count, left;
  142. uint8_t *buffer = NULL;
  143. SYS_LOG_DBG("offs 0x%x, buf %p, size %d", offs, buf, size);
  144. if (storage == NULL || storage->dev == NULL)
  145. return -EINVAL;
  146. if (offs % OTA_STORAGE_SD_SECTOR_SIZE) {
  147. SYS_LOG_ERR("invalid offset %d", offs);
  148. return -EINVAL;
  149. }
  150. count = size / OTA_STORAGE_SD_SECTOR_SIZE;
  151. left = size % OTA_STORAGE_SD_SECTOR_SIZE;
  152. offs /= OTA_STORAGE_SD_SECTOR_SIZE;
  153. if (count) {
  154. err = flash_read(storage->dev, offs<<9, buf, count<<9);
  155. if (err) {
  156. SYS_LOG_ERR("read error %d, offs 0x%x, count %d", err, offs, count);
  157. return -EIO;
  158. }
  159. }
  160. if (left) {
  161. buffer = (uint8_t *)mem_malloc(OTA_STORAGE_SD_SECTOR_SIZE);
  162. if (!buffer) {
  163. SYS_LOG_ERR("can not malloc %d size", OTA_STORAGE_SD_SECTOR_SIZE);
  164. return -ENOMEM;
  165. }
  166. buf += (count * OTA_STORAGE_SD_SECTOR_SIZE);
  167. offs += count;
  168. err = flash_read(storage->dev, offs<<9, buffer, 1<<9);
  169. if (err) {
  170. SYS_LOG_ERR("read error %d, offs 0x%x", err, offs);
  171. err = -EIO;
  172. }
  173. memcpy(buf, buffer, left);
  174. mem_free(buffer);
  175. }
  176. return err;
  177. }
  178. static int ota_storage_read_default(struct ota_storage *storage, int offs,
  179. uint8_t *buf, int size)
  180. {
  181. int err;
  182. int rlen = OTA_STORAGE_DEFAULT_READ_SEGMENT_SIZE;
  183. SYS_LOG_DBG("offs 0x%x, buf %p, size %d", offs, buf, size);
  184. if (storage == NULL || storage->dev == NULL)
  185. return -EINVAL;
  186. while (size > 0) {
  187. if (size < OTA_STORAGE_DEFAULT_READ_SEGMENT_SIZE)
  188. rlen = size;
  189. err = flash_read(storage->dev, offs, buf, rlen);
  190. if (err < 0) {
  191. SYS_LOG_ERR("read error %d, offs 0x%x, buf %p, size %d", err, offs, buf, size);
  192. return -EIO;
  193. }
  194. offs += rlen;
  195. buf += rlen;
  196. size -= rlen;
  197. }
  198. return 0;
  199. }
  200. int ota_storage_read(struct ota_storage *storage, int offs,
  201. uint8_t *buf, int size)
  202. {
  203. if (storage == NULL || storage->dev == NULL)
  204. return -EINVAL;
  205. if (IS_STORAGE_TYPE_SD_NAND(storage))
  206. return ota_storage_read_sd_nand(storage, offs, buf, size);
  207. else
  208. return ota_storage_read_default(storage, offs, buf, size);
  209. }
  210. int ota_storage_sd_nand_is_clean(struct ota_storage *storage, int offs, int size,
  211. uint8_t *buf, int buf_size)
  212. {
  213. int i, err;
  214. uint32_t *wptr = NULL;
  215. uint8_t *cptr;
  216. if (storage == NULL || buf == NULL || buf_size == 0)
  217. return -EINVAL;
  218. while (size > 0) {
  219. if (size < buf_size)
  220. buf_size = size;
  221. err = ota_storage_read_sd_nand(storage, offs, buf, buf_size);
  222. if (err) {
  223. SYS_LOG_ERR("read error 0x%x, offs 0x%x, size %d", err, offs, size);
  224. return err;
  225. }
  226. wptr = (uint32_t *)buf;
  227. for (i = 0; i < (buf_size >> 2); i++) {
  228. if (*wptr++ != 0xffffffff)
  229. return 0;
  230. }
  231. offs += buf_size;
  232. size -= buf_size;
  233. }
  234. /* check unaligned data */
  235. cptr = (uint8_t *)wptr;
  236. if (cptr && buf != cptr) {
  237. for (i = 0; i < (buf_size & 0x3); i++) {
  238. if (*cptr++ != 0xff)
  239. return 0;
  240. }
  241. }
  242. return 1;
  243. }
  244. int ota_storage_is_clean(struct ota_storage *storage, int offs, int size,
  245. uint8_t *buf, int buf_size)
  246. {
  247. int i, err, read_size;
  248. uint32_t *wptr = NULL;
  249. uint8_t *cptr;
  250. //SYS_LOG_INF("offs 0x%x, size %d", offs, size);
  251. if (storage == NULL)
  252. return -EINVAL;
  253. if (IS_STORAGE_TYPE_SD_NAND(storage)) {
  254. return 0; // always dirty for sd/nand
  255. //return ota_storage_sd_nand_is_clean(storage, offs, size, buf, size);
  256. }
  257. if (((unsigned int)buf & 0x3) || (buf_size & 0x3)) {
  258. return -EINVAL;
  259. }
  260. read_size = (buf_size > OTA_STORAGE_DEFAULT_READ_SEGMENT_SIZE)?
  261. OTA_STORAGE_DEFAULT_READ_SEGMENT_SIZE : buf_size;
  262. while (size > 0) {
  263. if (size < read_size)
  264. read_size = size;
  265. err = flash_read(storage->dev, offs, buf, read_size);
  266. if (err) {
  267. SYS_LOG_ERR("read error 0x%x, offs 0x%x, size %d", err, offs, size);
  268. return -EIO;
  269. }
  270. wptr = (uint32_t *)buf;
  271. for (i = 0; i < (read_size >> 2); i++) {
  272. if (*wptr++ != 0xffffffff)
  273. return 0;
  274. }
  275. offs += read_size;
  276. //buf += read_size;
  277. size -= read_size;
  278. }
  279. /* check unaligned data */
  280. cptr = (uint8_t *)wptr;
  281. if (cptr && buf != cptr) {
  282. for (i = 0; i < (read_size & 0x3); i++) {
  283. if (*cptr++ != 0xff)
  284. return 0;
  285. }
  286. }
  287. return 1;
  288. }
  289. int ota_storage_erase_sd_nand(struct ota_storage *storage, int offs, int size)
  290. {
  291. int err = 0;
  292. uint32_t count;
  293. uint8_t *buffer;
  294. if (storage == NULL)
  295. return -EINVAL;
  296. if (offs % OTA_STORAGE_SD_SECTOR_SIZE) {
  297. SYS_LOG_ERR("invalid offset %d", offs);
  298. return -EINVAL;
  299. }
  300. count = size / OTA_STORAGE_SD_SECTOR_SIZE;
  301. if (size % OTA_STORAGE_SD_SECTOR_SIZE)
  302. count++;
  303. offs /= OTA_STORAGE_SD_SECTOR_SIZE;
  304. buffer = (uint8_t *)mem_malloc(OTA_STORAGE_SD_SECTOR_SIZE);
  305. if (!buffer) {
  306. SYS_LOG_ERR("can not malloc %d size", OTA_STORAGE_SD_SECTOR_SIZE);
  307. return -ENOMEM;
  308. }
  309. memset(buffer, 0xff, OTA_STORAGE_SD_SECTOR_SIZE);
  310. while (count--) {
  311. err = flash_write(storage->dev, (offs++)<<9, buffer, 1<<9);
  312. if (err) {
  313. SYS_LOG_ERR("write error %d, offs 0x%x, count %d", err, offs, count);
  314. mem_free(buffer);
  315. return -EIO;
  316. }
  317. #ifdef CONFIG_WATCHDOG
  318. watchdog_clear();
  319. #endif
  320. }
  321. mem_free(buffer);
  322. return err;
  323. }
  324. int ota_storage_erase_spinor(struct ota_storage *storage, int offs, int size)
  325. {
  326. int err = 0;
  327. int erase_size = 0;
  328. if (storage == NULL)
  329. return -EINVAL;
  330. /* write aligned page data */
  331. while (size > 0) {
  332. if (size < storage->max_erase_seg) {
  333. erase_size = size;
  334. } else if (offs & (storage->max_erase_seg - 1)) {
  335. erase_size = storage->max_erase_seg - (offs & (storage->max_erase_seg - 1));
  336. } else {
  337. erase_size = storage->max_erase_seg;
  338. }
  339. err = flash_erase(storage->dev, offs, erase_size);
  340. if (err) {
  341. SYS_LOG_ERR("write error %d, offs 0x%x", err, offs);
  342. return -EIO;
  343. }
  344. size -= erase_size;
  345. offs += erase_size;
  346. #ifdef CONFIG_WATCHDOG
  347. watchdog_clear();
  348. #endif
  349. }
  350. return err;
  351. }
  352. int ota_storage_erase(struct ota_storage *storage, int offs, int size)
  353. {
  354. SYS_LOG_INF("offs 0x%x, size %d", offs, size);
  355. if (storage == NULL)
  356. return -EINVAL;
  357. if (IS_STORAGE_TYPE_SD_NAND(storage)) {
  358. // improve perf: only erase first page for sd_nand
  359. return ota_storage_erase_sd_nand(storage, offs, OTA_STORAGE_SD_SECTOR_SIZE);
  360. } else if (storage->storage_type == OTA_STORAGE_SPINOR || storage->storage_type == OTA_STORAGE_EXT_NOR) {
  361. return ota_storage_erase_spinor(storage, offs, size);
  362. }
  363. return flash_erase(storage->dev, offs, size);
  364. }
  365. struct ota_storage *ota_storage_find(int storage_id)
  366. {
  367. if (storage_id < OTA_STORAGE_MAX_TYPE && global_ota_storage[storage_id].dev)
  368. return &global_ota_storage[storage_id];
  369. return NULL;
  370. }
  371. struct ota_storage *ota_storage_init(const char *storage_name)
  372. {
  373. struct ota_storage *storage = NULL;
  374. const struct device *nor_dev;
  375. SYS_LOG_INF("init storage %s\n", storage_name);
  376. nor_dev = device_get_binding(storage_name);
  377. if (!nor_dev) {
  378. SYS_LOG_ERR("cannot found storage device %s", storage_name);
  379. return NULL;
  380. }
  381. if (strcmp(storage_name, CONFIG_XSPI_NOR_ACTS_DEV_NAME) == 0) {
  382. storage = &global_ota_storage[OTA_STORAGE_SPINOR];
  383. memset(storage, 0x0, sizeof(struct ota_storage));
  384. storage->storage_id = OTA_STORAGE_SPINOR;
  385. storage->storage_type = OTA_STORAGE_SPINOR;
  386. storage->dev = nor_dev;
  387. storage->dev_name = storage_name;
  388. storage->max_write_seg = OTA_STORAGE_DEFAULT_WRITE_SEGMENT_SIZE;
  389. storage->max_erase_seg = OTA_STORAGE_DEFAULT_ERASE_SEGMENT_SIZE;
  390. } else if (strcmp(storage_name, OTA_STORAGE_EXT_DEVICE_NAME) == 0) {
  391. #ifdef CONFIG_BOARD_NANDBOOT
  392. storage = &global_ota_storage[OTA_STORAGE_BOOTNAND];
  393. memset(storage, 0x0, sizeof(struct ota_storage));
  394. storage->storage_id = OTA_STORAGE_BOOTNAND;
  395. storage->storage_type = OTA_STORAGE_BOOTNAND;
  396. #else
  397. storage = &global_ota_storage[OTA_STORAGE_NAND];
  398. memset(storage, 0x0, sizeof(struct ota_storage));
  399. storage->storage_id = OTA_STORAGE_NAND;
  400. storage->storage_type = OTA_STORAGE_NAND;
  401. #endif
  402. storage->dev = nor_dev;
  403. storage->dev_name = storage_name;
  404. storage->max_write_seg = OTA_STORAGE_DEFAULT_WRITE_SEGMENT_SIZE;
  405. storage->max_erase_seg = OTA_STORAGE_DEFAULT_ERASE_SEGMENT_SIZE;
  406. } else if (strcmp(storage_name, OTA_STORAGE_SD_DEVICE_NAME) == 0) {
  407. storage = &global_ota_storage[OTA_STORAGE_SD];
  408. memset(storage, 0x0, sizeof(struct ota_storage));
  409. storage->storage_id = OTA_STORAGE_SD;
  410. storage->storage_type = OTA_STORAGE_SD;
  411. storage->dev = nor_dev;
  412. storage->dev_name = storage_name;
  413. storage->max_write_seg = OTA_STORAGE_DEFAULT_WRITE_SEGMENT_SIZE;
  414. storage->max_erase_seg = OTA_STORAGE_DEFAULT_ERASE_SEGMENT_SIZE;
  415. } else if (strcmp(storage_name, CONFIG_XSPI_EXT_NOR_ACTS_DEV_NAME) == 0) {
  416. storage = &global_ota_storage[OTA_STORAGE_EXT_NOR];
  417. memset(storage, 0x0, sizeof(struct ota_storage));
  418. storage->storage_id = OTA_STORAGE_EXT_NOR;
  419. storage->storage_type = OTA_STORAGE_EXT_NOR;
  420. storage->dev = nor_dev;
  421. storage->dev_name = storage_name;
  422. storage->max_write_seg = OTA_STORAGE_DEFAULT_WRITE_SEGMENT_SIZE;
  423. storage->max_erase_seg = OTA_STORAGE_DEFAULT_ERASE_SEGMENT_SIZE;
  424. } else {
  425. SYS_LOG_ERR("unmatch storage name %s\n", storage_name);
  426. }
  427. return storage;
  428. }
  429. void ota_storage_exit(struct ota_storage *storage)
  430. {
  431. SYS_LOG_INF("exit");
  432. if (storage && storage->dev)
  433. flash_flush(storage->dev, false);
  434. if (storage)
  435. memset(storage, 0, sizeof(struct ota_storage));
  436. storage = NULL;
  437. }