partition.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include <kernel.h>
  2. #include <init.h>
  3. #include <string.h>
  4. #include <soc.h>
  5. #include <crc.h>
  6. #include <board_cfg.h>
  7. #include <partition/partition.h>
  8. #include <logging/log.h>
  9. LOG_MODULE_REGISTER(partition, CONFIG_LOG_DEFAULT_LEVEL);
  10. #define PARTITION_TABLE_MAGIC 0x54504341 // 'ACPT'
  11. struct partition_table {
  12. u32_t magic;
  13. u16_t version;
  14. u16_t table_size;
  15. u16_t part_cnt;
  16. u16_t part_entry_size;
  17. u8_t reserved1[4];
  18. struct partition_entry parts[MAX_PARTITION_COUNT];
  19. u8_t Reserved2[4];
  20. u32_t table_crc;
  21. };
  22. static const struct partition_table *g_part_table;
  23. void partition_dump(void)
  24. {
  25. const struct partition_entry *part;
  26. char part_name[13];
  27. int i;
  28. printk("** Showing partition infomation **\n");
  29. printk("id name offset type file_id mirror_id flag\n");
  30. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  31. part = &g_part_table->parts[i];
  32. /* name field to str */
  33. memcpy(part_name, part->name, 8);
  34. part_name[8] = '\0';
  35. if (strlen(part_name) && part->type) {
  36. printk("%-4d%-10s0x%-8x%-6d%-9d%-11d%-6d\n",
  37. i, part_name, part->offset, part->type,
  38. part->file_id, part->mirror_id, part->flag);
  39. }
  40. }
  41. }
  42. int partition_get_max_file_size(const struct partition_entry *part)
  43. {
  44. if (!part)
  45. return -EINVAL;
  46. return (part->size - (part->file_offset - part->offset));
  47. }
  48. const struct partition_entry *partition_find_part(u32_t nor_phy_addr)
  49. {
  50. const struct partition_entry *part;
  51. unsigned int i;
  52. __ASSERT_NO_MSG(g_part_table);
  53. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  54. part = &g_part_table->parts[i];
  55. if ((part->offset <= nor_phy_addr) &&
  56. ((part->offset + part->size) > nor_phy_addr)) {
  57. return part;
  58. }
  59. }
  60. return NULL;
  61. }
  62. const struct partition_entry *partition_get_part_by_id(u8_t part_id)
  63. {
  64. __ASSERT_NO_MSG(g_part_table);
  65. if (part_id >= MAX_PARTITION_COUNT)
  66. return NULL;
  67. return &g_part_table->parts[part_id];
  68. }
  69. const struct partition_entry *partition_get_current_part(void)
  70. {
  71. u32_t cur_phy_addr;
  72. const boot_info_t *p_boot_info = soc_boot_get_info();
  73. __ASSERT_NO_MSG(g_part_table);
  74. cur_phy_addr = p_boot_info->system_phy_addr;
  75. return partition_find_part(cur_phy_addr);
  76. }
  77. u8_t partition_get_current_mirror_id(void)
  78. {
  79. const struct partition_entry *part;
  80. part = partition_get_current_part();
  81. if (part == NULL)
  82. return -1;
  83. return part->mirror_id;
  84. }
  85. u8_t partition_get_current_file_id(void)
  86. {
  87. const struct partition_entry *part;
  88. part = partition_get_current_part();
  89. if (part == NULL)
  90. return -1;
  91. return part->file_id;
  92. }
  93. int partition_is_mirror_part(const struct partition_entry *part)
  94. {
  95. u8_t mirror_id = 0;
  96. const struct partition_entry *cur_part;
  97. const boot_info_t *p_boot_info = soc_boot_get_info();
  98. __ASSERT_NO_MSG(g_part_table);
  99. if (partition_is_boot_part(part)) {
  100. cur_part = partition_find_part(p_boot_info->mbrec_phy_addr);
  101. if (cur_part) {
  102. mirror_id = cur_part->mirror_id;
  103. }
  104. } else if (partition_is_param_part(part)) {
  105. cur_part = partition_find_part(p_boot_info->param_phy_addr);
  106. if (cur_part) {
  107. mirror_id = cur_part->mirror_id;
  108. }
  109. } else {
  110. mirror_id = partition_get_current_mirror_id();
  111. }
  112. if ((part->mirror_id != mirror_id) && (part->mirror_id != PARTITION_MIRROR_ID_INVALID)) {
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. int partition_is_param_part(const struct partition_entry *part)
  118. {
  119. __ASSERT_NO_MSG(g_part_table);
  120. if (part->type == PARTITION_TYPE_PARAM)
  121. return 1;
  122. return 0;
  123. }
  124. int partition_is_boot_part(const struct partition_entry *part)
  125. {
  126. __ASSERT_NO_MSG(g_part_table);
  127. if (part->type == PARTITION_TYPE_BOOT)
  128. return 1;
  129. return 0;
  130. }
  131. const struct partition_entry *partition_get_part(u8_t file_id)
  132. {
  133. const struct partition_entry *part;
  134. int i;
  135. __ASSERT_NO_MSG(g_part_table);
  136. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  137. part = &g_part_table->parts[i];
  138. if ((part->file_id == file_id) && !partition_is_mirror_part(part)) {
  139. return part;
  140. }
  141. }
  142. return NULL;
  143. }
  144. const struct partition_entry *partition_get_part_for_temp(u8_t file_id)
  145. {
  146. const struct partition_entry *part;
  147. int i;
  148. __ASSERT_NO_MSG(g_part_table);
  149. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  150. part = &g_part_table->parts[i];
  151. if (part->file_id == file_id) {
  152. return part;
  153. }
  154. }
  155. return NULL;
  156. }
  157. const struct partition_entry *partition_get_temp_part(void)
  158. {
  159. const struct partition_entry *temp_part;
  160. temp_part = partition_get_part_for_temp(PARTITION_FILE_ID_OTA_TEMP);
  161. if (temp_part == NULL || (temp_part->type != PARTITION_TYPE_TEMP)) {
  162. LOG_ERR("cannot found temp part\n");
  163. return NULL;
  164. }
  165. return temp_part;
  166. }
  167. const struct partition_entry *partition_get_mirror_part(u8_t file_id)
  168. {
  169. const struct partition_entry *part;
  170. int i;
  171. __ASSERT_NO_MSG(g_part_table);
  172. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  173. part = &g_part_table->parts[i];
  174. if ((part->file_id == file_id) && partition_is_mirror_part(part)) {
  175. /* found */
  176. LOG_INF("found write part %d for file_id 0x%x\n", i, file_id);
  177. return part;
  178. }
  179. }
  180. return NULL;
  181. }
  182. bool partition_is_used_sector(const struct partition_entry *part)
  183. {
  184. __ASSERT_NO_MSG(g_part_table);
  185. __ASSERT_NO_MSG(part);
  186. return part->flag & PARTITION_FLAG_USED_SECTOR ? true : false;
  187. }
  188. const struct partition_entry *partition_get_stf_part(u8_t stor_id, u8_t file_id)
  189. {
  190. const struct partition_entry *part;
  191. int i;
  192. __ASSERT_NO_MSG(g_part_table);
  193. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  194. part = &g_part_table->parts[i];
  195. if (((part->storage_id == stor_id) || (part->storage_id== STORAGE_ID_MAX)) && (part->file_id == file_id)) {
  196. /* found */
  197. LOG_INF("found write part %d for file_id 0x%x\n", i, file_id);
  198. return part;
  199. }
  200. }
  201. return NULL;
  202. }
  203. struct device *partition_get_storage_dev(const struct partition_entry *part)
  204. {
  205. struct device *dev = NULL;
  206. __ASSERT_NO_MSG(g_part_table);
  207. if (!part) {
  208. return NULL;
  209. }
  210. switch (part->storage_id) {
  211. case STORAGE_ID_NOR:
  212. dev = (struct device *) device_get_binding(CONFIG_SPI_FLASH_NAME);
  213. break;
  214. case STORAGE_ID_SD:
  215. dev = (struct device *) device_get_binding(CONFIG_SD_NAME);
  216. break;
  217. case STORAGE_ID_NAND:
  218. case STORAGE_ID_BOOTNAND:
  219. dev = (struct device *) device_get_binding(CONFIG_SPINAND_FLASH_NAME);
  220. break;
  221. case STORAGE_ID_DATA_NOR:
  222. dev = (struct device *) device_get_binding(CONFIG_SPI_FLASH_1_NAME);
  223. break;
  224. default:
  225. break;
  226. }
  227. return dev;
  228. }
  229. int partition_file_mapping(u8_t file_id, u32_t vaddr)
  230. {
  231. const struct partition_entry *part;
  232. int err, crc_is_enabled;
  233. part = partition_get_part(file_id);
  234. if (part == NULL) {
  235. LOG_ERR("cannot found file_id %d\n", file_id);
  236. return -ENOENT;
  237. }
  238. crc_is_enabled = part->flag & PARTITION_FLAG_ENABLE_CRC ? 1 : 0;
  239. err = soc_memctrl_mapping(vaddr, part->file_offset, crc_is_enabled);
  240. if (err) {
  241. LOG_ERR("cannot mapping file_id %d\n", file_id);
  242. return err;
  243. }
  244. return 0;
  245. }
  246. static int partition_init(const struct device *dev)
  247. {
  248. u32_t cksum;
  249. g_part_table = (const struct partition_table *)soc_boot_get_part_tbl_addr();
  250. if (g_part_table == NULL ||
  251. g_part_table->magic != PARTITION_TABLE_MAGIC) {
  252. LOG_ERR("partition table (%p) has wrong magic\n", g_part_table);
  253. goto failed;
  254. }
  255. cksum = utils_crc32(0, (const uint8_t *)g_part_table, sizeof(struct partition_table) - 4);
  256. if (cksum != g_part_table->table_crc) {
  257. LOG_ERR("partition table (%p) checksum error\n", g_part_table);
  258. goto failed;
  259. }
  260. partition_dump();
  261. return 0;
  262. failed:
  263. g_part_table = NULL;
  264. return 0;
  265. }
  266. SYS_INIT(partition_init, PRE_KERNEL_1, 60);
  267. #if defined(CONFIG_SPINAND_ACTS) || defined(CONFIG_MMC_SDCARD)
  268. #include <drivers/spinand.h>
  269. #include <disk/disk_access.h>
  270. #include <drivers/mmc/sd.h>
  271. #define CONFIG_SPINAND_DEV_NAME "spinand"
  272. #define CONFIG_MMC_SDCARD_DEV_NAME "sd"
  273. static int partition_check(const struct device *dev)
  274. {
  275. const struct partition_entry *part;
  276. uint32_t sector_count;
  277. uint32_t sector_size;
  278. uint64_t capacity = 0, part_size;
  279. const struct device *dev_storage;
  280. int ret;
  281. part = &g_part_table->parts[g_part_table->part_cnt-1];
  282. printk("%d off=0x%x, size=0x%x, file_id=%d, stroage_id=%d\n",
  283. g_part_table->part_cnt , part->offset, part->size,
  284. part->file_id, part->storage_id);
  285. part_size = part->offset + part->size;
  286. #ifdef CONFIG_SPINAND_ACTS
  287. if((part->storage_id == STORAGE_ID_BOOTNAND) || (part->storage_id == STORAGE_ID_NAND)){
  288. dev_storage = device_get_binding(CONFIG_SPINAND_DEV_NAME);
  289. ret = spinand_storage_ioctl(dev_storage, DISK_IOCTL_GET_SECTOR_COUNT, (void *)&sector_count);
  290. ret |= spinand_storage_ioctl(dev_storage, DISK_IOCTL_GET_SECTOR_SIZE, (void *)&sector_size);
  291. if (!ret) {
  292. capacity = sector_count;
  293. capacity *= sector_size;
  294. printk("part size=0x%llx, nand size=0x%llx\n", part_size, capacity);
  295. if(part_size > capacity){
  296. printk("---error---: part size over nand size\n");
  297. k_panic();
  298. }
  299. }else{
  300. printk("err: nand get capacity fail\n");
  301. while(1);
  302. }
  303. }
  304. #endif
  305. #ifdef CONFIG_MMC_SDCARD
  306. if(part->storage_id == STORAGE_ID_SD){
  307. dev_storage = device_get_binding(CONFIG_MMC_SDCARD_DEV_NAME);
  308. ret = sd_card_storage_ioctl(dev_storage, DISK_IOCTL_GET_SECTOR_COUNT, (void *)&sector_count);
  309. ret |= sd_card_storage_ioctl(dev_storage, DISK_IOCTL_GET_SECTOR_SIZE, (void *)&sector_size);
  310. if (!ret) {
  311. capacity = sector_count;
  312. capacity *= sector_size;
  313. printk("part size=0x%llx, sdcard size=0x%llx\n", part_size, capacity);
  314. if(part_size > capacity){
  315. printk("---error---:part size over sdcard size\n");
  316. k_panic();
  317. }
  318. }else{
  319. printk("err: sd get capacity fail\n");
  320. while(1);
  321. }
  322. }
  323. #endif
  324. return 0;
  325. }
  326. SYS_INIT(partition_check, APPLICATION, 92);
  327. #endif