James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 1 | /* |
Roberto Vargas | 7fabe1a | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Dan Handley | 35e98e5 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 8 | #include <io_driver.h> |
Roberto Vargas | 7fabe1a | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 9 | #include <io_semihosting.h> |
Dan Handley | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 10 | #include <io_storage.h> |
Dan Handley | 35e98e5 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 11 | #include <semihosting.h> |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
| 15 | /* Identify the device type as semihosting */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 16 | static io_type_t device_type_sh(void) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 17 | { |
| 18 | return IO_TYPE_SEMIHOSTING; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /* Semi-hosting functions, device info and handle */ |
| 23 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 24 | static int sh_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); |
| 25 | static int sh_file_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 26 | io_entity_t *entity); |
| 27 | static int sh_file_seek(io_entity_t *entity, int mode, ssize_t offset); |
| 28 | static int sh_file_len(io_entity_t *entity, size_t *length); |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 29 | static int sh_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 30 | size_t *length_read); |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 31 | static int sh_file_write(io_entity_t *entity, const uintptr_t buffer, |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 32 | size_t length, size_t *length_written); |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 33 | static int sh_file_close(io_entity_t *entity); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 34 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 35 | static const io_dev_connector_t sh_dev_connector = { |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 36 | .dev_open = sh_dev_open |
| 37 | }; |
| 38 | |
| 39 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 40 | static const io_dev_funcs_t sh_dev_funcs = { |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 41 | .type = device_type_sh, |
| 42 | .open = sh_file_open, |
| 43 | .seek = sh_file_seek, |
| 44 | .size = sh_file_len, |
| 45 | .read = sh_file_read, |
| 46 | .write = sh_file_write, |
| 47 | .close = sh_file_close, |
| 48 | .dev_init = NULL, /* NOP */ |
| 49 | .dev_close = NULL, /* NOP */ |
| 50 | }; |
| 51 | |
| 52 | |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 53 | /* No state associated with this device so structure can be const */ |
| 54 | static const io_dev_info_t sh_dev_info = { |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 55 | .funcs = &sh_dev_funcs, |
| 56 | .info = (uintptr_t)NULL |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | /* Open a connection to the semi-hosting device */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 61 | static int sh_dev_open(const uintptr_t dev_spec __unused, |
| 62 | io_dev_info_t **dev_info) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 63 | { |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 64 | assert(dev_info != NULL); |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 65 | *dev_info = (io_dev_info_t *)&sh_dev_info; /* cast away const */ |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 66 | return 0; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | /* Open a file on the semi-hosting device */ |
Soren Brinkmann | 65cd299 | 2016-01-14 10:11:05 -0800 | [diff] [blame] | 71 | static int sh_file_open(io_dev_info_t *dev_info __unused, |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 72 | const uintptr_t spec, io_entity_t *entity) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 73 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 74 | int result = -ENOENT; |
dp-arm | dae695a | 2017-02-09 10:25:29 +0000 | [diff] [blame] | 75 | long sh_result; |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 76 | const io_file_spec_t *file_spec = (const io_file_spec_t *)spec; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 77 | |
| 78 | assert(file_spec != NULL); |
| 79 | assert(entity != NULL); |
| 80 | |
| 81 | sh_result = semihosting_file_open(file_spec->path, file_spec->mode); |
| 82 | |
| 83 | if (sh_result > 0) { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 84 | entity->info = (uintptr_t)sh_result; |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 85 | result = 0; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 86 | } |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* Seek to a particular file offset on the semi-hosting device */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 92 | static int sh_file_seek(io_entity_t *entity, int mode, ssize_t offset) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 93 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 94 | long file_handle, sh_result; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 95 | |
| 96 | assert(entity != NULL); |
| 97 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 98 | file_handle = (long)entity->info; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 99 | |
| 100 | sh_result = semihosting_file_seek(file_handle, offset); |
| 101 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 102 | return (sh_result == 0) ? 0 : -ENOENT; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | /* Return the size of a file on the semi-hosting device */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 107 | static int sh_file_len(io_entity_t *entity, size_t *length) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 108 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 109 | int result = -ENOENT; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 110 | |
| 111 | assert(entity != NULL); |
| 112 | assert(length != NULL); |
| 113 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 114 | long sh_handle = (long)entity->info; |
| 115 | long sh_result = semihosting_file_length(sh_handle); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 116 | |
| 117 | if (sh_result >= 0) { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 118 | result = 0; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 119 | *length = (size_t)sh_result; |
| 120 | } |
| 121 | |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /* Read data from a file on the semi-hosting device */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 127 | static int sh_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 128 | size_t *length_read) |
| 129 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 130 | int result = -ENOENT; |
dp-arm | dae695a | 2017-02-09 10:25:29 +0000 | [diff] [blame] | 131 | long sh_result; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 132 | size_t bytes = length; |
| 133 | long file_handle; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 134 | |
| 135 | assert(entity != NULL); |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 136 | assert(buffer != (uintptr_t)NULL); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 137 | assert(length_read != NULL); |
| 138 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 139 | file_handle = (long)entity->info; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 140 | |
| 141 | sh_result = semihosting_file_read(file_handle, &bytes, buffer); |
| 142 | |
| 143 | if (sh_result >= 0) { |
| 144 | *length_read = (bytes != length) ? bytes : length; |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 145 | result = 0; |
| 146 | } |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 147 | |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* Write data to a file on the semi-hosting device */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 153 | static int sh_file_write(io_entity_t *entity, const uintptr_t buffer, |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 154 | size_t length, size_t *length_written) |
| 155 | { |
dp-arm | dae695a | 2017-02-09 10:25:29 +0000 | [diff] [blame] | 156 | long sh_result; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 157 | long file_handle; |
| 158 | size_t bytes = length; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 159 | |
| 160 | assert(entity != NULL); |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 161 | assert(buffer != (uintptr_t)NULL); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 162 | assert(length_written != NULL); |
| 163 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 164 | file_handle = (long)entity->info; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 165 | |
| 166 | sh_result = semihosting_file_write(file_handle, &bytes, buffer); |
| 167 | |
Juan Castillo | 31833af | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 168 | *length_written = length - bytes; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 169 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 170 | return (sh_result == 0) ? 0 : -ENOENT; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | /* Close a file on the semi-hosting device */ |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 175 | static int sh_file_close(io_entity_t *entity) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 176 | { |
dp-arm | dae695a | 2017-02-09 10:25:29 +0000 | [diff] [blame] | 177 | long sh_result; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 178 | long file_handle; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 179 | |
| 180 | assert(entity != NULL); |
| 181 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 182 | file_handle = (long)entity->info; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 183 | |
| 184 | sh_result = semihosting_file_close(file_handle); |
| 185 | |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 186 | return (sh_result >= 0) ? 0 : -ENOENT; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | /* Exported functions */ |
| 191 | |
| 192 | /* Register the semi-hosting driver with the IO abstraction */ |
Dan Handley | 625de1d | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 193 | int register_io_dev_sh(const io_dev_connector_t **dev_con) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 194 | { |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 195 | int result; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 196 | assert(dev_con != NULL); |
| 197 | |
| 198 | result = io_register_device(&sh_dev_info); |
Juan Castillo | e098e24 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 199 | if (result == 0) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 200 | *dev_con = &sh_dev_connector; |
| 201 | |
| 202 | return result; |
| 203 | } |