Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Ambroise Vincent | a88a35d | 2019-02-14 09:48:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 9 | #include <string.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 10 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <lib/semihosting.h> |
| 12 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 13 | #ifndef SEMIHOSTING_SUPPORTED |
| 14 | #define SEMIHOSTING_SUPPORTED 1 |
| 15 | #endif |
| 16 | |
Dan Handley | a17fefa | 2014-05-14 12:38:32 +0100 | [diff] [blame] | 17 | long semihosting_call(unsigned long operation, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 18 | uintptr_t system_block_address); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 19 | |
| 20 | typedef struct { |
| 21 | const char *file_name; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 22 | unsigned long mode; |
| 23 | size_t name_length; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 24 | } smh_file_open_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 25 | |
| 26 | typedef struct { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 27 | long handle; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 28 | uintptr_t buffer; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 29 | size_t length; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 30 | } smh_file_read_write_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 31 | |
| 32 | typedef struct { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 33 | long handle; |
| 34 | ssize_t location; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 35 | } smh_file_seek_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 36 | |
| 37 | typedef struct { |
| 38 | char *command_line; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 39 | size_t command_length; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 40 | } smh_system_block_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 41 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 42 | long semihosting_connection_supported(void) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 43 | { |
| 44 | return SEMIHOSTING_SUPPORTED; |
| 45 | } |
| 46 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 47 | long semihosting_file_open(const char *file_name, size_t mode) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 48 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 49 | smh_file_open_block_t open_block; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 50 | |
| 51 | open_block.file_name = file_name; |
| 52 | open_block.mode = mode; |
| 53 | open_block.name_length = strlen(file_name); |
| 54 | |
| 55 | return semihosting_call(SEMIHOSTING_SYS_OPEN, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 56 | (uintptr_t) &open_block); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 59 | long semihosting_file_seek(long file_handle, ssize_t offset) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 60 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 61 | smh_file_seek_block_t seek_block; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 62 | long result; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 63 | |
| 64 | seek_block.handle = file_handle; |
| 65 | seek_block.location = offset; |
| 66 | |
| 67 | result = semihosting_call(SEMIHOSTING_SYS_SEEK, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 68 | (uintptr_t) &seek_block); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 69 | |
| 70 | if (result) |
| 71 | result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0); |
| 72 | |
| 73 | return result; |
| 74 | } |
| 75 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 76 | 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] | 77 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 78 | smh_file_read_write_block_t read_block; |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 79 | long result = -EINVAL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 80 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 81 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 82 | return result; |
| 83 | |
| 84 | read_block.handle = file_handle; |
| 85 | read_block.buffer = buffer; |
| 86 | read_block.length = *length; |
| 87 | |
| 88 | result = semihosting_call(SEMIHOSTING_SYS_READ, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 89 | (uintptr_t) &read_block); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 90 | |
| 91 | if (result == *length) { |
| 92 | return -EINVAL; |
| 93 | } else if (result < *length) { |
| 94 | *length -= result; |
| 95 | return 0; |
| 96 | } else |
| 97 | return result; |
| 98 | } |
| 99 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 100 | long semihosting_file_write(long file_handle, |
| 101 | size_t *length, |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 102 | const uintptr_t buffer) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 103 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 104 | smh_file_read_write_block_t write_block; |
Juan Castillo | 03fe10c | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 105 | long result = -EINVAL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 106 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 107 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | |
| 110 | write_block.handle = file_handle; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 111 | write_block.buffer = (uintptr_t)buffer; /* cast away const */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 112 | write_block.length = *length; |
| 113 | |
Juan Castillo | 03fe10c | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 114 | result = semihosting_call(SEMIHOSTING_SYS_WRITE, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 115 | (uintptr_t) &write_block); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 116 | |
Juan Castillo | 03fe10c | 2015-07-07 15:36:33 +0100 | [diff] [blame] | 117 | *length = result; |
| 118 | |
| 119 | return (result == 0) ? 0 : -EINVAL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 122 | long semihosting_file_close(long file_handle) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 123 | { |
| 124 | return semihosting_call(SEMIHOSTING_SYS_CLOSE, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 125 | (uintptr_t) &file_handle); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 128 | long semihosting_file_length(long file_handle) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 129 | { |
| 130 | return semihosting_call(SEMIHOSTING_SYS_FLEN, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 131 | (uintptr_t) &file_handle); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | char semihosting_read_char(void) |
| 135 | { |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 136 | return semihosting_call(SEMIHOSTING_SYS_READC, 0); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void semihosting_write_char(char character) |
| 140 | { |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 141 | semihosting_call(SEMIHOSTING_SYS_WRITEC, (uintptr_t) &character); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void semihosting_write_string(char *string) |
| 145 | { |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 146 | semihosting_call(SEMIHOSTING_SYS_WRITE0, (uintptr_t) string); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 149 | long semihosting_system(char *command_line) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 150 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 151 | smh_system_block_t system_block; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 152 | |
| 153 | system_block.command_line = command_line; |
| 154 | system_block.command_length = strlen(command_line); |
| 155 | |
| 156 | return semihosting_call(SEMIHOSTING_SYS_SYSTEM, |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 157 | (uintptr_t) &system_block); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 160 | long semihosting_get_flen(const char *file_name) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 161 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 162 | long file_handle; |
Ambroise Vincent | a88a35d | 2019-02-14 09:48:21 +0000 | [diff] [blame] | 163 | long length; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 164 | |
| 165 | assert(semihosting_connection_supported()); |
| 166 | |
| 167 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 168 | if (file_handle == -1) |
| 169 | return file_handle; |
| 170 | |
| 171 | /* Find the length of the file */ |
| 172 | length = semihosting_file_length(file_handle); |
| 173 | |
| 174 | return semihosting_file_close(file_handle) ? -1 : length; |
| 175 | } |
| 176 | |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 177 | long semihosting_download_file(const char *file_name, |
| 178 | size_t buf_size, |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 179 | uintptr_t buf) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 180 | { |
Ryan Harkin | cd52932 | 2014-02-10 17:17:04 +0000 | [diff] [blame] | 181 | long ret = -EINVAL; |
| 182 | size_t length; |
| 183 | long file_handle; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 184 | |
| 185 | /* Null pointer check */ |
| 186 | if (!buf) |
| 187 | return ret; |
| 188 | |
| 189 | assert(semihosting_connection_supported()); |
| 190 | |
| 191 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 192 | if (file_handle == -1) |
| 193 | return ret; |
| 194 | |
| 195 | /* Find the actual length of the file */ |
| 196 | length = semihosting_file_length(file_handle); |
| 197 | if (length == -1) |
| 198 | goto semihosting_fail; |
| 199 | |
| 200 | /* Signal error if we do not have enough space for the file */ |
| 201 | if (length > buf_size) |
| 202 | goto semihosting_fail; |
| 203 | |
| 204 | /* |
| 205 | * A successful read will return 0 in which case we pass back |
| 206 | * the actual number of bytes read. Else we pass a negative |
| 207 | * value indicating an error. |
| 208 | */ |
| 209 | ret = semihosting_file_read(file_handle, &length, buf); |
| 210 | if (ret) |
| 211 | goto semihosting_fail; |
| 212 | else |
| 213 | ret = length; |
| 214 | |
| 215 | semihosting_fail: |
| 216 | semihosting_file_close(file_handle); |
| 217 | return ret; |
| 218 | } |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 219 | |
| 220 | void semihosting_exit(uint32_t reason, uint32_t subcode) |
| 221 | { |
| 222 | #ifdef __aarch64__ |
| 223 | uint64_t parameters[] = {reason, subcode}; |
| 224 | |
| 225 | (void) semihosting_call(SEMIHOSTING_SYS_EXIT, (uintptr_t) ¶meters); |
| 226 | #else |
| 227 | /* The subcode is not supported on AArch32. */ |
| 228 | (void) semihosting_call(SEMIHOSTING_SYS_EXIT, reason); |
| 229 | #endif |
| 230 | } |