Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc cleanups #125

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Rust toolchain
run: rustup show
run: rustup show active-toolchain || rustup toolchain install

- name: clippy
env:
Expand All @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt install --yes make mtools parted
rustup show
rustup show active-toolchain || rustup toolchain install

- name: Build UEFI application
run: make
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
build
firmware
prefix
target
/build
/firmware
/target
50 changes: 26 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
TARGET?=x86_64-unknown-uefi
export BASEDIR?=system76-firmware-update

export LD=ld
export RUST_TARGET_PATH=$(CURDIR)/targets
BUILD=build/$(TARGET)

QEMU?=qemu-system-x86_64
QEMU_FLAGS=\
-accel kvm \
-M q35 \
-m 1024 \
-net none \
-vga std \
-bios /usr/share/OVMF/OVMF_CODE.fd
all: $(BUILD)/boot.img
# SPDX-License-Identifier: GPL-3.0-only

TARGET = x86_64-unknown-uefi
BUILD = build/$(TARGET)
QEMU = qemu-system-x86_64
OVMF = /usr/share/OVMF

export BASEDIR ?= system76-firmware-update

all: $(BUILD)/boot.efi

.PHONY: clean
clean:
cargo clean
rm -rf build

update:
git submodule update --init --recursive --remote
cargo update

qemu: $(BUILD)/boot.img
$(QEMU) $(QEMU_FLAGS) $<
.PHONY: qemu
qemu: $(BUILD)/boot.img $(OVMF)/OVMF_VARS.fd $(OVMF)/OVMF_CODE.fd
cp $(OVMF)/OVMF_CODE.fd target/
cp $(OVMF)/OVMF_VARS.fd target/
$(QEMU) -enable-kvm -M q35 -m 1024 -vga std \
-chardev stdio,mux=on,id=debug \
-device isa-serial,index=2,chardev=debug \
-device isa-debugcon,iobase=0x402,chardev=debug \
-drive if=pflash,format=raw,readonly=on,file=target/OVMF_CODE.fd \
-drive if=pflash,format=raw,readonly=on,file=target/OVMF_VARS.fd \
-net none \
$<

$(BUILD)/boot.img: $(BUILD)/efi.img
dd if=/dev/zero of=$@.tmp bs=512 count=100352
Expand All @@ -45,8 +46,9 @@ $(BUILD)/efi.img: $(BUILD)/boot.efi res/*
if [ -d firmware ]; then mcopy -i $@.tmp -s firmware ::$(BASEDIR); fi
mv $@.tmp $@

$(BUILD)/boot.efi: Cargo.lock Cargo.toml src/* src/*/*
mkdir -p $(BUILD)
.PHONY: $(BUILD)/boot.efi
$(BUILD)/boot.efi:
mkdir -p $(@D)
cargo rustc \
--target $(TARGET) \
--release \
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ firmware-update expects the firmware images to have specific names:

The mechanism used to apply updates depends on the firmware image:

