Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ANEP-ET committed Jun 12, 2018
2 parents 05aad58 + 75374e2 commit 2f1c7e8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions boot2snow/uefi_alloc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cargo.lock
target
6 changes: 6 additions & 0 deletions boot2snow/uefi_alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "uefi_alloc"
version = "0.1.0"

[dependencies]
uefi = { git = "https://github.com/SnowFlakeOS/uefi" }
21 changes: 21 additions & 0 deletions boot2snow/uefi_alloc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Redox OS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions boot2snow/uefi_alloc/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2018-03-31
56 changes: 56 additions & 0 deletions boot2snow/uefi_alloc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#![feature(alloc)]
#![feature(allocator_api)]
#![feature(const_fn)]
#![feature(try_trait)]
#![no_std]

extern crate alloc;
extern crate uefi;

use alloc::heap::{Alloc, AllocErr, Layout};
use core::ops::Try;
use uefi::Void;
use uefi::boot_services::{BootServices, MemoryType};

static mut S_BOOT_SERVICES: *const BootServices = 0 as *const BootServices;

pub unsafe fn init(table: *const BootServices) {
S_BOOT_SERVICES = table;
}

fn get_boot_services() -> Option<&'static BootServices> {
unsafe {
if S_BOOT_SERVICES as usize == 0 {
None
} else {
Some(&*S_BOOT_SERVICES)
}
}
}

pub struct Allocator;

unsafe impl<'a> Alloc for &'a Allocator {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
let boot_services = get_boot_services().unwrap();
let size = layout.size();
let align = layout.align();

// TODO: add support for other alignments.
if align > 8 {
let details = "Unsupported alignment for allocation, UEFI can only allocate 8-byte aligned addresses";
Err(AllocErr::Unsupported { details })
} else {
boot_services
.allocate_pool(size)
.map(|addr| addr as *mut u8)
// This is the only possible error, according to the spec.
.map_err(|_status| AllocErr::Exhausted { request: layout })
}
}

unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
let boot_services = get_boot_services().unwrap();
let _ = (boot_services.free_pool)(ptr as *mut Void);
}
}

0 comments on commit 2f1c7e8

Please sign in to comment.