Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | #include <semihosting.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #ifndef SEMIHOSTING_SUPPORTED |
| 13 | #define SEMIHOSTING_SUPPORTED 1 |
| 14 | #endif |
| 15 | |
| 16 | long semihosting_call(unsigned long operation, |
| 17 | void *system_block_address); |
| 18 | |
| 19 | typedef struct { |
| 20 | const char *file_name; |
| 21 | unsigned long mode; |
| 22 | size_t name_length; |
| 23 | } smh_file_open_block_t; |
| 24 | |
| 25 | typedef struct { |
| 26 | long handle; |
| 27 | uintptr_t buffer; |
| 28 | size_t length; |
| 29 | } smh_file_read_write_block_t; |
| 30 | |
| 31 | typedef struct { |
| 32 | long handle; |
| 33 | ssize_t location; |
| 34 | } smh_file_seek_block_t; |
| 35 | |
| 36 | typedef struct { |
| 37 | char *command_line; |
| 38 | size_t command_length; |
| 39 | } smh_system_block_t; |
| 40 | |
| 41 | long semihosting_connection_supported(void) |
| 42 | { |
| 43 | return SEMIHOSTING_SUPPORTED; |
| 44 | } |
| 45 | |
| 46 | long semihosting_file_open(const char *file_name, size_t mode) |
| 47 | { |
| 48 | smh_file_open_block_t open_block; |
| 49 | |
| 50 | open_block.file_name = file_name; |
| 51 | open_block.mode = mode; |
| 52 | open_block.name_length = strlen(file_name); |
| 53 | |
| 54 | return semihosting_call(SEMIHOSTING_SYS_OPEN, |
| 55 | (void *) &open_block); |
| 56 | } |
| 57 | |
| 58 | long semihosting_file_seek(long file_handle, ssize_t offset) |
| 59 | { |
| 60 | smh_file_seek_block_t seek_block; |
| 61 | long result; |
| 62 | |
| 63 | seek_block.handle = file_handle; |
| 64 | seek_block.location = offset; |
| 65 | |
| 66 | result = semihosting_call(SEMIHOSTING_SYS_SEEK, |
| 67 | (void *) &seek_block); |
| 68 | |
| 69 | if (result) |
| 70 | result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0); |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer) |
| 76 | { |
| 77 | smh_file_read_write_block_t read_block; |
| 78 | long result = -EINVAL; |
| 79 | |
| 80 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
| 81 | return result; |
| 82 | |
| 83 | read_block.handle = file_handle; |
| 84 | read_block.buffer = buffer; |
| 85 | read_block.length = *length; |
| 86 | |
| 87 | result = semihosting_call(SEMIHOSTING_SYS_READ, |
| 88 | (void *) &read_block); |
| 89 | |
| 90 | if (result == *length) { |
| 91 | return -EINVAL; |
| 92 | } else if (result < *length) { |
| 93 | *length -= result; |
| 94 | return 0; |
| 95 | } else |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | long semihosting_file_write(long file_handle, |
| 100 | size_t *length, |
| 101 | const uintptr_t buffer) |
| 102 | { |
| 103 | smh_file_read_write_block_t write_block; |
| 104 | |
| 105 | if ((length == NULL) || (buffer == (uintptr_t)NULL)) |
| 106 | return -EINVAL; |
| 107 | |
| 108 | write_block.handle = file_handle; |
| 109 | write_block.buffer = (uintptr_t)buffer; /* cast away const */ |
| 110 | write_block.length = *length; |
| 111 | |
| 112 | *length = semihosting_call(SEMIHOSTING_SYS_WRITE, |
| 113 | (void *) &write_block); |
| 114 | |
| 115 | return *length; |
| 116 | } |
| 117 | |
| 118 | long semihosting_file_close(long file_handle) |
| 119 | { |
| 120 | return semihosting_call(SEMIHOSTING_SYS_CLOSE, |
| 121 | (void *) &file_handle); |
| 122 | } |
| 123 | |
| 124 | long semihosting_file_length(long file_handle) |
| 125 | { |
| 126 | return semihosting_call(SEMIHOSTING_SYS_FLEN, |
| 127 | (void *) &file_handle); |
| 128 | } |
| 129 | |
| 130 | char semihosting_read_char(void) |
| 131 | { |
| 132 | return semihosting_call(SEMIHOSTING_SYS_READC, NULL); |
| 133 | } |
| 134 | |
| 135 | void semihosting_write_char(char character) |
| 136 | { |
| 137 | semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character); |
| 138 | } |
| 139 | |
| 140 | void semihosting_write_string(char *string) |
| 141 | { |
| 142 | semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string); |
| 143 | } |
| 144 | |
| 145 | long semihosting_system(char *command_line) |
| 146 | { |
| 147 | smh_system_block_t system_block; |
| 148 | |
| 149 | system_block.command_line = command_line; |
| 150 | system_block.command_length = strlen(command_line); |
| 151 | |
| 152 | return semihosting_call(SEMIHOSTING_SYS_SYSTEM, |
| 153 | (void *) &system_block); |
| 154 | } |
| 155 | |
| 156 | long semihosting_get_flen(const char *file_name) |
| 157 | { |
| 158 | long file_handle; |
| 159 | size_t length; |
| 160 | |
| 161 | assert(semihosting_connection_supported()); |
| 162 | |
| 163 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 164 | if (file_handle == -1) |
| 165 | return file_handle; |
| 166 | |
| 167 | /* Find the length of the file */ |
| 168 | length = semihosting_file_length(file_handle); |
| 169 | |
| 170 | return semihosting_file_close(file_handle) ? -1 : length; |
| 171 | } |
| 172 | |
| 173 | long semihosting_download_file(const char *file_name, |
| 174 | size_t buf_size, |
| 175 | uintptr_t buf) |
| 176 | { |
| 177 | long ret = -EINVAL; |
| 178 | size_t length; |
| 179 | long file_handle; |
| 180 | |
| 181 | /* Null pointer check */ |
| 182 | if (!buf) |
| 183 | return ret; |
| 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 ret; |
| 190 | |
| 191 | /* Find the actual length of the file */ |
| 192 | length = semihosting_file_length(file_handle); |
| 193 | if (length == -1) |
| 194 | goto semihosting_fail; |
| 195 | |
| 196 | /* Signal error if we do not have enough space for the file */ |
| 197 | if (length > buf_size) |
| 198 | goto semihosting_fail; |
| 199 | |
| 200 | /* |
| 201 | * A successful read will return 0 in which case we pass back |
| 202 | * the actual number of bytes read. Else we pass a negative |
| 203 | * value indicating an error. |
| 204 | */ |
| 205 | ret = semihosting_file_read(file_handle, &length, buf); |
| 206 | if (ret) |
| 207 | goto semihosting_fail; |
| 208 | else |
| 209 | ret = length; |
| 210 | |
| 211 | semihosting_fail: |
| 212 | semihosting_file_close(file_handle); |
| 213 | return ret; |
| 214 | } |