1 | /** @file
|
---|
2 | Serial Port Library backed by SBI console.
|
---|
3 |
|
---|
4 | Common functionality shared by PrePiDxeSerialPortLibRiscVSbi and
|
---|
5 | PrePiDxeSerialPortLibRiscVSbiRam implementations.
|
---|
6 |
|
---|
7 | Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
|
---|
8 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
9 |
|
---|
10 | **/
|
---|
11 |
|
---|
12 | #include "Common.h"
|
---|
13 |
|
---|
14 | /**
|
---|
15 | Return whether the DBCN extension is implemented.
|
---|
16 |
|
---|
17 | @retval TRUE Extension is implemented.
|
---|
18 | @retval FALSE Extension is not implemented.
|
---|
19 |
|
---|
20 | **/
|
---|
21 | BOOLEAN
|
---|
22 | SbiImplementsDbcn (
|
---|
23 | VOID
|
---|
24 | )
|
---|
25 | {
|
---|
26 | SBI_RET Ret;
|
---|
27 |
|
---|
28 | Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_DBCN);
|
---|
29 | if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
|
---|
30 | (Ret.Value != 0))
|
---|
31 | {
|
---|
32 | return TRUE;
|
---|
33 | }
|
---|
34 |
|
---|
35 | return FALSE;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | Return whether the legacy console putchar extension is implemented.
|
---|
40 |
|
---|
41 | @retval TRUE Extension is implemented.
|
---|
42 | @retval FALSE Extension is not implemented.
|
---|
43 |
|
---|
44 | **/
|
---|
45 | BOOLEAN
|
---|
46 | SbiImplementsLegacyPutchar (
|
---|
47 | VOID
|
---|
48 | )
|
---|
49 | {
|
---|
50 | SBI_RET Ret;
|
---|
51 |
|
---|
52 | Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
|
---|
53 | if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
|
---|
54 | (Ret.Value != 0))
|
---|
55 | {
|
---|
56 | return TRUE;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return FALSE;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | Write data from buffer to console via SBI legacy putchar extension.
|
---|
64 |
|
---|
65 | The number of bytes actually written to the SBI console is returned.
|
---|
66 | If the return value is less than NumberOfBytes, then the write operation failed.
|
---|
67 |
|
---|
68 | @param Buffer The pointer to the data buffer to be written.
|
---|
69 | @param NumberOfBytes The number of bytes to written to the serial device.
|
---|
70 |
|
---|
71 | @retval >=0 The number of bytes written to the serial device.
|
---|
72 | If this value is less than NumberOfBytes, then the
|
---|
73 | write operation failed.
|
---|
74 |
|
---|
75 | **/
|
---|
76 | UINTN
|
---|
77 | SbiLegacyPutchar (
|
---|
78 | IN UINT8 *Buffer,
|
---|
79 | IN UINTN NumberOfBytes
|
---|
80 | )
|
---|
81 | {
|
---|
82 | SBI_RET Ret;
|
---|
83 | UINTN Index;
|
---|
84 |
|
---|
85 | for (Index = 0; Index < NumberOfBytes; Index++) {
|
---|
86 | Ret = SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
|
---|
87 | if ((INT64)Ret.Error < 0) {
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | return Index;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | Write data from buffer to console via SBI DBCN.
|
---|
97 |
|
---|
98 | The number of bytes actually written to the SBI console is returned.
|
---|
99 | If the return value is less than NumberOfBytes, then the write operation failed.
|
---|
100 |
|
---|
101 | @param Buffer The pointer to the data buffer to be written.
|
---|
102 | @param NumberOfBytes The number of bytes to written to the serial device.
|
---|
103 |
|
---|
104 | @retval >=0 The number of bytes written to the serial device.
|
---|
105 | If this value is less than NumberOfBytes, then the
|
---|
106 | write operation failed.
|
---|
107 |
|
---|
108 | **/
|
---|
109 | UINTN
|
---|
110 | SbiDbcnWrite (
|
---|
111 | IN UINT8 *Buffer,
|
---|
112 | IN UINTN NumberOfBytes
|
---|
113 | )
|
---|
114 | {
|
---|
115 | SBI_RET Ret;
|
---|
116 |
|
---|
117 | Ret = SbiCall (
|
---|
118 | SBI_EXT_DBCN,
|
---|
119 | SBI_EXT_DBCN_WRITE,
|
---|
120 | 3,
|
---|
121 | NumberOfBytes,
|
---|
122 | ((UINTN)Buffer),
|
---|
123 | 0
|
---|
124 | );
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * May do partial writes. Don't bother decoding
|
---|
128 | * Ret.Error as we're only interested in number of
|
---|
129 | * bytes written to console.
|
---|
130 | */
|
---|
131 | return Ret.Value;
|
---|
132 | }
|
---|