1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use crate::proto::Protocol;
use crate::{unsafe_guid, Result, Status};
use bitflags::bitflags;
#[repr(C)]
#[unsafe_guid("bb25cf6f-f1d4-11d2-9a0c-0090273fc1fd")]
#[derive(Protocol)]
pub struct Serial<'boot> {
revision: u32,
reset: extern "efiapi" fn(&mut Serial) -> Status,
set_attributes: extern "efiapi" fn(
&Serial,
baud_rate: u64,
receive_fifo_depth: u32,
timeout: u32,
parity: Parity,
data_bits: u8,
stop_bits_type: StopBits,
) -> Status,
set_control_bits: extern "efiapi" fn(&mut Serial, ControlBits) -> Status,
get_control_bits: extern "efiapi" fn(&Serial, &mut ControlBits) -> Status,
write: unsafe extern "efiapi" fn(&mut Serial, &mut usize, *const u8) -> Status,
read: unsafe extern "efiapi" fn(&mut Serial, &mut usize, *mut u8) -> Status,
io_mode: &'boot IoMode,
}
impl<'boot> Serial<'boot> {
pub fn reset(&mut self) -> Result {
(self.reset)(self).into()
}
pub fn io_mode(&self) -> &IoMode {
self.io_mode
}
pub fn set_attributes(&mut self, mode: &IoMode) -> Result {
(self.set_attributes)(
self,
mode.baud_rate,
mode.receive_fifo_depth,
mode.timeout,
mode.parity,
mode.data_bits as u8,
mode.stop_bits,
)
.into()
}
pub fn get_control_bits(&self) -> Result<ControlBits> {
let mut bits = ControlBits::empty();
(self.get_control_bits)(self, &mut bits).into_with_val(|| bits)
}
pub fn set_control_bits(&mut self, bits: ControlBits) -> Result {
(self.set_control_bits)(self, bits).into()
}
pub fn read(&mut self, data: &mut [u8]) -> Result<(), usize> {
let mut buffer_size = data.len();
unsafe { (self.read)(self, &mut buffer_size, data.as_mut_ptr()) }.into_with(
|| debug_assert_eq!(buffer_size, data.len()),
|_| buffer_size,
)
}
pub fn write(&mut self, data: &[u8]) -> Result<(), usize> {
let mut buffer_size = data.len();
unsafe { (self.write)(self, &mut buffer_size, data.as_ptr()) }.into_with(
|| debug_assert_eq!(buffer_size, data.len()),
|_| buffer_size,
)
}
}
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct IoMode {
pub control_mask: ControlBits,
pub timeout: u32,
pub baud_rate: u64,
pub receive_fifo_depth: u32,
pub data_bits: u32,
pub parity: Parity,
pub stop_bits: StopBits,
}
bitflags! {
pub struct ControlBits: u32 {
const CLEAR_TO_SEND = 0x10;
const DATA_SET_READY = 0x20;
const RING_INDICATE = 0x40;
const CARRIER_DETECT = 0x80;
const INPUT_BUFFER_EMPTY = 0x100;
const OUTPUT_BUFFER_EMPTY = 0x200;
const DATA_TERMINAL_READY = 0x1;
const REQUEST_TO_SEND = 0x2;
const HARDWARE_LOOPBACK_ENABLE = 0x1000;
const SOFTWARE_LOOPBACK_ENABLE = 0x2000;
const HARDWARE_FLOW_CONTROL_ENABLE = 0x4000;
const SETTABLE =
ControlBits::DATA_TERMINAL_READY.bits
| ControlBits::REQUEST_TO_SEND.bits
| ControlBits::HARDWARE_LOOPBACK_ENABLE.bits
| ControlBits::SOFTWARE_LOOPBACK_ENABLE.bits
| ControlBits::HARDWARE_FLOW_CONTROL_ENABLE.bits;
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u32)]
pub enum Parity {
Default = 0,
None,
Even,
Odd,
Mark,
Space,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u32)]
pub enum StopBits {
Default = 0,
One,
OneFive,
Two,
}