3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2013 - 2014, ARM Ltd. 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
;
138 INT32 Result
= Semihost_SYS_CLOSE(&FileHandle
);
141 return RETURN_INVALID_PARAMETER
;
143 return RETURN_SUCCESS
;
155 if (Length
== NULL
) {
156 return RETURN_INVALID_PARAMETER
;
159 Result
= Semihost_SYS_FLEN(&FileHandle
);
162 return RETURN_ABORTED
;
165 return RETURN_SUCCESS
;
170 Get a temporary name for a file from the host running the debug agent.
172 @param[out] Buffer Pointer to the buffer where the temporary name has to
174 @param[in] Identifier File name identifier (integer in the range 0 to 255)
175 @param[in] Length Length of the buffer to store the temporary name
177 @retval RETURN_SUCCESS Temporary name returned
178 @retval RETURN_INVALID_PARAMETER Invalid buffer address
179 @retval RETURN_ABORTED Temporary name not returned
189 SEMIHOST_FILE_TMPNAME_BLOCK TmpNameBlock
;
192 if (Buffer
== NULL
) {
193 return RETURN_INVALID_PARAMETER
;
196 TmpNameBlock
.Buffer
= Buffer
;
197 TmpNameBlock
.Identifier
= Identifier
;
198 TmpNameBlock
.Length
= Length
;
200 Result
= Semihost_SYS_TMPNAME (&TmpNameBlock
);
203 return RETURN_ABORTED
;
205 return RETURN_SUCCESS
;
214 SEMIHOST_FILE_REMOVE_BLOCK RemoveBlock
;
217 // Remove any leading separator (e.g.: '\'). EFI Shell adds one.
218 if (*FileName
== '\\') {
222 RemoveBlock
.FileName
= FileName
;
223 RemoveBlock
.NameLength
= AsciiStrLen(FileName
);
225 Result
= Semihost_SYS_REMOVE(&RemoveBlock
);
228 return RETURN_SUCCESS
;
230 return RETURN_ABORTED
;
235 Rename a specified file.
237 @param[in] FileName Name of the file to rename.
238 @param[in] NewFileName The new name of the file.
240 @retval RETURN_SUCCESS File Renamed
241 @retval RETURN_INVALID_PARAMETER Either the current or the new name is not specified
242 @retval RETURN_ABORTED Rename failed
248 IN CHAR8
*NewFileName
251 SEMIHOST_FILE_RENAME_BLOCK RenameBlock
;
254 if ((FileName
== NULL
) || (NewFileName
== NULL
)) {
255 return RETURN_INVALID_PARAMETER
;
258 RenameBlock
.FileName
= FileName
;
259 RenameBlock
.FileNameLength
= AsciiStrLen (FileName
);
260 RenameBlock
.NewFileName
= NewFileName
;
261 RenameBlock
.NewFileNameLength
= AsciiStrLen (NewFileName
);
263 Result
= Semihost_SYS_RENAME (&RenameBlock
);
266 return RETURN_ABORTED
;
268 return RETURN_SUCCESS
;
273 SemihostReadCharacter (
277 return Semihost_SYS_READC();
281 SemihostWriteCharacter (
285 Semihost_SYS_WRITEC(&Character
);
289 SemihostWriteString (
293 Semihost_SYS_WRITE0(String
);
298 IN CHAR8
*CommandLine
301 SEMIHOST_SYSTEM_BLOCK SystemBlock
;
303 SystemBlock
.CommandLine
= CommandLine
;
304 SystemBlock
.CommandLength
= AsciiStrLen(CommandLine
);
306 return Semihost_SYS_SYSTEM(&SystemBlock
);