nvram_config.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Non-volatile memory driver
  9. */
  10. #include <errno.h>
  11. #include <sys/__assert.h>
  12. #include <stdbool.h>
  13. #include <kernel.h>
  14. #include <device.h>
  15. #include <init.h>
  16. #include <drivers/nvram_config.h>
  17. #include <string.h>
  18. #include <storage/flash_map.h>
  19. #include <partition/partition.h>
  20. #include "nvram_storage.h"
  21. #define LOG_LEVEL 2
  22. #include <logging/log.h>
  23. LOG_MODULE_REGISTER(nvram_cfg);
  24. #define CONFIG_NVRAM_FAST_SEARCH
  25. #define NVRAM_REGION_SEG_VERSION 0x1
  26. /* name size + item size <= buffer size */
  27. #define NVRAM_MAX_NAME_SIZE 112
  28. #define NVRAM_MAX_DATA_SIZE 512
  29. #define NVRAM_ITEM_ALIGN_SIZE 0x10
  30. #define NVRAM_ERASE_ALIGN_SIZE 0x1000
  31. #define NVRAM_BUFFER_SIZE 128
  32. enum {
  33. ITEM_STATUS_EMPTY = 0,
  34. ITEM_STATUS_VALID,
  35. ITEM_STATUS_OBSOLETE,
  36. ITEM_STATUS_INVALID,
  37. };
  38. struct __packed region_seg_header
  39. {
  40. uint32_t magic;
  41. uint8_t state;
  42. uint8_t crc;
  43. uint8_t version;
  44. uint8_t reserved;
  45. uint8_t seq_id;
  46. uint8_t head_size;
  47. uint16_t seg_size;
  48. uint8_t reserved2[4];
  49. };
  50. struct __packed nvram_item {
  51. uint8_t magic;
  52. uint8_t state;
  53. uint8_t crc;
  54. uint8_t hash;
  55. uint8_t reserved;
  56. uint8_t name_size;
  57. uint16_t data_size;
  58. char data[0];
  59. };
  60. struct region_info
  61. {
  62. struct device *storage;
  63. char name[16];
  64. uint32_t flag;
  65. /* write regions base address */
  66. uint32_t base_addr;
  67. /* total write regions size, aligned to erase size */
  68. int32_t total_size;
  69. /* current segment info */
  70. int32_t seg_size;
  71. uint32_t seg_offset;
  72. uint32_t seg_write_offset;
  73. uint8_t seg_seq_id;
  74. #ifdef CONFIG_NVRAM_FAST_SEARCH
  75. uint32_t *seg_item_map;
  76. int seg_item_map_size;
  77. #endif
  78. };
  79. /* region segment magic: 'NVRS' */
  80. #define NVRAM_REGION_SEG_MAGIC 0x5253564E
  81. /* region item magic: 'I' */
  82. #define NVRAM_REGION_ITEM_MAGIC 0x49
  83. #define NVRAM_REGION_SEG_HEADER_CRC_OFFSET 6
  84. #define NVRAM_REGION_ITEM_CRC_OFFSET 3
  85. #define NVRAM_REGION_SEG_STATE_VALID 0xff
  86. #define NVRAM_REGION_SEG_STATE_OBSOLETE 0x5a
  87. #define NVRAM_ITEM_STATE_VALID 0xff
  88. #define NVRAM_ITEM_STATE_OBSOLETE 0x5a
  89. #define NVRAM_SEG_ITEM_START_OFFSET (ROUND_UP(sizeof(struct region_seg_header), NVRAM_ITEM_ALIGN_SIZE))
  90. #ifdef CONFIG_NVRAM_FAST_SEARCH
  91. uint32_t user_region_item_map[CONFIG_NVRAM_USER_REGION_SEGMENT_SIZE / NVRAM_ITEM_ALIGN_SIZE / 32];
  92. #endif
  93. /* user config region */
  94. struct region_info user_nvram_region = {
  95. .name = "User Config",
  96. .base_addr = 0, /*init by nvram_config_init, load from partition table*/
  97. .total_size = 0, /*init by nvram_config_init, load from partition table*/
  98. .seg_size = CONFIG_NVRAM_USER_REGION_SEGMENT_SIZE,
  99. #ifdef CONFIG_NVRAM_FAST_SEARCH
  100. .seg_item_map = user_region_item_map,
  101. .seg_item_map_size = sizeof(user_region_item_map),
  102. #endif
  103. };
  104. /* factory config region */
  105. struct region_info factory_nvram_region = {
  106. .name = "Factory Config",
  107. .base_addr = 0, /*init by nvram_config_init, load from partition table*/
  108. .total_size = 0, /*init by nvram_config_init, load from partition table*/
  109. .seg_size = CONFIG_NVRAM_FACTORY_REGION_SEGMENT_SIZE,
  110. #ifdef CONFIG_NVRAM_FAST_SEARCH
  111. .seg_item_map = NULL,
  112. .seg_item_map_size = 0,
  113. #endif
  114. };
  115. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  116. /* factory writeable config region
  117. * used to store machine specific configs, like SN,MAC...
  118. */
  119. struct region_info factory_rw_nvram_region = {
  120. .name = "Factory RW",
  121. .base_addr = 0, /*init by nvram_config_init, load from partition table*/
  122. .total_size = 0, /*init by nvram_config_init, load from partition table*/
  123. .seg_size = CONFIG_NVRAM_FACTORY_RW_REGION_SEGMENT_SIZE,
  124. #ifdef CONFIG_NVRAM_FAST_SEARCH
  125. .seg_item_map = NULL,
  126. .seg_item_map_size = 0,
  127. #endif
  128. };
  129. #endif
  130. static uint8_t __act_s2_notsave nvram_buf[NVRAM_BUFFER_SIZE];
  131. static K_SEM_DEFINE(nvram_lock, 1, 1);
  132. static int region_read(struct region_info *region, uint32_t offset, uint8_t *buf, int len);
  133. static int region_write(struct region_info *region, uint32_t offset, const uint8_t *buf, int len);
  134. static int region_erase(struct region_info *region, uint32_t offset, int size);
  135. static int region_copy(struct region_info *region, uint32_t src_offset, uint32_t dest_offset, int len);
  136. static int region_is_empy(struct region_info *region, uint32_t offset, int32_t size);
  137. static uint8_t calc_hash(const uint8_t *key, int len)
  138. {
  139. uint8_t hash = 0;
  140. while(len--)
  141. hash += *key++;
  142. return (hash ^ 0xa5);
  143. }
  144. /*
  145. * Name: CRC-8/MAXIM x8+x5+x4+1
  146. * Poly: 0x31
  147. * Init: 0x00
  148. * Refin: True
  149. * Refout: True
  150. * Xorout: 0x00
  151. * Alias: DOW-CRC,CRC-8/IBUTTON
  152. * Use: Maxim(Dallas)'s some devices,e.g. DS18B20
  153. */
  154. static const unsigned char crc8_table[32] = {
  155. 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
  156. 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
  157. 0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
  158. 0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74
  159. };
  160. static unsigned char calc_crc8(const unsigned char *addr, int len, uint8_t initial_crc)
  161. {
  162. unsigned char crc = initial_crc;
  163. while (len--) {
  164. crc = *addr++ ^ crc;
  165. crc = crc8_table[crc & 0x0f] ^ crc8_table[16 + ((crc >> 4) & 0x0f)];
  166. }
  167. return crc;
  168. }
  169. static int region_read(struct region_info *region, uint32_t offset, uint8_t *buf, int len)
  170. {
  171. LOG_DBG("offset 0x%x len 0x%x, buf 0x%x\n", offset, len, (uint32_t)buf);
  172. if (!region->storage || (offset + len > region->total_size)) {
  173. LOG_ERR("invalid param storage %p, offset 0x%x, len 0x%x",
  174. region->storage, offset, len);
  175. return -EINVAL;
  176. }
  177. return nvram_storage_read(region->storage, region->base_addr + offset,
  178. buf, len);
  179. }
  180. static int region_write(struct region_info *region, uint32_t offset,
  181. const uint8_t *buf, int len)
  182. {
  183. LOG_DBG("offset 0x%x len 0x%x, buf 0x%x\n", offset, len, (uint32_t)buf);
  184. if (!region->storage || (offset + len > region->total_size)) {
  185. LOG_ERR("invalid param storage %p, offset 0x%x, len 0x%x",
  186. region->storage, offset, len);
  187. return -EINVAL;
  188. }
  189. return nvram_storage_write(region->storage, region->base_addr + offset,
  190. buf, len);
  191. }
  192. static int region_copy(struct region_info *region, uint32_t src_offset,
  193. uint32_t dest_offset, int len)
  194. {
  195. int err, write_size;
  196. uint8_t *buf;
  197. LOG_DBG("src_offset 0x%x dest_offset 0x%x len 0x%x\n", src_offset, dest_offset, len);
  198. buf = nvram_buf;
  199. write_size = NVRAM_BUFFER_SIZE;
  200. while (len > 0) {
  201. if (len < write_size)
  202. write_size = len;
  203. err = region_read(region, src_offset, buf, write_size);
  204. if (err) {
  205. return err;
  206. }
  207. region_write(region, dest_offset, buf, write_size);
  208. if (err) {
  209. return err;
  210. }
  211. src_offset += write_size;
  212. dest_offset += write_size;
  213. len -= write_size;
  214. }
  215. return 0;
  216. }
  217. static int region_is_empy(struct region_info *region, uint32_t offset, int32_t size)
  218. {
  219. int32_t read_size, i;
  220. uint32_t *pdat;
  221. read_size = NVRAM_BUFFER_SIZE;
  222. while (size > 0) {
  223. if (size < read_size)
  224. read_size = size;
  225. nvram_storage_read(region->storage, region->base_addr + offset,
  226. nvram_buf, read_size);
  227. pdat = (uint32_t *)nvram_buf;
  228. for (i = 0; i < read_size / 4; i++) {
  229. if (*pdat++ != 0xffffffff)
  230. return false;
  231. }
  232. size -= read_size;
  233. offset += read_size;
  234. }
  235. return true;
  236. }
  237. static int region_erase(struct region_info *region, uint32_t offset, int size)
  238. {
  239. LOG_DBG("offset 0x%x len 0x%x\n", offset, size);
  240. if (!region->storage || (offset + size > region->total_size) ||
  241. (offset & (NVRAM_ERASE_ALIGN_SIZE - 1) ||
  242. size & (NVRAM_ERASE_ALIGN_SIZE - 1))) {
  243. LOG_ERR("invalid param storage %p, offset 0x%x, len 0x%x",
  244. region->storage, offset, size);
  245. return -EINVAL;
  246. }
  247. while (size > 0) {
  248. if (!region_is_empy(region, offset, NVRAM_ERASE_ALIGN_SIZE)) {
  249. nvram_storage_erase(region->storage, region->base_addr + offset,
  250. NVRAM_ERASE_ALIGN_SIZE);
  251. }
  252. offset += NVRAM_ERASE_ALIGN_SIZE;
  253. size -= NVRAM_ERASE_ALIGN_SIZE;
  254. }
  255. return 0;
  256. }
  257. static int region_clear(struct region_info *region, int clear_size)
  258. {
  259. uint32_t offset, erase_size;
  260. clear_size = ROUND_UP(clear_size, region->seg_size);
  261. if (clear_size > (region->total_size - region->seg_size)) {
  262. LOG_ERR("clear_size 0x%x is too large\n", clear_size);
  263. clear_size = (region->total_size - region->seg_size);
  264. }
  265. offset = region->seg_offset + region->seg_size;
  266. if ((offset + clear_size) > region->total_size) {
  267. erase_size = region->total_size - offset;
  268. region_erase(region, offset, erase_size);
  269. /* wrap to region start pos */
  270. offset = 0;
  271. clear_size -= erase_size;
  272. }
  273. region_erase(region, offset, clear_size);
  274. return 0;
  275. }
  276. #ifdef CONFIG_NVRAM_FAST_SEARCH
  277. //#define BITS_PER_LONG 32
  278. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  279. /*
  280. * Find the first set bit in a memory region.
  281. */
  282. static unsigned int find_first_bit(const unsigned int *addr, unsigned int size)
  283. {
  284. const unsigned int *p = addr;
  285. unsigned int result = 0;
  286. unsigned int tmp;
  287. while (size & ~(BITS_PER_LONG - 1)) {
  288. if ((tmp = *(p++)))
  289. goto found;
  290. result += BITS_PER_LONG;
  291. size -= BITS_PER_LONG;
  292. }
  293. if (!size)
  294. return result;
  295. tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
  296. if (tmp == 0UL) /* Are any bits set? */
  297. return result + size; /* Nope. */
  298. found:
  299. return result + (find_lsb_set(tmp) - 1);
  300. }
  301. /*
  302. * Find the next set bit in a memory region.
  303. */
  304. static unsigned int find_next_bit(const unsigned int *addr, unsigned int size,
  305. unsigned int offset)
  306. {
  307. const unsigned int *p = addr + BITOP_WORD(offset);
  308. unsigned int result = offset & ~(BITS_PER_LONG - 1);
  309. unsigned int tmp;
  310. if (offset >= size)
  311. return size;
  312. size -= result;
  313. offset %= BITS_PER_LONG;
  314. if (offset) {
  315. tmp = *(p++);
  316. tmp &= (~0UL << offset);
  317. if (size < BITS_PER_LONG)
  318. goto found_first;
  319. if (tmp)
  320. goto found_middle;
  321. size -= BITS_PER_LONG;
  322. result += BITS_PER_LONG;
  323. }
  324. while (size & ~(BITS_PER_LONG - 1)) {
  325. if ((tmp = *(p++)))
  326. goto found_middle;
  327. result += BITS_PER_LONG;
  328. size -= BITS_PER_LONG;
  329. }
  330. if (!size)
  331. return result;
  332. tmp = *p;
  333. found_first:
  334. tmp &= (~0UL >> (BITS_PER_LONG - size));
  335. if (tmp == 0UL) /* Are any bits set? */
  336. return result + size; /* Nope. */
  337. found_middle:
  338. return result + (find_lsb_set(tmp) - 1);
  339. }
  340. #define ITEM_BITMAP_BIT_TO_OFFSET(bit) ((bit) * NVRAM_ITEM_ALIGN_SIZE)
  341. #define ITEM_BITMAP_OFFSET_TO_BIT(offset) ((offset) / NVRAM_ITEM_ALIGN_SIZE)
  342. static int item_bitmap_first_offset(const unsigned int *seg_item_map, int seg_size)
  343. {
  344. int bit;
  345. if (!seg_item_map)
  346. return NVRAM_SEG_ITEM_START_OFFSET;
  347. bit = find_first_bit(seg_item_map, ITEM_BITMAP_OFFSET_TO_BIT(seg_size));
  348. return (ITEM_BITMAP_BIT_TO_OFFSET(bit));
  349. }
  350. static int item_bitmap_next_offset(const unsigned int *seg_item_map, int seg_size, int offset)
  351. {
  352. int bit;
  353. if (!seg_item_map)
  354. return offset;
  355. bit = find_next_bit(seg_item_map, ITEM_BITMAP_OFFSET_TO_BIT(seg_size),
  356. ITEM_BITMAP_OFFSET_TO_BIT(offset));
  357. return (ITEM_BITMAP_BIT_TO_OFFSET(bit));
  358. }
  359. static void item_bitmap_update(unsigned int *seg_item_map, int offset, int is_set)
  360. {
  361. int bit, w, b;
  362. if (!seg_item_map)
  363. return;
  364. bit = ITEM_BITMAP_OFFSET_TO_BIT(offset);
  365. w = bit / 32;
  366. b = bit % 32;
  367. if (is_set)
  368. seg_item_map[w] |= 1u << b;
  369. else
  370. seg_item_map[w] &= ~(1u << b);
  371. }
  372. static void item_bitmap_clear_all(unsigned int *seg_item_map, int item_map_size)
  373. {
  374. if (!seg_item_map)
  375. return;
  376. memset(seg_item_map, 0, item_map_size);
  377. }
  378. #endif
  379. static int item_is_empty(struct nvram_item *item)
  380. {
  381. uint8_t *buf = (uint8_t *)item;
  382. int size = sizeof(struct nvram_item);
  383. while (size > 0) {
  384. if (*buf++ != 0xff)
  385. return false;
  386. size--;
  387. }
  388. return true;
  389. }
  390. static int item_calc_real_size(uint8_t name_size, uint16_t data_size)
  391. {
  392. return (sizeof(struct nvram_item) + name_size + data_size);
  393. }
  394. static int item_calc_aligned_size(uint8_t name_size, uint16_t data_size)
  395. {
  396. return ROUND_UP(item_calc_real_size(name_size, data_size), NVRAM_ITEM_ALIGN_SIZE);
  397. }
  398. static int item_get_real_size(struct nvram_item *item)
  399. {
  400. return item_calc_real_size(item->name_size, item->data_size);
  401. }
  402. static int item_get_aligned_size(struct nvram_item *item)
  403. {
  404. return item_calc_aligned_size(item->name_size, item->data_size);
  405. }
  406. static void item_update_state(struct region_info *region, int item_offset, uint8_t state)
  407. {
  408. int state_offs;
  409. state_offs = item_offset + (int)(&((struct nvram_item *)0)->state);
  410. LOG_DBG("set state_offs 0x%x state to 0x%x\n", state_offs, state);
  411. region_write(region, state_offs, &state, sizeof(state));
  412. }
  413. static uint8_t item_calc_data_crc(struct region_info *region, uint32_t offset, int32_t size, uint8_t crc)
  414. {
  415. int32_t read_size;
  416. read_size = NVRAM_BUFFER_SIZE;
  417. while (size > 0) {
  418. if (size < read_size)
  419. read_size = size;
  420. region_read(region, offset, nvram_buf, read_size);
  421. crc = calc_crc8(nvram_buf, read_size, crc);
  422. offset += read_size;
  423. size -= read_size;
  424. }
  425. return crc;
  426. }
  427. static int item_get_item_name(struct region_info *region, int item_offs, struct nvram_item *item, uint8_t *buf, uint8_t len)
  428. {
  429. uint32_t name_offset;
  430. if (!buf){
  431. return -1;
  432. }
  433. if (item->magic != NVRAM_REGION_ITEM_MAGIC) {
  434. if (!item_is_empty(item)) {
  435. LOG_ERR("invalid item maigc 0x%x", item->magic);
  436. return -1;
  437. }
  438. return -1;
  439. }
  440. if (item->state != NVRAM_ITEM_STATE_VALID) {
  441. if (item->state != NVRAM_ITEM_STATE_OBSOLETE) {
  442. LOG_ERR("invalid item status 0x%x", item->state);
  443. return -1;
  444. }
  445. return -1;
  446. }
  447. if (len < item->name_size){
  448. return -1;
  449. }else{
  450. len = item->name_size;
  451. }
  452. name_offset = item_offs + sizeof(struct nvram_item);
  453. /* check config name */
  454. region_read(region, name_offset, buf, len);
  455. return 0;
  456. }
  457. static int item_check_validity(struct region_info *region, int item_offs, struct nvram_item *item,
  458. int check_crc)
  459. {
  460. uint8_t name_hash, crc;
  461. uint32_t name_offset;
  462. if (item->magic != NVRAM_REGION_ITEM_MAGIC) {
  463. if (!item_is_empty(item)) {
  464. LOG_ERR("invalid item maigc 0x%x", item->magic);
  465. return ITEM_STATUS_INVALID;
  466. }
  467. return ITEM_STATUS_EMPTY;
  468. }
  469. if (item->state != NVRAM_ITEM_STATE_VALID) {
  470. if (item->state != NVRAM_ITEM_STATE_OBSOLETE) {
  471. LOG_ERR("invalid item status 0x%x", item->state);
  472. return ITEM_STATUS_INVALID;
  473. }
  474. return ITEM_STATUS_OBSOLETE;
  475. }
  476. name_offset = item_offs + sizeof(struct nvram_item);
  477. /* check config name */
  478. region_read(region, name_offset, nvram_buf, item->name_size);
  479. name_hash = calc_hash(nvram_buf, item->name_size);
  480. if (item->hash != name_hash) {
  481. return ITEM_STATUS_INVALID;
  482. }
  483. if (check_crc) {
  484. crc = calc_crc8((uint8_t *)item + NVRAM_REGION_ITEM_CRC_OFFSET,
  485. sizeof(struct nvram_item) - NVRAM_REGION_ITEM_CRC_OFFSET, 0);
  486. crc = calc_crc8(nvram_buf, item->name_size, crc);
  487. crc = item_calc_data_crc(region, name_offset + item->name_size, item->data_size, crc);
  488. if (item->crc != crc) {
  489. LOG_ERR("item crc error! offset 0x%x, crc 0x%x != item->crc 0x%x",
  490. item_offs, crc, item->crc);
  491. item_update_state(region, item_offs - region->seg_offset, NVRAM_ITEM_STATE_OBSOLETE);
  492. return ITEM_STATUS_INVALID;
  493. }
  494. }
  495. return ITEM_STATUS_VALID;
  496. }
  497. static int region_find_item(struct region_info *region, const char *name,
  498. struct nvram_item *item)
  499. {
  500. uint32_t item_offs;
  501. uint16_t hash;
  502. int32_t offs;
  503. if (!name || !item)
  504. return -EINVAL;
  505. hash = calc_hash(name, strlen(name) + 1);
  506. #ifdef CONFIG_NVRAM_FAST_SEARCH
  507. offs = item_bitmap_first_offset(region->seg_item_map, region->seg_size);
  508. #else
  509. offs = NVRAM_SEG_ITEM_START_OFFSET;
  510. #endif
  511. while (offs < region->seg_size) {
  512. item_offs = region->seg_offset + offs;
  513. /* read item header */
  514. region_read(region, item_offs, (uint8_t *)item, sizeof(struct nvram_item));
  515. if (item->magic != NVRAM_REGION_ITEM_MAGIC)
  516. break;
  517. if (item->state != NVRAM_ITEM_STATE_VALID) {
  518. goto next;
  519. }
  520. if (item->hash == hash) {
  521. /* read config name */
  522. region_read(region, item_offs + sizeof(struct nvram_item),
  523. nvram_buf, item->name_size);
  524. if (!memcmp(name, (const char *)nvram_buf, item->name_size)) {
  525. /* TODO: check data crc? */
  526. /* founded! */
  527. return item_offs;
  528. }
  529. }
  530. next:
  531. offs += item_get_aligned_size(item);
  532. #ifdef CONFIG_NVRAM_FAST_SEARCH
  533. offs = item_bitmap_next_offset(region->seg_item_map,
  534. region->seg_size, offs);
  535. #endif
  536. }
  537. return -ENOENT;
  538. }
  539. static void region_seg_update_state(struct region_info *region, uint32_t seg_offset, uint8_t state)
  540. {
  541. int state_offs;
  542. state_offs = seg_offset + (int)(&((struct region_seg_header *)0)->state);
  543. LOG_DBG("set seg_offset 0x%x state to 0x%x\n", region->seg_offset, state);
  544. region_write(region, state_offs, &state, sizeof(state));
  545. }
  546. static int region_write_data(struct region_info *region, uint32_t offset,
  547. const uint8_t *data, int32_t len)
  548. {
  549. int32_t size, wsize;
  550. uint32_t woffs;
  551. wsize = NVRAM_BUFFER_SIZE;
  552. woffs = 0;
  553. size = len;
  554. while (size > 0) {
  555. if (size < wsize)
  556. wsize = size;
  557. memcpy(nvram_buf, data + woffs, wsize);
  558. region_write(region, offset + woffs, nvram_buf, wsize);
  559. woffs += wsize;
  560. size -= wsize;
  561. }
  562. return 0;
  563. }
  564. static int region_write_item(struct region_info *region, uint32_t offset,
  565. const char *name, const uint8_t *data, int32_t len)
  566. {
  567. struct nvram_item *item;
  568. uint32_t item_data_offs, item_size;
  569. item = (struct nvram_item *)nvram_buf;
  570. memset(item, 0x0, sizeof(struct nvram_item));
  571. item->magic = NVRAM_REGION_ITEM_MAGIC;
  572. item->state = NVRAM_ITEM_STATE_VALID;
  573. item->name_size = strlen(name) + 1;
  574. item->hash = calc_hash(name, item->name_size);
  575. item->data_size = (uint16_t)len;
  576. item_data_offs = sizeof(struct nvram_item) + item->name_size;
  577. item_size = item_data_offs + item->data_size;
  578. item->crc = calc_crc8((uint8_t *)item + NVRAM_REGION_ITEM_CRC_OFFSET,
  579. sizeof(struct nvram_item) - NVRAM_REGION_ITEM_CRC_OFFSET, 0);
  580. item->crc = calc_crc8(name, item->name_size, item->crc);
  581. item->crc = calc_crc8(data, item->data_size, item->crc);
  582. /* write item header & name */
  583. if (item_data_offs > NVRAM_BUFFER_SIZE) {
  584. LOG_ERR("BUG! invalid item name size %d", item->name_size);
  585. return -EINVAL;
  586. }
  587. memcpy(&item->data[0], name, item->name_size);
  588. region_write(region, offset, (uint8_t *)item, item_data_offs);
  589. /* write item data */
  590. #if 0
  591. /* use original data buffer to write data */
  592. region_write(region, offset + item_data_offs, data, len);
  593. #else
  594. /* use nvram buffer to write data to avoid cache-miss when access spinor mapping memory */
  595. region_write_data(region, offset + item_data_offs, data, len);
  596. #endif
  597. return item_size;
  598. }
  599. int region_copy_seg(struct region_info *region, uint32_t new_seg_offset,
  600. uint32_t old_seg_offset, int copy_size, int check_crc)
  601. {
  602. struct nvram_item item;
  603. int item_offs, new_item_offs;
  604. int item_size, item_total_size;
  605. int status;
  606. #ifdef CONFIG_NVRAM_FAST_SEARCH
  607. /* clear item bitmap for new segment */
  608. item_bitmap_clear_all(region->seg_item_map, region->seg_item_map_size);
  609. #endif
  610. item_offs = old_seg_offset + NVRAM_SEG_ITEM_START_OFFSET;
  611. new_item_offs = new_seg_offset + NVRAM_SEG_ITEM_START_OFFSET;
  612. while (item_offs < (old_seg_offset + copy_size)) {
  613. /* read item header */
  614. region_read(region, item_offs, (uint8_t *)&item, sizeof(struct nvram_item));
  615. /* check item */
  616. status = item_check_validity(region, item_offs, &item, check_crc);
  617. LOG_DBG("item_offs: status 0x%x", status);
  618. if (status == ITEM_STATUS_INVALID || status == ITEM_STATUS_EMPTY) {
  619. /* invalid */
  620. LOG_ERR("BUG! invalid nvram region item 0x%x, status 0x%x",
  621. item_offs, status);
  622. break;
  623. }
  624. item_total_size = item_get_aligned_size(&item);
  625. if (status == ITEM_STATUS_VALID) {
  626. item_size = item_get_real_size(&item);
  627. LOG_DBG("valid item: copy from 0x%x to 0x%x, len 0x%x",
  628. item_offs, new_item_offs, item_size);
  629. region_copy(region, item_offs, new_item_offs, item_size);
  630. #ifdef CONFIG_NVRAM_FAST_SEARCH
  631. item_bitmap_update(region->seg_item_map,
  632. new_item_offs - new_seg_offset, 1);
  633. #endif
  634. new_item_offs += item_total_size;
  635. }
  636. item_offs += item_total_size;
  637. }
  638. region->seg_write_offset = new_item_offs;
  639. LOG_DBG("new_seg write offset 0x%x", region->seg_write_offset);
  640. return 0;
  641. }
  642. static int region_init_new_seg(struct region_info *region, uint32_t seg_offset)
  643. {
  644. struct region_seg_header seg_hdr;
  645. memset(&seg_hdr, 0x0, sizeof(struct region_seg_header));
  646. seg_hdr.magic = NVRAM_REGION_SEG_MAGIC;
  647. seg_hdr.state = NVRAM_REGION_SEG_STATE_VALID;
  648. seg_hdr.version = NVRAM_REGION_SEG_VERSION;
  649. seg_hdr.head_size = sizeof(struct region_seg_header);
  650. seg_hdr.seg_size = region->seg_size;
  651. seg_hdr.seq_id = region->seg_seq_id + 1;
  652. seg_hdr.crc = calc_crc8(((uint8_t *)&seg_hdr) + NVRAM_REGION_SEG_HEADER_CRC_OFFSET,
  653. sizeof(struct region_seg_header) - NVRAM_REGION_SEG_HEADER_CRC_OFFSET, 0);
  654. region_erase(region, seg_offset, region->seg_size);
  655. region_write(region, seg_offset, (uint8_t *)&seg_hdr, sizeof(struct region_seg_header));
  656. region->seg_seq_id = seg_hdr.seq_id;
  657. region->seg_offset = seg_offset;
  658. region->seg_write_offset = region->seg_offset + NVRAM_SEG_ITEM_START_OFFSET;
  659. #ifdef CONFIG_NVRAM_FAST_SEARCH
  660. item_bitmap_clear_all(region->seg_item_map, region->seg_item_map_size);
  661. #endif
  662. return 0;
  663. }
  664. static int region_purge_seg(struct region_info *region, int check_crc)
  665. {
  666. uint32_t new_seg_offset, old_seg_offset, seg_copy_size;
  667. LOG_DBG("purge seg offset 0x%x\n", region->seg_offset);
  668. new_seg_offset = region->seg_offset + region->seg_size;
  669. if (new_seg_offset >= region->total_size) {
  670. /* wrap to begin of region */
  671. new_seg_offset = 0;
  672. }
  673. /* check new seg */
  674. if (!region_is_empy(region, new_seg_offset, region->seg_size)) {
  675. /* sorry, must erase in this context */
  676. LOG_WRN("new region seg 0x%x is not empty, need erase\n", new_seg_offset);
  677. region_erase(region, new_seg_offset, region->seg_size);
  678. }
  679. old_seg_offset = region->seg_offset;
  680. seg_copy_size = region->seg_write_offset - region->seg_offset;
  681. /* init new seg */
  682. region_init_new_seg(region, new_seg_offset);
  683. /* copy valid data to new seg */
  684. region_copy_seg(region, new_seg_offset, old_seg_offset, seg_copy_size, check_crc);
  685. /* current seg set to obsolete */
  686. region_seg_update_state(region, old_seg_offset, NVRAM_REGION_SEG_STATE_OBSOLETE);
  687. return 0;
  688. }
  689. static int region_prepare_write_item(struct region_info *region, int item_size)
  690. {
  691. if ((region->seg_write_offset + item_size) > (region->seg_offset + region->seg_size)) {
  692. region_purge_seg(region, 0);
  693. }
  694. return 0;
  695. }
  696. static int region_is_same_config_data(struct region_info *region, const char *name,
  697. const void *data, int len)
  698. {
  699. struct nvram_item item;
  700. int old_item_offs, offset;
  701. int32_t pos, read_size;
  702. old_item_offs = region_find_item(region, name, &item);
  703. if(old_item_offs > 0 && len == item.data_size){
  704. pos = 0;
  705. read_size = NVRAM_BUFFER_SIZE;
  706. offset = old_item_offs + sizeof(struct nvram_item) + item.name_size;
  707. while (len > 0) {
  708. if (len < read_size)
  709. read_size = len;
  710. region_read(region, offset + pos, nvram_buf, read_size);
  711. if(memcmp(nvram_buf ,(uint8_t *)data + pos, read_size) != 0){
  712. return false;
  713. }
  714. pos += read_size;
  715. len -= read_size;
  716. }
  717. if(len == 0){
  718. return true;
  719. }
  720. }
  721. return false;
  722. }
  723. static int region_set(struct region_info *region, const char *name,
  724. const void *data, int len)
  725. {
  726. struct nvram_item item;
  727. int32_t name_len, new_item_size, item_len;
  728. int old_item_offs;
  729. if (!name || (!data && len) || len > NVRAM_MAX_DATA_SIZE)
  730. return -EINVAL;
  731. LOG_DBG("set config '%s', len %d\n", name, len);
  732. name_len = strlen(name) + 1;
  733. if (name_len > NVRAM_MAX_NAME_SIZE)
  734. return -EINVAL;
  735. if (len > 0) {
  736. if(region_is_same_config_data(region, name, data, len)){
  737. return 0;
  738. }
  739. /* write new config */
  740. new_item_size = item_calc_aligned_size(name_len, len);
  741. region_prepare_write_item(region, new_item_size);
  742. old_item_offs = region_find_item(region, name, &item);
  743. item_len = region_write_item(region, region->seg_write_offset, name, data, len);
  744. if (item_len > new_item_size) {
  745. LOG_ERR("BUG! new_item_size 0x%x, write item_len 0x%x\n",
  746. new_item_size, item_len);
  747. }
  748. #ifdef CONFIG_NVRAM_FAST_SEARCH
  749. item_bitmap_update(region->seg_item_map,
  750. region->seg_write_offset - region->seg_offset, 1);
  751. #endif
  752. region->seg_write_offset += new_item_size;
  753. }
  754. else {
  755. old_item_offs = region_find_item(region, name, &item);
  756. }
  757. LOG_DBG("name %s, old_item_offs 0x%x\n", name, old_item_offs);
  758. if (old_item_offs > 0) {
  759. /* set the old item state to obsolete */
  760. item_update_state(region, old_item_offs, NVRAM_ITEM_STATE_OBSOLETE);
  761. #ifdef CONFIG_NVRAM_FAST_SEARCH
  762. item_bitmap_update(region->seg_item_map, old_item_offs - region->seg_offset, 0);
  763. #endif
  764. }
  765. return 0;
  766. }
  767. static int region_get(struct region_info *region, const char *name,
  768. void *data, int max_len)
  769. {
  770. struct nvram_item item;
  771. uint32_t item_offs, data_offs, len;
  772. if (!name || !data || !max_len)
  773. return -EINVAL;
  774. LOG_DBG("get config '%s', max_len %d", name, max_len);
  775. /* search write region firstly */
  776. item_offs = region_find_item(region, name, &item);
  777. if ((int32_t)item_offs < 0)
  778. return -ENOENT;
  779. data_offs = item_offs + sizeof(struct nvram_item) + item.name_size;
  780. if (max_len < item.data_size)
  781. len = max_len;
  782. else
  783. len = item.data_size;
  784. region_read(region, data_offs, data, len);
  785. return len;
  786. }
  787. extern void print_buffer(const struct shell *shell,const char *addr, int width,
  788. int count, int linelen, unsigned long disp_addr);
  789. static void region_dump_data(const struct shell *shell, struct region_info *region, uint32_t offset, int32_t size)
  790. {
  791. int32_t pos, read_size;
  792. pos = 0;
  793. read_size = NVRAM_BUFFER_SIZE;
  794. while (size > 0) {
  795. if (size < read_size)
  796. read_size = size;
  797. region_read(region, offset + pos, nvram_buf, read_size);
  798. #ifdef CONFIG_SHELL_DBG
  799. print_buffer(shell, nvram_buf, 1, read_size, 16, pos);
  800. #endif
  801. pos += read_size;
  802. size -= read_size;
  803. /* sleep a while to avoid print buffer overflow */
  804. k_busy_wait(1000);
  805. }
  806. }
  807. static void region_dump(const struct shell *shell, struct region_info *region, int detailed)
  808. {
  809. struct nvram_item item;
  810. uint32_t offs, item_offs;
  811. int i;
  812. printk("region %s: base addr 0x%x total size 0x%x\n",
  813. region->name, region->base_addr, region->total_size);
  814. printk("region segment offs 0x%x, size 0x%x, seq_id 0x%x, write offset 0x%x\n",
  815. region->seg_offset, region->seg_size, region->seg_seq_id,
  816. region->seg_write_offset);
  817. if (!detailed)
  818. return;
  819. i = 0;
  820. offs = 0;
  821. #ifdef CONFIG_NVRAM_FAST_SEARCH
  822. offs = item_bitmap_first_offset(region->seg_item_map, region->seg_size);
  823. #else
  824. offs = NVRAM_SEG_ITEM_START_OFFSET;
  825. #endif
  826. while (offs < region->seg_size) {
  827. item_offs = region->seg_offset + offs;
  828. /* read item header */
  829. region_read(region, item_offs, (uint8_t *)&item, sizeof(struct nvram_item));
  830. if (item.magic != NVRAM_REGION_ITEM_MAGIC)
  831. break;
  832. if (item.state != NVRAM_ITEM_STATE_VALID) {
  833. goto next;
  834. }
  835. printk("[%2d] config item_offs 0x%x size 0x%x data size 0x%x\n",
  836. i, item_offs, item.name_size, item.data_size);
  837. /* read config name */
  838. region_read(region, item_offs + sizeof(struct nvram_item),
  839. nvram_buf, item.name_size);
  840. printk(" config name: %s\n", nvram_buf);
  841. printk(" config data:\n");
  842. region_dump_data(shell, region, item_offs + sizeof(struct nvram_item) + item.name_size,
  843. item.data_size);
  844. next:
  845. offs += item_get_aligned_size(&item);
  846. #ifdef CONFIG_NVRAM_FAST_SEARCH
  847. offs = item_bitmap_next_offset(region->seg_item_map,
  848. region->seg_size, offs);
  849. #endif
  850. i++;
  851. }
  852. }
  853. static bool is_valid_region_seg_header(struct region_seg_header *hdr)
  854. {
  855. uint32_t crc;
  856. /* validate magic number and state*/
  857. if (hdr->magic != NVRAM_REGION_SEG_MAGIC ||
  858. hdr->state != NVRAM_REGION_SEG_STATE_VALID)
  859. return false;
  860. /* validate header crc */
  861. crc = calc_crc8((uint8_t *)hdr + NVRAM_REGION_SEG_HEADER_CRC_OFFSET,
  862. sizeof(struct region_seg_header) - NVRAM_REGION_SEG_HEADER_CRC_OFFSET, 0);
  863. if (hdr->crc != crc) {
  864. LOG_ERR("invalid header crc 0x%x != hdr->crc 0x%x", crc, hdr->crc);
  865. return false;
  866. }
  867. return true;
  868. }
  869. static int region_seg_scan(struct region_info *region)
  870. {
  871. struct nvram_item item;
  872. int err, offs, item_offs, status;
  873. int need_purge = 0;
  874. uint8_t item_name[NVRAM_MAX_NAME_SIZE];
  875. int old_item_offs;
  876. struct nvram_item old_item;
  877. #ifdef CONFIG_NVRAM_FAST_SEARCH
  878. item_bitmap_clear_all(region->seg_item_map, region->seg_item_map_size);
  879. #endif
  880. offs = NVRAM_SEG_ITEM_START_OFFSET;
  881. item_offs = region->seg_offset + offs;
  882. while (offs < region->seg_size) {
  883. /* read item header */
  884. err = region_read(region, item_offs, (uint8_t *)&item,
  885. sizeof(struct nvram_item));
  886. if (err) {
  887. LOG_ERR("read item_offs 0x%x error", item_offs);
  888. return err;
  889. }
  890. /* read item header */
  891. status = item_check_validity(region, item_offs, &item, 1);
  892. LOG_DBG("item_offs 0x%x: status 0x%x", item_offs, status);
  893. if (status == ITEM_STATUS_VALID) {
  894. memset(item_name, 0, sizeof(item_name));
  895. if(item_get_item_name(region, item_offs, &item, item_name, sizeof(item_name)) == 0){
  896. old_item_offs = region_find_item(region, item_name, &old_item);
  897. if(old_item_offs > 0 && (old_item_offs != item_offs)){
  898. LOG_WRN("same item %s found new:0x%x old 0x%x\n", item_name, item_offs, old_item_offs);
  899. /* set the old item state to obsolete */
  900. item_update_state(region, old_item_offs, NVRAM_ITEM_STATE_OBSOLETE);
  901. #ifdef CONFIG_NVRAM_FAST_SEARCH
  902. item_bitmap_update(region->seg_item_map, old_item_offs - region->seg_offset, 0);
  903. #endif
  904. }
  905. }
  906. #ifdef CONFIG_NVRAM_FAST_SEARCH
  907. item_bitmap_update(region->seg_item_map, offs, 1);
  908. #endif
  909. } else if (status == ITEM_STATUS_EMPTY) {
  910. break;
  911. } else if (status != ITEM_STATUS_OBSOLETE) {
  912. /* invalid */
  913. LOG_ERR("found invalid item, need purge, item_offs 0x%x status %d",
  914. item_offs, status);
  915. need_purge = 1;
  916. break;
  917. }
  918. offs += item_get_aligned_size(&item);
  919. item_offs = region->seg_offset + offs;
  920. }
  921. region->seg_write_offset = item_offs;
  922. LOG_DBG("seg_offset 0x%x, write_offset 0x%x", region->seg_offset,
  923. region->seg_write_offset);
  924. if (offs >= region->seg_size) {
  925. /* region is full */
  926. need_purge = 1;
  927. } else {
  928. if (!region_is_empy(region, item_offs, region->seg_size - offs)) {
  929. LOG_ERR("region seg is not clean after write_offset 0x%x, need purge",
  930. item_offs);
  931. need_purge = 1;
  932. }
  933. }
  934. if (need_purge) {
  935. region_purge_seg(region, 1);
  936. }
  937. return 0;
  938. }
  939. static int region_scan(struct region_info *region, bool b_init_seg)
  940. {
  941. struct region_seg_header hdr;
  942. uint32_t offs = 0;
  943. int err, found = 0;
  944. while (offs < region->total_size) {
  945. err = region_read(region, offs, (uint8_t *)&hdr, sizeof(struct region_seg_header));
  946. if (err) {
  947. return err;
  948. }
  949. if (is_valid_region_seg_header(&hdr) &&
  950. (!found || ((int8_t)(hdr.seq_id - region->seg_seq_id)) > 0)) {
  951. region->seg_seq_id = hdr.seq_id;
  952. region->seg_offset = offs;
  953. found = 1;
  954. }
  955. offs += region->seg_size;
  956. }
  957. if (found) {
  958. LOG_DBG("found region seg offset 0x%x\n", region->seg_offset);
  959. err = region_seg_scan(region);
  960. if (err) {
  961. LOG_ERR("invalid region seg offset 0x%x\n", region->seg_offset);
  962. return -1;
  963. }
  964. }
  965. else {
  966. if(b_init_seg){
  967. LOG_DBG("region %s: first init\n", region->name);
  968. region_init_new_seg(region, 0);
  969. }else{
  970. LOG_ERR("region %s: init err\n", region->name);
  971. }
  972. }
  973. return 0;
  974. }
  975. int nvram_config_set_factory(const char *name, const void *data, int len)
  976. {
  977. int ret;
  978. k_sem_take(&nvram_lock, K_FOREVER);
  979. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  980. ret = region_set(&factory_rw_nvram_region, name ,data, len);
  981. nvram_storage_flush(factory_rw_nvram_region.storage);
  982. #else
  983. ret = region_set(&factory_nvram_region, name ,data, len);
  984. nvram_storage_flush(factory_nvram_region.storage);
  985. #endif
  986. k_sem_give(&nvram_lock);
  987. return ret;
  988. }
  989. int nvram_config_set(const char *name, const void *data, int len)
  990. {
  991. int ret;
  992. k_sem_take(&nvram_lock, K_FOREVER);
  993. ret = region_set(&user_nvram_region, name, data, len);
  994. nvram_storage_flush(user_nvram_region.storage);
  995. k_sem_give(&nvram_lock);
  996. return ret;
  997. }
  998. int nvram_config_get_factory(const char *name, void *data, int max_len)
  999. {
  1000. int ret;
  1001. k_sem_take(&nvram_lock, K_FOREVER);
  1002. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  1003. ret = region_get(&factory_rw_nvram_region, name, data, max_len);
  1004. if (ret >= 0) {
  1005. k_sem_give(&nvram_lock);
  1006. return ret;
  1007. }
  1008. #endif
  1009. ret = region_get(&factory_nvram_region, name, data, max_len);
  1010. k_sem_give(&nvram_lock);
  1011. return ret;
  1012. }
  1013. int nvram_config_get(const char *name, void *data, int max_len)
  1014. {
  1015. int ret;
  1016. k_sem_take(&nvram_lock, K_FOREVER);
  1017. /* config priority: user > factory rw > factory ro */
  1018. /* search user nvram region */
  1019. ret = region_get(&user_nvram_region, name, data, max_len);
  1020. if (ret >= 0){
  1021. k_sem_give(&nvram_lock);
  1022. return ret;
  1023. }
  1024. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  1025. ret = region_get(&factory_rw_nvram_region, name, data, max_len);
  1026. if (ret >= 0) {
  1027. k_sem_give(&nvram_lock);
  1028. return ret;
  1029. }
  1030. #endif
  1031. /* search factory nvram region */
  1032. ret = region_get(&factory_nvram_region, name, data, max_len);
  1033. if (ret < 0){
  1034. LOG_DBG("cannot found config %s", name);
  1035. }
  1036. k_sem_give(&nvram_lock);
  1037. return ret;
  1038. }
  1039. void nvram_config_dump(const struct shell *shell)
  1040. {
  1041. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  1042. region_dump(shell, &factory_rw_nvram_region, 1);
  1043. #endif
  1044. region_dump(shell, &factory_nvram_region, 1);
  1045. region_dump(shell, &user_nvram_region, 1);
  1046. }
  1047. int nvram_config_clear(int len)
  1048. {
  1049. int ret;
  1050. ret = region_clear(&user_nvram_region, len);
  1051. nvram_storage_flush(user_nvram_region.storage);
  1052. return ret;
  1053. }
  1054. int nvram_config_clear_all(void)
  1055. {
  1056. struct region_info *region = &user_nvram_region;
  1057. LOG_WRN("clear all user nvram config");
  1058. k_sem_take(&nvram_lock, K_FOREVER);
  1059. /* erase region */
  1060. region_erase(region, 0, region->total_size);
  1061. /* rescan region */
  1062. region_scan(region, true);
  1063. nvram_storage_flush(region->storage);
  1064. k_sem_give(&nvram_lock);
  1065. return 0;
  1066. }
  1067. int nvram_config_defrag_user(void)
  1068. {
  1069. struct region_info *region = &user_nvram_region;
  1070. LOG_WRN("defrag user nvram config");
  1071. k_sem_take(&nvram_lock, K_FOREVER);
  1072. /* copy to first region */
  1073. if (region->seg_offset > 0) {
  1074. region_erase(region, 0, region->seg_size);
  1075. region_copy(region, region->seg_offset, 0, region->seg_size);
  1076. }
  1077. /* erase other region */
  1078. region_erase(region, region->seg_size, region->total_size - region->seg_size);
  1079. /* rescan region */
  1080. region_scan(region, true);
  1081. nvram_storage_flush(region->storage);
  1082. k_sem_give(&nvram_lock);
  1083. return 0;
  1084. }
  1085. int nvram_config_init(const struct device *dev)
  1086. {
  1087. struct device *storage;
  1088. const struct partition_entry *nvram_part;
  1089. LOG_INF("init nvram config");
  1090. nvram_part = partition_get_part(PARTITION_FILE_ID_NVRAM_FACTORY);
  1091. if (nvram_part) {
  1092. factory_nvram_region.base_addr = nvram_part->offset;
  1093. factory_nvram_region.total_size = nvram_part->size;
  1094. LOG_INF("nvram fac:0x%x,0x%x\n", factory_nvram_region.base_addr, factory_nvram_region.total_size);
  1095. } else {
  1096. LOG_ERR("nvram fac partition NOT find");
  1097. }
  1098. nvram_part = partition_get_part(PARTITION_FILE_ID_NVRAM_USER);
  1099. if (nvram_part) {
  1100. user_nvram_region.base_addr = nvram_part->offset;
  1101. user_nvram_region.total_size = nvram_part->size;
  1102. LOG_INF("nvram user:0x%x,0x%x\n", user_nvram_region.base_addr, user_nvram_region.total_size);
  1103. } else {
  1104. LOG_ERR("nvram user partition NOT find");
  1105. }
  1106. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  1107. nvram_part = partition_get_part(PARTITION_FILE_ID_NVRAM_FACTORY_RW);
  1108. if (nvram_part) {
  1109. factory_rw_nvram_region.base_addr = nvram_part->offset;
  1110. factory_rw_nvram_region.total_size = nvram_part->size;
  1111. LOG_INF("nvram fac rw:0x%x,0x%x\n", factory_rw_nvram_region.base_addr, factory_rw_nvram_region.total_size);
  1112. } else {
  1113. LOG_ERR("nvram fac rw partition NOT find");
  1114. }
  1115. #endif
  1116. storage = nvram_storage_init();
  1117. if (!storage) {
  1118. LOG_ERR("NVRAM storage driver was not found!\n");
  1119. return -ENODEV;
  1120. }
  1121. factory_nvram_region.storage = storage;
  1122. user_nvram_region.storage = storage;
  1123. #ifdef CONFIG_NVRAM_STORAGE_FACTORY_RW_REGION
  1124. factory_rw_nvram_region.storage = storage;
  1125. region_scan(&factory_rw_nvram_region, true);
  1126. #endif
  1127. region_scan(&factory_nvram_region, false);
  1128. region_scan(&user_nvram_region, true);
  1129. /* clear next write regtion to avoid erasing in system */
  1130. region_clear(&user_nvram_region, user_nvram_region.total_size / 2);
  1131. nvram_storage_flush(user_nvram_region.storage);
  1132. return 0;
  1133. }
  1134. SYS_INIT(nvram_config_init, POST_KERNEL, CONFIG_NVRAM_CONFIG_INIT_PRIORITY);