Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
neri committed Nov 28, 2023
1 parent 8b48911 commit 7ac9090
Show file tree
Hide file tree
Showing 26 changed files with 814 additions and 430 deletions.
5 changes: 5 additions & 0 deletions boot/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boot/boot-efi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ cfg-if = {}
lib-efi = {path = "../lib-efi"}
uefi = {features = ['alloc']}
uefi-services = {default-features = false}
myelf = {path="../../lib/myelf"}
2 changes: 1 addition & 1 deletion boot/boot-efi/src/invocation/amd64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Invoke for Invocation {
new_sp: VirtualAddress,
) -> ! {
unsafe {
// For Intel processors, unlock NXE disable. (Surface 3)
// For Intel processors, unlock NXE disable. (ex: Surface 3)
if self.is_intel_processor() {
asm!("
rdmsr
Expand Down
2 changes: 1 addition & 1 deletion boot/boot-efi/src/invocation/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Invoke for Invocation {
mov cr0, {0}
", out(reg) _);

// For Intel processors, unlock NXE disable. (Surface 3)
// For Intel processors, unlock NXE disable. (ex: Surface 3)
if self.is_intel_processor() {
asm!("
rdmsr
Expand Down
157 changes: 0 additions & 157 deletions boot/boot-efi/src/loader/elf.rs

This file was deleted.

20 changes: 9 additions & 11 deletions boot/boot-efi/src/loader/elfldr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Minimal ELF File Loader
use super::{elf::*, *};
use super::*;
use crate::page::*;
use core::{intrinsics::transmute, ptr::copy_nonoverlapping};
use myelf::*;

pub struct ElfLoader<'a> {
elf_hdr: &'a elf64::Header,
Expand All @@ -25,25 +26,22 @@ impl<'a> ElfLoader<'a> {

fn _recognize(&mut self) -> bool {
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
let preferred_machine = Machine::X86_64;
let preferred_machine = EM_X86_64;
#[cfg(target_arch = "aarch64")]
let preferred_machine = Machine::AArch64;
let preferred_machine = EM_AARCH64;

let elf_hdr = self.elf_hdr;
if elf_hdr.is_valid()
&& elf_hdr.e_type == ElfType::EXEC
&& elf_hdr.e_machine == preferred_machine
{
if elf_hdr.is_valid(ET_EXEC, preferred_machine) {
let page_mask = UEFI_PAGE_SIZE - 1;
let image_base = VirtualAddress(
self.program_header()
.filter(|v| v.p_type == SegmentType::LOAD)
.filter(|v| v.p_type == PT_LOAD)
.fold(u64::MAX, |a, v| u64::min(a, v.p_vaddr))
& !page_mask,
);
let image_size = ((self
.program_header()
.filter(|v| v.p_type == SegmentType::LOAD)
.filter(|v| v.p_type == PT_LOAD)
.fold(0, |a, v| u64::max(a, v.p_vaddr + v.p_memsz))
- image_base.as_u64()
+ page_mask)
Expand Down Expand Up @@ -89,7 +87,7 @@ impl ImageLoader for ElfLoader<'_> {

// Step 2 - locate segments
for item in self.program_header() {
if item.p_type == SegmentType::LOAD {
if item.p_type == PT_LOAD {
let rva = (item.p_vaddr - image_base.as_u64()) as usize;
let p = vmem.add(rva);
let q: *const u8 = self.blob.as_ptr().add(item.p_offset as usize);
Expand All @@ -103,7 +101,7 @@ impl ImageLoader for ElfLoader<'_> {

// Step 4 - attributes
for item in self.program_header() {
if item.p_type == SegmentType::LOAD {
if item.p_type == PT_LOAD {
let va = VirtualAddress(item.p_vaddr & !page_mask);
let size = ((item.p_memsz + item.p_vaddr - va.as_u64() + page_mask)
& !page_mask) as usize;
Expand Down
1 change: 0 additions & 1 deletion boot/boot-efi/src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod elfldr;
pub use elfldr::*;
pub mod elf;

pub trait ImageLoader {
fn image_bounds(&self) -> (crate::page::VirtualAddress, usize);
Expand Down
77 changes: 71 additions & 6 deletions lib/meggl/src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use core::{
cell::UnsafeCell,
convert::TryFrom,
intrinsics::copy_nonoverlapping,
mem::{swap, transmute},
mem::{swap, transmute, ManuallyDrop},
num::NonZeroUsize,
ptr::slice_from_raw_parts_mut,
slice,
};
use paste::paste;
Expand Down Expand Up @@ -412,23 +413,23 @@ macro_rules! define_bitmap {
// The memory layouts of BitmapRefXX, BitmapRefMutXX and OwnedBitmapXX are guaranteed to be identical.
#[repr(C)]
pub struct [<BitmapRef $suffix>]<'a> {
slice: &'a [$slice_type],
size: Size,
stride: usize,
slice: &'a [$slice_type],
}

#[repr(C)]
pub struct [<BitmapRefMut $suffix>]<'a> {
slice: UnsafeCell<&'a mut [$slice_type]>,
size: Size,
stride: usize,
slice: UnsafeCell<&'a mut [$slice_type]>,
}

#[repr(C)]
pub struct [<OwnedBitmap $suffix>] {
slice: UnsafeCell<Box<[$slice_type]>>,
size: Size,
stride: usize,
slice: UnsafeCell<Box<[$slice_type]>>,
}

impl Drawable for [<BitmapRef $suffix>]<'_> {
Expand Down Expand Up @@ -562,13 +563,18 @@ macro_rules! define_bitmap {

impl [<OwnedBitmap $suffix>] {
#[inline]
pub fn from_vec(vec: Vec<$slice_type>, size: Size) -> Self {
pub fn from_boxed_slice(slice: Box<[$slice_type]>, size: Size) -> Self {
Self {
size: size,
stride: <Self as Drawable>::ColorType::stride_for(size.width()),
slice: UnsafeCell::new(vec.into_boxed_slice()),
slice: UnsafeCell::new(slice),
}
}

#[inline]
pub fn from_vec(vec: Vec<$slice_type>, size: Size) -> Self {
Self::from_boxed_slice(vec.into_boxed_slice(), size)
}
}

impl<'a> AsRef<[<BitmapRef $suffix>]<'a>> for [<BitmapRef $suffix>]<'a> {
Expand Down Expand Up @@ -1111,6 +1117,65 @@ impl BitmapRefMut32<'_> {
}
}

impl OwnedBitmap32 {
pub fn from_vec_rgba(mut vec: Vec<u8>, size: Size) -> Self {
const MAGIC_NUMBER: usize = 4;
let stride = size.width() as usize;
let count = stride * size.height() as usize;
let slice = unsafe {
vec.resize(count * MAGIC_NUMBER, 0);
let slice = vec.into_boxed_slice();
let mut slice = ManuallyDrop::new(slice);
let mut slice =
Box::from_raw(slice_from_raw_parts_mut(
slice.as_mut_ptr() as *mut u32,
count,
));
for pixel in slice.iter_mut() {
let rgba: [u8; 4] = transmute(*pixel);
let bgra = [rgba[2], rgba[1], rgba[0], rgba[3]];
*pixel = transmute(bgra);
}
transmute::<_, Box<[ARGB8888]>>(slice)
};
Self::from_boxed_slice(slice, size)
}

pub fn from_bytes_rgba(bytes: &[u8], size: Size) -> Option<Self> {
const MAGIC_NUMBER: usize = 4;
let stride = size.width() as usize;
let count = stride * size.height() as usize;
if bytes.len() < count * MAGIC_NUMBER {
return None;
}
let mut vec = Vec::with_capacity(count);
for rgba in bytes.chunks_exact(MAGIC_NUMBER).take(count) {
let rgba: [u8; MAGIC_NUMBER] = rgba.try_into().unwrap();
let argb = ColorComponents::from_rgba(rgba[0], rgba[1], rgba[2], Alpha8(rgba[3]))
.into_true_color();
vec.push(argb);
}
Some(Self::from_vec(vec, size))
}

pub fn from_bytes_rgb(bytes: &[u8], size: Size) -> Option<Self> {
const MAGIC_NUMBER: usize = 3;
let stride = size.width() as usize;
let count = stride * size.height() as usize;
if bytes.len() < count * MAGIC_NUMBER {
return None;
}
let mut vec = Vec::with_capacity(count);
for rgb in bytes.chunks_exact(MAGIC_NUMBER).take(count) {
let rgb: [u8; MAGIC_NUMBER] = rgb.try_into().unwrap();
let argb = ColorComponents::from_rgba(rgb[0], rgb[1], rgb[2], Alpha8::OPAQUE)
.into_true_color();
vec.push(argb);
}
Some(Self::from_vec(vec, size))
}
}

#[derive(Clone, Copy)]
pub enum BitmapRef<'a> {
Indexed(&'a BitmapRef8<'a>),
Expand Down
1 change: 1 addition & 0 deletions lib/myacpi/src/bgrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Bgrt {
}

#[repr(u8)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ImageType {
Bitmap = 0,
Expand Down
Loading

0 comments on commit 7ac9090

Please sign in to comment.