Skip to content

Commit 641190c

Browse files
committed
Update intel-spi and add gaze17
1 parent 790372e commit 641190c

File tree

7 files changed

+177
-43
lines changed

7 files changed

+177
-43
lines changed

Cargo.lock

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ lto = true
99

1010
[dependencies]
1111
coreboot-fs = "0.1.0"
12-
intel-spi = "0.1.3"
12+
intel-spi = "0.1.4"
1313
plain = "0.2.3"
1414
redox_dmi = "0.1.5"
1515
redox_hwio = "0.1.4"

src/app/bios.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use alloc::string::String;
55
use core::char;
66
use coreboot_fs::Rom;
77
use ecflash::EcFlash;
8-
use intel_spi::{HsfStsCtl, Spi, SpiKbl, SpiCnl};
8+
use intel_spi::{HsfStsCtl, Spi, SpiDev};
99
use plain::Plain;
1010
use std::fs::{find, load};
1111
use std::ptr;
1212
use std::vars::{get_boot_item, get_boot_order, set_boot_item, set_boot_order};
1313
use std::uefi::reset::ResetType;
1414
use std::uefi::status::{Error, Result, Status};
1515

16-
use super::{FIRMWARECAP, FIRMWAREDIR, FIRMWARENSH, FIRMWAREROM, H2OFFT, IFLASHV, UEFIFLASH, shell, Component};
16+
use super::{FIRMWARECAP, FIRMWAREDIR, FIRMWARENSH, FIRMWAREROM, H2OFFT, IFLASHV, UEFIFLASH, shell, Component, pci_mcfg, UefiMapper};
1717

1818
fn copy_region(region: intelflash::RegionKind, old_data: &[u8], new_data: &mut [u8]) -> core::result::Result<bool, String> {
1919
let old_opt = intelflash::Rom::new(old_data)?.get_region_base_limit(region)?;
@@ -102,44 +102,54 @@ impl BiosComponent {
102102
}
103103
}
104104

