blob: 2ba43f343e99dac6528148bc8366c8cacee0dc0e [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 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
7#include <assert.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +01008#include <errno.h>
9#include <semihosting.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010010#include <string.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010011
12#ifndef SEMIHOSTING_SUPPORTED
13#define SEMIHOSTING_SUPPORTED 1
14#endif
15
Dan Handleya17fefa2014-05-14 12:38:32 +010016long semihosting_call(unsigned long operation,
17 void *system_block_address);
Achin Gupta4f6ad662013-10-25 09:08:21 +010018
19typedef struct {
20 const char *file_name;
Ryan Harkincd529322014-02-10 17:17:04 +000021 unsigned long mode;
22 size_t name_length;
Dan Handleye2712bc2014-04-10 15:37:22 +010023} smh_file_open_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010024
25typedef struct {
Ryan Harkincd529322014-02-10 17:17:04 +000026 long handle;
Dan Handleya4cb68e2014-04-23 13:47:06 +010027 uintptr_t buffer;
Ryan Harkincd529322014-02-10 17:17:04 +000028 size_t length;
Dan Handleye2712bc2014-04-10 15:37:22 +010029} smh_file_read_write_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010030
31typedef struct {
Ryan Harkincd529322014-02-10 17:17:04 +000032 long handle;
33 ssize_t location;
Dan Handleye2712bc2014-04-10 15:37:22 +010034} smh_file_seek_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010035
36typedef struct {
37 char *command_line;
Ryan Harkincd529322014-02-10 17:17:04 +000038 size_t command_length;
Dan Handleye2712bc2014-04-10 15:37:22 +010039} smh_system_block_t;
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
Ryan Harkincd529322014-02-10 17:17:04 +000041long semihosting_connection_supported(void)
Achin Gupta4f6ad662013-10-25 09:08:21 +010042{
43 return SEMIHOSTING_SUPPORTED;
44}
45
Ryan Harkincd529322014-02-10 17:17:04 +000046long semihosting_file_open(const char *file_name, size_t mode)
Achin Gupta4f6ad662013-10-25 09:08:21 +010047{
Dan Handleye2712bc2014-04-10 15:37:22 +010048 smh_file_open_block_t open_block;
Achin Gupta4f6ad662013-10-25 09:08:21 +010049
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
Ryan Harkincd529322014-02-10 17:17:04 +000058long semihosting_file_seek(long file_handle, ssize_t offset)
Achin Gupta4f6ad662013-10-25 09:08:21 +010059{
Dan Handleye2712bc2014-04-10 15:37:22 +010060 smh_file_seek_block_t seek_block;
Ryan Harkincd529322014-02-10 17:17:04 +000061 long result;
Achin Gupta4f6ad662013-10-25 09:08:21 +010062
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
Dan Handleya4cb68e2014-04-23 13:47:06 +010075long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +010076{
Dan Handleye2712bc2014-04-10 15:37:22 +010077 smh_file_read_write_block_t read_block;
Ryan Harkincd529322014-02-10 17:17:04 +000078 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +010079
Dan Handleya4cb68e2014-04-23 13:47:06 +010080 if ((length == NULL) || (buffer == (uintptr_t)NULL))
Achin Gupta4f6ad662013-10-25 09:08:21 +010081 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
Ryan Harkincd529322014-02-10 17:17:04 +000099long semihosting_file_write(long file_handle,
100 size_t *length,
Dan Handleya4cb68e2014-04-23 13:47:06 +0100101 const uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100102{
Dan Handleye2712bc2014-04-10 15:37:22 +0100103 smh_file_read_write_block_t write_block;
Juan Castillo03fe10c2015-07-07 15:36:33 +0100104 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100105
Dan Handleya4cb68e2014-04-23 13:47:06 +0100106 if ((length == NULL) || (buffer == (uintptr_t)NULL))
Achin Gupta4f6ad662013-10-25 09:08:21 +0100107 return -EINVAL;
108
109 write_block.handle = file_handle;
Dan Handleya4cb68e2014-04-23 13:47:06 +0100110 write_block.buffer = (uintptr_t)buffer; /* cast away const */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100111 write_block.length = *length;
112
Juan Castillo03fe10c2015-07-07 15:36:33 +0100113 result = semihosting_call(SEMIHOSTING_SYS_WRITE,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100114 (void *) &write_block);
115
Juan Castillo03fe10c2015-07-07 15:36:33 +0100116 *length = result;
117
118 return (result == 0) ? 0 : -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100119}
120
Ryan Harkincd529322014-02-10 17:17:04 +0000121long semihosting_file_close(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100122{
123 return semihosting_call(SEMIHOSTING_SYS_CLOSE,
124 (void *) &file_handle);
125}
126
Ryan Harkincd529322014-02-10 17:17:04 +0000127long semihosting_file_length(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100128{
129 return semihosting_call(SEMIHOSTING_SYS_FLEN,
130 (void *) &file_handle);
131}
132
133char semihosting_read_char(void)
134{
135 return semihosting_call(SEMIHOSTING_SYS_READC, NULL);
136}
137
138void semihosting_write_char(char character)
139{
140 semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character);
141}
142
143void semihosting_write_string(char *string)
144{
145 semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string);
146}
147
Ryan Harkincd529322014-02-10 17:17:04 +0000148long semihosting_system(char *command_line)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149{
Dan Handleye2712bc2014-04-10 15:37:22 +0100150 smh_system_block_t system_block;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100151
152 system_block.command_line = command_line;
153 system_block.command_length = strlen(command_line);
154
155 return semihosting_call(SEMIHOSTING_SYS_SYSTEM,
156 (void *) &system_block);
157}
158
Ryan Harkincd529322014-02-10 17:17:04 +0000159long semihosting_get_flen(const char *file_name)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100160{
Ryan Harkincd529322014-02-10 17:17:04 +0000161 long file_handle;
162 size_t length;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100163
164 assert(semihosting_connection_supported());
165
166 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
167 if (file_handle == -1)
168 return file_handle;
169
170 /* Find the length of the file */
171 length = semihosting_file_length(file_handle);
172
173 return semihosting_file_close(file_handle) ? -1 : length;
174}
175
Ryan Harkincd529322014-02-10 17:17:04 +0000176long semihosting_download_file(const char *file_name,
177 size_t buf_size,
Dan Handleya4cb68e2014-04-23 13:47:06 +0100178 uintptr_t buf)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100179{
Ryan Harkincd529322014-02-10 17:17:04 +0000180 long ret = -EINVAL;
181 size_t length;
182 long file_handle;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100183
184 /* Null pointer check */
185 if (!buf)
186 return ret;
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 ret;
193
194 /* Find the actual length of the file */
195 length = semihosting_file_length(file_handle);
196 if (length == -1)
197 goto semihosting_fail;
198
199 /* Signal error if we do not have enough space for the file */
200 if (length > buf_size)
201 goto semihosting_fail;
202
203 /*
204 * A successful read will return 0 in which case we pass back
205 * the actual number of bytes read. Else we pass a negative
206 * value indicating an error.
207 */
208 ret = semihosting_file_read(file_handle, &length, buf);
209 if (ret)
210 goto semihosting_fail;
211 else
212 ret = length;
213
214semihosting_fail:
215 semihosting_file_close(file_handle);
216 return ret;
217}