1561cd33eSHarry Liebel /* 2*69c043b2SJeenu Viswambharan * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved. 3561cd33eSHarry Liebel * 4561cd33eSHarry Liebel * Redistribution and use in source and binary forms, with or without 5561cd33eSHarry Liebel * modification, are permitted provided that the following conditions are met: 6561cd33eSHarry Liebel * 7561cd33eSHarry Liebel * Redistributions of source code must retain the above copyright notice, this 8561cd33eSHarry Liebel * list of conditions and the following disclaimer. 9561cd33eSHarry Liebel * 10561cd33eSHarry Liebel * Redistributions in binary form must reproduce the above copyright notice, 11561cd33eSHarry Liebel * this list of conditions and the following disclaimer in the documentation 12561cd33eSHarry Liebel * and/or other materials provided with the distribution. 13561cd33eSHarry Liebel * 14561cd33eSHarry Liebel * Neither the name of ARM nor the names of its contributors may be used 15561cd33eSHarry Liebel * to endorse or promote products derived from this software without specific 16561cd33eSHarry Liebel * prior written permission. 17561cd33eSHarry Liebel * 18561cd33eSHarry Liebel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19561cd33eSHarry Liebel * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20561cd33eSHarry Liebel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21561cd33eSHarry Liebel * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22561cd33eSHarry Liebel * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23561cd33eSHarry Liebel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24561cd33eSHarry Liebel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25561cd33eSHarry Liebel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26561cd33eSHarry Liebel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27561cd33eSHarry Liebel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28561cd33eSHarry Liebel * POSSIBILITY OF SUCH DAMAGE. 29561cd33eSHarry Liebel */ 30561cd33eSHarry Liebel 31561cd33eSHarry Liebel #include <assert.h> 3235e98e55SDan Handley #include <debug.h> 3397043ac9SDan Handley #include <io_driver.h> 3497043ac9SDan Handley #include <io_storage.h> 3597043ac9SDan Handley #include <string.h> 36561cd33eSHarry Liebel 37561cd33eSHarry Liebel /* As we need to be able to keep state for seek, only one file can be open 38561cd33eSHarry Liebel * at a time. Make this a structure and point to the entity->info. When we 39561cd33eSHarry Liebel * can malloc memory we can change this to support more open files. 40561cd33eSHarry Liebel */ 41561cd33eSHarry Liebel typedef struct { 42561cd33eSHarry Liebel /* Use the 'in_use' flag as any value for base and file_pos could be 43561cd33eSHarry Liebel * valid. 44561cd33eSHarry Liebel */ 45561cd33eSHarry Liebel int in_use; 46625de1d4SDan Handley uintptr_t base; 47561cd33eSHarry Liebel size_t file_pos; 486d70bfa1SGerald Lejeune size_t size; 49fb037bfbSDan Handley } file_state_t; 50561cd33eSHarry Liebel 51fb037bfbSDan Handley static file_state_t current_file = {0}; 52561cd33eSHarry Liebel 53561cd33eSHarry Liebel /* Identify the device type as memmap */ 54fb037bfbSDan Handley io_type_t device_type_memmap(void) 55561cd33eSHarry Liebel { 56561cd33eSHarry Liebel return IO_TYPE_MEMMAP; 57561cd33eSHarry Liebel } 58561cd33eSHarry Liebel 59561cd33eSHarry Liebel /* Memmap device functions */ 60625de1d4SDan Handley static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); 61625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, 62fb037bfbSDan Handley io_entity_t *entity); 63fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode, 64561cd33eSHarry Liebel ssize_t offset); 656d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length); 66625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, 67561cd33eSHarry Liebel size_t length, size_t *length_read); 68625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, 69561cd33eSHarry Liebel size_t length, size_t *length_written); 70fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity); 71fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info); 72561cd33eSHarry Liebel 73561cd33eSHarry Liebel 74625de1d4SDan Handley static const io_dev_connector_t memmap_dev_connector = { 75561cd33eSHarry Liebel .dev_open = memmap_dev_open 76561cd33eSHarry Liebel }; 77561cd33eSHarry Liebel 78561cd33eSHarry Liebel 79625de1d4SDan Handley static const io_dev_funcs_t memmap_dev_funcs = { 80561cd33eSHarry Liebel .type = device_type_memmap, 81561cd33eSHarry Liebel .open = memmap_block_open, 82561cd33eSHarry Liebel .seek = memmap_block_seek, 836d70bfa1SGerald Lejeune .size = memmap_block_len, 84561cd33eSHarry Liebel .read = memmap_block_read, 85561cd33eSHarry Liebel .write = memmap_block_write, 86561cd33eSHarry Liebel .close = memmap_block_close, 87561cd33eSHarry Liebel .dev_init = NULL, 88561cd33eSHarry Liebel .dev_close = memmap_dev_close, 89561cd33eSHarry Liebel }; 90561cd33eSHarry Liebel 91561cd33eSHarry Liebel 92625de1d4SDan Handley /* No state associated with this device so structure can be const */ 93625de1d4SDan Handley static const io_dev_info_t memmap_dev_info = { 94561cd33eSHarry Liebel .funcs = &memmap_dev_funcs, 95561cd33eSHarry Liebel .info = (uintptr_t)NULL 96561cd33eSHarry Liebel }; 97561cd33eSHarry Liebel 98561cd33eSHarry Liebel 99561cd33eSHarry Liebel /* Open a connection to the memmap device */ 10065cd299fSSoren Brinkmann static int memmap_dev_open(const uintptr_t dev_spec __unused, 101fb037bfbSDan Handley io_dev_info_t **dev_info) 102561cd33eSHarry Liebel { 103561cd33eSHarry Liebel assert(dev_info != NULL); 104625de1d4SDan Handley *dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */ 105561cd33eSHarry Liebel 106e098e244SJuan Castillo return 0; 107561cd33eSHarry Liebel } 108561cd33eSHarry Liebel 109561cd33eSHarry Liebel 110561cd33eSHarry Liebel 111561cd33eSHarry Liebel /* Close a connection to the memmap device */ 112fb037bfbSDan Handley static int memmap_dev_close(io_dev_info_t *dev_info) 113561cd33eSHarry Liebel { 114561cd33eSHarry Liebel /* NOP */ 115561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */ 116e098e244SJuan Castillo return 0; 117561cd33eSHarry Liebel } 118561cd33eSHarry Liebel 119561cd33eSHarry Liebel 120561cd33eSHarry Liebel /* Open a file on the memmap device */ 121625de1d4SDan Handley static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec, 122fb037bfbSDan Handley io_entity_t *entity) 123561cd33eSHarry Liebel { 124e098e244SJuan Castillo int result = -ENOMEM; 125fb037bfbSDan Handley const io_block_spec_t *block_spec = (io_block_spec_t *)spec; 126561cd33eSHarry Liebel 127*69c043b2SJeenu Viswambharan assert(block_spec->length >= 0); 128*69c043b2SJeenu Viswambharan 129561cd33eSHarry Liebel /* Since we need to track open state for seek() we only allow one open 130561cd33eSHarry Liebel * spec at a time. When we have dynamic memory we can malloc and set 131561cd33eSHarry Liebel * entity->info. 132561cd33eSHarry Liebel */ 133561cd33eSHarry Liebel if (current_file.in_use == 0) { 134561cd33eSHarry Liebel assert(block_spec != NULL); 135561cd33eSHarry Liebel assert(entity != NULL); 136561cd33eSHarry Liebel 137561cd33eSHarry Liebel current_file.in_use = 1; 138561cd33eSHarry Liebel current_file.base = block_spec->offset; 139561cd33eSHarry Liebel /* File cursor offset for seek and incremental reads etc. */ 140561cd33eSHarry Liebel current_file.file_pos = 0; 1416d70bfa1SGerald Lejeune current_file.size = block_spec->length; 142561cd33eSHarry Liebel entity->info = (uintptr_t)¤t_file; 143e098e244SJuan Castillo result = 0; 144561cd33eSHarry Liebel } else { 14508c28d53SJeenu Viswambharan WARN("A Memmap device is already active. Close first.\n"); 146561cd33eSHarry Liebel } 147561cd33eSHarry Liebel 148561cd33eSHarry Liebel return result; 149561cd33eSHarry Liebel } 150561cd33eSHarry Liebel 151561cd33eSHarry Liebel 152561cd33eSHarry Liebel /* Seek to a particular file offset on the memmap device */ 153fb037bfbSDan Handley static int memmap_block_seek(io_entity_t *entity, int mode, ssize_t offset) 154561cd33eSHarry Liebel { 155e098e244SJuan Castillo int result = -ENOENT; 156*69c043b2SJeenu Viswambharan file_state_t *fp; 157561cd33eSHarry Liebel 158561cd33eSHarry Liebel /* We only support IO_SEEK_SET for the moment. */ 159561cd33eSHarry Liebel if (mode == IO_SEEK_SET) { 160561cd33eSHarry Liebel assert(entity != NULL); 161561cd33eSHarry Liebel 162*69c043b2SJeenu Viswambharan fp = (file_state_t *) entity->info; 163*69c043b2SJeenu Viswambharan 164*69c043b2SJeenu Viswambharan /* Assert that new file position is valid */ 165*69c043b2SJeenu Viswambharan assert((offset >= 0) && (offset < fp->size)); 166*69c043b2SJeenu Viswambharan 167*69c043b2SJeenu Viswambharan /* Reset file position */ 168*69c043b2SJeenu Viswambharan fp->file_pos = offset; 169e098e244SJuan Castillo result = 0; 170561cd33eSHarry Liebel } 171561cd33eSHarry Liebel 172561cd33eSHarry Liebel return result; 173561cd33eSHarry Liebel } 174561cd33eSHarry Liebel 175561cd33eSHarry Liebel 1766d70bfa1SGerald Lejeune /* Return the size of a file on the memmap device */ 1776d70bfa1SGerald Lejeune static int memmap_block_len(io_entity_t *entity, size_t *length) 1786d70bfa1SGerald Lejeune { 1796d70bfa1SGerald Lejeune assert(entity != NULL); 1806d70bfa1SGerald Lejeune assert(length != NULL); 1816d70bfa1SGerald Lejeune 1826d70bfa1SGerald Lejeune *length = ((file_state_t *)entity->info)->size; 1836d70bfa1SGerald Lejeune 1846d70bfa1SGerald Lejeune return 0; 1856d70bfa1SGerald Lejeune } 1866d70bfa1SGerald Lejeune 1876d70bfa1SGerald Lejeune 188561cd33eSHarry Liebel /* Read data from a file on the memmap device */ 189625de1d4SDan Handley static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, 190561cd33eSHarry Liebel size_t length, size_t *length_read) 191561cd33eSHarry Liebel { 192fb037bfbSDan Handley file_state_t *fp; 193*69c043b2SJeenu Viswambharan size_t pos_after; 194561cd33eSHarry Liebel 195561cd33eSHarry Liebel assert(entity != NULL); 196625de1d4SDan Handley assert(buffer != (uintptr_t)NULL); 197561cd33eSHarry Liebel assert(length_read != NULL); 198561cd33eSHarry Liebel 199fb037bfbSDan Handley fp = (file_state_t *) entity->info; 200561cd33eSHarry Liebel 201*69c043b2SJeenu Viswambharan /* Assert that file position is valid for this read operation */ 202*69c043b2SJeenu Viswambharan pos_after = fp->file_pos + length; 203*69c043b2SJeenu Viswambharan assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); 204*69c043b2SJeenu Viswambharan 205625de1d4SDan Handley memcpy((void *)buffer, (void *)(fp->base + fp->file_pos), length); 206561cd33eSHarry Liebel 207561cd33eSHarry Liebel *length_read = length; 208*69c043b2SJeenu Viswambharan 209*69c043b2SJeenu Viswambharan /* Set file position after read */ 210*69c043b2SJeenu Viswambharan fp->file_pos = pos_after; 211561cd33eSHarry Liebel 212e098e244SJuan Castillo return 0; 213561cd33eSHarry Liebel } 214561cd33eSHarry Liebel 215561cd33eSHarry Liebel 216561cd33eSHarry Liebel /* Write data to a file on the memmap device */ 217625de1d4SDan Handley static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, 218561cd33eSHarry Liebel size_t length, size_t *length_written) 219561cd33eSHarry Liebel { 220fb037bfbSDan Handley file_state_t *fp; 221*69c043b2SJeenu Viswambharan size_t pos_after; 222561cd33eSHarry Liebel 223561cd33eSHarry Liebel assert(entity != NULL); 224625de1d4SDan Handley assert(buffer != (uintptr_t)NULL); 225561cd33eSHarry Liebel assert(length_written != NULL); 226561cd33eSHarry Liebel 227fb037bfbSDan Handley fp = (file_state_t *) entity->info; 228561cd33eSHarry Liebel 229*69c043b2SJeenu Viswambharan /* Assert that file position is valid for this write operation */ 230*69c043b2SJeenu Viswambharan pos_after = fp->file_pos + length; 231*69c043b2SJeenu Viswambharan assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); 232*69c043b2SJeenu Viswambharan 233625de1d4SDan Handley memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length); 234561cd33eSHarry Liebel 235561cd33eSHarry Liebel *length_written = length; 236561cd33eSHarry Liebel 237*69c043b2SJeenu Viswambharan /* Set file position after write */ 238*69c043b2SJeenu Viswambharan fp->file_pos = pos_after; 239561cd33eSHarry Liebel 240e098e244SJuan Castillo return 0; 241561cd33eSHarry Liebel } 242561cd33eSHarry Liebel 243561cd33eSHarry Liebel 244561cd33eSHarry Liebel /* Close a file on the memmap device */ 245fb037bfbSDan Handley static int memmap_block_close(io_entity_t *entity) 246561cd33eSHarry Liebel { 247561cd33eSHarry Liebel assert(entity != NULL); 248561cd33eSHarry Liebel 249561cd33eSHarry Liebel entity->info = 0; 250561cd33eSHarry Liebel 251561cd33eSHarry Liebel /* This would be a mem free() if we had malloc.*/ 252561cd33eSHarry Liebel memset((void *)¤t_file, 0, sizeof(current_file)); 253561cd33eSHarry Liebel 254e098e244SJuan Castillo return 0; 255561cd33eSHarry Liebel } 256561cd33eSHarry Liebel 257561cd33eSHarry Liebel 258561cd33eSHarry Liebel /* Exported functions */ 259561cd33eSHarry Liebel 260561cd33eSHarry Liebel /* Register the memmap driver with the IO abstraction */ 261625de1d4SDan Handley int register_io_dev_memmap(const io_dev_connector_t **dev_con) 262561cd33eSHarry Liebel { 263e098e244SJuan Castillo int result; 264561cd33eSHarry Liebel assert(dev_con != NULL); 265561cd33eSHarry Liebel 266561cd33eSHarry Liebel result = io_register_device(&memmap_dev_info); 267e098e244SJuan Castillo if (result == 0) 268561cd33eSHarry Liebel *dev_con = &memmap_dev_connector; 269561cd33eSHarry Liebel 270561cd33eSHarry Liebel return result; 271561cd33eSHarry Liebel } 272