easyflash.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * This file is part of the EasyFlash Library.
  3. *
  4. * Copyright (c) 2014-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: It is an head file for this library. You can see all be called functions.
  26. * Created on: 2014-09-10
  27. */
  28. #ifndef EASYFLASH_H_
  29. #define EASYFLASH_H_
  30. #include <stdint.h>
  31. #include <stddef.h>
  32. #include <stdbool.h>
  33. #include <ef_cfg.h>
  34. #include <ef_def.h>
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* easyflash.c */
  39. EfErrCode easyflash_init(void);
  40. #ifdef EF_USING_ENV
  41. /* only supported on ef_env.c */
  42. size_t ef_get_env_blob(const char *key, void *value_buf, size_t buf_len, size_t *saved_value_len);
  43. bool ef_get_env_obj(const char *key, env_node_obj_t env);
  44. size_t ef_read_env_value(env_node_obj_t env, uint8_t *value_buf, size_t buf_len);
  45. EfErrCode ef_set_env_blob(const char *key, const void *value_buf, size_t buf_len);
  46. /* ef_env.c, ef_env_legacy_wl.c and ef_env_legacy.c */
  47. EfErrCode ef_load_env(void);
  48. void ef_print_env(void);
  49. char *ef_get_env(const char *key);
  50. EfErrCode ef_set_env(const char *key, const char *value);
  51. EfErrCode ef_del_env(const char *key);
  52. EfErrCode ef_save_env(void);
  53. EfErrCode ef_env_set_default(void);
  54. size_t ef_get_env_write_bytes(void);
  55. EfErrCode ef_set_and_save_env(const char *key, const char *value);
  56. EfErrCode ef_del_and_save_env(const char *key);
  57. #endif
  58. /* 开机加载诊断(2026-10-29 新增,仅 ef_env.c 的 NG 模式实现)
  59. * 统计"最近一次 ef_load_env() 中扇区头校验失败、被重新 format_sector()"的情况。
  60. * format_cnt : 本次加载执行 format_sector() 的次数(正常应为 0)
  61. * format_fail_cnt : 其中擦除或写头返回错误的次数(非 0 = 擦写真的失败,
  62. * 意味着硬件/寿命问题,且下次开机会再次重试 → 常态性变慢)
  63. * 用途:把原先 check_sec_hdr_cb() 里"静默格式化"的行为变成可观测、可上报的指标。 */
  64. typedef struct {
  65. uint32_t format_cnt;
  66. uint32_t format_fail_cnt;
  67. } ef_boot_diag_t;
  68. void ef_env_get_boot_diag(ef_boot_diag_t *diag);
  69. #ifdef EF_USING_IAP
  70. /* ef_iap.c */
  71. EfErrCode ef_erase_bak_app(size_t app_size);
  72. EfErrCode ef_erase_user_app(uint32_t user_app_addr, size_t user_app_size);
  73. EfErrCode ef_erase_spec_user_app(uint32_t user_app_addr, size_t app_size,
  74. EfErrCode (*app_erase)(uint32_t addr, size_t size));
  75. EfErrCode ef_erase_bl(uint32_t bl_addr, size_t bl_size);
  76. EfErrCode ef_write_data_to_bak(uint8_t *data, size_t size, size_t *cur_size,
  77. size_t total_size);
  78. EfErrCode ef_copy_app_from_bak(uint32_t user_app_addr, size_t app_size);
  79. EfErrCode ef_copy_spec_app_from_bak(uint32_t user_app_addr, size_t app_size,
  80. EfErrCode (*app_write)(uint32_t addr, const uint32_t *buf, size_t size));
  81. EfErrCode ef_copy_bl_from_bak(uint32_t bl_addr, size_t bl_size);
  82. uint32_t ef_get_bak_app_start_addr(void);
  83. #endif
  84. #ifdef EF_USING_LOG
  85. /* ef_log.c */
  86. EfErrCode ef_log_read(size_t index, uint32_t *log, size_t size);
  87. EfErrCode ef_log_write(const uint32_t *log, size_t size);
  88. EfErrCode ef_log_clean(void);
  89. size_t ef_log_get_used_size(void);
  90. #endif
  91. /* ef_utils.c */
  92. uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size);
  93. /* ef_port.c */
  94. EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size);
  95. EfErrCode ef_port_erase(uint32_t addr, size_t size);
  96. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size);
  97. void ef_port_env_lock(void);
  98. void ef_port_env_unlock(void);
  99. void ef_log_debug(const char *file, const long line, const char *format, ...);
  100. void ef_log_info(const char *format, ...);
  101. void ef_print(const char *format, ...);
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif /* EASYFLASH_H_ */