blob: e0845c1a5012ee5d13c3d5ba7a0147a498e5a8d4 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
johpow01d4cb8532020-07-30 17:11:03 -05002 * Copyright (c) 2013-2020, 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>
Dan Handley2bd4ef22014-04-09 13:14:54 +01009#include <string.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010010
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <lib/semihosting.h>
12
Achin Gupta4f6ad662013-10-25 09:08:21 +010013#ifndef SEMIHOSTING_SUPPORTED
14#define SEMIHOSTING_SUPPORTED 1
15#endif
16
johpow01d4cb8532020-07-30 17:11:03 -050017long semihosting_call(unsigned long operation, uintptr_t 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
johpow01d4cb8532020-07-30 17:11:03 -050054 return semihosting_call(SEMIHOSTING_SYS_OPEN, (uintptr_t)&open_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +010055}
56
Ryan Harkincd529322014-02-10 17:17:04 +000057long semihosting_file_seek(long file_handle, ssize_t offset)
Achin Gupta4f6ad662013-10-25 09:08:21 +010058{
Dan Handleye2712bc2014-04-10 15:37:22 +010059 smh_file_seek_block_t seek_block;
Ryan Harkincd529322014-02-10 17:17:04 +000060 long result;
Achin Gupta4f6ad662013-10-25 09:08:21 +010061
62 seek_block.handle = file_handle;
63 seek_block.location = offset;
64
johpow01d4cb8532020-07-30 17:11:03 -050065 result = semihosting_call(SEMIHOSTING_SYS_SEEK, (uintptr_t)&seek_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +010066
johpow01d4cb8532020-07-30 17:11:03 -050067 if (result != 0) {
Achin Gupta4f6ad662013-10-25 09:08:21 +010068 result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0);
johpow01d4cb8532020-07-30 17:11:03 -050069 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010070
71 return result;
72}
73
Dan Handleya4cb68e2014-04-23 13:47:06 +010074long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +010075{
Dan Handleye2712bc2014-04-10 15:37:22 +010076 smh_file_read_write_block_t read_block;
Ryan Harkincd529322014-02-10 17:17:04 +000077 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +010078
johpow01d4cb8532020-07-30 17:11:03 -050079 if ((length == NULL) || (buffer == (uintptr_t)NULL)) {
Achin Gupta4f6ad662013-10-25 09:08:21 +010080 return result;
johpow01d4cb8532020-07-30 17:11:03 -050081 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010082
83 read_block.handle = file_handle;
84 read_block.buffer = buffer;
85 read_block.length = *length;
86
johpow01d4cb8532020-07-30 17:11:03 -050087 result = semihosting_call(SEMIHOSTING_SYS_READ, (uintptr_t)&read_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +010088
89 if (result == *length) {
90 return -EINVAL;
91 } else if (result < *length) {
92 *length -= result;
93 return 0;
johpow01d4cb8532020-07-30 17:11:03 -050094 } else {
Achin Gupta4f6ad662013-10-25 09:08:21 +010095 return result;
johpow01d4cb8532020-07-30 17:11:03 -050096 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010097}
98
johpow01d4cb8532020-07-30 17:11:03 -050099long semihosting_file_write(long file_handle, size_t *length,
100 const uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100101{
Dan Handleye2712bc2014-04-10 15:37:22 +0100102 smh_file_read_write_block_t write_block;
Juan Castillo03fe10c2015-07-07 15:36:33 +0100103 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100104
johpow01d4cb8532020-07-30 17:11:03 -0500105 if ((length == NULL) || (buffer == (uintptr_t)NULL)) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100106 return -EINVAL;
johpow01d4cb8532020-07-30 17:11:03 -0500107 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100108
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,
johpow01d4cb8532020-07-30 17:11:03 -0500114 (uintptr_t)&write_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100115
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{
johpow01d4cb8532020-07-30 17:11:03 -0500123 return semihosting_call(SEMIHOSTING_SYS_CLOSE, (uintptr_t)&file_handle);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100124}
125
Ryan Harkincd529322014-02-10 17:17:04 +0000126long semihosting_file_length(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100127{
johpow01d4cb8532020-07-30 17:11:03 -0500128 return semihosting_call(SEMIHOSTING_SYS_FLEN, (uintptr_t)&file_handle);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129}
130
131char semihosting_read_char(void)
132{
Andrew Walbranf3a68392020-01-15 14:18:04 +0000133 return semihosting_call(SEMIHOSTING_SYS_READC, 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100134}
135
136void semihosting_write_char(char character)
137{
johpow01d4cb8532020-07-30 17:11:03 -0500138 semihosting_call(SEMIHOSTING_SYS_WRITEC, (uintptr_t)&character);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100139}
140
141void semihosting_write_string(char *string)
142{
johpow01d4cb8532020-07-30 17:11:03 -0500143 semihosting_call(SEMIHOSTING_SYS_WRITE0, (uintptr_t)string);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100144}
145
Ryan Harkincd529322014-02-10 17:17:04 +0000146long semihosting_system(char *command_line)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100147{
Dan Handleye2712bc2014-04-10 15:37:22 +0100148 smh_system_block_t system_block;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149
150 system_block.command_line = command_line;
151 system_block.command_length = strlen(command_line);
152
153 return semihosting_call(SEMIHOSTING_SYS_SYSTEM,
johpow01d4cb8532020-07-30 17:11:03 -0500154 (uintptr_t)&system_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100155}
156
Ryan Harkincd529322014-02-10 17:17:04 +0000157long semihosting_get_flen(const char *file_name)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100158{
Ryan Harkincd529322014-02-10 17:17:04 +0000159 long file_handle;
Ambroise Vincenta88a35d2019-02-14 09:48:21 +0000160 long length;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100161
johpow01d4cb8532020-07-30 17:11:03 -0500162 assert(semihosting_connection_supported() != 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100163
164 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
johpow01d4cb8532020-07-30 17:11:03 -0500165 if (file_handle == -1) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100166 return file_handle;
johpow01d4cb8532020-07-30 17:11:03 -0500167 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100168
169 /* Find the length of the file */
170 length = semihosting_file_length(file_handle);
171
johpow01d4cb8532020-07-30 17:11:03 -0500172 return (semihosting_file_close(file_handle) != 0) ? -1 : length;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100173}
174
Ryan Harkincd529322014-02-10 17:17:04 +0000175long semihosting_download_file(const char *file_name,
176 size_t buf_size,
Dan Handleya4cb68e2014-04-23 13:47:06 +0100177 uintptr_t buf)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100178{
Ryan Harkincd529322014-02-10 17:17:04 +0000179 long ret = -EINVAL;
180 size_t length;
181 long file_handle;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100182
183 /* Null pointer check */
johpow01d4cb8532020-07-30 17:11:03 -0500184 if (buf == 0U) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100185 return ret;
johpow01d4cb8532020-07-30 17:11:03 -0500186 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100187
johpow01d4cb8532020-07-30 17:11:03 -0500188 assert(semihosting_connection_supported() != 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100189
190 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
johpow01d4cb8532020-07-30 17:11:03 -0500191 if (file_handle == -1) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100192 return ret;
johpow01d4cb8532020-07-30 17:11:03 -0500193 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100194
195 /* Find the actual length of the file */
196 length = semihosting_file_length(file_handle);
johpow01d4cb8532020-07-30 17:11:03 -0500197 if (length == (size_t)(-1)) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100198 goto semihosting_fail;
johpow01d4cb8532020-07-30 17:11:03 -0500199 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100200
201 /* Signal error if we do not have enough space for the file */
johpow01d4cb8532020-07-30 17:11:03 -0500202 if (length > buf_size) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100203 goto semihosting_fail;
johpow01d4cb8532020-07-30 17:11:03 -0500204 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100205
206 /*
207 * A successful read will return 0 in which case we pass back
208 * the actual number of bytes read. Else we pass a negative
209 * value indicating an error.
210 */
211 ret = semihosting_file_read(file_handle, &length, buf);
johpow01d4cb8532020-07-30 17:11:03 -0500212 if (ret != 0) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100213 goto semihosting_fail;
johpow01d4cb8532020-07-30 17:11:03 -0500214 } else {
215 ret = (long)length;
216 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100217
218semihosting_fail:
219 semihosting_file_close(file_handle);
220 return ret;
221}
Andrew Walbranf3a68392020-01-15 14:18:04 +0000222
223void semihosting_exit(uint32_t reason, uint32_t subcode)
224{
225#ifdef __aarch64__
226 uint64_t parameters[] = {reason, subcode};
227
johpow01d4cb8532020-07-30 17:11:03 -0500228 (void)semihosting_call(SEMIHOSTING_SYS_EXIT, (uintptr_t)&parameters);
Andrew Walbranf3a68392020-01-15 14:18:04 +0000229#else
230 /* The subcode is not supported on AArch32. */
johpow01d4cb8532020-07-30 17:11:03 -0500231 (void)semihosting_call(SEMIHOSTING_SYS_EXIT, reason);
Andrew Walbranf3a68392020-01-15 14:18:04 +0000232#endif
233}