1 /* 2 * Copyright (c) 2013-2014, ARM Limited and Contributors. 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 <errno.h> 33 #include <semihosting.h> 34 #include <string.h> 35 36 #ifndef SEMIHOSTING_SUPPORTED 37 #define SEMIHOSTING_SUPPORTED 1 38 #endif 39 40 long semihosting_call(unsigned long operation, 41 void *system_block_address); 42 43 typedef struct { 44 const char *file_name; 45 unsigned long mode; 46 size_t name_length; 47 } smh_file_open_block_t; 48 49 typedef struct { 50 long handle; 51 uintptr_t buffer; 52 size_t length; 53 } smh_file_read_write_block_t; 54 55 typedef struct { 56 long handle; 57 ssize_t location; 58 } smh_file_seek_block_t; 59 60 typedef struct { 61 char *command_line; 62 size_t command_length; 63 } smh_system_block_t; 64 65 long semihosting_connection_supported(void) 66 { 67 return SEMIHOSTING_SUPPORTED; 68 } 69 70 long semihosting_file_open(const char *file_name, size_t mode) 71 { 72 smh_file_open_block_t open_block; 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 82 long semihosting_file_seek(long file_handle, ssize_t offset) 83 { 84 smh_file_seek_block_t seek_block; 85 long result; 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 99 long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer) 100 { 101 smh_file_read_write_block_t read_block; 102 long result = -EINVAL; 103 104 if ((length == NULL) || (buffer == (uintptr_t)NULL)) 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 123 long semihosting_file_write(long file_handle, 124 size_t *length, 125 const uintptr_t buffer) 126 { 127 smh_file_read_write_block_t write_block; 128 long result = -EINVAL; 129 130 if ((length == NULL) || (buffer == (uintptr_t)NULL)) 131 return -EINVAL; 132 133 write_block.handle = file_handle; 134 write_block.buffer = (uintptr_t)buffer; /* cast away const */ 135 write_block.length = *length; 136 137 result = semihosting_call(SEMIHOSTING_SYS_WRITE, 138 (void *) &write_block); 139 140 *length = result; 141 142 return (result == 0) ? 0 : -EINVAL; 143 } 144 145 long semihosting_file_close(long file_handle) 146 { 147 return semihosting_call(SEMIHOSTING_SYS_CLOSE, 148 (void *) &file_handle); 149 } 150 151 long semihosting_file_length(long file_handle) 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 172 long semihosting_system(char *command_line) 173 { 174 smh_system_block_t system_block; 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 183 long semihosting_get_flen(const char *file_name) 184 { 185 long file_handle; 186 size_t length; 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 200 long semihosting_download_file(const char *file_name, 201 size_t buf_size, 202 uintptr_t buf) 203 { 204 long ret = -EINVAL; 205 size_t length; 206 long file_handle; 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 } 242