105-
pub fn spi(&self) -> Option<(&'static mut dyn Spi, HsfStsCtl)> {
105+
pub fn spi(&self) -> Option<(SpiDev<'static, UefiMapper>, HsfStsCtl)> {
106+
static mut UEFI_MAPPER: UefiMapper = UefiMapper;
107+
106108
match self.bios_vendor.as_str() {
107109
"coreboot" => match self.system_version.as_str() {
108-
"galp2" |
109-
"galp3" |
110-
"galp3-b" => {
111-
let spi_kbl = unsafe {
112-
&mut *(SpiKbl::address() as *mut SpiKbl)
113-
};
114-
let hsfsts_ctl = spi_kbl.hsfsts_ctl();
115-
Some((spi_kbl as &mut dyn Spi, hsfsts_ctl))
116-
},
117110
"addw1" |
118111
"addw2" |
119112
"bonw14" |
120113
"darp5" |
121114
"darp6" |
122-
"darp7" | // Technically TGL-U but protocol is the same
115+
"darp7" |
116+
"galp2" |
117+
"galp3" |
118+
"galp3-b" |
123119
"galp3-c" |
124120
"galp4" |
125-
"galp5" | // Technically TGL-U but protocol is the same
121+
"galp5" |
126122
"gaze14" |
127123
"gaze15" |
128-
"gaze16-3050" | // Technically TGL-H but protocol is the same
129-
"gaze16-3060" | // Technically TGL-H but protocol is the same
130-
"gaze16-3060-b" | // Technically TGL-H but protocol is the same
124+
"gaze16-3050" |
125+
"gaze16-3060" |
126+
"gaze16-3060-b" |
127+
"gaze17-3050" |
128+
"gaze17-3060" |
129+
"gaze17-3060-b" |
131130
"lemp9" |
132-
"lemp10" | // Technically TGL-U but protocol is the same
131+
"lemp10" |
133132
"oryp5" |
134133
"oryp6" |
135134
"oryp7" |
136-
"oryp8" // Technically TGL-H but protocol is the same
135+
"oryp8"
137136
=> {
138-
let spi_cnl = unsafe {
139-
&mut *(SpiCnl::address() as *mut SpiCnl)
137+
let mcfg = match pci_mcfg() {
138+
Some(some) => some,
139+
None => {
140+
println!("failed to get MCFG table");
141+
return None;
142+
}
143+
};
144+
let spi = match unsafe { SpiDev::new(mcfg, &mut UEFI_MAPPER) } {
145+
Ok(ok) => ok,
146+
Err(err) => {
147+
println!("failed to get SPI device: {}", err);
148+
return None;
149+
}
140150
};
141-
let hsfsts_ctl = spi_cnl.hsfsts_ctl();
142-
Some((spi_cnl as &mut dyn Spi, hsfsts_ctl))
151+
let hsfsts_ctl = spi.regs.hsfsts_ctl();
152+
Some((spi, hsfsts_ctl))
143153
},
144154
_ => None,
145155
},
@@ -200,7 +210,7 @@ impl Component for BiosComponent {
200210

201211
fn validate(&self) -> Result<bool> {
202212
let data = load(self.path())?;
203-
if let Some((spi, _hsfsts_ctl)) = self.spi() {
213+
if let Some((mut spi, _hsfsts_ctl)) = self.spi() {
204214
// if hsfsts_ctl.contains(HsfStsCtl::FDOPSS) {
205215
// println!("SPI currently locked, attempting to unlock");
206216
// Self::spi_unlock();
@@ -224,7 +234,7 @@ impl Component for BiosComponent {
224234
}
225235

226236
fn flash(&self) -> Result<()> {
227-
if let Some((spi, _hsfsts_ctl)) = self.spi() {
237+
if let Some((mut spi, _hsfsts_ctl)) = self.spi() {
228238
// Read new data
229239
let mut new;
230240
{

src/app/ec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ impl EcComponent {
163163
"system76/gaze16-3060".to_string()
164164
}
165165
},
166+
"NPxxPNJ_K" => "system76/gaze17-3050".to_string(),
167+
"NPxxPNP" => {
168+
// If the builtin ethernet at 00:1f.6 is present, this is a -b variant
169+
if pci_read(0x00, 0x1f, 0x6, 0x00).unwrap() == 0x1a1e8086 {
170+
"system76/gaze17-3060-b".to_string()
171+
} else {
172+
"system76/gaze17-3060".to_string()
173+
}
174+
},
166175
"NS50MU" => "system76/darp7".to_string(),
167176
"NV40Mx" | "NV40Mx-DV" | "NV40MJ" => "system76/galp5".to_string(),
168177
"PB50Ex" => "system76/addw1".to_string(),

src/app/mapper.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use intel_spi::{Mapper, PhysicalAddress, VirtualAddress};
2+
3+
pub struct UefiMapper;
4+
5+
impl Mapper for UefiMapper {
6+
unsafe fn map_aligned(&mut self, address: PhysicalAddress, _size: usize) -> Result<VirtualAddress, &'static str> {
7+
Ok(VirtualAddress(address.0 as usize))
8+
}
9+
10+
unsafe fn unmap_aligned(&mut self, _address: VirtualAddress, _size: usize) -> Result<(), &'static str> {
11+
Ok(())
12+
}
13+
14+
fn page_size(&self) -> usize {
15+
//TODO: get dynamically
16+
4096
17+
}
18+
}

src/app/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ use crate::text::TextDisplay;
2929
pub use self::bios::BiosComponent;
3030
pub use self::component::Component;
3131
pub use self::ec::EcComponent;
32-
pub use self::pci::pci_read;
32+
pub use self::mapper::UefiMapper;
33+
pub use self::pci::{pci_mcfg, pci_read};
3334

3435
mod bios;
3536
mod component;
3637
mod ec;
38+
mod mapper;
3739
mod pci;
3840

3941
static ECROM: &str = concat!("\\", env!("BASEDIR"), "\\firmware\\ec.rom");

src/app/pci.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,98 @@
1+
use core::{mem, slice};
12
use hwio::{Io, Pio};
3+
use std::uefi::guid::GuidKind;
4+
5+
#[allow(dead_code)]
6+
#[repr(packed)]
7+
struct Rsdp {
8+
signature: [u8; 8], // b"RSD PTR "
9+
chksum: u8,
10+
oem_id: [u8; 6],
11+
revision: u8,
12+
rsdt_addr: u32,
13+
// the following fields are only available for ACPI 2.0, and are reserved otherwise
14+
length: u32,
15+
xsdt_addr: u64,
16+
extended_chksum: u8,
17+
_rsvd: [u8; 3],
18+
}
19+
20+
#[allow(dead_code)]
21+
#[repr(packed)]
22+
struct SdtHeader {
23+
signature: [u8; 4],
24+
length: u32,
25+
revision: u8,
26+
checksum: u8,
27+
oem_id: [u8; 6],
28+
oem_table_id: u64,
29+
oem_revision: u32,
30+
creator_id: u32,
31+
creator_revision: u32,
32+
}
33+
34+
unsafe fn rsdp_mcfg(rsdp: &Rsdp) -> Option<&'static [u8]> {
35+
if rsdp.signature != *b"RSD PTR " {
36+
return None;
37+
}
38+
39+
if rsdp.rsdt_addr != 0 {
40+
let rsdt = &*(rsdp.rsdt_addr as *const SdtHeader);
41+
if let Some(rsdt_data_len) = (rsdt.length as usize).checked_sub(mem::size_of::<SdtHeader>()) {
42+
let entries = slice::from_raw_parts(
43+
(rsdt as *const SdtHeader).offset(1) as *const u32,
44+
rsdt_data_len / mem::size_of::<u32>()
45+
);
46+
for &entry in entries {
47+
let sdt = &*(entry as *const SdtHeader);
48+
if sdt.signature == *b"MCFG" {
49+
return Some(slice::from_raw_parts(
50+
sdt as *const SdtHeader as *const u8,
51+
sdt.length as usize
52+
));
53+
}
54+
}
55+
}
56+
}
57+
58+
if rsdp.revision >= 2 && rsdp.xsdt_addr != 0 {
59+
let xsdt = &*(rsdp.xsdt_addr as *const SdtHeader);
60+
if let Some(rsdt_data_len) = (xsdt.length as usize).checked_sub(mem::size_of::<SdtHeader>()) {
61+
let entries = slice::from_raw_parts(
62+
(xsdt as *const SdtHeader).offset(1) as *const u64,
63+
rsdt_data_len / mem::size_of::<u64>()
64+
);
65+
for &entry in entries {
66+
let sdt = &*(entry as *const SdtHeader);
67+
if sdt.signature == *b"MCFG" {
68+
return Some(slice::from_raw_parts(
69+
sdt as *const SdtHeader as *const u8,
70+
sdt.length as usize
71+
));
72+
}
73+
}
74+
}
75+
}
76+
77+
None
78+
}
79+
80+
pub fn pci_mcfg() -> Option<&'static [u8]> {
81+
for table in std::system_table().config_tables() {
82+
match table.VendorGuid.kind() {
83+
GuidKind::Acpi |
84+
GuidKind::Acpi2 => unsafe {
85+
let rsdp = &*(table.VendorTable as *const Rsdp);
86+
match rsdp_mcfg(rsdp) {
87+
Some(some) => return Some(some),
88+
None => (),
89+
}
90+
},
91+
_ => ()
92+
};
93+
}
94+
None
95+
}
296

397
pub fn pci_read(bus: u8, dev: u8, func: u8, offset: u8) -> Result<u32, String> {
498
if dev > 0x1f {

0 commit comments

Comments
 (0)