Skip to content

Commit 942b9bc

Browse files
Some serial port fixes.
Tested with QEMU BIOS.
1 parent 02cafef commit 942b9bc

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/commands/hardware.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ pub static SHUTDOWN_ITEM: menu::Item<Ctx> = menu::Item {
1515
item_type: menu::ItemType::Callback {
1616
function: shutdown,
1717
parameters: &[
18-
menu::Parameter::Named { parameter_name: "reboot", help: Some("Reboot after shutting down") },
19-
menu::Parameter::Named { parameter_name: "bootloader", help: Some("Reboot into the bootloader after shutting down") }
18+
menu::Parameter::Named {
19+
parameter_name: "reboot",
20+
help: Some("Reboot after shutting down"),
21+
},
22+
menu::Parameter::Named {
23+
parameter_name: "bootloader",
24+
help: Some("Reboot into the bootloader after shutting down"),
25+
},
2026
],
2127
},
2228
command: "shutdown",

src/commands/input.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,32 @@ pub static KBTEST_ITEM: menu::Item<Ctx> = menu::Item {
1313

1414
/// Called when the "kbtest" command is executed.
1515
fn kbtest(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
16-
osprintln!("Press ESC to quit");
17-
loop {
16+
osprintln!("Press Ctrl-X to quit");
17+
const CTRL_X: u8 = 0x18;
18+
'outer: loop {
1819
if let Some(ev) = crate::STD_INPUT.lock().get_raw() {
1920
osprintln!("Event: {ev:?}");
20-
if ev == pc_keyboard::DecodedKey::RawKey(pc_keyboard::KeyCode::Escape)
21-
|| ev == pc_keyboard::DecodedKey::Unicode('\u{001b}')
22-
{
23-
break;
21+
if ev == pc_keyboard::DecodedKey::Unicode(CTRL_X as char) {
22+
break 'outer;
23+
}
24+
}
25+
let mut buffer = [0u8; 8];
26+
let count = if let Some(serial) = crate::SERIAL_CONSOLE.lock().as_mut() {
27+
serial
28+
.read_data(&mut buffer)
29+
.ok()
30+
.and_then(|n| if n == 0 { None } else { Some(n) })
31+
} else {
32+
None
33+
};
34+
if let Some(count) = count {
35+
osprintln!("Serial RX: {:x?}", &buffer[0..count]);
36+
for b in &buffer[0..count] {
37+
if *b == CTRL_X {
38+
break 'outer;
39+
}
2440
}
2541
}
2642
}
43+
osprintln!("Finished.");
2744
}

src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl StdInput {
115115
/// The data you get might be cut in the middle of a UTF-8 character.
116116
fn get_data(&mut self, buffer: &mut [u8]) -> usize {
117117
let count = self.get_buffered_data(buffer);
118-
if buffer.len() == 0 || count > 0 {
118+
if buffer.is_empty() || count > 0 {
119119
return count;
120120
}
121121

@@ -169,14 +169,14 @@ impl StdInput {
169169
#[macro_export]
170170
macro_rules! osprint {
171171
($($arg:tt)*) => {
172-
crate::VGA_CONSOLE.with(|guard| {
172+
$crate::VGA_CONSOLE.with(|guard| {
173173
if let Some(console) = guard.as_mut() {
174174
#[allow(unused)]
175175
use core::fmt::Write as _;
176176
write!(console, $($arg)*).unwrap();
177177
}
178178
}).unwrap();
179-
crate::SERIAL_CONSOLE.with(|guard| {
179+
$crate::SERIAL_CONSOLE.with(|guard| {
180180
if let Some(console) = guard.as_mut() {
181181
#[allow(unused)]
182182
use core::fmt::Write as _;
@@ -276,7 +276,11 @@ impl SerialConsole {
276276
fn read_data(&mut self, buffer: &mut [u8]) -> Result<usize, bios::Error> {
277277
let api = API.get();
278278
let ffi_buffer = bios::FfiBuffer::new(buffer);
279-
let res = (api.serial_read)(self.0, ffi_buffer, bios::FfiOption::Some(bios::Timeout::new_ms(0)));
279+
let res = (api.serial_read)(
280+
self.0,
281+
ffi_buffer,
282+
bios::FfiOption::Some(bios::Timeout::new_ms(0)),
283+
);
280284
res.into()
281285
}
282286
}
@@ -377,17 +381,20 @@ pub extern "C" fn os_main(api: &bios::Api) -> ! {
377381
height as isize,
378382
);
379383
vga.clear();
380-
let mut guard = VGA_CONSOLE.try_lock().unwrap();
384+
let mut guard = VGA_CONSOLE.lock();
381385
*guard = Some(vga);
386+
// Drop the lock before trying to grab it again to print something!
382387
drop(guard);
383388
osprintln!("\u{001b}[0mConfigured VGA console {}x{}", width, height);
384389
}
385390
}
386391

387392
if let Some((idx, serial_config)) = config.get_serial_console() {
388393
let _ignored = (api.serial_configure)(idx, serial_config);
389-
let mut guard = SERIAL_CONSOLE.try_lock().unwrap();
394+
let mut guard = SERIAL_CONSOLE.lock();
390395
*guard = Some(SerialConsole(idx));
396+
// Drop the lock before trying to grab it again to print something!
397+
drop(guard);
391398
osprintln!("Configured Serial console on Serial {}", idx);
392399
}
393400

0 commit comments

Comments
 (0)