]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/SemiHostingSerialPortLib/SerialPortLib.c
ArmPkg: Apply uncrustify changes
[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 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 written.
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 written 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;
59 UINTN DestinationIndex;
60 UINT8 CurrentCharacter;
61
62 SourceIndex = 0;
63 DestinationIndex = 0;
64
65 while (SourceIndex < NumberOfBytes) {
66 CurrentCharacter = Buffer[SourceIndex++];
67
68 switch (CurrentCharacter) {
69 case '\r':
70 continue;
71
72 case '\n':
73 PrintBuffer[DestinationIndex++] = ' ';
74 // fall through
75
76 default:
77 PrintBuffer[DestinationIndex++] = CurrentCharacter;
78 break;
79 }
80
81 if (DestinationIndex > PRINT_BUFFER_THRESHOLD) {
82 PrintBuffer[DestinationIndex] = '\0';
83 SemihostWriteString ((CHAR8 *)PrintBuffer);
84
85 DestinationIndex = 0;
86 }
87 }
88
89 if (DestinationIndex > 0) {
90 PrintBuffer[DestinationIndex] = '\0';
91 SemihostWriteString ((CHAR8 *)PrintBuffer);
92 }
93
94 return NumberOfBytes;
95 }
96
97 /**
98 Read data from serial device and save the datas in buffer.
99
100 @param Buffer Point of data buffer which need to be written.
101 @param NumberOfBytes Number of output bytes which are cached in Buffer.
102
103 @retval 0 Read data failed.
104 @retval !0 Actual number of bytes read from serial device.
105
106 **/
107 UINTN
108 EFIAPI
109 SerialPortRead (
110 OUT UINT8 *Buffer,
111 IN UINTN NumberOfBytes
112 )
113 {
114 *Buffer = SemihostReadCharacter ();
115 return 1;
116 }
117
118 /**
119 Check to see if any data is available to be read from the debug device.
120
121 @retval TRUE At least one byte of data is available to be read
122 @retval FALSE No data is available to be read
123
124 **/
125 BOOLEAN
126 EFIAPI
127 SerialPortPoll (
128 VOID
129 )
130 {
131 // Since SemiHosting read character is blocking always say we have a char ready?
132 return SemihostConnectionSupported ();
133 }