sdfs_manager.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file sd file system manager interface
  8. */
  9. #include <os_common_api.h>
  10. #include <zephyr.h>
  11. //#include <logging/sys_log.h>
  12. #include <init.h>
  13. #include <kernel.h>
  14. #include <kernel_structs.h>
  15. //#include <flash.h>
  16. #include <mem_manager.h>
  17. #include <sdfs_manager.h>
  18. #include <board_cfg.h>
  19. static const char * const sd_fs_volume_names[] = {
  20. #if CONFIG_AEM_WATCH_SUPPORT
  21. #ifdef CONFIG_SPINAND_ACTS
  22. "/NAND:A", "/NAND:K", "/NAND:C", "/NAND:B", "/NAND:L"
  23. #elif CONFIG_BOARD_EMMCBOOT
  24. "/SD:A", "/SD:K", "/SD:C", "/SD:B", "/SD:L"
  25. #elif CONFIG_SDFS_NOR_NOT_XIP
  26. "/NOR:A", "/NOR:K", "/NOR:C", "/NOR:B", "/NOR:L"
  27. #elif CONFIG_DISK_ACCESS_SD
  28. "/SD:A", "/SD:K", "/SD:C", "/SD:B", "/SD:L"
  29. #else
  30. "/DNOR:A", "/DNOR:K", "/DNOR:C", "/DNOR:B", "/DNOR:L"
  31. #endif
  32. #else
  33. #ifdef CONFIG_SPINAND_ACTS
  34. "/NAND:A", "/NAND:K", "/NAND:C", "/NAND:B",
  35. #elif CONFIG_BOARD_EMMCBOOT
  36. "/SD:A", "/SD:K", "/SD:C", "/SD:B"
  37. #elif CONFIG_DISK_ACCESS_SD
  38. "/SD:A", "/SD:K", "/SD:C", "/SD:B"
  39. #elif CONFIG_SDFS_NOR_NOT_XIP
  40. #if IS_ENABLED(CONFIG_SPI_FLASH_2)
  41. "/DNOR:A", "/DNOR:K", "/DNOR:C", "/DNOR:B"
  42. #else
  43. "/NOR:A", "/NOR:K", "/NOR:C", "/NOR:B"
  44. #endif
  45. #else
  46. "/DNOR:A", "/DNOR:K", "/DNOR:C", "/DNOR:B"
  47. #endif
  48. #endif
  49. };
  50. struct sd_fs_volume {
  51. struct fs_mount_t *mp;
  52. uint8_t allocated : 1; /* 0: buffer not allocated, 1: buffer allocated */
  53. uint8_t mounted : 1; /* 0: volume not mounted, 1: volume mounted */
  54. };
  55. static struct sd_fs_volume sd_fs_volumes[ARRAY_SIZE(sd_fs_volume_names)];
  56. int sdfs_manager_init(void)
  57. {
  58. int i;
  59. int res = 0;
  60. struct fs_mount_t *mp = NULL;
  61. for (i = 0; i < ARRAY_SIZE(sd_fs_volume_names); i++) {
  62. /* fs_manager_init() should be called only once */
  63. if (sd_fs_volumes[i].allocated == 1) {
  64. continue;
  65. }
  66. mp = mem_malloc(sizeof(struct fs_mount_t));
  67. if (mp == NULL) {
  68. goto malloc_failed;
  69. }
  70. mp->type = FS_SDFS;
  71. mp->mnt_point = sd_fs_volume_names[i];
  72. res = fs_mount(mp);
  73. if (res != 0) {
  74. // SYS_LOG_WRN("fs (%s) init failed (%d)", sd_fs_volume_names[i], res);
  75. mem_free(mp);
  76. } else {
  77. SYS_LOG_INF("fs (%s) init success", sd_fs_volume_names[i]);
  78. sd_fs_volumes[i].mp = mp;
  79. sd_fs_volumes[i].allocated = 1;
  80. sd_fs_volumes[i].mounted = 1;
  81. }
  82. }
  83. return res;
  84. malloc_failed:
  85. SYS_LOG_ERR("malloc failed");
  86. if (mp) {
  87. mem_free(mp);
  88. }
  89. return -ENOMEM;
  90. }