summaryrefslogtreecommitdiffstats
path: root/bins/pci/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'bins/pci/src/main.rs')
-rw-r--r--bins/pci/src/main.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/bins/pci/src/main.rs b/bins/pci/src/main.rs
new file mode 100644
index 0000000..1182d83
--- /dev/null
+++ b/bins/pci/src/main.rs
@@ -0,0 +1,53 @@
1//! pci — list PCI devices via the exports table.
2
3#![no_std]
4#![no_main]
5
6#[panic_handler]
7fn panic(_info: &core::panic::PanicInfo) -> ! {
8 loop { core::hint::spin_loop(); }
9}
10
11fn put(exports: &rsapi::Exports, buf: &[u8]) {
12 (exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: buf.len() });
13}
14
15fn hex2(b: u8, out: &mut [u8]) {
16 let hi = b >> 4; let lo = b & 0xf;
17 out[0] = if hi < 10 { b'0' + hi } else { b'a' + hi - 10 };
18 out[1] = if lo < 10 { b'0' + lo } else { b'a' + lo - 10 };
19}
20
21#[unsafe(no_mangle)]
22pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 {
23 let count = (exports.pci_probe)();
24 let mut i = 0u32;
25 while i < count {
26 let mut d = rsapi::PciDevice { bus: 0, device: 0, function: 0, class: 0, subclass: 0, vendor: 0, device_id: 0 };
27 if (exports.pci_device)(i, &mut d) {
28 // "XX:XX.F class=XX:XX ven=XXXX dev=XXXX" = 42 bytes max
29 let mut buf = [0u8; 42];
30 hex2(d.bus, &mut buf[0..2]); buf[2] = b':';
31 hex2(d.device, &mut buf[3..5]); buf[5] = b'.';
32 buf[6] = b'0' + d.function;
33 let class_lbl = b" class=";
34 buf[7..7+class_lbl.len()].copy_from_slice(class_lbl);
35 let o = 7 + class_lbl.len();
36 hex2(d.class, &mut buf[o..o+2]); buf[o+2] = b':';
37 hex2(d.subclass, &mut buf[o+3..o+5]);
38 let ven_lbl = b" ven=";
39 buf[o+5..o+5+ven_lbl.len()].copy_from_slice(ven_lbl);
40 let v = o + 5 + ven_lbl.len();
41 hex2((d.vendor >> 8) as u8, &mut buf[v..v+2]);
42 hex2((d.vendor & 0xff) as u8, &mut buf[v+2..v+4]);
43 let dev_lbl = b" dev=";
44 buf[v+4..v+4+dev_lbl.len()].copy_from_slice(dev_lbl);
45 let dd = v + 4 + dev_lbl.len();
46 hex2((d.device_id >> 8) as u8, &mut buf[dd..dd+2]);
47 hex2((d.device_id & 0xff) as u8, &mut buf[dd+2..dd+4]);
48 put(exports, &buf[..dd+4]);
49 }
50 i += 1;
51 }
52 0
53}