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