Skip to content

Commit 231a87e

Browse files
eliasolVirv12
authored andcommitted
Handle network media loss
1 parent 544ee80 commit 231a87e

6 files changed

Lines changed: 45 additions & 6 deletions

File tree

pixie-uefi/src/os/executor/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use futures::channel::oneshot;
1515
use spin::Mutex;
1616
use uefi::proto::console::text::Color;
1717

18+
use crate::os;
1819
use crate::os::executor::event::{Event, EventTrigger};
1920
use crate::os::send_wrapper::SendWrapper;
2021
use crate::os::timer::Timer;
@@ -239,11 +240,7 @@ impl Executor {
239240
let task = EXECUTOR.lock().ready_tasks.pop_front();
240241
let Some(task) = task else {
241242
// If we don't have anything ready, sleep until the next interrupt.
242-
// SAFETY: hlt is available on all reasonable x86 processors and has no safety
243-
// requirements.
244-
unsafe {
245-
core::arch::asm!("hlt");
246-
}
243+
os::util::hlt();
247244
do_wake(true);
248245
continue;
249246
};

pixie-uefi/src/os/input.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use spin::Mutex;
33
use uefi::boot::ScopedProtocol;
44
use uefi::proto::console::text::{Input, Key};
55

6+
use crate::os;
67
use crate::os::error::Result;
78
use crate::os::executor::Executor;
89
use crate::os::send_wrapper::SendWrapper;
@@ -21,3 +22,12 @@ pub async fn read_key() -> Result<Key> {
2122
Executor::wait_for_interrupt().await;
2223
}
2324
}
25+
26+
pub fn wait_for_key() -> Result<()> {
27+
loop {
28+
os::util::hlt();
29+
if INPUT.lock().read_key()?.is_some() {
30+
return Ok(());
31+
}
32+
}
33+
}

pixie-uefi/src/os/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod net;
2222
mod send_wrapper;
2323
mod timer;
2424
pub mod ui;
25+
pub mod util;
2526

2627
static INITIALIZED: AtomicBool = AtomicBool::new(false);
2728

pixie-uefi/src/os/net/interface.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use uefi::Status;
66

77
use super::ETH_PACKET_SIZE;
88
use crate::os::send_wrapper::SendWrapper;
9+
use crate::os::{input, ui};
10+
use crate::power_control;
911

1012
type Snp = SendWrapper<ScopedProtocol<SimpleNetwork>>;
1113

@@ -68,7 +70,19 @@ impl TxToken for SnpTxToken<'_> {
6870
snp.transmit(0, payload, None, None, None)
6971
.expect("Failed to transmit frame");
7072
// Wait until sending is complete.
71-
while snp.get_recycled_transmit_buffer_status().unwrap().is_none() {}
73+
while snp.get_recycled_transmit_buffer_status().unwrap().is_none() {
74+
if snp.mode().media_present.0 == 0 {
75+
let err = uefi::boot::set_watchdog_timer(0, 0x10000, None);
76+
if let Err(err) = err {
77+
if err.status() != Status::UNSUPPORTED {
78+
log::error!("Error disabling watchdog: {err:?}");
79+
}
80+
}
81+
ui::red_screen();
82+
input::wait_for_key().expect("Failed to wait for input");
83+
power_control::reset();
84+
}
85+
}
7286
ret
7387
}
7488
}

pixie-uefi/src/os/ui.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,13 @@ pub fn update_content<F: Fn(&mut DrawArea)>(f: F) {
307307
.try_lock()
308308
.expect("content draw area is locked"));
309309
}
310+
311+
pub fn red_screen() {
312+
let mut s = SCREEN.lock();
313+
s.back_buffer.fill(ScreenChar {
314+
c: ' '.try_into().unwrap(),
315+
fg: Color::Red,
316+
bg: Color::Red,
317+
});
318+
s.flush();
319+
}

pixie-uefi/src/os/util.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub fn hlt() {
2+
// SAFETY: hlt is available on all reasonable x86 processors and has no safety
3+
// requirements.
4+
unsafe {
5+
core::arch::asm!("hlt");
6+
}
7+
}

0 commit comments

Comments
 (0)