3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2013 - 2021, Arm Limited. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/SemihostLib.h>
14 #include "SemihostPrivate.h"
17 SemihostConnectionSupported (
21 return SEMIHOST_SUPPORTED
;
31 SEMIHOST_FILE_OPEN_BLOCK OpenBlock
;
34 if (FileHandle
== NULL
) {
35 return RETURN_INVALID_PARAMETER
;
38 // Remove any leading separator (e.g.: '\'). EFI Shell adds one.
39 if (*FileName
== '\\') {
43 OpenBlock
.FileName
= FileName
;
44 OpenBlock
.Mode
= Mode
;
45 OpenBlock
.NameLength
= AsciiStrLen (FileName
);
47 Result
= SEMIHOST_SYS_OPEN (&OpenBlock
);
50 return RETURN_NOT_FOUND
;
53 return RETURN_SUCCESS
;
63 SEMIHOST_FILE_SEEK_BLOCK SeekBlock
;
66 SeekBlock
.Handle
= FileHandle
;
67 SeekBlock
.Location
= Offset
;
69 Result
= SEMIHOST_SYS_SEEK (&SeekBlock
);
71 // Semihosting does not behave as documented. It returns the offset on
74 return RETURN_ABORTED
;
76 return RETURN_SUCCESS
;
87 SEMIHOST_FILE_READ_WRITE_BLOCK ReadBlock
;
90 if ((Length
== NULL
) || (Buffer
== NULL
)) {
91 return RETURN_INVALID_PARAMETER
;
94 ReadBlock
.Handle
= FileHandle
;
95 ReadBlock
.Buffer
= Buffer
;
96 ReadBlock
.Length
= *Length
;
98 Result
= SEMIHOST_SYS_READ (&ReadBlock
);
100 if ((*Length
!= 0) && (Result
== *Length
)) {
101 return RETURN_ABORTED
;
104 return RETURN_SUCCESS
;
111 IN OUT UINTN
*Length
,
115 SEMIHOST_FILE_READ_WRITE_BLOCK WriteBlock
;
117 if ((Length
== NULL
) || (Buffer
== NULL
)) {
118 return RETURN_INVALID_PARAMETER
;
121 WriteBlock
.Handle
= FileHandle
;
122 WriteBlock
.Buffer
= Buffer
;
123 WriteBlock
.Length
= *Length
;
125 *Length
= SEMIHOST_SYS_WRITE (&WriteBlock
);
128 return RETURN_ABORTED
;
130 return RETURN_SUCCESS
;
139 if (SEMIHOST_SYS_CLOSE (&FileHandle
) == -1) {
140 return RETURN_INVALID_PARAMETER
;
142 return RETURN_SUCCESS
;
154 if (Length
== NULL
) {
155 return RETURN_INVALID_PARAMETER
;
158 Result
= SEMIHOST_SYS_FLEN (&FileHandle
);
161 return RETURN_ABORTED
;
164 return RETURN_SUCCESS
;
169 Get a temporary name for a file from the host running the debug agent.
171 @param[out] Buffer Pointer to the buffer where the temporary name has to
173 @param[in] Identifier File name identifier (integer in the range 0 to 255)
174 @param[in] Length Length of the buffer to store the temporary name
176 @retval RETURN_SUCCESS Temporary name returned
177 @retval RETURN_INVALID_PARAMETER Invalid buffer address
178 @retval RETURN_ABORTED Temporary name not returned
182 SemihostFileTmpName (
188 SEMIHOST_FILE_TMPNAME_BLOCK TmpNameBlock
;
191 if (Buffer
== NULL
) {
192 return RETURN_INVALID_PARAMETER
;
195 TmpNameBlock
.Buffer
= Buffer
;
196 TmpNameBlock
.Identifier
= Identifier
;
197 TmpNameBlock
.Length
= Length
;
199 Result
= SEMIHOST_SYS_TMPNAME (&TmpNameBlock
);
202 return RETURN_ABORTED
;
204 return RETURN_SUCCESS
;
213 SEMIHOST_FILE_REMOVE_BLOCK RemoveBlock
;
216 // Remove any leading separator (e.g.: '\'). EFI Shell adds one.
217 if (*FileName
== '\\') {
221 RemoveBlock
.FileName
= FileName
;
222 RemoveBlock
.NameLength
= AsciiStrLen (FileName
);
224 Result
= SEMIHOST_SYS_REMOVE (&RemoveBlock
);
227 return RETURN_SUCCESS
;
229 return RETURN_ABORTED
;
234 Rename a specified file.
236 @param[in] FileName Name of the file to rename.
237 @param[in] NewFileName The new name of the file.
239 @retval RETURN_SUCCESS File Renamed
240 @retval RETURN_INVALID_PARAMETER Either the current or the new name is not specified
241 @retval RETURN_ABORTED Rename failed
247 IN CHAR8
*NewFileName
250 SEMIHOST_FILE_RENAME_BLOCK RenameBlock
;
253 if ((FileName
== NULL
) || (NewFileName
== NULL
)) {
254 return RETURN_INVALID_PARAMETER
;
257 RenameBlock
.FileName
= FileName
;
258 RenameBlock
.FileNameLength
= AsciiStrLen (FileName
);
259 RenameBlock
.NewFileName
= NewFileName
;
260 RenameBlock
.NewFileNameLength
= AsciiStrLen (NewFileName
);
262 Result
= SEMIHOST_SYS_RENAME (&RenameBlock
);
265 return RETURN_ABORTED
;
267 return RETURN_SUCCESS
;
272 SemihostReadCharacter (
276 return SEMIHOST_SYS_READC ();
280 SemihostWriteCharacter (
284 SEMIHOST_SYS_WRITEC (&Character
);
288 SemihostWriteString (
292 SEMIHOST_SYS_WRITE0 (String
);
297 IN CHAR8
*CommandLine
300 SEMIHOST_SYSTEM_BLOCK SystemBlock
;
302 SystemBlock
.CommandLine
= CommandLine
;
303 SystemBlock
.CommandLength
= AsciiStrLen (CommandLine
);
305 return SEMIHOST_SYS_SYSTEM (&SystemBlock
);