io_memmap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <platform_def.h>
  9. #include <common/debug.h>
  10. #include <drivers/io/io_driver.h>
  11. #include <drivers/io/io_memmap.h>
  12. #include <drivers/io/io_storage.h>
  13. #include <lib/utils.h>
  14. /* As we need to be able to keep state for seek, only one file can be open
  15. * at a time. Make this a structure and point to the entity->info. When we
  16. * can malloc memory we can change this to support more open files.
  17. */
  18. typedef struct {
  19. /* Use the 'in_use' flag as any value for base and file_pos could be
  20. * valid.
  21. */
  22. int in_use;
  23. uintptr_t base;
  24. unsigned long long file_pos;
  25. unsigned long long size;
  26. } memmap_file_state_t;
  27. static memmap_file_state_t current_memmap_file = {0};
  28. /* Identify the device type as memmap */
  29. static io_type_t device_type_memmap(void)
  30. {
  31. return IO_TYPE_MEMMAP;
  32. }
  33. /* Memmap device functions */
  34. static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
  35. static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
  36. io_entity_t *entity);
  37. static int memmap_block_seek(io_entity_t *entity, int mode,
  38. signed long long offset);
  39. static int memmap_block_len(io_entity_t *entity, size_t *length);
  40. static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
  41. size_t length, size_t *length_read);
  42. static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
  43. size_t length, size_t *length_written);
  44. static int memmap_block_close(io_entity_t *entity);
  45. static int memmap_dev_close(io_dev_info_t *dev_info);
  46. static const io_dev_connector_t memmap_dev_connector = {
  47. .dev_open = memmap_dev_open
  48. };
  49. static const io_dev_funcs_t memmap_dev_funcs = {
  50. .type = device_type_memmap,
  51. .open = memmap_block_open,
  52. .seek = memmap_block_seek,
  53. .size = memmap_block_len,
  54. .read = memmap_block_read,
  55. .write = memmap_block_write,
  56. .close = memmap_block_close,
  57. .dev_init = NULL,
  58. .dev_close = memmap_dev_close,
  59. };
  60. /* No state associated with this device so structure can be const */
  61. static io_dev_info_t memmap_dev_info = {
  62. .funcs = &memmap_dev_funcs,
  63. .info = (uintptr_t)NULL
  64. };
  65. /* Open a connection to the memmap device */
  66. static int memmap_dev_open(const uintptr_t dev_spec __unused,
  67. io_dev_info_t **dev_info)
  68. {
  69. assert(dev_info != NULL);
  70. *dev_info = &memmap_dev_info;
  71. return 0;
  72. }
  73. /* Close a connection to the memmap device */
  74. static int memmap_dev_close(io_dev_info_t *dev_info)
  75. {
  76. /* NOP */
  77. /* TODO: Consider tracking open files and cleaning them up here */
  78. return 0;
  79. }
  80. /* Open a file on the memmap device */
  81. static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
  82. io_entity_t *entity)
  83. {
  84. int result = -ENOMEM;
  85. const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
  86. /* Since we need to track open state for seek() we only allow one open
  87. * spec at a time. When we have dynamic memory we can malloc and set
  88. * entity->info.
  89. */
  90. if (current_memmap_file.in_use == 0) {
  91. assert(block_spec != NULL);
  92. assert(entity != NULL);
  93. current_memmap_file.in_use = 1;
  94. current_memmap_file.base = block_spec->offset;
  95. /* File cursor offset for seek and incremental reads etc. */
  96. current_memmap_file.file_pos = 0;
  97. current_memmap_file.size = block_spec->length;
  98. entity->info = (uintptr_t)&current_memmap_file;
  99. result = 0;
  100. } else {
  101. WARN("A Memmap device is already active. Close first.\n");
  102. }
  103. return result;
  104. }
  105. /* Seek to a particular file offset on the memmap device */
  106. static int memmap_block_seek(io_entity_t *entity, int mode,
  107. signed long long offset)
  108. {
  109. int result = -ENOENT;
  110. memmap_file_state_t *fp;
  111. /* We only support IO_SEEK_SET for the moment. */
  112. if (mode == IO_SEEK_SET) {
  113. assert(entity != NULL);
  114. fp = (memmap_file_state_t *) entity->info;
  115. /* Assert that new file position is valid */
  116. assert((offset >= 0) &&
  117. ((unsigned long long)offset < fp->size));
  118. /* Reset file position */
  119. fp->file_pos = (unsigned long long)offset;
  120. result = 0;
  121. }
  122. return result;
  123. }
  124. /* Return the size of a file on the memmap device */
  125. static int memmap_block_len(io_entity_t *entity, size_t *length)
  126. {
  127. assert(entity != NULL);
  128. assert(length != NULL);
  129. *length = (size_t)((memmap_file_state_t *)entity->info)->size;
  130. return 0;
  131. }
  132. /* Read data from a file on the memmap device */
  133. static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
  134. size_t length, size_t *length_read)
  135. {
  136. memmap_file_state_t *fp;
  137. unsigned long long pos_after;
  138. assert(entity != NULL);
  139. assert(length_read != NULL);
  140. fp = (memmap_file_state_t *) entity->info;
  141. /* Assert that file position is valid for this read operation */
  142. pos_after = fp->file_pos + length;
  143. assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
  144. memcpy((void *)buffer,
  145. (void *)((uintptr_t)(fp->base + fp->file_pos)), length);
  146. *length_read = length;
  147. /* Set file position after read */
  148. fp->file_pos = pos_after;
  149. return 0;
  150. }
  151. /* Write data to a file on the memmap device */
  152. static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
  153. size_t length, size_t *length_written)
  154. {
  155. memmap_file_state_t *fp;
  156. unsigned long long pos_after;
  157. assert(entity != NULL);
  158. assert(length_written != NULL);
  159. fp = (memmap_file_state_t *) entity->info;
  160. /* Assert that file position is valid for this write operation */
  161. pos_after = fp->file_pos + length;
  162. assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
  163. memcpy((void *)((uintptr_t)(fp->base + fp->file_pos)),
  164. (void *)buffer, length);
  165. *length_written = length;
  166. /* Set file position after write */
  167. fp->file_pos = pos_after;
  168. return 0;
  169. }
  170. /* Close a file on the memmap device */
  171. static int memmap_block_close(io_entity_t *entity)
  172. {
  173. assert(entity != NULL);
  174. entity->info = 0;
  175. /* This would be a mem free() if we had malloc.*/
  176. zeromem((void *)&current_memmap_file, sizeof(current_memmap_file));
  177. return 0;
  178. }
  179. /* Exported functions */
  180. /* Register the memmap driver with the IO abstraction */
  181. int register_io_dev_memmap(const io_dev_connector_t **dev_con)
  182. {
  183. int result;
  184. assert(dev_con != NULL);
  185. result = io_register_device(&memmap_dev_info);
  186. if (result == 0)
  187. *dev_con = &memmap_dev_connector;
  188. return result;
  189. }