Skip to content

Commit

Permalink
lspci
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSawa committed Jan 7, 2022
1 parent 0f78f68 commit 577c622
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
45 changes: 40 additions & 5 deletions crates/kernel/src/gui/widgets/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ impl Terminal {
self.cursor_col = 0;
}

fn push_char_impl(&mut self, c: char, add_to_command: bool) {
fn push_char_impl(&mut self, c: char, command_related: bool) {
if c == '\n' {
self.new_line();
let command = core::mem::take(&mut self.current_string);
self.execute_command(command);
self.prompt();
if command_related {
let command = core::mem::take(&mut self.current_string);
self.execute_command(command);
self.prompt();
}
} else if c == '\x08' {
if self.current_string.pop().is_some() {
// Delete cursor
Expand Down Expand Up @@ -162,7 +164,7 @@ impl Terminal {
if self.cursor_col == self.cols {
self.new_line();
}
if add_to_command {
if command_related {
self.current_string.push(c);
}
}
Expand All @@ -177,6 +179,19 @@ impl Terminal {
self.push_char_impl('>', false);
}

fn as_result_writer(&mut self) -> impl '_ + core::fmt::Write {
struct W<'a>(&'a mut Terminal);
impl<'a> core::fmt::Write for W<'a> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for c in s.chars() {
self.0.push_char_impl(c, false);
}
Ok(())
}
}
W(self)
}

fn execute_command(&mut self, command: String) {
if let Some((command, args)) = command.split_once(' ') {
if command == "echo" {
Expand All @@ -196,6 +211,26 @@ impl Terminal {
for buffer in self.buffers.iter_mut() {
buffer.fill_rectangle(BG_COLOR, buffer.bounding_box());
}
} else if command == "lspci" {
use core::fmt::Write;
for device in crate::pci::scan_devices() {
for func in device.scan_functions() {
let (base, sub, interface) = func.class().to_code();
writeln!(
self.as_result_writer(),
"{:02x}:{:02x}.{} vend={:04x} head={:02x} class={:02x}.{:02x}.{:02x}",
func.bus(),
func.device(),
func.function(),
func.vendor_id(),
func.header_type(),
base,
sub,
interface
)
.ok();
}
}
} else {
for c in "Unknown command".chars() {
self.push_char_impl(c, false);
Expand Down
27 changes: 24 additions & 3 deletions crates/kernel/src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,49 @@ pub struct PCICapabilityHeader {
}

impl PCIClass {
fn from_code(base: u8, sub: u8, interface: u8) -> Self {
pub fn from_code(base: u8, sub: u8, interface: u8) -> Self {
match base {
0x0c => Self::SerialBusController(SerialBusSubclass::from_code(sub, interface)),
_ => Self::Unimplemented(base, sub, interface),
}
}
pub fn to_code(self) -> (u8, u8, u8) {
match self {
Self::SerialBusController(a) => {
let (b, c) = a.to_code();
(0x03, b, c)
}
Self::Unimplemented(a, b, c) => (a, b, c),
}
}
}
impl SerialBusSubclass {
fn from_code(sub: u8, interface: u8) -> Self {
pub fn from_code(sub: u8, interface: u8) -> Self {
match sub {
0x03 => Self::USBController(USBProgramInterface::from_code(interface)),
_ => Self::Unimplemented(sub, interface),
}
}
pub fn to_code(self) -> (u8, u8) {
match self {
Self::USBController(a) => (0x03, a.to_code()),
Self::Unimplemented(a, b) => (a, b),
}
}
}
impl USBProgramInterface {
fn from_code(interface: u8) -> Self {
pub fn from_code(interface: u8) -> Self {
match interface {
0x30 => Self::XHCI,
_ => Self::Unimplemented(interface),
}
}
pub fn to_code(self) -> u8 {
match self {
Self::XHCI => 0x30,
Self::Unimplemented(a) => a,
}
}
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Getters)]
Expand Down

0 comments on commit 577c622

Please sign in to comment.