fal_partition.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-17 armink the first version
  9. */
  10. #include <fal.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. /* partition magic word */
  14. #define FAL_PART_MAGIC_WORD 0x45503130
  15. #define FAL_PART_MAGIC_WORD_H 0x4550L
  16. #define FAL_PART_MAGIC_WORD_L 0x3130L
  17. #define FAL_PART_MAGIC_WROD 0x45503130
  18. struct part_flash_info
  19. {
  20. const struct fal_flash_dev *flash_dev;
  21. };
  22. /**
  23. * FAL partition table config has defined on 'fal_cfg.h'.
  24. * When this option is disable, it will auto find the partition table on a specified location in flash partition.
  25. */
  26. #ifdef FAL_PART_HAS_TABLE_CFG
  27. /* check partition table definition */
  28. #if !defined(FAL_PART_TABLE)
  29. #error "You must defined FAL_PART_TABLE on 'fal_cfg.h'"
  30. #endif
  31. #ifdef __CC_ARM /* ARM Compiler */
  32. #define SECTION(x) __attribute__((section(x)))
  33. #define USED __attribute__((used))
  34. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  35. #define SECTION(x) @ x
  36. #define USED __root
  37. #elif defined (__GNUC__) /* GNU GCC Compiler */
  38. #define SECTION(x) __attribute__((section(x)))
  39. #define USED __attribute__((used))
  40. #else
  41. #error not supported tool chain
  42. #endif /* __CC_ARM */
  43. /* partition table definition */
  44. USED static const struct fal_partition partition_table_def[] SECTION("FalPartTable") = FAL_PART_TABLE;
  45. static const struct fal_partition *partition_table = NULL;
  46. /* partition and flash object information cache table */
  47. static struct part_flash_info part_flash_cache[sizeof(partition_table_def) / sizeof(partition_table_def[0])] = { 0 };
  48. #else /* FAL_PART_HAS_TABLE_CFG */
  49. #if !defined(FAL_PART_TABLE_FLASH_DEV_NAME)
  50. #error "You must defined FAL_PART_TABLE_FLASH_DEV_NAME on 'fal_cfg.h'"
  51. #endif
  52. /* check partition table end offset address definition */
  53. #if !defined(FAL_PART_TABLE_END_OFFSET)
  54. #error "You must defined FAL_PART_TABLE_END_OFFSET on 'fal_cfg.h'"
  55. #endif
  56. static struct fal_partition *partition_table = NULL;
  57. static struct part_flash_info *part_flash_cache = NULL;
  58. #endif /* FAL_PART_HAS_TABLE_CFG */
  59. static uint8_t init_ok = 0;
  60. static size_t partition_table_len = 0;
  61. /**
  62. * print the partition table
  63. */
  64. void fal_show_part_table(void)
  65. {
  66. char *item1 = "name", *item2 = "flash_dev";
  67. size_t i, part_name_max = strlen(item1), flash_dev_name_max = strlen(item2);
  68. const struct fal_partition *part;
  69. if (partition_table_len)
  70. {
  71. for (i = 0; i < partition_table_len; i++)
  72. {
  73. part = &partition_table[i];
  74. if (strlen(part->name) > part_name_max)
  75. {
  76. part_name_max = strlen(part->name);
  77. }
  78. if (strlen(part->flash_name) > flash_dev_name_max)
  79. {
  80. flash_dev_name_max = strlen(part->flash_name);
  81. }
  82. }
  83. }
  84. log_i("==================== FAL partition table ====================");
  85. log_i("| %-*.*s | %-*.*s | offset | length |", part_name_max, FAL_DEV_NAME_MAX, item1, flash_dev_name_max,
  86. FAL_DEV_NAME_MAX, item2);
  87. log_i("-------------------------------------------------------------");
  88. for (i = 0; i < partition_table_len; i++)
  89. {
  90. #ifdef FAL_PART_HAS_TABLE_CFG
  91. part = &partition_table[i];
  92. #else
  93. part = &partition_table[partition_table_len - i - 1];
  94. #endif
  95. log_i("| %-*.*s | %-*.*s | 0x%08lx | 0x%08x |", part_name_max, FAL_DEV_NAME_MAX, part->name, flash_dev_name_max,
  96. FAL_DEV_NAME_MAX, part->flash_name, part->offset, part->len);
  97. }
  98. log_i("=============================================================");
  99. }
  100. static int check_and_update_part_cache(const struct fal_partition *table, size_t len)
  101. {
  102. const struct fal_flash_dev *flash_dev = NULL;
  103. size_t i;
  104. #ifndef FAL_PART_HAS_TABLE_CFG
  105. if (part_flash_cache)
  106. {
  107. FAL_FREE(part_flash_cache);
  108. }
  109. part_flash_cache = FAL_MALLOC(len * sizeof(struct part_flash_info));
  110. if (part_flash_cache == NULL)
  111. {
  112. log_e("Initialize failed! No memory for partition table cache");
  113. return -2;
  114. }
  115. #endif
  116. for (i = 0; i < len; i++)
  117. {
  118. flash_dev = fal_flash_device_find(table[i].flash_name);
  119. if (flash_dev == NULL)
  120. {
  121. log_d("Warning: Do NOT found the flash device(%s).", table[i].flash_name);
  122. continue;
  123. }
  124. if (table[i].offset >= (long)flash_dev->len)
  125. {
  126. log_e("Initialize failed! Partition(%s) offset address(%ld) out of flash bound(<%d).",
  127. table[i].name, table[i].offset, flash_dev->len);
  128. partition_table_len = 0;
  129. return -1;
  130. }
  131. part_flash_cache[i].flash_dev = flash_dev;
  132. }
  133. return 0;
  134. }
  135. /**
  136. * Initialize all flash partition on FAL partition table
  137. *
  138. * @return partitions total number
  139. */
  140. int fal_partition_init(void)
  141. {
  142. if (init_ok)
  143. {
  144. return partition_table_len;
  145. }
  146. #ifdef FAL_PART_HAS_TABLE_CFG
  147. partition_table = &partition_table_def[0];
  148. partition_table_len = sizeof(partition_table_def) / sizeof(partition_table_def[0]);
  149. #else
  150. /* load partition table from the end address FAL_PART_TABLE_END_OFFSET, error return 0 */
  151. long part_table_offset = FAL_PART_TABLE_END_OFFSET;
  152. size_t table_num = 0, table_item_size = 0;
  153. uint8_t part_table_find_ok = 0;
  154. uint32_t read_magic_word;
  155. fal_partition_t new_part = NULL;
  156. size_t i;
  157. const struct fal_flash_dev *flash_dev = NULL;
  158. flash_dev = fal_flash_device_find(FAL_PART_TABLE_FLASH_DEV_NAME);
  159. if (flash_dev == NULL)
  160. {
  161. log_e("Initialize failed! Flash device (%s) NOT found.", FAL_PART_TABLE_FLASH_DEV_NAME);
  162. goto _exit;
  163. }
  164. /* check partition table offset address */
  165. if (part_table_offset < 0 || part_table_offset >= (long) flash_dev->len)
  166. {
  167. log_e("Setting partition table end offset address(%ld) out of flash bound(<%d).", part_table_offset, flash_dev->len);
  168. goto _exit;
  169. }
  170. table_item_size = sizeof(struct fal_partition);
  171. new_part = (fal_partition_t)FAL_MALLOC(table_item_size);
  172. if (new_part == NULL)
  173. {
  174. log_e("Initialize failed! No memory for table buffer.");
  175. goto _exit;
  176. }
  177. /* find partition table location */
  178. {
  179. uint8_t read_buf[64];
  180. part_table_offset -= sizeof(read_buf);
  181. while (part_table_offset >= 0)
  182. {
  183. if (flash_dev->ops.read(part_table_offset, read_buf, sizeof(read_buf)) > 0)
  184. {
  185. /* find magic word in read buf */
  186. for (i = 0; i < sizeof(read_buf) - sizeof(read_magic_word) + 1; i++)
  187. {
  188. read_magic_word = read_buf[0 + i] + (read_buf[1 + i] << 8) + (read_buf[2 + i] << 16) + (read_buf[3 + i] << 24);
  189. if (read_magic_word == ((FAL_PART_MAGIC_WORD_H << 16) + FAL_PART_MAGIC_WORD_L))
  190. {
  191. part_table_find_ok = 1;
  192. part_table_offset += i;
  193. log_d("Find the partition table on '%s' offset @0x%08lx.", FAL_PART_TABLE_FLASH_DEV_NAME,
  194. part_table_offset);
  195. break;
  196. }
  197. }
  198. }
  199. else
  200. {
  201. /* read failed */
  202. break;
  203. }
  204. if (part_table_find_ok)
  205. {
  206. break;
  207. }
  208. else
  209. {
  210. /* calculate next read buf position */
  211. if (part_table_offset >= (long)sizeof(read_buf))
  212. {
  213. part_table_offset -= sizeof(read_buf);
  214. part_table_offset += (sizeof(read_magic_word) - 1);
  215. }
  216. else if (part_table_offset != 0)
  217. {
  218. part_table_offset = 0;
  219. }
  220. else
  221. {
  222. /* find failed */
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. /* load partition table */
  229. while (part_table_find_ok)
  230. {
  231. memset(new_part, 0x00, table_num);
  232. if (flash_dev->ops.read(part_table_offset - table_item_size * (table_num), (uint8_t *) new_part,
  233. table_item_size) < 0)
  234. {
  235. log_e("Initialize failed! Flash device (%s) read error!", flash_dev->name);
  236. table_num = 0;
  237. break;
  238. }
  239. if (new_part->magic_word != ((FAL_PART_MAGIC_WORD_H << 16) + FAL_PART_MAGIC_WORD_L))
  240. {
  241. break;
  242. }
  243. partition_table = (fal_partition_t) FAL_REALLOC(partition_table, table_item_size * (table_num + 1));
  244. if (partition_table == NULL)
  245. {
  246. log_e("Initialize failed! No memory for partition table");
  247. table_num = 0;
  248. break;
  249. }
  250. memcpy(partition_table + table_num, new_part, table_item_size);
  251. table_num++;
  252. };
  253. if (table_num == 0)
  254. {
  255. log_e("Partition table NOT found on flash: %s (len: %d) from offset: 0x%08x.", FAL_PART_TABLE_FLASH_DEV_NAME,
  256. FAL_DEV_NAME_MAX, FAL_PART_TABLE_END_OFFSET);
  257. goto _exit;
  258. }
  259. else
  260. {
  261. partition_table_len = table_num;
  262. }
  263. #endif /* FAL_PART_HAS_TABLE_CFG */
  264. /* check the partition table device exists */
  265. if (check_and_update_part_cache(partition_table, partition_table_len) != 0)
  266. {
  267. goto _exit;
  268. }
  269. init_ok = 1;
  270. _exit:
  271. #if FAL_DEBUG
  272. fal_show_part_table();
  273. #endif
  274. #ifndef FAL_PART_HAS_TABLE_CFG
  275. if (new_part)
  276. {
  277. FAL_FREE(new_part);
  278. }
  279. #endif /* !FAL_PART_HAS_TABLE_CFG */
  280. return partition_table_len;
  281. }
  282. /**
  283. * find the partition by name
  284. *
  285. * @param name partition name
  286. *
  287. * @return != NULL: partition
  288. * NULL: not found
  289. */
  290. const struct fal_partition *fal_partition_find(const char *name)
  291. {
  292. assert(init_ok);
  293. size_t i;
  294. for (i = 0; i < partition_table_len; i++)
  295. {
  296. if (!strcmp(name, partition_table[i].name))
  297. {
  298. return &partition_table[i];
  299. }
  300. }
  301. return NULL;
  302. }
  303. static const struct fal_flash_dev *flash_device_find_by_part(const struct fal_partition *part)
  304. {
  305. assert(part >= partition_table);
  306. assert(part <= &partition_table[partition_table_len - 1]);
  307. return part_flash_cache[part - partition_table].flash_dev;
  308. }
  309. /**
  310. * get the partition table
  311. *
  312. * @param len return the partition table length
  313. *
  314. * @return partition table
  315. */
  316. const struct fal_partition *fal_get_partition_table(size_t *len)
  317. {
  318. assert(init_ok);
  319. assert(len);
  320. *len = partition_table_len;
  321. return partition_table;
  322. }
  323. /**
  324. * set partition table temporarily
  325. * This setting will modify the partition table temporarily, the setting will be lost after restart.
  326. *
  327. * @param table partition table
  328. * @param len partition table length
  329. */
  330. void fal_set_partition_table_temp(struct fal_partition *table, size_t len)
  331. {
  332. assert(init_ok);
  333. assert(table);
  334. check_and_update_part_cache(table, len);
  335. partition_table_len = len;
  336. partition_table = table;
  337. }
  338. /**
  339. * read data from partition
  340. *
  341. * @param part partition
  342. * @param addr relative address for partition
  343. * @param buf read buffer
  344. * @param size read size
  345. *
  346. * @return >= 0: successful read data size
  347. * -1: error
  348. */
  349. int fal_partition_read(const struct fal_partition *part, uint32_t addr, uint8_t *buf, size_t size)
  350. {
  351. int ret = 0;
  352. const struct fal_flash_dev *flash_dev = NULL;
  353. assert(part);
  354. assert(buf);
  355. if (addr + size > part->len)
  356. {
  357. log_e("Partition read error! Partition address out of bound.");
  358. return -1;
  359. }
  360. flash_dev = flash_device_find_by_part(part);
  361. if (flash_dev == NULL)
  362. {
  363. log_e("Partition read error! Don't found flash device(%s) of the partition(%s).", part->flash_name, part->name);
  364. return -1;
  365. }
  366. ret = flash_dev->ops.read(part->offset + addr, buf, size);
  367. if (ret < 0)
  368. {
  369. log_e("Partition read error! Flash device(%s) read error!", part->flash_name);
  370. }
  371. return ret;
  372. }
  373. /**
  374. * write data to partition
  375. *
  376. * @param part partition
  377. * @param addr relative address for partition
  378. * @param buf write buffer
  379. * @param size write size
  380. *
  381. * @return >= 0: successful write data size
  382. * -1: error
  383. */
  384. int fal_partition_write(const struct fal_partition *part, uint32_t addr, const uint8_t *buf, size_t size)
  385. {
  386. int ret = 0;
  387. const struct fal_flash_dev *flash_dev = NULL;
  388. assert(part);
  389. assert(buf);
  390. if (addr + size > part->len)
  391. {
  392. log_e("Partition write error! Partition address out of bound.");
  393. return -1;
  394. }
  395. flash_dev = flash_device_find_by_part(part);
  396. if (flash_dev == NULL)
  397. {
  398. log_e("Partition write error! Don't found flash device(%s) of the partition(%s).", part->flash_name, part->name);
  399. return -1;
  400. }
  401. ret = flash_dev->ops.write(part->offset + addr, buf, size);
  402. if (ret < 0)
  403. {
  404. log_e("Partition write error! Flash device(%s) write error!", part->flash_name);
  405. }
  406. return ret;
  407. }
  408. /**
  409. * erase partition data
  410. *
  411. * @param part partition
  412. * @param addr relative address for partition
  413. * @param size erase size
  414. *
  415. * @return >= 0: successful erased data size
  416. * -1: error
  417. */
  418. int fal_partition_erase(const struct fal_partition *part, uint32_t addr, size_t size)
  419. {
  420. int ret = 0;
  421. const struct fal_flash_dev *flash_dev = NULL;
  422. assert(part);
  423. if (addr + size > part->len)
  424. {
  425. log_e("Partition erase error! Partition address out of bound.");
  426. return -1;
  427. }
  428. flash_dev = flash_device_find_by_part(part);
  429. if (flash_dev == NULL)
  430. {
  431. log_e("Partition erase error! Don't found flash device(%s) of the partition(%s).", part->flash_name, part->name);
  432. return -1;
  433. }
  434. ret = flash_dev->ops.erase(part->offset + addr, size);
  435. if (ret < 0)
  436. {
  437. log_e("Partition erase error! Flash device(%s) erase error!", part->flash_name);
  438. }
  439. return ret;
  440. }
  441. /**
  442. * erase partition all data
  443. *
  444. * @param part partition
  445. *
  446. * @return >= 0: successful erased data size
  447. * -1: error
  448. */
  449. int fal_partition_erase_all(const struct fal_partition *part)
  450. {
  451. return fal_partition_erase(part, 0, part->len);
  452. }