Updating ArmLib.h to add functions needed to turn on paging in CpuDxe. Also added...
[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
4 Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r
5 \r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <Uefi.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/SemihostLib.h>\r
19#include <Library/SerialPortLib.h>\r
20\r
21\r
22/*\r
23\r
24 Programmed hardware of Serial port.\r
25\r
26 @return Always return EFI_UNSUPPORTED.\r
27\r
28**/\r
29RETURN_STATUS\r
30EFIAPI\r
31SerialPortInitialize (\r
32 VOID\r
33 )\r
34{\r
35 if (SemihostConnectionSupported ()) {\r
36 return RETURN_SUCCESS;\r
37 } else {\r
38 return RETURN_UNSUPPORTED;\r
39 }\r
40}\r
41\r
42/**\r
43 Write data to serial device.\r
44\r
45 @param Buffer Point of data buffer which need to be writed.\r
46 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
47\r
48 @retval 0 Write data failed.\r
49 @retval !0 Actual number of bytes writed to serial device.\r
50\r
51**/\r
52\r
53#define PRINT_BUFFER_SIZE 512\r
54#define PRINT_BUFFER_THRESHOLD (PRINT_BUFFER_SIZE - 4)\r
55\r
56UINTN\r
57EFIAPI\r
58SerialPortWrite (\r
59 IN UINT8 *Buffer,\r
60 IN UINTN NumberOfBytes\r
61)\r
62{\r
63 UINT8 PrintBuffer[PRINT_BUFFER_SIZE];\r
64 UINTN SourceIndex = 0;\r
65 UINTN DestinationIndex = 0;\r
66 UINT8 CurrentCharacter;\r
67\r
68 while (SourceIndex < NumberOfBytes)\r
69 {\r
70 CurrentCharacter = Buffer[SourceIndex++];\r
71 \r
72 switch (CurrentCharacter)\r
73 {\r
74 case '\r':\r
75 continue;\r
76\r
77 case '\n':\r
78 PrintBuffer[DestinationIndex++] = ' ';\r
79 // fall through\r
80\r
81 default:\r
82 PrintBuffer[DestinationIndex++] = CurrentCharacter;\r
83 break;\r
84 }\r
85\r
86 if (DestinationIndex > PRINT_BUFFER_THRESHOLD)\r
87 {\r
88 PrintBuffer[DestinationIndex] = '\0';\r
89 SemihostWriteString ((CHAR8 *) PrintBuffer);\r
90\r
91 DestinationIndex = 0;\r
92 }\r
93 }\r
94 \r
95 if (DestinationIndex > 0)\r
96 {\r
97 PrintBuffer[DestinationIndex] = '\0';\r
98 SemihostWriteString ((CHAR8 *) PrintBuffer);\r
99 }\r
100\r
101 return 0;\r
102}\r
103\r
104\r
105/**\r
106 Read data from serial device and save the datas in buffer.\r
107\r
108 @param Buffer Point of data buffer which need to be writed.\r
109 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
110\r
111 @retval 0 Read data failed.\r
112 @retval !0 Aactual number of bytes read from serial device.\r
113\r
114**/\r
115UINTN\r
116EFIAPI\r
117SerialPortRead (\r
118 OUT UINT8 *Buffer,\r
119 IN UINTN NumberOfBytes\r
120)\r
121{\r
122 *Buffer = SemihostReadCharacter ();\r
123 return 1;\r
124}\r
125\r
126\r
127\r
128/**\r
129 Check to see if any data is avaiable to be read from the debug device.\r
130\r
131 @retval TRUE At least one byte of data is avaiable to be read\r
132 @retval FALS No data is avaiable to be read\r
133\r
134**/\r
135BOOLEAN\r
136EFIAPI\r
137SerialPortPoll (\r
138 VOID\r
139 )\r
140{\r
141 // Since SemiHosting read character is blocking always say we have a char ready?\r
142 return SemihostConnectionSupported ();\r
143}\r
144\r