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; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 128 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 129 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | |
| 132 | write_block.handle = file_handle; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 133 | write_block.buffer = (uintptr_t)buffer; /* cast away const */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 134 | write_block.length = *length; |
| 135 | |
| 136 | *length = semihosting_call(SEMIHOSTING_SYS_WRITE, |
| 137 | (void *) &write_block); |
| 138 | |
| 139 | return *length; |
| 140 | } |
| 141 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 142 | long semihosting_file_close(long file_handle) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 143 | { |
| 144 | return semihosting_call(SEMIHOSTING_SYS_CLOSE, |
| 145 | (void *) &file_handle); |
| 146 | } |
| 147 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 148 | long semihosting_file_length(long file_handle) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 149 | { |
| 150 | return semihosting_call(SEMIHOSTING_SYS_FLEN, |
| 151 | (void *) &file_handle); |
| 152 | } |
| 153 | |
| 154 | char semihosting_read_char(void) |
| 155 | { |
| 156 | return semihosting_call(SEMIHOSTING_SYS_READC, NULL); |
| 157 | } |
| 158 | |
| 159 | void semihosting_write_char(char character) |
| 160 | { |
| 161 | semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character); |
| 162 | } |
| 163 | |
| 164 | void semihosting_write_string(char *string) |
| 165 | { |
| 166 | semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string); |
| 167 | } |
| 168 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 169 | long semihosting_system(char *command_line) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 170 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 171 | smh_system_block_t system_block; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 172 | |
| 173 | system_block.command_line = command_line; |
| 174 | system_block.command_length = strlen(command_line); |
| 175 | |
| 176 | return semihosting_call(SEMIHOSTING_SYS_SYSTEM, |
| 177 | (void *) &system_block); |
| 178 | } |
| 179 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 180 | long semihosting_get_flen(const char *file_name) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 181 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 182 | long file_handle; |
| 183 | size_t length; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 184 | |
| 185 | assert(semihosting_connection_supported()); |
| 186 | |
| 187 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 188 | if (file_handle == -1) |
| 189 | return file_handle; |
| 190 | |
| 191 | /* Find the length of the file */ |
| 192 | length = semihosting_file_length(file_handle); |
| 193 | |
| 194 | return semihosting_file_close(file_handle) ? -1 : length; |
| 195 | } |
| 196 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 197 | long semihosting_download_file(const char *file_name, |
| 198 | size_t buf_size, |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 199 | uintptr_t buf) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 200 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 201 | long ret = -EINVAL; |
| 202 | size_t length; |
| 203 | long file_handle; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 204 | |
| 205 | /* Null pointer check */ |
| 206 | if (!buf) |
| 207 | return ret; |
| 208 | |
| 209 | assert(semihosting_connection_supported()); |
| 210 | |
| 211 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 212 | if (file_handle == -1) |
| 213 | return ret; |
| 214 | |
| 215 | /* Find the actual length of the file */ |
| 216 | length = semihosting_file_length(file_handle); |
| 217 | if (length == -1) |
| 218 | goto semihosting_fail; |
| 219 | |
| 220 | /* Signal error if we do not have enough space for the file */ |
| 221 | if (length > buf_size) |
| 222 | goto semihosting_fail; |
| 223 | |
| 224 | /* |
| 225 | * A successful read will return 0 in which case we pass back |
| 226 | * the actual number of bytes read. Else we pass a negative |
| 227 | * value indicating an error. |
| 228 | */ |
| 229 | ret = semihosting_file_read(file_handle, &length, buf); |
| 230 | if (ret) |
| 231 | goto semihosting_fail; |
| 232 | else |
| 233 | ret = length; |
| 234 | |
| 235 | semihosting_fail: |
| 236 | semihosting_file_close(file_handle); |
| 237 | return ret; |
| 238 | } |