Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013, ARM Limited. All rights reserved. |
| 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> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <errno.h> |
| 35 | #include <semihosting.h> |
| 36 | |
| 37 | #ifndef SEMIHOSTING_SUPPORTED |
| 38 | #define SEMIHOSTING_SUPPORTED 1 |
| 39 | #endif |
| 40 | |
| 41 | extern int semihosting_call(unsigned int operation, |
| 42 | void *system_block_address); |
| 43 | |
| 44 | typedef struct { |
| 45 | const char *file_name; |
| 46 | unsigned int mode; |
| 47 | unsigned int name_length; |
| 48 | } smh_file_open_block; |
| 49 | |
| 50 | typedef struct { |
| 51 | int handle; |
| 52 | void *buffer; |
| 53 | unsigned int length; |
| 54 | } smh_file_read_write_block; |
| 55 | |
| 56 | typedef struct { |
| 57 | int handle; |
| 58 | unsigned int location; |
| 59 | } smh_file_seek_block; |
| 60 | |
| 61 | typedef struct { |
| 62 | char *command_line; |
| 63 | unsigned int command_length; |
| 64 | } smh_system_block; |
| 65 | |
| 66 | int semihosting_connection_supported(void) |
| 67 | { |
| 68 | return SEMIHOSTING_SUPPORTED; |
| 69 | } |
| 70 | |
| 71 | int semihosting_file_open(const char *file_name, unsigned int mode) |
| 72 | { |
| 73 | smh_file_open_block open_block; |
| 74 | |
| 75 | open_block.file_name = file_name; |
| 76 | open_block.mode = mode; |
| 77 | open_block.name_length = strlen(file_name); |
| 78 | |
| 79 | return semihosting_call(SEMIHOSTING_SYS_OPEN, |
| 80 | (void *) &open_block); |
| 81 | } |
| 82 | |
| 83 | int semihosting_file_seek(int file_handle, unsigned int offset) |
| 84 | { |
| 85 | smh_file_seek_block seek_block; |
| 86 | int result; |
| 87 | |
| 88 | seek_block.handle = file_handle; |
| 89 | seek_block.location = offset; |
| 90 | |
| 91 | result = semihosting_call(SEMIHOSTING_SYS_SEEK, |
| 92 | (void *) &seek_block); |
| 93 | |
| 94 | if (result) |
| 95 | result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0); |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | int semihosting_file_read(int file_handle, int *length, void *buffer) |
| 101 | { |
| 102 | smh_file_read_write_block read_block; |
| 103 | int result = -EINVAL; |
| 104 | |
| 105 | if ((length == NULL) || (buffer == NULL)) |
| 106 | return result; |
| 107 | |
| 108 | read_block.handle = file_handle; |
| 109 | read_block.buffer = buffer; |
| 110 | read_block.length = *length; |
| 111 | |
| 112 | result = semihosting_call(SEMIHOSTING_SYS_READ, |
| 113 | (void *) &read_block); |
| 114 | |
| 115 | if (result == *length) { |
| 116 | return -EINVAL; |
| 117 | } else if (result < *length) { |
| 118 | *length -= result; |
| 119 | return 0; |
| 120 | } else |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | int semihosting_file_write(int file_handle, int *length, void *buffer) |
| 125 | { |
| 126 | smh_file_read_write_block write_block; |
| 127 | |
| 128 | if ((length == NULL) || (buffer == NULL)) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | write_block.handle = file_handle; |
| 132 | write_block.buffer = buffer; |
| 133 | write_block.length = *length; |
| 134 | |
| 135 | *length = semihosting_call(SEMIHOSTING_SYS_WRITE, |
| 136 | (void *) &write_block); |
| 137 | |
| 138 | return *length; |
| 139 | } |
| 140 | |
| 141 | int semihosting_file_close(int file_handle) |
| 142 | { |
| 143 | return semihosting_call(SEMIHOSTING_SYS_CLOSE, |
| 144 | (void *) &file_handle); |
| 145 | } |
| 146 | |
| 147 | int semihosting_file_length(int file_handle) |
| 148 | { |
| 149 | return semihosting_call(SEMIHOSTING_SYS_FLEN, |
| 150 | (void *) &file_handle); |
| 151 | } |
| 152 | |
| 153 | char semihosting_read_char(void) |
| 154 | { |
| 155 | return semihosting_call(SEMIHOSTING_SYS_READC, NULL); |
| 156 | } |
| 157 | |
| 158 | void semihosting_write_char(char character) |
| 159 | { |
| 160 | semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character); |
| 161 | } |
| 162 | |
| 163 | void semihosting_write_string(char *string) |
| 164 | { |
| 165 | semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string); |
| 166 | } |
| 167 | |
| 168 | int semihosting_system(char *command_line) |
| 169 | { |
| 170 | smh_system_block system_block; |
| 171 | |
| 172 | system_block.command_line = command_line; |
| 173 | system_block.command_length = strlen(command_line); |
| 174 | |
| 175 | return semihosting_call(SEMIHOSTING_SYS_SYSTEM, |
| 176 | (void *) &system_block); |
| 177 | } |
| 178 | |
| 179 | int semihosting_get_flen(const char *file_name) |
| 180 | { |
| 181 | int file_handle, length; |
| 182 | |
| 183 | assert(semihosting_connection_supported()); |
| 184 | |
| 185 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 186 | if (file_handle == -1) |
| 187 | return file_handle; |
| 188 | |
| 189 | /* Find the length of the file */ |
| 190 | length = semihosting_file_length(file_handle); |
| 191 | |
| 192 | return semihosting_file_close(file_handle) ? -1 : length; |
| 193 | } |
| 194 | |
| 195 | int semihosting_download_file(const char *file_name, |
| 196 | int buf_size, |
| 197 | void *buf) |
| 198 | { |
| 199 | int ret = -EINVAL, file_handle, length; |
| 200 | |
| 201 | /* Null pointer check */ |
| 202 | if (!buf) |
| 203 | return ret; |
| 204 | |
| 205 | assert(semihosting_connection_supported()); |
| 206 | |
| 207 | file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); |
| 208 | if (file_handle == -1) |
| 209 | return ret; |
| 210 | |
| 211 | /* Find the actual length of the file */ |
| 212 | length = semihosting_file_length(file_handle); |
| 213 | if (length == -1) |
| 214 | goto semihosting_fail; |
| 215 | |
| 216 | /* Signal error if we do not have enough space for the file */ |
| 217 | if (length > buf_size) |
| 218 | goto semihosting_fail; |
| 219 | |
| 220 | /* |
| 221 | * A successful read will return 0 in which case we pass back |
| 222 | * the actual number of bytes read. Else we pass a negative |
| 223 | * value indicating an error. |
| 224 | */ |
| 225 | ret = semihosting_file_read(file_handle, &length, buf); |
| 226 | if (ret) |
| 227 | goto semihosting_fail; |
| 228 | else |
| 229 | ret = length; |
| 230 | |
| 231 | semihosting_fail: |
| 232 | semihosting_file_close(file_handle); |
| 233 | return ret; |
| 234 | } |