]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/SemiHostingSerialPortLib/SerialPortLib.c
ArmPkg: Add OemMiscLibNull library to ArmPkg.dsc
[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
16\r
17/*\r
18\r
19 Programmed hardware of Serial port.\r
20\r
21 @return Always return EFI_UNSUPPORTED.\r
22\r
23**/\r
24RETURN_STATUS\r
25EFIAPI\r
26SerialPortInitialize (\r
27 VOID\r
28 )\r
29{\r
30 if (SemihostConnectionSupported ()) {\r
31 return RETURN_SUCCESS;\r
32 } else {\r
33 return RETURN_UNSUPPORTED;\r
34 }\r
35}\r
36\r
37/**\r
38 Write data to serial device.\r
39\r
40 @param Buffer Point of data buffer which need to be writed.\r
41 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
42\r
43 @retval 0 Write data failed.\r
44 @retval !0 Actual number of bytes writed to serial device.\r
45\r
46**/\r
47\r
48#define PRINT_BUFFER_SIZE 512\r
49#define PRINT_BUFFER_THRESHOLD (PRINT_BUFFER_SIZE - 4)\r
50\r
51UINTN\r
52EFIAPI\r
53SerialPortWrite (\r
54 IN UINT8 *Buffer,\r
55 IN UINTN NumberOfBytes\r
56)\r
57{\r
58 UINT8 PrintBuffer[PRINT_BUFFER_SIZE];\r
02c621f3
PG
59 UINTN SourceIndex;\r
60 UINTN DestinationIndex;\r
2ef2b01e
A
61 UINT8 CurrentCharacter;\r
62\r
02c621f3
PG
63 SourceIndex = 0;\r
64 DestinationIndex = 0;\r
65\r
2ef2b01e
A
66 while (SourceIndex < NumberOfBytes)\r
67 {\r
68 CurrentCharacter = Buffer[SourceIndex++];\r
3402aac7 69\r
2ef2b01e
A
70 switch (CurrentCharacter)\r
71 {\r
72 case '\r':\r
73 continue;\r
74\r
75 case '\n':\r
76 PrintBuffer[DestinationIndex++] = ' ';\r
77 // fall through\r
78\r
79 default:\r
80 PrintBuffer[DestinationIndex++] = CurrentCharacter;\r
81 break;\r
82 }\r
83\r
84 if (DestinationIndex > PRINT_BUFFER_THRESHOLD)\r
85 {\r
86 PrintBuffer[DestinationIndex] = '\0';\r
87 SemihostWriteString ((CHAR8 *) PrintBuffer);\r
88\r
89 DestinationIndex = 0;\r
90 }\r
91 }\r
3402aac7 92\r
2ef2b01e
A
93 if (DestinationIndex > 0)\r
94 {\r
95 PrintBuffer[DestinationIndex] = '\0';\r
96 SemihostWriteString ((CHAR8 *) PrintBuffer);\r
97 }\r
98\r
5f4f1334 99 return NumberOfBytes;\r
2ef2b01e
A
100}\r
101\r
102\r
103/**\r
104 Read data from serial device and save the datas in buffer.\r
105\r
106 @param Buffer Point of data buffer which need to be writed.\r
107 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
108\r
109 @retval 0 Read data failed.\r
ff5fef14 110 @retval !0 Actual number of bytes read from serial device.\r
2ef2b01e
A
111\r
112**/\r
113UINTN\r
114EFIAPI\r
115SerialPortRead (\r
116 OUT UINT8 *Buffer,\r
117 IN UINTN NumberOfBytes\r
118)\r
119{\r
120 *Buffer = SemihostReadCharacter ();\r
121 return 1;\r
122}\r
123\r
124\r
125\r
126/**\r
ff5fef14 127 Check to see if any data is available to be read from the debug device.\r
2ef2b01e 128\r
ff5fef14
AC
129 @retval TRUE At least one byte of data is available to be read\r
130 @retval FALSE No data is available to be read\r
2ef2b01e
A
131\r
132**/\r
133BOOLEAN\r
134EFIAPI\r
135SerialPortPoll (\r
136 VOID\r
137 )\r
138{\r
139 // Since SemiHosting read character is blocking always say we have a char ready?\r
140 return SemihostConnectionSupported ();\r
141}\r
142\r