easyflash.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: Initialize interface for this library.
  26. * Created on: 2014-09-09
  27. */
  28. /*
  29. *
  30. * This all Backup Area Flash storage index. All used flash area configure is under here.
  31. * |----------------------------| Storage Size
  32. * | Environment variables area | ENV area size @see ENV_AREA_SIZE
  33. * |----------------------------|
  34. * | Saved log area | Log area size @see LOG_AREA_SIZE
  35. * |----------------------------|
  36. * |(IAP)Downloaded application | IAP already downloaded application, unfixed size
  37. * |----------------------------|
  38. *
  39. * @note all area sizes must be aligned with EF_ERASE_MIN_SIZE
  40. *
  41. * The EasyFlash add the NG (Next Generation) mode start from V4.0. All old mode before V4.0, called LEGACY mode.
  42. *
  43. * - NG (Next Generation) mode is default mode from V4.0. It's easy to settings, only defined the ENV_AREA_SIZE.
  44. * - The LEGACY mode has been DEPRECATED. It is NOT RECOMMENDED to continue using.
  45. * Beacuse it will use ram to buffer the ENV and spend more flash erase times.
  46. * If you want use it please using the V3.X version.
  47. */
  48. #include <easyflash.h>
  49. #if !defined(EF_START_ADDR)
  50. #error "Please configure backup area start address (in ef_cfg.h)"
  51. #endif
  52. #if !defined(EF_ERASE_MIN_SIZE)
  53. #error "Please configure minimum size of flash erasure (in ef_cfg.h)"
  54. #endif
  55. /**
  56. * EasyFlash system initialize.
  57. *
  58. * @return result
  59. */
  60. EfErrCode easyflash_init(void) {
  61. extern EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size);
  62. extern EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size);
  63. extern EfErrCode ef_iap_init(void);
  64. extern EfErrCode ef_log_init(void);
  65. size_t default_env_set_size = 0;
  66. const ef_env *default_env_set;
  67. EfErrCode result = EF_NO_ERR;
  68. static bool init_ok = false;
  69. if (init_ok) {
  70. return EF_NO_ERR;
  71. }
  72. result = ef_port_init(&default_env_set, &default_env_set_size);
  73. #ifdef EF_USING_ENV
  74. if (result == EF_NO_ERR) {
  75. result = ef_env_init(default_env_set, default_env_set_size);
  76. }
  77. #endif
  78. #ifdef EF_USING_IAP
  79. if (result == EF_NO_ERR) {
  80. result = ef_iap_init();
  81. }
  82. #endif
  83. #ifdef EF_USING_LOG
  84. if (result == EF_NO_ERR) {
  85. result = ef_log_init();
  86. }
  87. #endif
  88. if (result == EF_NO_ERR) {
  89. init_ok = true;
  90. EF_INFO("EasyFlash V%s is initialize success.\n", EF_SW_VERSION);
  91. } else {
  92. EF_INFO("EasyFlash V%s is initialize fail.\n", EF_SW_VERSION);
  93. }
  94. EF_INFO("You can get the latest version on https://github.com/armink/EasyFlash .\n");
  95. return result;
  96. }