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