]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/SemiHostingSerialPortLib/SerialPortLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPkg / Library / SemiHostingSerialPortLib / SerialPortLib.c
CommitLineData
2ef2b01e
A
1/** @file\r
2 Serial I/O Port library functions with no library constructor/destructor\r
3\r
d6ebcab7 4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
02c621f3 5 Copyright (c) 2021, Arm Limited. All rights reserved.<BR>\r
3402aac7 6\r
4059386c 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
2ef2b01e
A
8\r
9**/\r
10\r
11#include <Uefi.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/SemihostLib.h>\r
14#include <Library/SerialPortLib.h>\r
15\r
2ef2b01e
A
16/*\r
17\r
18 Programmed hardware of Serial port.\r
19\r
20 @return Always return EFI_UNSUPPORTED.\r
21\r
22**/\r
23RETURN_STATUS\r
24EFIAPI\r
25SerialPortInitialize (\r
26 VOID\r
27 )\r
28{\r
29 if (SemihostConnectionSupported ()) {\r
30 return RETURN_SUCCESS;\r
31 } else {\r
32 return RETURN_UNSUPPORTED;\r
33 }\r
34}\r
35\r
36/**\r
37 Write data to serial device.\r
38\r
b8de64be 39 @param Buffer Point of data buffer which need to be written.\r
2ef2b01e
A
40 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
41\r
42 @retval 0 Write data failed.\r
b8de64be 43 @retval !0 Actual number of bytes written to serial device.\r
2ef2b01e
A
44\r
45**/\r
46\r
47#define PRINT_BUFFER_SIZE 512\r
48#define PRINT_BUFFER_THRESHOLD (PRINT_BUFFER_SIZE - 4)\r
49\r
50UINTN\r
51EFIAPI\r
52SerialPortWrite (\r
429309e0
MK
53 IN UINT8 *Buffer,\r
54 IN UINTN NumberOfBytes\r
55 )\r
2ef2b01e 56{\r
429309e0
MK
57 UINT8 PrintBuffer[PRINT_BUFFER_SIZE];\r
58 UINTN SourceIndex;\r
59 UINTN DestinationIndex;\r
60 UINT8 CurrentCharacter;\r
2ef2b01e 61\r
02c621f3
PG
62 SourceIndex = 0;\r
63 DestinationIndex = 0;\r
64\r
429309e0
MK
65 while (SourceIndex < NumberOfBytes) {\r
66 CurrentCharacter = Buffer[SourceIndex++];\r
3402aac7 67\r
429309e0 68 switch (CurrentCharacter) {\r
2ef2b01e 69 case '\r':\r
429309e0 70 continue;\r
2ef2b01e
A
71\r
72 case '\n':\r
429309e0
MK
73 PrintBuffer[DestinationIndex++] = ' ';\r
74 // fall through\r
2ef2b01e
A
75\r
76 default:\r
429309e0
MK
77 PrintBuffer[DestinationIndex++] = CurrentCharacter;\r
78 break;\r
79 }\r
2ef2b01e 80\r
429309e0
MK
81 if (DestinationIndex > PRINT_BUFFER_THRESHOLD) {\r
82 PrintBuffer[DestinationIndex] = '\0';\r
83 SemihostWriteString ((CHAR8 *)PrintBuffer);\r
2ef2b01e 84\r
429309e0
MK
85 DestinationIndex = 0;\r
86 }\r
2ef2b01e 87 }\r
3402aac7 88\r
429309e0
MK
89 if (DestinationIndex > 0) {\r
90 PrintBuffer[DestinationIndex] = '\0';\r
91 SemihostWriteString ((CHAR8 *)PrintBuffer);\r
2ef2b01e
A
92 }\r
93\r
5f4f1334 94 return NumberOfBytes;\r
2ef2b01e
A
95}\r
96\r
2ef2b01e
A
97/**\r
98 Read data from serial device and save the datas in buffer.\r
99\r
b8de64be 100 @param Buffer Point of data buffer which need to be written.\r
2ef2b01e
A
101 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
102\r
103 @retval 0 Read data failed.\r
ff5fef14 104 @retval !0 Actual number of bytes read from serial device.\r
2ef2b01e
A
105\r
106**/\r
107UINTN\r
108EFIAPI\r
109SerialPortRead (\r
429309e0
MK
110 OUT UINT8 *Buffer,\r
111 IN UINTN NumberOfBytes\r
112 )\r
2ef2b01e
A
113{\r
114 *Buffer = SemihostReadCharacter ();\r
115 return 1;\r
116}\r
117\r
2ef2b01e 118/**\r
ff5fef14 119 Check to see if any data is available to be read from the debug device.\r
2ef2b01e 120\r
ff5fef14
AC
121 @retval TRUE At least one byte of data is available to be read\r
122 @retval FALSE No data is available to be read\r
2ef2b01e
A
123\r
124**/\r
125BOOLEAN\r
126EFIAPI\r
127SerialPortPoll (\r
128 VOID\r
129 )\r
130{\r
131 // Since SemiHosting read character is blocking always say we have a char ready?\r
132 return SemihostConnectionSupported ();\r
133}\r