Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <assert.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <semihosting.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 34 | #include <string.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 35 | |
| 36 | #ifndef SEMIHOSTING_SUPPORTED |
| 37 | #define SEMIHOSTING_SUPPORTED 1 |
| 38 | #endif |
| 39 | |
Dan Handley | a17fefa | 2014-05-14 12:38:32 +0100 | [diff] [blame] | 40 | long semihosting_call(unsigned long operation, |
| 41 | void *system_block_address); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 42 | |
| 43 | typedef struct { |
| 44 | const char *file_name; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 45 | unsigned long mode; |
| 46 | size_t name_length; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 47 | } smh_file_open_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 48 | |
| 49 | typedef struct { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 50 | long handle; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 51 | uintptr_t buffer; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 52 | size_t length; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 53 | } smh_file_read_write_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 54 | |
| 55 | typedef struct { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 56 | long handle; |
| 57 | ssize_t location; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 58 | } smh_file_seek_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 59 | |
| 60 | typedef struct { |
| 61 | char *command_line; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 62 | size_t command_length; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 63 | } smh_system_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 64 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 65 | long semihosting_connection_supported(void) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 66 | { |
| 67 | return SEMIHOSTING_SUPPORTED; |
| 68 | } |
| 69 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 70 | long semihosting_file_open(const char *file_name, size_t mode) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 71 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 72 | smh_file_open_block_t open_block; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 73 | |
| 74 | open_block.file_name = file_name; |
| 75 | open_block.mode = mode; |
| 76 | open_block.name_length = strlen(file_name); |
| 77 | |
| 78 | return semihosting_call(SEMIHOSTING_SYS_OPEN, |
| 79 | (void *) &open_block); |
| 80 | } |
| 81 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 82 | long semihosting_file_seek(long file_handle, ssize_t offset) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 83 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 84 | smh_file_seek_block_t seek_block; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 85 | long result; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 86 | |
| 87 | seek_block.handle = file_handle; |
| 88 | seek_block.location = offset; |
| 89 | |
| 90 | result = semihosting_call(SEMIHOSTING_SYS_SEEK, |
| 91 | (void *) &seek_block); |
| 92 | |
| 93 | if (result) |
| 94 | result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0); |
| 95 | |
| 96 | return result; |
| 97 | } |
| 98 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 99 | long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 100 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 101 | smh_file_read_write_block_t read_block; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 102 | long result = -EINVAL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 103 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 104 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 105 | return result; |
| 106 | |
| 107 | read_block.handle = file_handle; |
| 108 | read_block.buffer = buffer; |
| 109 | read_block.length = *length; |
| 110 | |
| 111 | result = semihosting_call(SEMIHOSTING_SYS_READ, |
| 112 | (void *) &read_block); |
| 113 | |
| 114 | if (result == *length) { |
| 115 | return -EINVAL; |
| 116 | } else if (result < *length) { |
| 117 | *length -= result; |
| 118 | return 0; |
| 119 | } else |
| 120 | return result; |
| 121 | } |
| 122 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 123 | long semihosting_file_write(long file_handle, |
| 124 | size_t *length, |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 125 | const uintptr_t buffer) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 126 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 127 | smh_file_read_write_block_t write_block; |
Juan Castillo | 03fe10c | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 128 | long result = -EINVAL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 129 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 130 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 131 | return -EINVAL; |
| 132 | |
| 133 | write_block.handle = file_handle; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 134 | write_block.buffer = (uintptr_t)buffer; /* cast away const */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 135 | write_block.length = *length; |
| 136 | |
Juan Castillo | 03fe10c | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 137 | result = semihosting_call(SEMIHOSTING_SYS_WRITE, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 138 | (void *) &write_block); |
| 139 | |
Juan Castillo | 03fe10c | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 140 | *length = result; |
| 141 | |
| 142 | return (result == 0) ? 0 : -EINVAL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 145 | long semihosting_file_close(long file_handle) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 146 | { |
| 147 | return semihosting_call(SEMIHOSTING_SYS_CLOSE, |
| 148 | (void *) &file_handle); |
| 149 | } |
| 150 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 151 | long semihosting_file_length(long file_handle) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 152 | { |
| 153 | return semihosting_call(SEMIHOSTING_SYS_FLEN, |
| 154 | (void *) &file_handle); |
| 155 | } |
| 156 | |
| 157 | char semihosting_read_char(void) |
| 158 | { |
| 159 | return semihosting_call(SEMIHOSTING_SYS_READC, NULL); |
| 160 | } |
| 161 | |
| 162 | void semihosting_write_char(char character) |
| 163 | { |
| 164 | semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character); |
| 165 | } |
| 166 | |
| 167 | void semihosting_write_string(char *string) |
| 168 | { |
| 169 | semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string); |
| 170 | } |
| 171 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 172 | long semihosting_system(char *command_line) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 173 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 174 | smh_system_block_t system_block; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 175 | |
| 176 | system_block.command_line = command_line; |
| 177 | system_block.command_length = strlen(command_line); |
| 178 | |
| 179 | return semihosting_call(SEMIHOSTING_SYS_SYSTEM, |
| 180 | (void *) &system_block); |
| 181 | } |
| 182 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 183 | long semihosting_get_flen(const char *file_name) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 184 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 185 | long file_handle; |
| 186 | size_t length; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 187 | |
| 188 | assert(semihosting_connection_supported()); |
| 189 | |
| 190 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 191 | if (file_handle == -1) |
| 192 | return file_handle; |
| 193 | |
| 194 | /* Find the length of the file */ |
| 195 | length = semihosting_file_length(file_handle); |
| 196 | |
| 197 | return semihosting_file_close(file_handle) ? -1 : length; |
| 198 | } |
| 199 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 200 | long semihosting_download_file(const char *file_name, |
| 201 | size_t buf_size, |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 202 | uintptr_t buf) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 203 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 204 | long ret = -EINVAL; |
| 205 | size_t length; |
| 206 | long file_handle; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 207 | |
| 208 | /* Null pointer check */ |
| 209 | if (!buf) |
| 210 | return ret; |
| 211 | |
| 212 | assert(semihosting_connection_supported()); |
| 213 | |
| 214 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 215 | if (file_handle == -1) |
| 216 | return ret; |
| 217 | |
| 218 | /* Find the actual length of the file */ |
| 219 | length = semihosting_file_length(file_handle); |
| 220 | if (length == -1) |
| 221 | goto semihosting_fail; |
| 222 | |
| 223 | /* Signal error if we do not have enough space for the file */ |
| 224 | if (length > buf_size) |
| 225 | goto semihosting_fail; |
| 226 | |
| 227 | /* |
| 228 | * A successful read will return 0 in which case we pass back |
| 229 | * the actual number of bytes read. Else we pass a negative |
| 230 | * value indicating an error. |
| 231 | */ |
| 232 | ret = semihosting_file_read(file_handle, &length, buf); |
| 233 | if (ret) |
| 234 | goto semihosting_fail; |
| 235 | else |
| 236 | ret = length; |
| 237 | |
| 238 | semihosting_fail: |
| 239 | semihosting_file_close(file_handle); |
| 240 | return ret; |
| 241 | } |