io_semihosting.c 4.49 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
 */

#include <assert.h>

9
#include <platform_def.h>
10

11
12
13
14
#include <drivers/io/io_driver.h>
#include <drivers/io/io_semihosting.h>
#include <drivers/io/io_storage.h>
#include <lib/semihosting.h>
15
16

/* Identify the device type as semihosting */
17
static io_type_t device_type_sh(void)
18
19
20
21
22
23
24
{
	return IO_TYPE_SEMIHOSTING;
}


/* Semi-hosting functions, device info and handle */

25
26
static int sh_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
static int sh_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
27
28
29
		io_entity_t *entity);
static int sh_file_seek(io_entity_t *entity, int mode, ssize_t offset);
static int sh_file_len(io_entity_t *entity, size_t *length);
30
static int sh_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
31
		size_t *length_read);
32
static int sh_file_write(io_entity_t *entity, const uintptr_t buffer,
33
		size_t length, size_t *length_written);
34
static int sh_file_close(io_entity_t *entity);
35

36
static const io_dev_connector_t sh_dev_connector = {
37
38
39
40
	.dev_open = sh_dev_open
};


41
static const io_dev_funcs_t sh_dev_funcs = {
42
43
44
45
46
47
48
49
50
51
52
53
	.type = device_type_sh,
	.open = sh_file_open,
	.seek = sh_file_seek,
	.size = sh_file_len,
	.read = sh_file_read,
	.write = sh_file_write,
	.close = sh_file_close,
	.dev_init = NULL,	/* NOP */
	.dev_close = NULL,	/* NOP */
};


54
55
/* No state associated with this device so structure can be const */
static const io_dev_info_t sh_dev_info = {
56
57
58
59
60
61
	.funcs = &sh_dev_funcs,
	.info = (uintptr_t)NULL
};


/* Open a connection to the semi-hosting device */
62
63
static int sh_dev_open(const uintptr_t dev_spec __unused,
		io_dev_info_t **dev_info)
64
65
{
	assert(dev_info != NULL);
66
	*dev_info = (io_dev_info_t *)&sh_dev_info; /* cast away const */
67
	return 0;
68
69
70
71
}


/* Open a file on the semi-hosting device */
72
static int sh_file_open(io_dev_info_t *dev_info __unused,
73
		const uintptr_t spec, io_entity_t *entity)
74
{
75
	int result = -ENOENT;
dp-arm's avatar
dp-arm committed
76
	long sh_result;
77
	const io_file_spec_t *file_spec = (const io_file_spec_t *)spec;
78
79
80
81
82
83
84

	assert(file_spec != NULL);
	assert(entity != NULL);

	sh_result = semihosting_file_open(file_spec->path, file_spec->mode);

	if (sh_result > 0) {
85
		entity->info = (uintptr_t)sh_result;
86
		result = 0;
87
88
89
90
91
92
	}
	return result;
}


/* Seek to a particular file offset on the semi-hosting device */
93
static int sh_file_seek(io_entity_t *entity, int mode, ssize_t offset)
94
{
95
	long file_handle, sh_result;
96
97
98

	assert(entity != NULL);

99
	file_handle = (long)entity->info;
100
101
102

	sh_result = semihosting_file_seek(file_handle, offset);

103
	return (sh_result == 0) ? 0 : -ENOENT;
104
105
106
107
}


/* Return the size of a file on the semi-hosting device */
108
static int sh_file_len(io_entity_t *entity, size_t *length)
109
{
110
	int result = -ENOENT;
111
112
113
114

	assert(entity != NULL);
	assert(length != NULL);

115
116
	long sh_handle = (long)entity->info;
	long sh_result = semihosting_file_length(sh_handle);
117
118

	if (sh_result >= 0) {
119
		result = 0;
120
121
122
123
124
125
126
127
		*length = (size_t)sh_result;
	}

	return result;
}


/* Read data from a file on the semi-hosting device */
128
static int sh_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
129
130
		size_t *length_read)
{
131
	int result = -ENOENT;
dp-arm's avatar
dp-arm committed
132
	long sh_result;
133
134
	size_t bytes = length;
	long file_handle;
135
136
137
138

	assert(entity != NULL);
	assert(length_read != NULL);

139
	file_handle = (long)entity->info;
140
141
142
143
144

	sh_result = semihosting_file_read(file_handle, &bytes, buffer);

	if (sh_result >= 0) {
		*length_read = (bytes != length) ? bytes : length;
145
146
		result = 0;
	}
147
148
149
150
151
152

	return result;
}


/* Write data to a file on the semi-hosting device */
153
static int sh_file_write(io_entity_t *entity, const uintptr_t buffer,
154
155
		size_t length, size_t *length_written)
{
dp-arm's avatar
dp-arm committed
156
	long sh_result;
157
158
	long file_handle;
	size_t bytes = length;
159
160
161
162

	assert(entity != NULL);
	assert(length_written != NULL);

163
	file_handle = (long)entity->info;
164
165
166

	sh_result = semihosting_file_write(file_handle, &bytes, buffer);

167
	*length_written = length - bytes;
168

169
	return (sh_result == 0) ? 0 : -ENOENT;
170
171
172
173
}


/* Close a file on the semi-hosting device */
174
static int sh_file_close(io_entity_t *entity)
175
{
dp-arm's avatar
dp-arm committed
176
	long sh_result;
177
	long file_handle;
178
179
180

	assert(entity != NULL);

181
	file_handle = (long)entity->info;
182
183
184

	sh_result = semihosting_file_close(file_handle);

185
	return (sh_result >= 0) ? 0 : -ENOENT;
186
187
188
189
190
191
}


/* Exported functions */

/* Register the semi-hosting driver with the IO abstraction */
192
int register_io_dev_sh(const io_dev_connector_t **dev_con)
193
{
194
	int result;
195
196
197
	assert(dev_con != NULL);

	result = io_register_device(&sh_dev_info);
198
	if (result == 0)
199
200
201
202
		*dev_con = &sh_dev_connector;

	return result;
}