blob: 849ec1207e2018926fbdfc9c2f9e4e899397b995 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
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 Gupta4f6ad662013-10-25 09:08:21 +010032#include <errno.h>
33#include <semihosting.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010034#include <string.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010035
36#ifndef SEMIHOSTING_SUPPORTED
37#define SEMIHOSTING_SUPPORTED 1
38#endif
39
Dan Handleya17fefa2014-05-14 12:38:32 +010040long semihosting_call(unsigned long operation,
41 void *system_block_address);
Achin Gupta4f6ad662013-10-25 09:08:21 +010042
43typedef struct {
44 const char *file_name;
Ryan Harkincd529322014-02-10 17:17:04 +000045 unsigned long mode;
46 size_t name_length;
Dan Handleye2712bc2014-04-10 15:37:22 +010047} smh_file_open_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010048
49typedef struct {
Ryan Harkincd529322014-02-10 17:17:04 +000050 long handle;
Dan Handleya4cb68e2014-04-23 13:47:06 +010051 uintptr_t buffer;
Ryan Harkincd529322014-02-10 17:17:04 +000052 size_t length;
Dan Handleye2712bc2014-04-10 15:37:22 +010053} smh_file_read_write_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010054
55typedef struct {
Ryan Harkincd529322014-02-10 17:17:04 +000056 long handle;
57 ssize_t location;
Dan Handleye2712bc2014-04-10 15:37:22 +010058} smh_file_seek_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010059
60typedef struct {
61 char *command_line;
Ryan Harkincd529322014-02-10 17:17:04 +000062 size_t command_length;
Dan Handleye2712bc2014-04-10 15:37:22 +010063} smh_system_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010064
Ryan Harkincd529322014-02-10 17:17:04 +000065long semihosting_connection_supported(void)
Achin Gupta4f6ad662013-10-25 09:08:21 +010066{
67 return SEMIHOSTING_SUPPORTED;
68}
69
Ryan Harkincd529322014-02-10 17:17:04 +000070long semihosting_file_open(const char *file_name, size_t mode)
Achin Gupta4f6ad662013-10-25 09:08:21 +010071{
Dan Handleye2712bc2014-04-10 15:37:22 +010072 smh_file_open_block_t open_block;
Achin Gupta4f6ad662013-10-25 09:08:21 +010073
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 Harkincd529322014-02-10 17:17:04 +000082long semihosting_file_seek(long file_handle, ssize_t offset)
Achin Gupta4f6ad662013-10-25 09:08:21 +010083{
Dan Handleye2712bc2014-04-10 15:37:22 +010084 smh_file_seek_block_t seek_block;
Ryan Harkincd529322014-02-10 17:17:04 +000085 long result;
Achin Gupta4f6ad662013-10-25 09:08:21 +010086
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 Handleya4cb68e2014-04-23 13:47:06 +010099long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100100{
Dan Handleye2712bc2014-04-10 15:37:22 +0100101 smh_file_read_write_block_t read_block;
Ryan Harkincd529322014-02-10 17:17:04 +0000102 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103
Dan Handleya4cb68e2014-04-23 13:47:06 +0100104 if ((length == NULL) || (buffer == (uintptr_t)NULL))
Achin Gupta4f6ad662013-10-25 09:08:21 +0100105 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 Harkincd529322014-02-10 17:17:04 +0000123long semihosting_file_write(long file_handle,
124 size_t *length,
Dan Handleya4cb68e2014-04-23 13:47:06 +0100125 const uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100126{
Dan Handleye2712bc2014-04-10 15:37:22 +0100127 smh_file_read_write_block_t write_block;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100128
Dan Handleya4cb68e2014-04-23 13:47:06 +0100129 if ((length == NULL) || (buffer == (uintptr_t)NULL))
Achin Gupta4f6ad662013-10-25 09:08:21 +0100130 return -EINVAL;
131
132 write_block.handle = file_handle;
Dan Handleya4cb68e2014-04-23 13:47:06 +0100133 write_block.buffer = (uintptr_t)buffer; /* cast away const */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100134 write_block.length = *length;
135
136 *length = semihosting_call(SEMIHOSTING_SYS_WRITE,
137 (void *) &write_block);
138
139 return *length;
140}
141
Ryan Harkincd529322014-02-10 17:17:04 +0000142long semihosting_file_close(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100143{
144 return semihosting_call(SEMIHOSTING_SYS_CLOSE,
145 (void *) &file_handle);
146}
147
Ryan Harkincd529322014-02-10 17:17:04 +0000148long semihosting_file_length(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149{
150 return semihosting_call(SEMIHOSTING_SYS_FLEN,
151 (void *) &file_handle);
152}
153
154char semihosting_read_char(void)
155{
156 return semihosting_call(SEMIHOSTING_SYS_READC, NULL);
157}
158
159void semihosting_write_char(char character)
160{
161 semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character);
162}
163
164void semihosting_write_string(char *string)
165{
166 semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string);
167}
168
Ryan Harkincd529322014-02-10 17:17:04 +0000169long semihosting_system(char *command_line)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100170{
Dan Handleye2712bc2014-04-10 15:37:22 +0100171 smh_system_block_t system_block;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100172
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 Harkincd529322014-02-10 17:17:04 +0000180long semihosting_get_flen(const char *file_name)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100181{
Ryan Harkincd529322014-02-10 17:17:04 +0000182 long file_handle;
183 size_t length;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100184
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 Harkincd529322014-02-10 17:17:04 +0000197long semihosting_download_file(const char *file_name,
198 size_t buf_size,
Dan Handleya4cb68e2014-04-23 13:47:06 +0100199 uintptr_t buf)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100200{
Ryan Harkincd529322014-02-10 17:17:04 +0000201 long ret = -EINVAL;
202 size_t length;
203 long file_handle;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100204
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
235semihosting_fail:
236 semihosting_file_close(file_handle);
237 return ret;
238}