ef_env.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. /*
  2. * This file is part of the EasyFlash Library.
  3. *
  4. * Copyright (c) 2019, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Environment variables operating interface. This is the Next Generation version.
  26. * Created on: 2019-02-02
  27. */
  28. #include <string.h>
  29. #include <easyflash.h>
  30. #if defined(EF_USING_ENV) && !defined(EF_ENV_USING_LEGACY_MODE)
  31. #ifndef EF_WRITE_GRAN
  32. #error "Please configure flash write granularity (in ef_cfg.h)"
  33. #endif
  34. #if EF_WRITE_GRAN != 1 && EF_WRITE_GRAN != 8 && EF_WRITE_GRAN != 32 && EF_WRITE_GRAN != 64
  35. #error "the write gran can be only setting as 1, 8, 32 and 64"
  36. #endif
  37. /* magic word(`E`, `F`, `4`, `0`) */
  38. #define SECTOR_MAGIC_WORD 0x30344645
  39. /* magic word(`K`, `V`, `4`, `0`) */
  40. #define ENV_MAGIC_WORD 0x3034564B
  41. /* the using status sector table length */
  42. #ifndef USING_SECTOR_TABLE_LEN
  43. #define USING_SECTOR_TABLE_LEN 4
  44. #endif
  45. /* the string ENV value buffer size for legacy ef_get_env() function */
  46. #ifndef EF_STR_ENV_VALUE_MAX_SIZE
  47. #define EF_STR_ENV_VALUE_MAX_SIZE 128
  48. #endif
  49. /* the sector remain threshold before full status */
  50. #ifndef EF_SEC_REMAIN_THRESHOLD
  51. #define EF_SEC_REMAIN_THRESHOLD (ENV_HDR_DATA_SIZE + EF_ENV_NAME_MAX)
  52. #endif
  53. /* the total remain empty sector threshold before GC */
  54. #ifndef EF_GC_EMPTY_SEC_THRESHOLD
  55. #define EF_GC_EMPTY_SEC_THRESHOLD 2
  56. #endif
  57. /* the ENV cache table size, it will improve ENV search speed when using cache */
  58. #ifndef EF_ENV_CACHE_TABLE_SIZE
  59. #define EF_ENV_CACHE_TABLE_SIZE 16
  60. #endif
  61. /* the sector cache table size, it will improve ENV save speed when using cache */
  62. #ifndef EF_SECTOR_CACHE_TABLE_SIZE
  63. #define EF_SECTOR_CACHE_TABLE_SIZE 4
  64. #endif
  65. #if EF_ENV_CACHE_TABLE_SIZE > 0xFFFF
  66. #error "The ENV cache table size must less than 0xFFFF"
  67. #endif
  68. #if (EF_ENV_CACHE_TABLE_SIZE > 0) && (EF_SECTOR_CACHE_TABLE_SIZE > 0)
  69. #define EF_ENV_USING_CACHE
  70. #endif
  71. /* the sector is not combined value */
  72. #define SECTOR_NOT_COMBINED 0xFFFFFFFF
  73. /* the next address is get failed */
  74. #define FAILED_ADDR 0xFFFFFFFF
  75. /* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  76. * would return 16.
  77. */
  78. #define EF_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
  79. /* align by write granularity */
  80. #define EF_WG_ALIGN(size) (EF_ALIGN(size, (EF_WRITE_GRAN + 7)/8))
  81. /**
  82. * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
  83. * would return 12.
  84. */
  85. #define EF_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
  86. /* align down by write granularity */
  87. #define EF_WG_ALIGN_DOWN(size) (EF_ALIGN_DOWN(size, (EF_WRITE_GRAN + 7)/8))
  88. #if (EF_WRITE_GRAN == 1)
  89. #define STATUS_TABLE_SIZE(status_number) ((status_number * EF_WRITE_GRAN + 7)/8)
  90. #else
  91. #define STATUS_TABLE_SIZE(status_number) (((status_number - 1) * EF_WRITE_GRAN + 7)/8)
  92. #endif
  93. #define STORE_STATUS_TABLE_SIZE STATUS_TABLE_SIZE(SECTOR_STORE_STATUS_NUM)
  94. #define DIRTY_STATUS_TABLE_SIZE STATUS_TABLE_SIZE(SECTOR_DIRTY_STATUS_NUM)
  95. #define ENV_STATUS_TABLE_SIZE STATUS_TABLE_SIZE(ENV_STATUS_NUM)
  96. #define SECTOR_SIZE EF_ERASE_MIN_SIZE
  97. #define SECTOR_NUM (ENV_AREA_SIZE / (EF_ERASE_MIN_SIZE))
  98. #if (SECTOR_NUM < 2)
  99. #error "The sector number must lager then or equal to 2"
  100. #endif
  101. #if (EF_GC_EMPTY_SEC_THRESHOLD == 0 || EF_GC_EMPTY_SEC_THRESHOLD >= SECTOR_NUM)
  102. #error "There is at least one empty sector for GC."
  103. #endif
  104. #define SECTOR_HDR_DATA_SIZE (EF_WG_ALIGN(sizeof(struct sector_hdr_data)))
  105. #define SECTOR_DIRTY_OFFSET ((unsigned long)(&((struct sector_hdr_data *)0)->status_table.dirty))
  106. #define ENV_HDR_DATA_SIZE (EF_WG_ALIGN(sizeof(struct env_hdr_data)))
  107. #define ENV_MAGIC_OFFSET ((unsigned long)(&((struct env_hdr_data *)0)->magic))
  108. #define ENV_LEN_OFFSET ((unsigned long)(&((struct env_hdr_data *)0)->len))
  109. #define ENV_NAME_LEN_OFFSET ((unsigned long)(&((struct env_hdr_data *)0)->name_len))
  110. #define VER_NUM_ENV_NAME "__ver_num__"
  111. enum sector_store_status {
  112. SECTOR_STORE_UNUSED,
  113. SECTOR_STORE_EMPTY,
  114. SECTOR_STORE_USING,
  115. SECTOR_STORE_FULL,
  116. SECTOR_STORE_STATUS_NUM,
  117. };
  118. typedef enum sector_store_status sector_store_status_t;
  119. enum sector_dirty_status {
  120. SECTOR_DIRTY_UNUSED,
  121. SECTOR_DIRTY_FALSE,
  122. SECTOR_DIRTY_TRUE,
  123. SECTOR_DIRTY_GC,
  124. SECTOR_DIRTY_STATUS_NUM,
  125. };
  126. typedef enum sector_dirty_status sector_dirty_status_t;
  127. struct sector_hdr_data {
  128. struct {
  129. uint8_t store[STORE_STATUS_TABLE_SIZE]; /**< sector store status @see sector_store_status_t */
  130. uint8_t dirty[DIRTY_STATUS_TABLE_SIZE]; /**< sector dirty status @see sector_dirty_status_t */
  131. } status_table;
  132. uint32_t magic; /**< magic word(`E`, `F`, `4`, `0`) */
  133. uint32_t combined; /**< the combined next sector number, 0xFFFFFFFF: not combined */
  134. uint32_t reserved;
  135. };
  136. typedef struct sector_hdr_data *sector_hdr_data_t;
  137. struct sector_meta_data {
  138. bool check_ok; /**< sector header check is OK */
  139. struct {
  140. sector_store_status_t store; /**< sector store status @see sector_store_status_t */
  141. sector_dirty_status_t dirty; /**< sector dirty status @see sector_dirty_status_t */
  142. } status;
  143. uint32_t addr; /**< sector start address */
  144. uint32_t magic; /**< magic word(`E`, `F`, `4`, `0`) */
  145. uint32_t combined; /**< the combined next sector number, 0xFFFFFFFF: not combined */
  146. size_t remain; /**< remain size */
  147. uint32_t empty_env; /**< the next empty ENV node start address */
  148. };
  149. typedef struct sector_meta_data *sector_meta_data_t;
  150. struct env_hdr_data {
  151. uint8_t status_table[ENV_STATUS_TABLE_SIZE]; /**< ENV node status, @see node_status_t */
  152. uint32_t magic; /**< magic word(`K`, `V`, `4`, `0`) */
  153. uint32_t len; /**< ENV node total length (header + name + value), must align by EF_WRITE_GRAN */
  154. uint32_t crc32; /**< ENV node crc32(name_len + data_len + name + value) */
  155. uint8_t name_len; /**< name length */
  156. uint32_t value_len; /**< value length */
  157. };
  158. typedef struct env_hdr_data *env_hdr_data_t;
  159. struct env_cache_node {
  160. uint16_t name_crc; /**< ENV name's CRC32 low 16bit value */
  161. uint16_t active; /**< ENV node access active degree */
  162. uint32_t addr; /**< ENV node address */
  163. };
  164. typedef struct env_cache_node *env_cache_node_t;
  165. struct sector_cache_node {
  166. uint32_t addr; /**< sector start address */
  167. uint32_t empty_addr; /**< sector empty address */
  168. };
  169. typedef struct sector_cache_node *sector_cache_node_t;
  170. static void gc_collect(void);
  171. /* ENV start address in flash */
  172. static uint32_t env_start_addr = 0;
  173. /* default ENV set, must be initialized by user */
  174. static ef_env const *default_env_set;
  175. /* default ENV set size, must be initialized by user */
  176. static size_t default_env_set_size = 0;
  177. /* initialize OK flag */
  178. static bool init_ok = false;
  179. /* request a GC check */
  180. static bool gc_request = false;
  181. /* is in recovery check status when first reboot */
  182. static bool in_recovery_check = false;
  183. #ifdef EF_ENV_USING_CACHE
  184. /* ENV cache table */
  185. struct env_cache_node env_cache_table[EF_ENV_CACHE_TABLE_SIZE] = { 0 };
  186. /* sector cache table, it caching the sector info which status is current using */
  187. struct sector_cache_node sector_cache_table[EF_SECTOR_CACHE_TABLE_SIZE] = { 0 };
  188. #endif /* EF_ENV_USING_CACHE */
  189. static size_t set_status(uint8_t status_table[], size_t status_num, size_t status_index)
  190. {
  191. size_t byte_index = ~0UL;
  192. /*
  193. * | write garn | status0 | status1 | status2 |
  194. * ---------------------------------------------------------------------------------
  195. * | 1bit | 0xFF | 0x7F | 0x3F |
  196. * | 8bit | 0xFFFF | 0x00FF | 0x0000 |
  197. * | 32bit | 0xFFFFFFFF FFFFFFFF | 0x00FFFFFF FFFFFFFF | 0x00FFFFFF 00FFFFFF |
  198. */
  199. memset(status_table, 0xFF, STATUS_TABLE_SIZE(status_num));
  200. if (status_index > 0) {
  201. #if (EF_WRITE_GRAN == 1)
  202. byte_index = (status_index - 1) / 8;
  203. status_table[byte_index] &= ~(0x80 >> ((status_index - 1) % 8));
  204. #else
  205. byte_index = (status_index - 1) * (EF_WRITE_GRAN / 8);
  206. status_table[byte_index] = 0x00;
  207. #endif /* EF_WRITE_GRAN == 1 */
  208. }
  209. return byte_index;
  210. }
  211. static size_t get_status(uint8_t status_table[], size_t status_num)
  212. {
  213. size_t i = 0, status_num_bak = --status_num;
  214. while (status_num --) {
  215. /* get the first 0 position from end address to start address */
  216. #if (EF_WRITE_GRAN == 1)
  217. if ((status_table[status_num / 8] & (0x80 >> (status_num % 8))) == 0x00) {
  218. break;
  219. }
  220. #else /* (EF_WRITE_GRAN == 8) || (EF_WRITE_GRAN == 32) || (EF_WRITE_GRAN == 64) */
  221. if (status_table[status_num * EF_WRITE_GRAN / 8] == 0x00) {
  222. break;
  223. }
  224. #endif /* EF_WRITE_GRAN == 1 */
  225. i++;
  226. }
  227. return status_num_bak - i;
  228. }
  229. static EfErrCode write_status(uint32_t addr, uint8_t status_table[], size_t status_num, size_t status_index)
  230. {
  231. EfErrCode result = EF_NO_ERR;
  232. size_t byte_index;
  233. EF_ASSERT(status_index < status_num);
  234. EF_ASSERT(status_table);
  235. /* set the status first */
  236. byte_index = set_status(status_table, status_num, status_index);
  237. /* the first status table value is all 1, so no need to write flash */
  238. if (byte_index == ~0UL) {
  239. return EF_NO_ERR;
  240. }
  241. #if (EF_WRITE_GRAN == 1)
  242. result = ef_port_write(addr + byte_index, (uint32_t *)&status_table[byte_index], 1);
  243. #else /* (EF_WRITE_GRAN == 8) || (EF_WRITE_GRAN == 32) || (EF_WRITE_GRAN == 64) */
  244. /* write the status by write granularity
  245. * some flash (like stm32 onchip) NOT supported repeated write before erase */
  246. result = ef_port_write(addr + byte_index, (uint32_t *) &status_table[byte_index], EF_WRITE_GRAN / 8);
  247. #endif /* EF_WRITE_GRAN == 1 */
  248. return result;
  249. }
  250. static size_t read_status(uint32_t addr, uint8_t status_table[], size_t total_num)
  251. {
  252. EF_ASSERT(status_table);
  253. ef_port_read(addr, (uint32_t *) status_table, STATUS_TABLE_SIZE(total_num));
  254. return get_status(status_table, total_num);
  255. }
  256. #ifdef EF_ENV_USING_CACHE
  257. /*
  258. * It's only caching the current using status sector's empty_addr
  259. */
  260. static void update_sector_cache(uint32_t sec_addr, uint32_t empty_addr)
  261. {
  262. size_t i, empty_index = EF_SECTOR_CACHE_TABLE_SIZE;
  263. for (i = 0; i < EF_SECTOR_CACHE_TABLE_SIZE; i++) {
  264. if ((empty_addr > sec_addr) && (empty_addr < sec_addr + SECTOR_SIZE)) {
  265. /* update the sector empty_addr in cache */
  266. if (sector_cache_table[i].addr == sec_addr) {
  267. sector_cache_table[i].addr = sec_addr;
  268. sector_cache_table[i].empty_addr = empty_addr;
  269. return;
  270. } else if ((sector_cache_table[i].addr == FAILED_ADDR) && (empty_index == EF_SECTOR_CACHE_TABLE_SIZE)) {
  271. empty_index = i;
  272. }
  273. } else if (sector_cache_table[i].addr == sec_addr) {
  274. /* delete the sector which status is not current using */
  275. sector_cache_table[i].addr = FAILED_ADDR;
  276. return;
  277. }
  278. }
  279. /* add the sector empty_addr to cache */
  280. if (empty_index < EF_SECTOR_CACHE_TABLE_SIZE) {
  281. sector_cache_table[empty_index].addr = sec_addr;
  282. sector_cache_table[empty_index].empty_addr = empty_addr;
  283. }
  284. }
  285. /*
  286. * Get sector info from cache. It's return true when cache is hit.
  287. */
  288. static bool get_sector_from_cache(uint32_t sec_addr, uint32_t *empty_addr)
  289. {
  290. size_t i;
  291. for (i = 0; i < EF_SECTOR_CACHE_TABLE_SIZE; i++) {
  292. if (sector_cache_table[i].addr == sec_addr) {
  293. if (empty_addr) {
  294. *empty_addr = sector_cache_table[i].empty_addr;
  295. }
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. static void update_env_cache(const char *name, size_t name_len, uint32_t addr)
  302. {
  303. size_t i, empty_index = EF_ENV_CACHE_TABLE_SIZE, min_activity_index = EF_ENV_CACHE_TABLE_SIZE;
  304. uint16_t name_crc = (uint16_t) (ef_calc_crc32(0, name, name_len) >> 16), min_activity = 0xFFFF;
  305. for (i = 0; i < EF_ENV_CACHE_TABLE_SIZE; i++) {
  306. if (addr != FAILED_ADDR) {
  307. /* update the ENV address in cache */
  308. if (env_cache_table[i].name_crc == name_crc) {
  309. env_cache_table[i].addr = addr;
  310. return;
  311. } else if ((env_cache_table[i].addr == FAILED_ADDR) && (empty_index == EF_ENV_CACHE_TABLE_SIZE)) {
  312. empty_index = i;
  313. } else if (env_cache_table[i].addr != FAILED_ADDR) {
  314. if (env_cache_table[i].active > 0) {
  315. env_cache_table[i].active--;
  316. }
  317. if (env_cache_table[i].active < min_activity) {
  318. min_activity_index = i;
  319. min_activity = env_cache_table[i].active;
  320. }
  321. }
  322. } else if (env_cache_table[i].name_crc == name_crc) {
  323. /* delete the ENV */
  324. env_cache_table[i].addr = FAILED_ADDR;
  325. env_cache_table[i].active = 0;
  326. return;
  327. }
  328. }
  329. /* add the ENV to cache, using LRU (Least Recently Used) like algorithm */
  330. if (empty_index < EF_ENV_CACHE_TABLE_SIZE) {
  331. env_cache_table[empty_index].addr = addr;
  332. env_cache_table[empty_index].name_crc = name_crc;
  333. env_cache_table[empty_index].active = 0;
  334. } else if (min_activity_index < EF_ENV_CACHE_TABLE_SIZE) {
  335. env_cache_table[min_activity_index].addr = addr;
  336. env_cache_table[min_activity_index].name_crc = name_crc;
  337. env_cache_table[min_activity_index].active = 0;
  338. }
  339. }
  340. /*
  341. * Get ENV info from cache. It's return true when cache is hit.
  342. */
  343. static bool get_env_from_cache(const char *name, size_t name_len, uint32_t *addr)
  344. {
  345. size_t i;
  346. uint16_t name_crc = (uint16_t) (ef_calc_crc32(0, name, name_len) >> 16);
  347. for (i = 0; i < EF_ENV_CACHE_TABLE_SIZE; i++) {
  348. if ((env_cache_table[i].addr != FAILED_ADDR) && (env_cache_table[i].name_crc == name_crc)) {
  349. char saved_name[EF_ENV_NAME_MAX];
  350. /* read the ENV name in flash */
  351. ef_port_read(env_cache_table[i].addr + ENV_HDR_DATA_SIZE, (uint32_t *) saved_name, EF_ENV_NAME_MAX);
  352. if (!strncmp(name, saved_name, name_len)) {
  353. *addr = env_cache_table[i].addr;
  354. if (env_cache_table[i].active >= 0xFFFF - EF_ENV_CACHE_TABLE_SIZE) {
  355. env_cache_table[i].active = 0xFFFF;
  356. } else {
  357. env_cache_table[i].active += EF_ENV_CACHE_TABLE_SIZE;
  358. }
  359. return true;
  360. }
  361. }
  362. }
  363. return false;
  364. }
  365. #endif /* EF_ENV_USING_CACHE */
  366. /*
  367. * find the continue 0xFF flash address to end address
  368. */
  369. static uint32_t continue_ff_addr(uint32_t start, uint32_t end)
  370. {
  371. uint8_t buf[32], last_data = 0x00;
  372. size_t i, addr = start, read_size;
  373. for (; start < end; start += sizeof(buf)) {
  374. if (start + sizeof(buf) < end) {
  375. read_size = sizeof(buf);
  376. } else {
  377. read_size = end - start;
  378. }
  379. ef_port_read(start, (uint32_t *) buf, read_size);
  380. for (i = 0; i < read_size; i++) {
  381. if (last_data != 0xFF && buf[i] == 0xFF) {
  382. addr = start + i;
  383. }
  384. last_data = buf[i];
  385. }
  386. }
  387. if (last_data == 0xFF) {
  388. return EF_WG_ALIGN(addr);
  389. } else {
  390. return end;
  391. }
  392. }
  393. /*
  394. * find the next ENV address by magic word on the flash
  395. */
  396. static uint32_t find_next_env_addr(uint32_t start, uint32_t end)
  397. {
  398. uint8_t buf[32];
  399. uint32_t start_bak = start, i;
  400. uint32_t magic;
  401. #ifdef EF_ENV_USING_CACHE
  402. uint32_t empty_env;
  403. if (get_sector_from_cache(EF_ALIGN_DOWN(start, SECTOR_SIZE), &empty_env) && start == empty_env) {
  404. return FAILED_ADDR;
  405. }
  406. #endif /* EF_ENV_USING_CACHE */
  407. for (; start < end; start += (sizeof(buf) - sizeof(uint32_t))) {
  408. ef_port_read(start, (uint32_t *) buf, sizeof(buf));
  409. for (i = 0; i < sizeof(buf) - sizeof(uint32_t) && start + i < end; i++) {
  410. #ifndef EF_BIG_ENDIAN /* Little Endian Order */
  411. magic = buf[i] + (buf[i + 1] << 8) + (buf[i + 2] << 16) + (buf[i + 3] << 24);
  412. #else /* Big Endian Order */
  413. magic = buf[i + 3] + (buf[i + 2] << 8) + (buf[i + 1] << 16) + (buf[i] << 24);
  414. #endif
  415. if (magic == ENV_MAGIC_WORD && (start + i - ENV_MAGIC_OFFSET) >= start_bak) {
  416. return start + i - ENV_MAGIC_OFFSET;
  417. }
  418. }
  419. }
  420. return FAILED_ADDR;
  421. }
  422. static uint32_t get_next_env_addr(sector_meta_data_t sector, env_node_obj_t pre_env)
  423. {
  424. uint32_t addr = FAILED_ADDR;
  425. if (sector->status.store == SECTOR_STORE_EMPTY) {
  426. return FAILED_ADDR;
  427. }
  428. if (pre_env->addr.start == FAILED_ADDR) {
  429. /* the first ENV address */
  430. addr = sector->addr + SECTOR_HDR_DATA_SIZE;
  431. } else {
  432. if (pre_env->addr.start <= sector->addr + SECTOR_SIZE) {
  433. if (pre_env->crc_is_ok) {
  434. addr = pre_env->addr.start + pre_env->len;
  435. } else {
  436. /* when pre_env CRC check failed, maybe the flash has error data
  437. * find_next_env_addr after pre_env address */
  438. addr = pre_env->addr.start + EF_WG_ALIGN(1);
  439. }
  440. /* check and find next ENV address */
  441. addr = find_next_env_addr(addr, sector->addr + SECTOR_SIZE - SECTOR_HDR_DATA_SIZE);
  442. if (addr > sector->addr + SECTOR_SIZE || pre_env->len == 0) {
  443. //TODO ��������ģʽ
  444. return FAILED_ADDR;
  445. }
  446. } else {
  447. /* no ENV */
  448. return FAILED_ADDR;
  449. }
  450. }
  451. return addr;
  452. }
  453. static EfErrCode read_env(env_node_obj_t env)
  454. {
  455. struct env_hdr_data env_hdr;
  456. uint8_t buf[32];
  457. uint32_t calc_crc32 = 0, crc_data_len, env_name_addr;
  458. EfErrCode result = EF_NO_ERR;
  459. size_t len, size;
  460. /* read ENV header raw data */
  461. ef_port_read(env->addr.start, (uint32_t *)&env_hdr, sizeof(struct env_hdr_data));
  462. env->status = (env_status_t) get_status(env_hdr.status_table, ENV_STATUS_NUM);
  463. env->len = env_hdr.len;
  464. if (env->len == ~0UL || env->len > ENV_AREA_SIZE || env->len < ENV_NAME_LEN_OFFSET) {
  465. /* the ENV length was not write, so reserved the meta data for current ENV */
  466. env->len = ENV_HDR_DATA_SIZE;
  467. if (env->status != ENV_ERR_HDR) {
  468. env->status = ENV_ERR_HDR;
  469. EF_DEBUG("Error: The ENV @0x%08X length has an error.\n", env->addr.start);
  470. write_status(env->addr.start, env_hdr.status_table, ENV_STATUS_NUM, ENV_ERR_HDR);
  471. }
  472. env->crc_is_ok = false;
  473. return EF_READ_ERR;
  474. } else if (env->len > SECTOR_SIZE - SECTOR_HDR_DATA_SIZE && env->len < ENV_AREA_SIZE) {
  475. //TODO ��������ģʽ������д�볤��û��д������
  476. EF_ASSERT(0);
  477. }
  478. /* CRC32 data len(header.name_len + header.value_len + name + value) */
  479. crc_data_len = env->len - ENV_NAME_LEN_OFFSET;
  480. /* calculate the CRC32 value */
  481. for (len = 0, size = 0; len < crc_data_len; len += size) {
  482. if (len + sizeof(buf) < crc_data_len) {
  483. size = sizeof(buf);
  484. } else {
  485. size = crc_data_len - len;
  486. }
  487. ef_port_read(env->addr.start + ENV_NAME_LEN_OFFSET + len, (uint32_t *) buf, EF_WG_ALIGN(size));
  488. calc_crc32 = ef_calc_crc32(calc_crc32, buf, size);
  489. }
  490. /* check CRC32 */
  491. if (calc_crc32 != env_hdr.crc32) {
  492. env->crc_is_ok = false;
  493. result = EF_READ_ERR;
  494. } else {
  495. env->crc_is_ok = true;
  496. /* the name is behind aligned ENV header */
  497. env_name_addr = env->addr.start + ENV_HDR_DATA_SIZE;
  498. ef_port_read(env_name_addr, (uint32_t *) env->name, EF_WG_ALIGN(env_hdr.name_len));
  499. /* the value is behind aligned name */
  500. env->addr.value = env_name_addr + EF_WG_ALIGN(env_hdr.name_len);
  501. env->value_len = env_hdr.value_len;
  502. env->name_len = env_hdr.name_len;
  503. }
  504. return result;
  505. }
  506. static EfErrCode read_sector_meta_data(uint32_t addr, sector_meta_data_t sector, bool traversal)
  507. {
  508. EfErrCode result = EF_NO_ERR;
  509. struct sector_hdr_data sec_hdr;
  510. EF_ASSERT(addr % SECTOR_SIZE == 0);
  511. EF_ASSERT(sector);
  512. /* read sector header raw data */
  513. ef_port_read(addr, (uint32_t *)&sec_hdr, sizeof(struct sector_hdr_data));
  514. sector->addr = addr;
  515. sector->magic = sec_hdr.magic;
  516. /* check magic word */
  517. if (sector->magic != SECTOR_MAGIC_WORD) {
  518. sector->check_ok = false;
  519. sector->combined = SECTOR_NOT_COMBINED;
  520. return EF_ENV_INIT_FAILED;
  521. }
  522. sector->check_ok = true;
  523. /* get other sector meta data */
  524. sector->combined = sec_hdr.combined;
  525. sector->status.store = (sector_store_status_t) get_status(sec_hdr.status_table.store, SECTOR_STORE_STATUS_NUM);
  526. sector->status.dirty = (sector_dirty_status_t) get_status(sec_hdr.status_table.dirty, SECTOR_DIRTY_STATUS_NUM);
  527. /* traversal all ENV and calculate the remain space size */
  528. if (traversal) {
  529. sector->remain = 0;
  530. sector->empty_env = sector->addr + SECTOR_HDR_DATA_SIZE;
  531. if (sector->status.store == SECTOR_STORE_EMPTY) {
  532. sector->remain = SECTOR_SIZE - SECTOR_HDR_DATA_SIZE;
  533. } else if (sector->status.store == SECTOR_STORE_USING) {
  534. struct env_node_obj env_meta;
  535. #ifdef EF_ENV_USING_CACHE
  536. if (get_sector_from_cache(addr, &sector->empty_env)) {
  537. sector->remain = SECTOR_SIZE - (sector->empty_env - sector->addr);
  538. return result;
  539. }
  540. #endif /* EF_ENV_USING_CACHE */
  541. sector->remain = SECTOR_SIZE - SECTOR_HDR_DATA_SIZE;
  542. env_meta.addr.start = FAILED_ADDR;
  543. while ((env_meta.addr.start = get_next_env_addr(sector, &env_meta)) != FAILED_ADDR) {
  544. read_env(&env_meta);
  545. if (!env_meta.crc_is_ok) {
  546. if (env_meta.status != ENV_PRE_WRITE && env_meta.status!= ENV_ERR_HDR) {
  547. EF_INFO("Error: The ENV (@0x%08X) CRC32 check failed!\n", env_meta.addr.start);
  548. sector->remain = 0;
  549. result = EF_READ_ERR;
  550. break;
  551. }
  552. }
  553. sector->empty_env += env_meta.len;
  554. sector->remain -= env_meta.len;
  555. }
  556. /* check the empty ENV address by read continue 0xFF on flash */
  557. {
  558. uint32_t ff_addr;
  559. ff_addr = continue_ff_addr(sector->empty_env, sector->addr + SECTOR_SIZE);
  560. /* check the flash data is clean */
  561. if (sector->empty_env != ff_addr) {
  562. /* update the sector information */
  563. sector->empty_env = ff_addr;
  564. sector->remain = SECTOR_SIZE - (ff_addr - sector->addr);
  565. }
  566. }
  567. #ifdef EF_ENV_USING_CACHE
  568. update_sector_cache(sector->addr, sector->empty_env);
  569. #endif
  570. }
  571. }
  572. return result;
  573. }
  574. static uint32_t get_next_sector_addr(sector_meta_data_t pre_sec)
  575. {
  576. uint32_t next_addr;
  577. if (pre_sec->addr == FAILED_ADDR) {
  578. return env_start_addr;
  579. } else {
  580. /* check ENV sector combined */
  581. if (pre_sec->combined == SECTOR_NOT_COMBINED) {
  582. next_addr = pre_sec->addr + SECTOR_SIZE;
  583. } else {
  584. next_addr = pre_sec->addr + pre_sec->combined * SECTOR_SIZE;
  585. }
  586. /* check range */
  587. if (next_addr < env_start_addr + ENV_AREA_SIZE) {
  588. return next_addr;
  589. } else {
  590. /* no sector */
  591. return FAILED_ADDR;
  592. }
  593. }
  594. }
  595. static void env_iterator(env_node_obj_t env, void *arg1, void *arg2,
  596. bool (*callback)(env_node_obj_t env, void *arg1, void *arg2))
  597. {
  598. struct sector_meta_data sector;
  599. uint32_t sec_addr;
  600. sector.addr = FAILED_ADDR;
  601. /* search all sectors */
  602. while ((sec_addr = get_next_sector_addr(&sector)) != FAILED_ADDR) {
  603. if (read_sector_meta_data(sec_addr, &sector, false) != EF_NO_ERR) {
  604. continue;
  605. }
  606. if (callback == NULL) {
  607. continue;
  608. }
  609. /* sector has ENV */
  610. if (sector.status.store == SECTOR_STORE_USING || sector.status.store == SECTOR_STORE_FULL) {
  611. env->addr.start = FAILED_ADDR;
  612. /* search all ENV */
  613. while ((env->addr.start = get_next_env_addr(&sector, env)) != FAILED_ADDR) {
  614. read_env(env);
  615. /* iterator is interrupted when callback return true */
  616. if (callback(env, arg1, arg2)) {
  617. return;
  618. }
  619. }
  620. }
  621. }
  622. }
  623. static bool find_env_cb(env_node_obj_t env, void *arg1, void *arg2)
  624. {
  625. const char *key = arg1;
  626. bool *find_ok = arg2;
  627. size_t key_len = strlen(key);
  628. if (key_len != env->name_len) {
  629. return false;
  630. }
  631. /* check ENV */
  632. if (env->crc_is_ok && env->status == ENV_WRITE && !strncmp(env->name, key, key_len)) {
  633. *find_ok = true;
  634. return true;
  635. }
  636. return false;
  637. }
  638. static bool find_env_no_cache(const char *key, env_node_obj_t env)
  639. {
  640. bool find_ok = false;
  641. env_iterator(env, (void *)key, &find_ok, find_env_cb);
  642. return find_ok;
  643. }
  644. static bool find_env(const char *key, env_node_obj_t env)
  645. {
  646. bool find_ok = false;
  647. #ifdef EF_ENV_USING_CACHE
  648. size_t key_len = strlen(key);
  649. if (get_env_from_cache(key, key_len, &env->addr.start)) {
  650. read_env(env);
  651. return true;
  652. }
  653. #endif /* EF_ENV_USING_CACHE */
  654. find_ok = find_env_no_cache(key, env);
  655. #ifdef EF_ENV_USING_CACHE
  656. if (find_ok) {
  657. update_env_cache(key, key_len, env->addr.start);
  658. }
  659. #endif /* EF_ENV_USING_CACHE */
  660. return find_ok;
  661. }
  662. static bool ef_is_str(uint8_t *value, size_t len)
  663. {
  664. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  665. size_t i;
  666. for (i = 0; i < len; i++) {
  667. if (!__is_print(value[i])) {
  668. return false;
  669. }
  670. }
  671. return true;
  672. }
  673. static size_t get_env(const char *key, void *value_buf, size_t buf_len, size_t *value_len)
  674. {
  675. struct env_node_obj env;
  676. size_t read_len = 0;
  677. if (find_env(key, &env)) {
  678. if (value_len) {
  679. *value_len = env.value_len;
  680. }
  681. if (buf_len > env.value_len) {
  682. read_len = env.value_len;
  683. } else {
  684. read_len = buf_len;
  685. }
  686. if (value_buf){
  687. ef_port_read(env.addr.value, (uint32_t *) value_buf, read_len);
  688. }
  689. } else if (value_len) {
  690. *value_len = 0;
  691. }
  692. return read_len;
  693. }
  694. /**
  695. * Get a ENV object by key name
  696. *
  697. * @param key ENV name
  698. * @param env ENV object
  699. *
  700. * @return TRUE: find the ENV is OK, else return false
  701. */
  702. bool ef_get_env_obj(const char *key, env_node_obj_t env)
  703. {
  704. bool find_ok = false;
  705. if (!init_ok) {
  706. EF_INFO("ENV isn't initialize OK.\n");
  707. return 0;
  708. }
  709. /* lock the ENV cache */
  710. ef_port_env_lock();
  711. find_ok = find_env(key, env);
  712. /* unlock the ENV cache */
  713. ef_port_env_unlock();
  714. return find_ok;
  715. }
  716. /**
  717. * Get a blob ENV value by key name.
  718. *
  719. * @param key ENV name
  720. * @param value_buf ENV blob buffer
  721. * @param buf_len ENV blob buffer length
  722. * @param saved_value_len return the length of the value saved on the flash, 0: NOT found
  723. *
  724. * @return the actually get size on successful
  725. */
  726. size_t ef_get_env_blob(const char *key, void *value_buf, size_t buf_len, size_t *saved_value_len)
  727. {
  728. size_t read_len = 0;
  729. if (!init_ok) {
  730. EF_INFO("ENV isn't initialize OK.\n");
  731. return 0;
  732. }
  733. /* lock the ENV cache */
  734. ef_port_env_lock();
  735. read_len = get_env(key, value_buf, buf_len, saved_value_len);
  736. /* unlock the ENV cache */
  737. ef_port_env_unlock();
  738. return read_len;
  739. }
  740. /**
  741. * Get an ENV value by key name.
  742. *
  743. * @note this function is NOT supported reentrant
  744. * @note this function is DEPRECATED
  745. *
  746. * @param key ENV name
  747. *
  748. * @return value
  749. */
  750. char *ef_get_env(const char *key)
  751. {
  752. static char value[EF_STR_ENV_VALUE_MAX_SIZE + 1];
  753. size_t get_size;
  754. if ((get_size = ef_get_env_blob(key, value, EF_STR_ENV_VALUE_MAX_SIZE, NULL)) > 0) {
  755. /* the return value must be string */
  756. if (ef_is_str((uint8_t *)value, get_size)) {
  757. value[get_size] = '\0';
  758. return value;
  759. } else {
  760. EF_INFO("Warning: The ENV value isn't string. Could not be returned\n");
  761. return NULL;
  762. }
  763. }
  764. return NULL;
  765. }
  766. /**
  767. * read the ENV value by ENV object
  768. *
  769. * @param env ENV object
  770. * @param value_buf the buffer for store ENV value
  771. * @param buf_len buffer length
  772. *
  773. * @return the actually read size on successful
  774. */
  775. size_t ef_read_env_value(env_node_obj_t env, uint8_t *value_buf, size_t buf_len)
  776. {
  777. size_t read_len = 0;
  778. EF_ASSERT(env);
  779. EF_ASSERT(value_buf);
  780. if (!init_ok) {
  781. EF_INFO("ENV isn't initialize OK.\n");
  782. return 0;
  783. }
  784. if (env->crc_is_ok) {
  785. /* lock the ENV cache */
  786. ef_port_env_lock();
  787. if (buf_len > env->value_len) {
  788. read_len = env->value_len;
  789. } else {
  790. read_len = buf_len;
  791. }
  792. ef_port_read(env->addr.value, (uint32_t *) value_buf, read_len);
  793. /* unlock the ENV cache */
  794. ef_port_env_unlock();
  795. }
  796. return read_len;
  797. }
  798. static EfErrCode write_env_hdr(uint32_t addr, env_hdr_data_t env_hdr) {
  799. EfErrCode result = EF_NO_ERR;
  800. /* write the status will by write granularity */
  801. result = write_status(addr, env_hdr->status_table, ENV_STATUS_NUM, ENV_PRE_WRITE);
  802. if (result != EF_NO_ERR) {
  803. return result;
  804. }
  805. /* write other header data */
  806. result = ef_port_write(addr + ENV_MAGIC_OFFSET, &env_hdr->magic, sizeof(struct env_hdr_data) - ENV_MAGIC_OFFSET);
  807. return result;
  808. }
  809. static EfErrCode format_sector(uint32_t addr, uint32_t combined_value)
  810. {
  811. EfErrCode result = EF_NO_ERR;
  812. struct sector_hdr_data sec_hdr;
  813. EF_ASSERT(addr % SECTOR_SIZE == 0);
  814. result = ef_port_erase(addr, SECTOR_SIZE);
  815. if (result == EF_NO_ERR) {
  816. /* initialize the header data */
  817. memset(&sec_hdr, 0xFF, sizeof(struct sector_hdr_data));
  818. set_status(sec_hdr.status_table.store, SECTOR_STORE_STATUS_NUM, SECTOR_STORE_EMPTY);
  819. set_status(sec_hdr.status_table.dirty, SECTOR_DIRTY_STATUS_NUM, SECTOR_DIRTY_FALSE);
  820. sec_hdr.magic = SECTOR_MAGIC_WORD;
  821. sec_hdr.combined = combined_value;
  822. sec_hdr.reserved = 0xFFFFFFFF;
  823. /* save the header */
  824. result = ef_port_write(addr, (uint32_t *)&sec_hdr, sizeof(struct sector_hdr_data));
  825. #ifdef EF_ENV_USING_CACHE
  826. /* delete the sector cache */
  827. update_sector_cache(addr, addr + SECTOR_SIZE);
  828. #endif /* EF_ENV_USING_CACHE */
  829. }
  830. return result;
  831. }
  832. static EfErrCode update_sec_status(sector_meta_data_t sector, size_t new_env_len, bool *is_full)
  833. {
  834. uint8_t status_table[STORE_STATUS_TABLE_SIZE];
  835. EfErrCode result = EF_NO_ERR;
  836. /* change the current sector status */
  837. if (sector->status.store == SECTOR_STORE_EMPTY) {
  838. /* change the sector status to using */
  839. result = write_status(sector->addr, status_table, SECTOR_STORE_STATUS_NUM, SECTOR_STORE_USING);
  840. } else if (sector->status.store == SECTOR_STORE_USING) {
  841. /* check remain size */
  842. if (sector->remain < EF_SEC_REMAIN_THRESHOLD || sector->remain - new_env_len < EF_SEC_REMAIN_THRESHOLD) {
  843. /* change the sector status to full */
  844. result = write_status(sector->addr, status_table, SECTOR_STORE_STATUS_NUM, SECTOR_STORE_FULL);
  845. #ifdef EF_ENV_USING_CACHE
  846. /* delete the sector cache */
  847. update_sector_cache(sector->addr, sector->addr + SECTOR_SIZE);
  848. #endif /* EF_ENV_USING_CACHE */
  849. if (is_full) {
  850. *is_full = true;
  851. }
  852. } else if (is_full) {
  853. *is_full = false;
  854. }
  855. }
  856. return result;
  857. }
  858. static void sector_iterator(sector_meta_data_t sector, sector_store_status_t status, void *arg1, void *arg2,
  859. bool (*callback)(sector_meta_data_t sector, void *arg1, void *arg2), bool traversal_env) {
  860. uint32_t sec_addr;
  861. /* search all sectors */
  862. sector->addr = FAILED_ADDR;
  863. while ((sec_addr = get_next_sector_addr(sector)) != FAILED_ADDR) {
  864. read_sector_meta_data(sec_addr, sector, false);
  865. if (status == SECTOR_STORE_UNUSED || status == sector->status.store) {
  866. if (traversal_env) {
  867. read_sector_meta_data(sec_addr, sector, traversal_env);
  868. }
  869. /* iterator is interrupted when callback return true */
  870. if (callback && callback(sector, arg1, arg2)) {
  871. return;
  872. }
  873. }
  874. }
  875. }
  876. static bool sector_statistics_cb(sector_meta_data_t sector, void *arg1, void *arg2)
  877. {
  878. size_t *empty_sector = arg1, *using_sector = arg2;
  879. if (sector->check_ok && sector->status.store == SECTOR_STORE_EMPTY) {
  880. (*empty_sector)++;
  881. } else if (sector->check_ok && sector->status.store == SECTOR_STORE_USING) {
  882. (*using_sector)++;
  883. }
  884. return false;
  885. }
  886. static bool alloc_env_cb(sector_meta_data_t sector, void *arg1, void *arg2)
  887. {
  888. size_t *env_size = arg1;
  889. uint32_t *empty_env = arg2;
  890. /* 1. sector has space
  891. * 2. the NO dirty sector
  892. * 3. the dirty sector only when the gc_request is false */
  893. if (sector->check_ok && sector->remain > *env_size
  894. && ((sector->status.dirty == SECTOR_DIRTY_FALSE)
  895. || (sector->status.dirty == SECTOR_DIRTY_TRUE && !gc_request))) {
  896. *empty_env = sector->empty_env;
  897. return true;
  898. }
  899. return false;
  900. }
  901. static uint32_t alloc_env(sector_meta_data_t sector, size_t env_size)
  902. {
  903. uint32_t empty_env = FAILED_ADDR;
  904. size_t empty_sector = 0, using_sector = 0;
  905. /* sector status statistics */
  906. sector_iterator(sector, SECTOR_STORE_UNUSED, &empty_sector, &using_sector, sector_statistics_cb, false);
  907. if (using_sector > 0) {
  908. /* alloc the ENV from the using status sector first */
  909. sector_iterator(sector, SECTOR_STORE_USING, &env_size, &empty_env, alloc_env_cb, true);
  910. }
  911. if (empty_sector > 0 && empty_env == FAILED_ADDR) {
  912. if (empty_sector > EF_GC_EMPTY_SEC_THRESHOLD || gc_request) {
  913. sector_iterator(sector, SECTOR_STORE_EMPTY, &env_size, &empty_env, alloc_env_cb, true);
  914. } else {
  915. /* no space for new ENV now will GC and retry */
  916. EF_DEBUG("Trigger a GC check after alloc ENV failed.\n");
  917. gc_request = true;
  918. }
  919. }
  920. return empty_env;
  921. }
  922. static EfErrCode del_env(const char *key, env_node_obj_t old_env, bool complete_del) {
  923. EfErrCode result = EF_NO_ERR;
  924. uint32_t dirty_status_addr;
  925. static bool last_is_complete_del = false;
  926. #if (ENV_STATUS_TABLE_SIZE >= DIRTY_STATUS_TABLE_SIZE)
  927. uint8_t status_table[ENV_STATUS_TABLE_SIZE];
  928. #else
  929. uint8_t status_table[DIRTY_STATUS_TABLE_SIZE];
  930. #endif
  931. /* need find ENV */
  932. if (!old_env) {
  933. struct env_node_obj env;
  934. /* find ENV */
  935. if (find_env(key, &env)) {
  936. old_env = &env;
  937. } else {
  938. EF_DEBUG("Not found '%s' in ENV.\n", key);
  939. return EF_ENV_NAME_ERR;
  940. }
  941. }
  942. /* change and save the new status */
  943. if (!complete_del) {
  944. result = write_status(old_env->addr.start, status_table, ENV_STATUS_NUM, ENV_PRE_DELETE);
  945. last_is_complete_del = true;
  946. } else {
  947. result = write_status(old_env->addr.start, status_table, ENV_STATUS_NUM, ENV_DELETED);
  948. if (!last_is_complete_del && result == EF_NO_ERR) {
  949. #ifdef EF_ENV_USING_CACHE
  950. /* delete the ENV in flash and cache */
  951. if (key != NULL) {
  952. /* when using del_env(key, NULL, true) or del_env(key, env, true) in ef_del_env() and set_env() */
  953. update_env_cache(key, strlen(key), FAILED_ADDR);
  954. } else if (old_env != NULL) {
  955. /* when using del_env(NULL, env, true) in move_env() */
  956. update_env_cache(old_env->name, old_env->name_len, FAILED_ADDR);
  957. }
  958. #endif /* EF_ENV_USING_CACHE */
  959. }
  960. last_is_complete_del = false;
  961. }
  962. dirty_status_addr = EF_ALIGN_DOWN(old_env->addr.start, SECTOR_SIZE) + SECTOR_DIRTY_OFFSET;
  963. /* read and change the sector dirty status */
  964. if (result == EF_NO_ERR
  965. && read_status(dirty_status_addr, status_table, SECTOR_DIRTY_STATUS_NUM) == SECTOR_DIRTY_FALSE) {
  966. result = write_status(dirty_status_addr, status_table, SECTOR_DIRTY_STATUS_NUM, SECTOR_DIRTY_TRUE);
  967. }
  968. return result;
  969. }
  970. /*
  971. * move the ENV to new space
  972. */
  973. static EfErrCode move_env(env_node_obj_t env)
  974. {
  975. EfErrCode result = EF_NO_ERR;
  976. uint8_t status_table[ENV_STATUS_TABLE_SIZE];
  977. uint32_t env_addr;
  978. struct sector_meta_data sector;
  979. /* prepare to delete the current ENV */
  980. if (env->status == ENV_WRITE) {
  981. del_env(NULL, env, false);
  982. }
  983. if ((env_addr = alloc_env(&sector, env->len)) != FAILED_ADDR) {
  984. if (in_recovery_check) {
  985. struct env_node_obj env_bak;
  986. char name[EF_ENV_NAME_MAX + 1] = { 0 };
  987. strncpy(name, env->name, env->name_len);
  988. /* check the ENV in flash is already create success */
  989. if (find_env_no_cache(name, &env_bak)) {
  990. /* already create success, don't need to duplicate */
  991. result = EF_NO_ERR;
  992. goto __exit;
  993. }
  994. }
  995. } else {
  996. return EF_ENV_FULL;
  997. }
  998. /* start move the ENV */
  999. {
  1000. uint8_t buf[32];
  1001. size_t len, size, env_len = env->len;
  1002. /* update the new ENV sector status first */
  1003. update_sec_status(&sector, env->len, NULL);
  1004. write_status(env_addr, status_table, ENV_STATUS_NUM, ENV_PRE_WRITE);
  1005. env_len -= ENV_MAGIC_OFFSET;
  1006. for (len = 0, size = 0; len < env_len; len += size) {
  1007. if (len + sizeof(buf) < env_len) {
  1008. size = sizeof(buf);
  1009. } else {
  1010. size = env_len - len;
  1011. }
  1012. ef_port_read(env->addr.start + ENV_MAGIC_OFFSET + len, (uint32_t *) buf, EF_WG_ALIGN(size));
  1013. result = ef_port_write(env_addr + ENV_MAGIC_OFFSET + len, (uint32_t *) buf, size);
  1014. }
  1015. write_status(env_addr, status_table, ENV_STATUS_NUM, ENV_WRITE);
  1016. #ifdef EF_ENV_USING_CACHE
  1017. update_sector_cache(EF_ALIGN_DOWN(env_addr, SECTOR_SIZE),
  1018. env_addr + ENV_HDR_DATA_SIZE + EF_WG_ALIGN(env->name_len) + EF_WG_ALIGN(env->value_len));
  1019. update_env_cache(env->name, env->name_len, env_addr);
  1020. #endif /* EF_ENV_USING_CACHE */
  1021. }
  1022. //EF_DEBUG("Moved the ENV (%.*s) from 0x%08X to 0x%08X.\n", env->name_len, env->name, env->addr.start, env_addr);
  1023. __exit:
  1024. del_env(NULL, env, true);
  1025. return result;
  1026. }
  1027. static uint32_t new_env(sector_meta_data_t sector, size_t env_size)
  1028. {
  1029. bool already_gc = false;
  1030. uint32_t empty_env = FAILED_ADDR;
  1031. __retry:
  1032. if ((empty_env = alloc_env(sector, env_size)) == FAILED_ADDR && gc_request && !already_gc) {
  1033. EF_DEBUG("Warning: Alloc an ENV (size %d) failed when new ENV. Now will GC then retry.\n", env_size);
  1034. gc_collect();
  1035. already_gc = true;
  1036. goto __retry;
  1037. }
  1038. return empty_env;
  1039. }
  1040. static uint32_t new_env_by_kv(sector_meta_data_t sector, size_t key_len, size_t buf_len)
  1041. {
  1042. size_t env_len = ENV_HDR_DATA_SIZE + EF_WG_ALIGN(key_len) + EF_WG_ALIGN(buf_len);
  1043. return new_env(sector, env_len);
  1044. }
  1045. static bool gc_check_cb(sector_meta_data_t sector, void *arg1, void *arg2)
  1046. {
  1047. size_t *empty_sec = arg1;
  1048. if (sector->check_ok) {
  1049. *empty_sec = *empty_sec + 1;
  1050. }
  1051. return false;
  1052. }
  1053. static bool do_gc(sector_meta_data_t sector, void *arg1, void *arg2)
  1054. {
  1055. struct env_node_obj env;
  1056. if (sector->check_ok && (sector->status.dirty == SECTOR_DIRTY_TRUE || sector->status.dirty == SECTOR_DIRTY_GC)) {
  1057. uint8_t status_table[DIRTY_STATUS_TABLE_SIZE];
  1058. /* change the sector status to GC */
  1059. write_status(sector->addr + SECTOR_DIRTY_OFFSET, status_table, SECTOR_DIRTY_STATUS_NUM, SECTOR_DIRTY_GC);
  1060. /* search all ENV */
  1061. env.addr.start = FAILED_ADDR;
  1062. while ((env.addr.start = get_next_env_addr(sector, &env)) != FAILED_ADDR) {
  1063. read_env(&env);
  1064. if (env.crc_is_ok && (env.status == ENV_WRITE || env.status == ENV_PRE_DELETE)) {
  1065. /* move the ENV to new space */
  1066. if (move_env(&env) != EF_NO_ERR) {
  1067. EF_DEBUG("Error: Moved the ENV (%.*s) for GC failed.\n", env.name_len, env.name);
  1068. }
  1069. }
  1070. }
  1071. format_sector(sector->addr, SECTOR_NOT_COMBINED);
  1072. EF_DEBUG("Collect a sector @0x%08X\n", sector->addr);
  1073. }
  1074. return false;
  1075. }
  1076. /*
  1077. * The GC will be triggered on the following scene:
  1078. * 1. alloc an ENV when the flash not has enough space
  1079. * 2. write an ENV then the flash not has enough space
  1080. */
  1081. static void gc_collect(void)
  1082. {
  1083. struct sector_meta_data sector;
  1084. size_t empty_sec = 0;
  1085. /* GC check the empty sector number */
  1086. sector_iterator(&sector, SECTOR_STORE_EMPTY, &empty_sec, NULL, gc_check_cb, false);
  1087. /* do GC collect */
  1088. EF_DEBUG("The remain empty sector is %d, GC threshold is %d.\n", empty_sec, EF_GC_EMPTY_SEC_THRESHOLD);
  1089. if (empty_sec <= EF_GC_EMPTY_SEC_THRESHOLD) {
  1090. sector_iterator(&sector, SECTOR_STORE_UNUSED, NULL, NULL, do_gc, false);
  1091. }
  1092. gc_request = false;
  1093. }
  1094. static EfErrCode align_write(uint32_t addr, const uint32_t *buf, size_t size)
  1095. {
  1096. EfErrCode result = EF_NO_ERR;
  1097. size_t align_remain;
  1098. #if (EF_WRITE_GRAN / 8 > 0)
  1099. uint8_t align_data[EF_WRITE_GRAN / 8];
  1100. size_t align_data_size = sizeof(align_data);
  1101. #else
  1102. /* For compatibility with C89 */
  1103. uint8_t align_data_u8, *align_data = &align_data_u8;
  1104. size_t align_data_size = 1;
  1105. #endif
  1106. memset(align_data, 0xFF, align_data_size);
  1107. result = ef_port_write(addr, buf, EF_WG_ALIGN_DOWN(size));
  1108. align_remain = size - EF_WG_ALIGN_DOWN(size);
  1109. if (result == EF_NO_ERR && align_remain) {
  1110. memcpy(align_data, (uint8_t *)buf + EF_WG_ALIGN_DOWN(size), align_remain);
  1111. result = ef_port_write(addr + EF_WG_ALIGN_DOWN(size), (uint32_t *) align_data, align_data_size);
  1112. }
  1113. return result;
  1114. }
  1115. static EfErrCode create_env_blob(sector_meta_data_t sector, const char *key, const void *value, size_t len)
  1116. {
  1117. EfErrCode result = EF_NO_ERR;
  1118. struct env_hdr_data env_hdr;
  1119. bool is_full = false;
  1120. uint32_t env_addr = sector->empty_env;
  1121. if (strlen(key) > EF_ENV_NAME_MAX) {
  1122. EF_INFO("Error: The ENV name length is more than %d\n", EF_ENV_NAME_MAX);
  1123. return EF_ENV_NAME_ERR;
  1124. }
  1125. memset(&env_hdr, 0xFF, sizeof(struct env_hdr_data));
  1126. env_hdr.magic = ENV_MAGIC_WORD;
  1127. env_hdr.name_len = strlen(key);
  1128. env_hdr.value_len = len;
  1129. env_hdr.len = ENV_HDR_DATA_SIZE + EF_WG_ALIGN(env_hdr.name_len) + EF_WG_ALIGN(env_hdr.value_len);
  1130. if (env_hdr.len > SECTOR_SIZE - SECTOR_HDR_DATA_SIZE) {
  1131. EF_INFO("Error: The ENV size is too big\n");
  1132. return EF_ENV_FULL;
  1133. }
  1134. if (env_addr != FAILED_ADDR || (env_addr = new_env(sector, env_hdr.len)) != FAILED_ADDR) {
  1135. size_t align_remain;
  1136. /* update the sector status */
  1137. if (result == EF_NO_ERR) {
  1138. result = update_sec_status(sector, env_hdr.len, &is_full);
  1139. }
  1140. if (result == EF_NO_ERR) {
  1141. uint8_t ff = 0xFF;
  1142. /* start calculate CRC32 */
  1143. env_hdr.crc32 = ef_calc_crc32(0, &env_hdr.name_len, ENV_HDR_DATA_SIZE - ENV_NAME_LEN_OFFSET);
  1144. env_hdr.crc32 = ef_calc_crc32(env_hdr.crc32, key, env_hdr.name_len);
  1145. align_remain = EF_WG_ALIGN(env_hdr.name_len) - env_hdr.name_len;
  1146. while (align_remain--) {
  1147. env_hdr.crc32 = ef_calc_crc32(env_hdr.crc32, &ff, 1);
  1148. }
  1149. env_hdr.crc32 = ef_calc_crc32(env_hdr.crc32, value, env_hdr.value_len);
  1150. align_remain = EF_WG_ALIGN(env_hdr.value_len) - env_hdr.value_len;
  1151. while (align_remain--) {
  1152. env_hdr.crc32 = ef_calc_crc32(env_hdr.crc32, &ff, 1);
  1153. }
  1154. /* write ENV header data */
  1155. result = write_env_hdr(env_addr, &env_hdr);
  1156. }
  1157. /* write key name */
  1158. if (result == EF_NO_ERR) {
  1159. result = align_write(env_addr + ENV_HDR_DATA_SIZE, (uint32_t *) key, env_hdr.name_len);
  1160. #ifdef EF_ENV_USING_CACHE
  1161. if (!is_full) {
  1162. update_sector_cache(sector->addr,
  1163. env_addr + ENV_HDR_DATA_SIZE + EF_WG_ALIGN(env_hdr.name_len) + EF_WG_ALIGN(env_hdr.value_len));
  1164. }
  1165. update_env_cache(key, env_hdr.name_len, env_addr);
  1166. #endif /* EF_ENV_USING_CACHE */
  1167. }
  1168. /* write value */
  1169. if (result == EF_NO_ERR) {
  1170. result = align_write(env_addr + ENV_HDR_DATA_SIZE + EF_WG_ALIGN(env_hdr.name_len), value,
  1171. env_hdr.value_len);
  1172. }
  1173. /* change the ENV status to ENV_WRITE */
  1174. if (result == EF_NO_ERR) {
  1175. result = write_status(env_addr, env_hdr.status_table, ENV_STATUS_NUM, ENV_WRITE);
  1176. }
  1177. /* trigger GC collect when current sector is full */
  1178. if (result == EF_NO_ERR && is_full) {
  1179. EF_DEBUG("Trigger a GC check after created ENV.\n");
  1180. gc_request = true;
  1181. }
  1182. } else {
  1183. result = EF_ENV_FULL;
  1184. }
  1185. return result;
  1186. }
  1187. /**
  1188. * Delete an ENV.
  1189. *
  1190. * @param key ENV name
  1191. *
  1192. * @return result
  1193. */
  1194. EfErrCode ef_del_env(const char *key)
  1195. {
  1196. EfErrCode result = EF_NO_ERR;
  1197. if (!init_ok) {
  1198. EF_INFO("Error: ENV isn't initialize OK.\n");
  1199. return EF_ENV_INIT_FAILED;
  1200. }
  1201. /* lock the ENV cache */
  1202. ef_port_env_lock();
  1203. result = del_env(key, NULL, true);
  1204. /* unlock the ENV cache */
  1205. ef_port_env_unlock();
  1206. return result;
  1207. }
  1208. /**
  1209. * The same to ef_del_env on this mode
  1210. * It's compatibility with older versions (less then V4.0).
  1211. *
  1212. * @note this function is DEPRECATED
  1213. *
  1214. * @param key ENV name
  1215. *
  1216. * @return result
  1217. */
  1218. EfErrCode ef_del_and_save_env(const char *key)
  1219. {
  1220. return ef_del_env(key);
  1221. }
  1222. static EfErrCode set_env(const char *key, const void *value_buf, size_t buf_len)
  1223. {
  1224. EfErrCode result = EF_NO_ERR;
  1225. static struct env_node_obj env;
  1226. static struct sector_meta_data sector;
  1227. bool env_is_found = false;
  1228. if (value_buf == NULL) {
  1229. result = del_env(key, NULL, true);
  1230. } else {
  1231. /* make sure the flash has enough space */
  1232. if (new_env_by_kv(&sector, strlen(key), buf_len) == FAILED_ADDR) {
  1233. return EF_ENV_FULL;
  1234. }
  1235. env_is_found = find_env(key, &env);
  1236. /* prepare to delete the old ENV */
  1237. if (env_is_found) {
  1238. result = del_env(key, &env, false);
  1239. }
  1240. /* create the new ENV */
  1241. if (result == EF_NO_ERR) {
  1242. result = create_env_blob(&sector, key, value_buf, buf_len);
  1243. }
  1244. /* delete the old ENV */
  1245. if (env_is_found && result == EF_NO_ERR) {
  1246. result = del_env(key, &env, true);
  1247. }
  1248. /* process the GC after set ENV */
  1249. if (gc_request) {
  1250. gc_collect();
  1251. }
  1252. }
  1253. return result;
  1254. }
  1255. /**
  1256. * Set a blob ENV. If it value is NULL, delete it.
  1257. * If not find it in flash, then create it.
  1258. *
  1259. * @param key ENV name
  1260. * @param value ENV value
  1261. * @param len ENV value length
  1262. *
  1263. * @return result
  1264. */
  1265. EfErrCode ef_set_env_blob(const char *key, const void *value_buf, size_t buf_len)
  1266. {
  1267. EfErrCode result = EF_NO_ERR;
  1268. if (!init_ok) {
  1269. EF_INFO("ENV isn't initialize OK.\n");
  1270. return EF_ENV_INIT_FAILED;
  1271. }
  1272. /* lock the ENV cache */
  1273. ef_port_env_lock();
  1274. result = set_env(key, value_buf, buf_len);
  1275. /* unlock the ENV cache */
  1276. ef_port_env_unlock();
  1277. return result;
  1278. }
  1279. /**
  1280. * Set a string ENV. If it value is NULL, delete it.
  1281. * If not find it in flash, then create it.
  1282. *
  1283. * @param key ENV name
  1284. * @param value ENV value
  1285. *
  1286. * @return result
  1287. */
  1288. EfErrCode ef_set_env(const char *key, const char *value)
  1289. {
  1290. return ef_set_env_blob(key, value, strlen(value));
  1291. }
  1292. /**
  1293. * The same to ef_set_env on this mode.
  1294. * It's compatibility with older versions (less then V4.0).
  1295. *
  1296. * @note this function is DEPRECATED
  1297. *
  1298. * @param key ENV name
  1299. * @param value ENV value
  1300. *
  1301. * @return result
  1302. */
  1303. EfErrCode ef_set_and_save_env(const char *key, const char *value)
  1304. {
  1305. return ef_set_env_blob(key, value, strlen(value));
  1306. }
  1307. /**
  1308. * Save ENV to flash.
  1309. *
  1310. * @note this function is DEPRECATED
  1311. */
  1312. EfErrCode ef_save_env(void)
  1313. {
  1314. /* do nothing not cur mode */
  1315. return EF_NO_ERR;
  1316. }
  1317. /**
  1318. * ENV set default.
  1319. *
  1320. * @return result
  1321. */
  1322. EfErrCode ef_env_set_default(void)
  1323. {
  1324. EfErrCode result = EF_NO_ERR;
  1325. uint32_t addr, i, value_len;
  1326. struct sector_meta_data sector;
  1327. EF_ASSERT(default_env_set);
  1328. EF_ASSERT(default_env_set_size);
  1329. /* lock the ENV cache */
  1330. ef_port_env_lock();
  1331. /* format all sectors */
  1332. for (addr = env_start_addr; addr < env_start_addr + ENV_AREA_SIZE; addr += SECTOR_SIZE) {
  1333. result = format_sector(addr, SECTOR_NOT_COMBINED);
  1334. if (result != EF_NO_ERR) {
  1335. goto __exit;
  1336. }
  1337. }
  1338. /* create default ENV */
  1339. for (i = 0; i < default_env_set_size; i++) {
  1340. /* It seems to be a string when value length is 0.
  1341. * This mechanism is for compatibility with older versions (less then V4.0). */
  1342. if (default_env_set[i].value_len == 0) {
  1343. value_len = strlen(default_env_set[i].value);
  1344. } else {
  1345. value_len = default_env_set[i].value_len;
  1346. }
  1347. sector.empty_env = FAILED_ADDR;
  1348. create_env_blob(&sector, default_env_set[i].key, default_env_set[i].value, value_len);
  1349. if (result != EF_NO_ERR) {
  1350. goto __exit;
  1351. }
  1352. }
  1353. __exit:
  1354. /* unlock the ENV cache */
  1355. ef_port_env_unlock();
  1356. return result;
  1357. }
  1358. static bool print_env_cb(env_node_obj_t env, void *arg1, void *arg2)
  1359. {
  1360. bool value_is_str = true, print_value = false;
  1361. size_t *using_size = arg1;
  1362. if (env->crc_is_ok) {
  1363. /* calculate the total using flash size */
  1364. *using_size += env->len;
  1365. /* check ENV */
  1366. if (env->status == ENV_WRITE) {
  1367. ef_print("%.*s=", env->name_len, env->name);
  1368. if (env->value_len < EF_STR_ENV_VALUE_MAX_SIZE ) {
  1369. uint8_t buf[32];
  1370. size_t len, size;
  1371. __reload:
  1372. /* check the value is string */
  1373. for (len = 0, size = 0; len < env->value_len; len += size) {
  1374. if (len + sizeof(buf) < env->value_len) {
  1375. size = sizeof(buf);
  1376. } else {
  1377. size = env->value_len - len;
  1378. }
  1379. ef_port_read(env->addr.value + len, (uint32_t *) buf, EF_WG_ALIGN(size));
  1380. if (print_value) {
  1381. ef_print("%.*s", size, buf);
  1382. } else if (!ef_is_str(buf, size)) {
  1383. value_is_str = false;
  1384. break;
  1385. }
  1386. }
  1387. } else {
  1388. value_is_str = false;
  1389. }
  1390. if (value_is_str && !print_value) {
  1391. print_value = true;
  1392. goto __reload;
  1393. } else if (!value_is_str) {
  1394. ef_print("blob @0x%08X %dbytes", env->addr.value, env->value_len);
  1395. }
  1396. ef_print("\n");
  1397. }
  1398. }
  1399. return false;
  1400. }
  1401. /**
  1402. * Print ENV.
  1403. */
  1404. void ef_print_env(void)
  1405. {
  1406. struct env_node_obj env;
  1407. size_t using_size = 0;
  1408. if (!init_ok) {
  1409. EF_INFO("ENV isn't initialize OK.\n");
  1410. return;
  1411. }
  1412. /* lock the ENV cache */
  1413. ef_port_env_lock();
  1414. env_iterator(&env, &using_size, NULL, print_env_cb);
  1415. ef_print("\nmode: next generation\n");
  1416. ef_print("size: %lu/%lu bytes.\n", using_size + (SECTOR_NUM - EF_GC_EMPTY_SEC_THRESHOLD) * SECTOR_HDR_DATA_SIZE,
  1417. ENV_AREA_SIZE - SECTOR_SIZE * EF_GC_EMPTY_SEC_THRESHOLD);
  1418. /* unlock the ENV cache */
  1419. ef_port_env_unlock();
  1420. }
  1421. #ifdef EF_ENV_AUTO_UPDATE
  1422. /*
  1423. * Auto update ENV to latest default when current EF_ENV_VER_NUM is changed.
  1424. */
  1425. static void env_auto_update(void)
  1426. {
  1427. size_t saved_ver_num, setting_ver_num = EF_ENV_VER_NUM;
  1428. if (get_env(VER_NUM_ENV_NAME, &saved_ver_num, sizeof(size_t), NULL) > 0) {
  1429. /* check version number */
  1430. if (saved_ver_num != setting_ver_num) {
  1431. struct env_node_obj env;
  1432. size_t i, value_len;
  1433. struct sector_meta_data sector;
  1434. EF_DEBUG("Update the ENV from version %d to %d.\n", saved_ver_num, setting_ver_num);
  1435. for (i = 0; i < default_env_set_size; i++) {
  1436. /* add a new ENV when it's not found */
  1437. if (!find_env(default_env_set[i].key, &env)) {
  1438. /* It seems to be a string when value length is 0.
  1439. * This mechanism is for compatibility with older versions (less then V4.0). */
  1440. if (default_env_set[i].value_len == 0) {
  1441. value_len = strlen(default_env_set[i].value);
  1442. } else {
  1443. value_len = default_env_set[i].value_len;
  1444. }
  1445. sector.empty_env = FAILED_ADDR;
  1446. create_env_blob(&sector, default_env_set[i].key, default_env_set[i].value, value_len);
  1447. }
  1448. }
  1449. } else {
  1450. /* version number not changed now return */
  1451. return;
  1452. }
  1453. }
  1454. set_env(VER_NUM_ENV_NAME, &setting_ver_num, sizeof(size_t));
  1455. }
  1456. #endif /* EF_ENV_AUTO_UPDATE */
  1457. static bool check_sec_hdr_cb(sector_meta_data_t sector, void *arg1, void *arg2)
  1458. {
  1459. if (!sector->check_ok) {
  1460. size_t *failed_count = arg1;
  1461. EF_INFO("Warning: Sector header check failed. Format this sector (0x%08x).\n", sector->addr);
  1462. (*failed_count) ++;
  1463. format_sector(sector->addr, SECTOR_NOT_COMBINED);
  1464. }
  1465. return false;
  1466. }
  1467. static bool check_and_recovery_gc_cb(sector_meta_data_t sector, void *arg1, void *arg2)
  1468. {
  1469. if (sector->check_ok && sector->status.dirty == SECTOR_DIRTY_GC) {
  1470. /* make sure the GC request flag to true */
  1471. gc_request = true;
  1472. /* resume the GC operate */
  1473. gc_collect();
  1474. }
  1475. return false;
  1476. }
  1477. static bool check_and_recovery_env_cb(env_node_obj_t env, void *arg1, void *arg2)
  1478. {
  1479. /* recovery the prepare deleted ENV */
  1480. if (env->crc_is_ok && env->status == ENV_PRE_DELETE) {
  1481. EF_INFO("Found an ENV (%.*s) which has changed value failed. Now will recovery it.\n", env->name_len, env->name);
  1482. /* recovery the old ENV */
  1483. if (move_env(env) == EF_NO_ERR) {
  1484. EF_DEBUG("Recovery the ENV successful.\n");
  1485. } else {
  1486. EF_DEBUG("Warning: Moved an ENV (size %d) failed when recovery. Now will GC then retry.\n", env->len);
  1487. return true;
  1488. }
  1489. } else if (env->status == ENV_PRE_WRITE) {
  1490. uint8_t status_table[ENV_STATUS_TABLE_SIZE];
  1491. /* the ENV has not write finish, change the status to error */
  1492. //TODO �����쳣�����״̬װ��ͼ
  1493. write_status(env->addr.start, status_table, ENV_STATUS_NUM, ENV_ERR_HDR);
  1494. return true;
  1495. }
  1496. return false;
  1497. }
  1498. /**
  1499. * Check and load the flash ENV meta data.
  1500. *
  1501. * @return result
  1502. */
  1503. EfErrCode ef_load_env(void)
  1504. {
  1505. EfErrCode result = EF_NO_ERR;
  1506. struct env_node_obj env;
  1507. struct sector_meta_data sector;
  1508. size_t check_failed_count = 0;
  1509. in_recovery_check = true;
  1510. /* check all sector header */
  1511. sector_iterator(&sector, SECTOR_STORE_UNUSED, &check_failed_count, NULL, check_sec_hdr_cb, false);
  1512. /* all sector header check failed */
  1513. if (check_failed_count == SECTOR_NUM) {
  1514. EF_INFO("Warning: All sector header check failed. Set it to default.\n");
  1515. ef_env_set_default();
  1516. }
  1517. /* lock the ENV cache */
  1518. ef_port_env_lock();
  1519. /* check all sector header for recovery GC */
  1520. sector_iterator(&sector, SECTOR_STORE_UNUSED, NULL, NULL, check_and_recovery_gc_cb, false);
  1521. __retry:
  1522. /* check all ENV for recovery */
  1523. env_iterator(&env, NULL, NULL, check_and_recovery_env_cb);
  1524. if (gc_request) {
  1525. gc_collect();
  1526. goto __retry;
  1527. }
  1528. in_recovery_check = false;
  1529. /* unlock the ENV cache */
  1530. ef_port_env_unlock();
  1531. return result;
  1532. }
  1533. /**
  1534. * Flash ENV initialize.
  1535. *
  1536. * @param default_env default ENV set for user
  1537. * @param default_env_size default ENV set size
  1538. *
  1539. * @return result
  1540. */
  1541. EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size) {
  1542. EfErrCode result = EF_NO_ERR;
  1543. #ifdef EF_ENV_USING_CACHE
  1544. size_t i;
  1545. #endif
  1546. EF_ASSERT(default_env);
  1547. EF_ASSERT(ENV_AREA_SIZE);
  1548. /* must be aligned with erase_min_size */
  1549. EF_ASSERT(ENV_AREA_SIZE % EF_ERASE_MIN_SIZE == 0);
  1550. /* sector number must be greater than or equal to 2 */
  1551. EF_ASSERT(SECTOR_NUM >= 2);
  1552. /* must be aligned with write granularity */
  1553. EF_ASSERT((EF_STR_ENV_VALUE_MAX_SIZE * 8) % EF_WRITE_GRAN == 0);
  1554. if (init_ok) {
  1555. return EF_NO_ERR;
  1556. }
  1557. #ifdef EF_ENV_USING_CACHE
  1558. for (i = 0; i < EF_SECTOR_CACHE_TABLE_SIZE; i++) {
  1559. sector_cache_table[i].addr = FAILED_ADDR;
  1560. }
  1561. for (i = 0; i < EF_ENV_CACHE_TABLE_SIZE; i++) {
  1562. env_cache_table[i].addr = FAILED_ADDR;
  1563. }
  1564. #endif /* EF_ENV_USING_CACHE */
  1565. env_start_addr = EF_START_ADDR;
  1566. default_env_set = default_env;
  1567. default_env_set_size = default_env_size;
  1568. EF_DEBUG("ENV start address is 0x%08X, size is %d bytes.\n", EF_START_ADDR, ENV_AREA_SIZE);
  1569. result = ef_load_env();
  1570. #ifdef EF_ENV_AUTO_UPDATE
  1571. if (result == EF_NO_ERR) {
  1572. env_auto_update();
  1573. }
  1574. #endif
  1575. if (result == EF_NO_ERR) {
  1576. init_ok = true;
  1577. }
  1578. return result;
  1579. }
  1580. #endif /* defined(EF_USING_ENV) && !defined(EF_ENV_USING_LEGACY_MODE) */