blob: 1a7ac60b61078ec9b5d938d752cf075fe470c7db [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 <string.h>
33#include <errno.h>
34#include <semihosting.h>
35
36#ifndef SEMIHOSTING_SUPPORTED
37#define SEMIHOSTING_SUPPORTED 1
38#endif
39
40extern int semihosting_call(unsigned int operation,
41 void *system_block_address);
42
43typedef struct {
44 const char *file_name;
45 unsigned int mode;
46 unsigned int name_length;
47} smh_file_open_block;
48
49typedef struct {
50 int handle;
51 void *buffer;
52 unsigned int length;
53} smh_file_read_write_block;
54
55typedef struct {
56 int handle;
57 unsigned int location;
58} smh_file_seek_block;
59
60typedef struct {
61 char *command_line;
62 unsigned int command_length;
63} smh_system_block;
64
65int semihosting_connection_supported(void)
66{
67 return SEMIHOSTING_SUPPORTED;
68}
69
70int semihosting_file_open(const char *file_name, unsigned int mode)
71{
72 smh_file_open_block 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
82int semihosting_file_seek(int file_handle, unsigned int offset)
83{
84 smh_file_seek_block seek_block;
85 int 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
99int semihosting_file_read(int file_handle, int *length, void *buffer)
100{
101 smh_file_read_write_block read_block;
102 int result = -EINVAL;
103
104 if ((length == NULL) || (buffer == 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
James Morrissey9d72b4e2014-02-10 17:04:32 +0000123int semihosting_file_write(int file_handle, int *length, const void *buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100124{
125 smh_file_read_write_block write_block;
126
127 if ((length == NULL) || (buffer == NULL))
128 return -EINVAL;
129
130 write_block.handle = file_handle;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000131 write_block.buffer = (void *)buffer;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100132 write_block.length = *length;
133
134 *length = semihosting_call(SEMIHOSTING_SYS_WRITE,
135 (void *) &write_block);
136
137 return *length;
138}
139
140int semihosting_file_close(int file_handle)
141{
142 return semihosting_call(SEMIHOSTING_SYS_CLOSE,
143 (void *) &file_handle);
144}
145
146int semihosting_file_length(int file_handle)
147{
148 return semihosting_call(SEMIHOSTING_SYS_FLEN,
149 (void *) &file_handle);
150}
151
152char semihosting_read_char(void)
153{
154 return semihosting_call(SEMIHOSTING_SYS_READC, NULL);
155}
156
157void semihosting_write_char(char character)
158{
159 semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character);
160}
161
162void semihosting_write_string(char *string)
163{
164 semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string);
165}
166
167int semihosting_system(char *command_line)
168{
169 smh_system_block system_block;
170
171 system_block.command_line = command_line;
172 system_block.command_length = strlen(command_line);
173
174 return semihosting_call(SEMIHOSTING_SYS_SYSTEM,
175 (void *) &system_block);
176}
177
178int semihosting_get_flen(const char *file_name)
179{
180 int file_handle, length;
181
182 assert(semihosting_connection_supported());
183
184 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
185 if (file_handle == -1)
186 return file_handle;
187
188 /* Find the length of the file */
189 length = semihosting_file_length(file_handle);
190
191 return semihosting_file_close(file_handle) ? -1 : length;
192}
193
194int semihosting_download_file(const char *file_name,
195 int buf_size,
196 void *buf)
197{
198 int ret = -EINVAL, file_handle, length;
199
200 /* Null pointer check */
201 if (!buf)
202 return ret;
203
204 assert(semihosting_connection_supported());
205
206 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
207 if (file_handle == -1)
208 return ret;
209
210 /* Find the actual length of the file */
211 length = semihosting_file_length(file_handle);
212 if (length == -1)
213 goto semihosting_fail;
214
215 /* Signal error if we do not have enough space for the file */
216 if (length > buf_size)
217 goto semihosting_fail;
218
219 /*
220 * A successful read will return 0 in which case we pass back
221 * the actual number of bytes read. Else we pass a negative
222 * value indicating an error.
223 */
224 ret = semihosting_file_read(file_handle, &length, buf);
225 if (ret)
226 goto semihosting_fail;
227 else
228 ret = length;
229
230semihosting_fail:
231 semihosting_file_close(file_handle);
232 return ret;
233}