blob: 163a82d843609cc8d286272390282e2201bc7a02 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Manish V Badarkhede920262022-09-26 15:06:56 +01002 * Copyright (c) 2013-2022, 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
Manish V Badarkhede920262022-09-26 15:06:56 +010067 if (result < 0) {
Achin Gupta4f6ad662013-10-25 09:08:21 +010068 result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0);
Manish V Badarkhede920262022-09-26 15:06:56 +010069 } else {
70 result = 0;
johpow01d4cb8532020-07-30 17:11:03 -050071 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010072
73 return result;
74}
75
Dan Handleya4cb68e2014-04-23 13:47:06 +010076long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +010077{
Dan Handleye2712bc2014-04-10 15:37:22 +010078 smh_file_read_write_block_t read_block;
Ryan Harkincd529322014-02-10 17:17:04 +000079 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +010080
johpow01d4cb8532020-07-30 17:11:03 -050081 if ((length == NULL) || (buffer == (uintptr_t)NULL)) {
Achin Gupta4f6ad662013-10-25 09:08:21 +010082 return result;
johpow01d4cb8532020-07-30 17:11:03 -050083 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010084
85 read_block.handle = file_handle;
86 read_block.buffer = buffer;
87 read_block.length = *length;
88
johpow01d4cb8532020-07-30 17:11:03 -050089 result = semihosting_call(SEMIHOSTING_SYS_READ, (uintptr_t)&read_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +010090
91 if (result == *length) {
92 return -EINVAL;
93 } else if (result < *length) {
94 *length -= result;
95 return 0;
johpow01d4cb8532020-07-30 17:11:03 -050096 } else {
Achin Gupta4f6ad662013-10-25 09:08:21 +010097 return result;
johpow01d4cb8532020-07-30 17:11:03 -050098 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010099}
100
johpow01d4cb8532020-07-30 17:11:03 -0500101long semihosting_file_write(long file_handle, size_t *length,
102 const uintptr_t buffer)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103{
Dan Handleye2712bc2014-04-10 15:37:22 +0100104 smh_file_read_write_block_t write_block;
Juan Castillo03fe10c2015-07-07 15:36:33 +0100105 long result = -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100106
johpow01d4cb8532020-07-30 17:11:03 -0500107 if ((length == NULL) || (buffer == (uintptr_t)NULL)) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100108 return -EINVAL;
johpow01d4cb8532020-07-30 17:11:03 -0500109 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100110
111 write_block.handle = file_handle;
Dan Handleya4cb68e2014-04-23 13:47:06 +0100112 write_block.buffer = (uintptr_t)buffer; /* cast away const */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100113 write_block.length = *length;
114
Juan Castillo03fe10c2015-07-07 15:36:33 +0100115 result = semihosting_call(SEMIHOSTING_SYS_WRITE,
johpow01d4cb8532020-07-30 17:11:03 -0500116 (uintptr_t)&write_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100117
Juan Castillo03fe10c2015-07-07 15:36:33 +0100118 *length = result;
119
120 return (result == 0) ? 0 : -EINVAL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100121}
122
Ryan Harkincd529322014-02-10 17:17:04 +0000123long semihosting_file_close(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100124{
johpow01d4cb8532020-07-30 17:11:03 -0500125 return semihosting_call(SEMIHOSTING_SYS_CLOSE, (uintptr_t)&file_handle);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100126}
127
Ryan Harkincd529322014-02-10 17:17:04 +0000128long semihosting_file_length(long file_handle)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129{
johpow01d4cb8532020-07-30 17:11:03 -0500130 return semihosting_call(SEMIHOSTING_SYS_FLEN, (uintptr_t)&file_handle);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100131}
132
133char semihosting_read_char(void)
134{
Andrew Walbranf3a68392020-01-15 14:18:04 +0000135 return semihosting_call(SEMIHOSTING_SYS_READC, 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100136}
137
138void semihosting_write_char(char character)
139{
johpow01d4cb8532020-07-30 17:11:03 -0500140 semihosting_call(SEMIHOSTING_SYS_WRITEC, (uintptr_t)&character);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100141}
142
143void semihosting_write_string(char *string)
144{
johpow01d4cb8532020-07-30 17:11:03 -0500145 semihosting_call(SEMIHOSTING_SYS_WRITE0, (uintptr_t)string);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100146}
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,
johpow01d4cb8532020-07-30 17:11:03 -0500156 (uintptr_t)&system_block);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100157}
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;
Ambroise Vincenta88a35d2019-02-14 09:48:21 +0000162 long length;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100163
johpow01d4cb8532020-07-30 17:11:03 -0500164 assert(semihosting_connection_supported() != 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100165
166 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
johpow01d4cb8532020-07-30 17:11:03 -0500167 if (file_handle == -1) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100168 return file_handle;
johpow01d4cb8532020-07-30 17:11:03 -0500169 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100170
171 /* Find the length of the file */
172 length = semihosting_file_length(file_handle);
173
johpow01d4cb8532020-07-30 17:11:03 -0500174 return (semihosting_file_close(file_handle) != 0) ? -1 : length;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100175}
176
Ryan Harkincd529322014-02-10 17:17:04 +0000177long semihosting_download_file(const char *file_name,
178 size_t buf_size,
Dan Handleya4cb68e2014-04-23 13:47:06 +0100179 uintptr_t buf)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100180{
Ryan Harkincd529322014-02-10 17:17:04 +0000181 long ret = -EINVAL;
182 size_t length;
183 long file_handle;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100184
185 /* Null pointer check */
johpow01d4cb8532020-07-30 17:11:03 -0500186 if (buf == 0U) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100187 return ret;
johpow01d4cb8532020-07-30 17:11:03 -0500188 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100189
johpow01d4cb8532020-07-30 17:11:03 -0500190 assert(semihosting_connection_supported() != 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100191
192 file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
johpow01d4cb8532020-07-30 17:11:03 -0500193 if (file_handle == -1) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100194 return ret;
johpow01d4cb8532020-07-30 17:11:03 -0500195 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100196
197 /* Find the actual length of the file */
198 length = semihosting_file_length(file_handle);
johpow01d4cb8532020-07-30 17:11:03 -0500199 if (length == (size_t)(-1)) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100200 goto semihosting_fail;
johpow01d4cb8532020-07-30 17:11:03 -0500201 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100202
203 /* Signal error if we do not have enough space for the file */
johpow01d4cb8532020-07-30 17:11:03 -0500204 if (length > buf_size) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100205 goto semihosting_fail;
johpow01d4cb8532020-07-30 17:11:03 -0500206 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100207
208 /*
209 * A successful read will return 0 in which case we pass back
210 * the actual number of bytes read. Else we pass a negative
211 * value indicating an error.
212 */
213 ret = semihosting_file_read(file_handle, &length, buf);
johpow01d4cb8532020-07-30 17:11:03 -0500214 if (ret != 0) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100215 goto semihosting_fail;
johpow01d4cb8532020-07-30 17:11:03 -0500216 } else {
217 ret = (long)length;
218 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100219
220semihosting_fail:
221 semihosting_file_close(file_handle);
222 return ret;
223}
Andrew Walbranf3a68392020-01-15 14:18:04 +0000224
225void semihosting_exit(uint32_t reason, uint32_t subcode)
226{
227#ifdef __aarch64__
228 uint64_t parameters[] = {reason, subcode};
229
johpow01d4cb8532020-07-30 17:11:03 -0500230 (void)semihosting_call(SEMIHOSTING_SYS_EXIT, (uintptr_t)&parameters);
Andrew Walbranf3a68392020-01-15 14:18:04 +0000231#else
232 /* The subcode is not supported on AArch32. */
johpow01d4cb8532020-07-30 17:11:03 -0500233 (void)semihosting_call(SEMIHOSTING_SYS_EXIT, reason);
Andrew Walbranf3a68392020-01-15 14:18:04 +0000234#endif
235}