- coreboot-based SBIOS: firmware-update flashes using [intel-spi](https://github.com/system76/intel-spi)
- System76 EC: firmware-update flashes using [ecflash](https://github.com/system76/ecflash)
- Proprietary: The vendor-provided tools are used
- coreboot-based system firmware: [intel-spi](https://github.com/system76/intel-spi)
- System76 EC: [ectool](https://github.com/system76/ec)
- Proprietary: Vendor-provided tools
10 changes: 7 additions & 3 deletions src/app/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ impl Component for BiosComponent {
new_offset,
new_size / 1024
);
let slice = data.get(offset..offset + size).ok_or(Status::DEVICE_ERROR)?;
let slice = data
.get(offset..offset + size)
.ok_or(Status::DEVICE_ERROR)?;

if slice.len() == new_slice.len() {
new_slice.copy_from_slice(slice);
Expand All @@ -407,7 +409,7 @@ impl Component for BiosComponent {
area_name
);
}
} else if areas.get(area_name).is_some() {
} else if areas.contains_key(area_name) {
println!(
"{}: found in old firmware, but not found in new firmware",
area_name
Expand Down Expand Up @@ -487,7 +489,9 @@ impl Component for BiosComponent {

// Have coreboot reset the option table to the defaults.
let mut cmos_options = cmos::CmosOptionTable::new();
unsafe { cmos_options.invalidate_checksum(); }
unsafe {
cmos_options.invalidate_checksum();
}
} else {
find(FIRMWARENSH)?;

Expand Down
66 changes: 34 additions & 32 deletions src/app/ec.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-only

use ecflash::{Ec, EcFile, EcFlash};
use ectool::{timeout, Access, AccessLpcDirect, Firmware, SecurityState, Spi, SpiRom, SpiTarget, Timeout};
use plain::Plain;
use std::prelude::*;
use std::uefi::{
self,
reset::ResetType,
};
use core::cell::Cell;
use core::ptr;
use core::str;
use ecflash::{Ec, EcFile, EcFlash};
use ectool::{
timeout, Access, AccessLpcDirect, Firmware, SecurityState, Spi, SpiRom, SpiTarget, Timeout,
};
use plain::Plain;
use std::prelude::*;
use std::uefi::{self, reset::ResetType};
use std::{
ffi::wstr,
fs::{find, load},
};

use super::{pci_read, shell, Component, EC2ROM, ECROM, ECTAG, FIRMWAREDIR, FIRMWARENSH, sideband::Sideband};
use super::{
pci_read, shell, sideband::Sideband, Component, EC2ROM, ECROM, ECTAG, FIRMWAREDIR, FIRMWARENSH,
};

pub struct UefiTimeout {
duration: u64,
Expand Down Expand Up @@ -47,7 +48,10 @@ impl Timeout for UefiTimeout {

pub enum EcKind {
Pang(ectool::Pmc<UefiTimeout>, String),
System76(ectool::Ec<AccessLpcDirect<UefiTimeout>>, ectool::Pmc<UefiTimeout>),
System76(
ectool::Ec<AccessLpcDirect<UefiTimeout>>,
ectool::Pmc<UefiTimeout>,
),
Legacy(EcFlash),
Unknown,
}
Expand All @@ -74,21 +78,21 @@ impl EcKind {
}
}

if system_version == "pang12" || system_version == "pang13" || system_version == "pang14" ||
system_version == "pang15" {
if system_version == "pang12"
|| system_version == "pang13"
|| system_version == "pang14"
|| system_version == "pang15"
{
return EcKind::Pang(
ectool::Pmc::new(0x62, UefiTimeout::new(100_000)),
system_version
system_version,
);
}
}

if let Ok(access) = AccessLpcDirect::new(UefiTimeout::new(100_000)) {
if let Ok(ec) = ectool::Ec::new(access) {
return EcKind::System76(
ec,
ectool::Pmc::new(0x62, UefiTimeout::new(100_000))
);
return EcKind::System76(ec, ectool::Pmc::new(0x62, UefiTimeout::new(100_000)));
}
}

Expand All @@ -104,15 +108,15 @@ impl EcKind {
EcKind::Pang(ref mut pmc, _system_version) => {
let ecwr = pmc.acpi_read(0x80).unwrap_or(0);
(ecwr & 0x01) == 0x01
},
}
EcKind::System76(_ec, ref mut pmc) => {
let adp = pmc.acpi_read(0x10).unwrap_or(0);
(adp & 0x01) == 0x01
},
}
EcKind::Legacy(ref mut ec) => {
let adp = ec.get_param(0x10).unwrap_or(0);
(adp & 0x01) == 0x01
},
}
EcKind::Unknown => true,
}
}
Expand All @@ -121,7 +125,7 @@ impl EcKind {
match self {
EcKind::Pang(_pmc, system_version) => {
return system_version.clone();
},
}
EcKind::System76(ec, _pmc) => {
let data_size = ec.access().data_size();
let mut data = vec![0; data_size];
Expand Down Expand Up @@ -149,7 +153,7 @@ impl EcKind {
Err(err) => {
println!("Failed to read build time: {:?}", err);
return String::new();
},
}
}
}

Expand All @@ -160,16 +164,15 @@ impl EcKind {
Err(err) => {
println!("Failed to read build date: {:?}", err);
return String::new();
},
}
}
}

return format!(
"20{:02}/{:02}/{:02}_{:02}:{:02}:{:02}",
ymd[0], ymd[1], ymd[2],
hms[0], hms[1], hms[2]
ymd[0], ymd[1], ymd[2], hms[0], hms[1], hms[2]
);
},
}
EcKind::System76(ec, _pmc) => {
let data_size = ec.access().data_size();
let mut data = vec![0; data_size];
Expand Down Expand Up @@ -247,7 +250,7 @@ impl EcComponent {
} else {
"system76/lemp13-b".to_string()
}
},
}
"N130ZU" => "system76/galp3-c".to_string(),
"N140CU" => "system76/galp4".to_string(),
"N150ZU" => "system76/darp5".to_string(),
Expand Down Expand Up @@ -288,7 +291,7 @@ impl EcComponent {
"system76/darp10".to_string()
}
}
},
}
"NV40Mx" | "NV40Mx-DV" | "NV40MJ" => "system76/galp5".to_string(),
"NV4xPZ" => "system76/galp6".to_string(),
"NV40RZ" => "system76/galp7".to_string(),
Expand Down Expand Up @@ -489,7 +492,7 @@ pub unsafe fn security_unlock() -> core::result::Result<(), ectool::Error> {
ResetType::Shutdown,
Status(0),
0,
ptr::null()
ptr::null(),
);
}
},
Expand Down Expand Up @@ -746,7 +749,7 @@ impl Component for EcComponent {
println!("{} Flash Error: {}", self.name(), status);
Err(Status::DEVICE_ERROR)
}
},
}
EcKind::System76(_ec, _pmc) => {
// System76 EC requires reset to load new firmware
requires_reset = true;
Expand Down Expand Up @@ -792,8 +795,7 @@ impl Component for EcComponent {
| uefi::fs::FILE_MODE_READ
| uefi::fs::FILE_MODE_WRITE,
0,
)
{
) {
Status::SUCCESS => {
unsafe {
let _ = ((*file).Close)(&mut *file);
Expand Down
2 changes: 2 additions & 0 deletions src/app/mapper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-only

use intel_spi::{Mapper, PhysicalAddress, VirtualAddress};

pub struct UefiMapper;
Expand Down
23 changes: 18 additions & 5 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::prelude::*;
use std::proto::Protocol;
use std::uefi::reset::ResetType;
use std::vars::{
get_boot_current, get_boot_item, get_boot_next, get_boot_order,
set_boot_item, set_boot_next, set_boot_order,
get_boot_current, get_boot_item, get_boot_next, get_boot_order, set_boot_item, set_boot_next,
set_boot_order,
};

use crate::display::{Display, Output, ScaledDisplay};
Expand Down Expand Up @@ -159,7 +159,13 @@ fn reset_dmi() -> Result<()> {
))?;

let empty = [];
Result::from((uefi.RuntimeServices.SetVariable)(wname.as_ptr(), &guid, attributes, 0, empty.as_ptr()))?;
Result::from((uefi.RuntimeServices.SetVariable)(
wname.as_ptr(),
&guid,
attributes,
0,
empty.as_ptr(),
))?;
}

Ok(())
Expand Down Expand Up @@ -278,7 +284,9 @@ fn inner() -> Result<()> {
// XXX: Probably better to check for HECI device.
if cmos_options.me_state() {
println!("Disabling CSME for writing SPI flash");
unsafe { cmos_options.set_me_state(false); }
unsafe {
cmos_options.set_me_state(false);
}

println!("System will reboot in 5 seconds");
let _ = (std::system_table().BootServices.Stall)(5_000_000);
Expand Down Expand Up @@ -396,7 +404,12 @@ pub fn main() -> Result<()> {
for i in 0..output.0.Mode.MaxMode {
let mut mode_ptr = ::core::ptr::null_mut();
let mut mode_size = 0;
Result::from((output.0.QueryMode)(output.0, i, &mut mode_size, &mut mode_ptr))?;
Result::from((output.0.QueryMode)(
output.0,
i,
&mut mode_size,
&mut mode_ptr,
))?;

let mode = unsafe { &mut *mode_ptr };
let w = mode.HorizontalResolution;
Expand Down
2 changes: 2 additions & 0 deletions src/app/pci.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-only

use core::{mem, slice};
use hwio::{Io, Pio};
use std::prelude::*;
Expand Down
8 changes: 5 additions & 3 deletions src/app/sideband.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2018-2021 System76 <info@system76.com>
//
// SPDX-License-Identifier: GPL-3.0-only
// Copyright 2018-2021 System76 <info@system76.com>

use core::ptr;

Expand All @@ -14,10 +13,13 @@ pub struct Sideband {
pub addr: u64,
}

#[allow(dead_code)]
impl Sideband {
pub unsafe fn new(sbreg_phys: usize) -> Self {
// On UEFI, physical memory is identity mapped
Self { addr: sbreg_phys as u64 }
Self {
addr: sbreg_phys as u64,
}
}

#[must_use]
Expand Down
Loading
Loading