PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mqx/util_lib/Sources/util.c

https://github.com/andersmalm/cyassl
C | 178 lines | 135 code | 27 blank | 16 comment | 33 complexity | 3ba34efc06e4b4eefe8dac38f7764852 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* util.c */
  2. #include <mqx.h>
  3. #include <bsp.h>
  4. #include <mfs.h>
  5. #include <fio.h>
  6. #include "util.h"
  7. #if !BSPCFG_ENABLE_IO_SUBSYSTEM
  8. #error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined \
  9. non-zero in user_config.h. Please recompile BSP with this option.
  10. #endif
  11. #ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
  12. #error This application requires BSP_DEFAULT_IO_CHANNEL to be not \
  13. NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero \
  14. in user_config.h and recompile BSP with this option.
  15. #endif
  16. #if defined BSP_SDCARD_ESDHC_CHANNEL
  17. #if ! BSPCFG_ENABLE_ESDHC
  18. #error This application requires BSPCFG_ENABLE_ESDHC defined \
  19. non-zero in user_config.h. Please recompile libraries with \
  20. this option.
  21. #endif
  22. #elif defined BSP_SDCARD_SDHC_CHANNEL
  23. #if ! BSPCFG_ENABLE_SDHC
  24. #error This application requires BSPCFG_ENABLE_SDHC defined \
  25. non-zero in user_config.h. Please recompile libraries with \
  26. this option.
  27. #endif
  28. #endif
  29. #if defined (BSP_SDCARD_SPI_CHANNEL)
  30. #define SDCARD_COM_CHANNEL BSP_SDCARD_SPI_CHANNEL
  31. #elif defined (BSP_SDCARD_ESDHC_CHANNEL)
  32. #define SDCARD_COM_CHANNEL BSP_SDCARD_ESDHC_CHANNEL
  33. #elif defined (BSP_SDCARD_SDHC_CHANNEL)
  34. #define SDCARD_COM_CHANNEL BSP_SDCARD_SDHC_CHANNEL
  35. #else
  36. #error "SDCARD low level communication device not defined!"
  37. #endif
  38. int sdcard_open(MQX_FILE_PTR *com_handle, MQX_FILE_PTR *sdcard_handle,
  39. MQX_FILE_PTR *partman_handle, MQX_FILE_PTR *filesystem_handle,
  40. char *partman_name, char *filesystem_name)
  41. {
  42. _mqx_int error_code;
  43. _mqx_uint param;
  44. /* Open low level communication device */
  45. *com_handle = fopen(SDCARD_COM_CHANNEL, NULL);
  46. if (NULL == *com_handle) {
  47. printf("Error installing communication handle.\n");
  48. return -60;
  49. }
  50. /* Install SD card device */
  51. error_code = _io_sdcard_install("sdcard:", (pointer) &_bsp_sdcard0_init,
  52. *com_handle);
  53. if (error_code != MQX_OK) {
  54. printf("Error installing SD card device (0x%x)\n", error_code);
  55. return -61;
  56. }
  57. _time_delay(200);
  58. /* Open the device which MFS will be installed on */
  59. *sdcard_handle = fopen("sdcard:", 0);
  60. if (*sdcard_handle == NULL) {
  61. printf("Unable to open SD card device.\n");
  62. return -62;
  63. }
  64. /* Install partition manager over SD card driver */
  65. error_code = _io_part_mgr_install(*sdcard_handle, partman_name, 0);
  66. if (error_code != MFS_NO_ERROR) {
  67. printf("Error installing partition manager: %s\n", MFS_Error_text(
  68. (uint_32) error_code));
  69. return -63;
  70. }
  71. /* Open partition manager */
  72. *partman_handle = fopen(partman_name, NULL);
  73. if (*partman_handle == NULL) {
  74. error_code = ferror(*partman_handle);
  75. printf("Error opening partition manager: %s\n", MFS_Error_text(
  76. (uint_32) error_code));
  77. return -64;
  78. }
  79. /* Validate partition 1 */
  80. param = 1;
  81. error_code = _io_ioctl(*partman_handle, IO_IOCTL_VAL_PART, &param);
  82. if (error_code == MQX_OK) {
  83. /* Install MFS over partition 1 */
  84. error_code = _io_mfs_install(*partman_handle, filesystem_name, param);
  85. if (error_code != MFS_NO_ERROR) {
  86. printf("Error initializing MFS over partition: %s\n",
  87. MFS_Error_text((uint_32) error_code));
  88. return -65;
  89. }
  90. } else {
  91. /* Install MFS over SD card driver */
  92. error_code = _io_mfs_install(*sdcard_handle, filesystem_name,
  93. (_file_size) 0);
  94. if (error_code != MFS_NO_ERROR) {
  95. printf("Error initializing MFS: %s\n", MFS_Error_text(
  96. (uint_32) error_code));
  97. return -66;
  98. }
  99. } /* end Validate partition 1 */
  100. /* Open file system */
  101. *filesystem_handle = fopen(filesystem_name, NULL);
  102. error_code = ferror(*filesystem_handle);
  103. if ((error_code != MFS_NO_ERROR) && (error_code != MFS_NOT_A_DOS_DISK)) {
  104. printf("Error opening filesystem: %s\n", MFS_Error_text(
  105. (uint_32) error_code));
  106. return -67;
  107. }
  108. if (error_code == MFS_NOT_A_DOS_DISK) {
  109. printf("NOT A DOS DISK! You must format to continue.\n");
  110. return -68;
  111. }
  112. return 0;
  113. }
  114. int sdcard_close(MQX_FILE_PTR *sdcard_handle, MQX_FILE_PTR *partman_handle,
  115. MQX_FILE_PTR *filesystem_handle,
  116. char *partman_name, char *filesystem_name)
  117. {
  118. _mqx_int error_code;
  119. /* Close the filesystem */
  120. if (MQX_OK != fclose(*filesystem_handle)) {
  121. printf("Error closing filesystem.\n");
  122. return -69;
  123. }
  124. *filesystem_handle = NULL;
  125. /* Uninstall MFS */
  126. error_code = _io_dev_uninstall(filesystem_name);
  127. if (error_code != MFS_NO_ERROR) {
  128. printf("Error uninstalling filesystem.\n");
  129. return -70;
  130. }
  131. /* Close partition manager */
  132. if (MQX_OK != fclose(*partman_handle)) {
  133. printf("Unable to close partition manager.\n");
  134. return -71;
  135. }
  136. *partman_handle = NULL;
  137. /* Uninstall partition manager */
  138. error_code = _io_dev_uninstall(partman_name);
  139. if (error_code != MFS_NO_ERROR) {
  140. printf("Error uninstalling partition manager.\n");
  141. return -72;
  142. }
  143. /* Close the SD card device */
  144. if (MQX_OK != fclose(*sdcard_handle)) {
  145. printf("Unable to close SD card device.\n");
  146. return -73;
  147. }
  148. *sdcard_handle = NULL;
  149. return 0;
  150. }
  151. /* EOF */