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