Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 1 | /* |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 8 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <platform_def.h> |
| 11 | |
| 12 | #include <common/debug.h> |
| 13 | #include <drivers/io/io_driver.h> |
| 14 | #include <drivers/io/io_memmap.h> |
| 15 | #include <drivers/io/io_storage.h> |
| 16 | #include <lib/utils.h> |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 17 | |
| 18 | /* As we need to be able to keep state for seek, only one file can be open |
| 19 | * at a time. Make this a structure and point to the entity->info. When we |
| 20 | * can malloc memory we can change this to support more open files. |
| 21 | */ |
| 22 | typedef struct { |
| 23 | /* Use the 'in_use' flag as any value for base and file_pos could be |
| 24 | * valid. |
| 25 | */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 26 | int in_use; |
| 27 | uintptr_t base; |
| 28 | unsigned long long file_pos; |
| 29 | unsigned long long size; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 30 | } file_state_t; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 31 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 32 | static file_state_t current_file = {0}; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 33 | |
| 34 | /* Identify the device type as memmap */ |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 35 | static io_type_t device_type_memmap(void) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 36 | { |
| 37 | return IO_TYPE_MEMMAP; |
| 38 | } |
| 39 | |
| 40 | /* Memmap device functions */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 41 | static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); |
| 42 | static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 43 | io_entity_t *entity); |
| 44 | static int memmap_block_seek(io_entity_t *entity, int mode, |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 45 | signed long long offset); |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 46 | static int memmap_block_len(io_entity_t *entity, size_t *length); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 47 | static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 48 | size_t length, size_t *length_read); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 49 | static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 50 | size_t length, size_t *length_written); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 51 | static int memmap_block_close(io_entity_t *entity); |
| 52 | static int memmap_dev_close(io_dev_info_t *dev_info); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 53 | |
| 54 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 55 | static const io_dev_connector_t memmap_dev_connector = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 56 | .dev_open = memmap_dev_open |
| 57 | }; |
| 58 | |
| 59 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 60 | static const io_dev_funcs_t memmap_dev_funcs = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 61 | .type = device_type_memmap, |
| 62 | .open = memmap_block_open, |
| 63 | .seek = memmap_block_seek, |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 64 | .size = memmap_block_len, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 65 | .read = memmap_block_read, |
| 66 | .write = memmap_block_write, |
| 67 | .close = memmap_block_close, |
| 68 | .dev_init = NULL, |
| 69 | .dev_close = memmap_dev_close, |
| 70 | }; |
| 71 | |
| 72 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 73 | /* No state associated with this device so structure can be const */ |
| 74 | static const io_dev_info_t memmap_dev_info = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 75 | .funcs = &memmap_dev_funcs, |
| 76 | .info = (uintptr_t)NULL |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | /* Open a connection to the memmap device */ |
Soren Brinkmann | 46dd170 | 2016-01-14 10:11:05 -0800 | [diff] [blame] | 81 | static int memmap_dev_open(const uintptr_t dev_spec __unused, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 82 | io_dev_info_t **dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 83 | { |
| 84 | assert(dev_info != NULL); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 85 | *dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */ |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 86 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 87 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | /* Close a connection to the memmap device */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 93 | static int memmap_dev_close(io_dev_info_t *dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 94 | { |
| 95 | /* NOP */ |
| 96 | /* TODO: Consider tracking open files and cleaning them up here */ |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 97 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | /* Open a file on the memmap device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 102 | static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 103 | io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 104 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 105 | int result = -ENOMEM; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 106 | const io_block_spec_t *block_spec = (io_block_spec_t *)spec; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 107 | |
| 108 | /* Since we need to track open state for seek() we only allow one open |
| 109 | * spec at a time. When we have dynamic memory we can malloc and set |
| 110 | * entity->info. |
| 111 | */ |
| 112 | if (current_file.in_use == 0) { |
| 113 | assert(block_spec != NULL); |
| 114 | assert(entity != NULL); |
| 115 | |
| 116 | current_file.in_use = 1; |
| 117 | current_file.base = block_spec->offset; |
| 118 | /* File cursor offset for seek and incremental reads etc. */ |
| 119 | current_file.file_pos = 0; |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 120 | current_file.size = block_spec->length; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 121 | entity->info = (uintptr_t)¤t_file; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 122 | result = 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 123 | } else { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 124 | WARN("A Memmap device is already active. Close first.\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Seek to a particular file offset on the memmap device */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 132 | static int memmap_block_seek(io_entity_t *entity, int mode, |
| 133 | signed long long offset) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 134 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 135 | int result = -ENOENT; |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 136 | file_state_t *fp; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 137 | |
| 138 | /* We only support IO_SEEK_SET for the moment. */ |
| 139 | if (mode == IO_SEEK_SET) { |
| 140 | assert(entity != NULL); |
| 141 | |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 142 | fp = (file_state_t *) entity->info; |
| 143 | |
| 144 | /* Assert that new file position is valid */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 145 | assert((offset >= 0) && |
| 146 | ((unsigned long long)offset < fp->size)); |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 147 | |
| 148 | /* Reset file position */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 149 | fp->file_pos = (unsigned long long)offset; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 150 | result = 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 157 | /* Return the size of a file on the memmap device */ |
| 158 | static int memmap_block_len(io_entity_t *entity, size_t *length) |
| 159 | { |
| 160 | assert(entity != NULL); |
| 161 | assert(length != NULL); |
| 162 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 163 | *length = (size_t)((file_state_t *)entity->info)->size; |
Gerald Lejeune | 2a1f2d1 | 2015-03-18 14:41:42 +0100 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 169 | /* Read data from a file on the memmap device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 170 | static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 171 | size_t length, size_t *length_read) |
| 172 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 173 | file_state_t *fp; |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 174 | unsigned long long pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 175 | |
| 176 | assert(entity != NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 177 | assert(length_read != NULL); |
| 178 | |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 179 | fp = (file_state_t *) entity->info; |
| 180 | |
| 181 | /* Assert that file position is valid for this read operation */ |
| 182 | pos_after = fp->file_pos + length; |
| 183 | assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 184 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 185 | memcpy((void *)buffer, |
| 186 | (void *)((uintptr_t)(fp->base + fp->file_pos)), length); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 187 | |
| 188 | *length_read = length; |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 189 | |
| 190 | /* Set file position after read */ |
| 191 | fp->file_pos = pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 192 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 193 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | /* Write data to a file on the memmap device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 198 | static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 199 | size_t length, size_t *length_written) |
| 200 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 201 | file_state_t *fp; |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 202 | unsigned long long pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 203 | |
| 204 | assert(entity != NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 205 | assert(length_written != NULL); |
| 206 | |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 207 | fp = (file_state_t *) entity->info; |
| 208 | |
| 209 | /* Assert that file position is valid for this write operation */ |
| 210 | pos_after = fp->file_pos + length; |
| 211 | assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 212 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 213 | memcpy((void *)((uintptr_t)(fp->base + fp->file_pos)), |
| 214 | (void *)buffer, length); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 215 | |
| 216 | *length_written = length; |
| 217 | |
Jeenu Viswambharan | e6bae8c | 2017-02-13 13:06:18 +0000 | [diff] [blame] | 218 | /* Set file position after write */ |
| 219 | fp->file_pos = pos_after; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 220 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 221 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
| 225 | /* Close a file on the memmap device */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 226 | static int memmap_block_close(io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 227 | { |
| 228 | assert(entity != NULL); |
| 229 | |
| 230 | entity->info = 0; |
| 231 | |
| 232 | /* This would be a mem free() if we had malloc.*/ |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 233 | zeromem((void *)¤t_file, sizeof(current_file)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 234 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 235 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | /* Exported functions */ |
| 240 | |
| 241 | /* Register the memmap driver with the IO abstraction */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 242 | int register_io_dev_memmap(const io_dev_connector_t **dev_con) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 243 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 244 | int result; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 245 | assert(dev_con != NULL); |
| 246 | |
| 247 | result = io_register_device(&memmap_dev_info); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 248 | if (result == 0) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 249 | *dev_con = &memmap_dev_connector; |
| 250 | |
| 251 | return result; |
| 252 | } |