Skip to content

Commit

Permalink
Boot2Snow: Add uefi_alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ANEP-ET committed Apr 12, 2018
1 parent 9d210df commit 912245c
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 146 deletions.
8 changes: 6 additions & 2 deletions arch/x86_64/boot2snow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ name = "Boot2Snow"
version = "0.1.0"
authors = ["xuserwhat <[email protected]>"]

[dependencies.compiler_builtins]
git = "https://github.com/rust-lang-nursery/compiler-builtins"
features = ["mem"]

[dependencies]
uefi = {path = "../../../share/libuefi"}
uefi = {path = "../../../share/uefi"}
uefi_alloc = {path = "../../../share/uefi_alloc"}
rlibc = "1.0"
compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins" }
utf16_literal = "0.1.0"

[lib]
Expand Down
10 changes: 7 additions & 3 deletions arch/x86_64/boot2snow/src/boot2snow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ pub extern fn init() -> Result<(), ()> {
(runtime_services.set_virtual_address_map)(map_size, ent_size, ent_ver, map.as_ptr()).expect("Sorry, set_virtual_address_map() failed :(");
}

let video_info = kernel_proto::VideoInfo {
physbaseptr: gop.mode.frame_buffer_base as *mut Color,
xresolution: unsafe { (*gop.mode.info).horizontal_resolution },
yresolution: unsafe { (*gop.mode.info).vertical_resolution }
};

let boot_info = kernel_proto::Info {
runtime_services: runtime_services as *const _ as *const (),

Expand All @@ -93,9 +99,7 @@ pub extern fn init() -> Result<(), ()> {
map_entnum: map.len() as u32,
map_entsz: size_of::<MemoryDescriptor>() as u32,

vid_addr: gop.mode.frame_buffer_base as *mut Color,
width: unsafe { (*gop.mode.info).horizontal_resolution },
height: unsafe { (*gop.mode.info).vertical_resolution }
video_info: &video_info
};

// - Execute kernel (passing a magic value and general boot information)
Expand Down
41 changes: 17 additions & 24 deletions arch/x86_64/boot2snow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#![feature(lang_items)]
#![no_std]
#![feature(alloc)]
#![feature(asm)]
#![feature(proc_macro)]
#![feature(try_trait)]
#![feature(compiler_builtins_lib)]
#![feature(const_fn)]
#![no_std]
#![feature(core_intrinsics)]
#![feature(global_allocator)]
#![feature(lang_items)]
#![feature(try_trait)]
#![feature(proc_macro)]

use core::ops::Try;
use core::mem;
Expand All @@ -19,7 +23,10 @@ use uefi::{SimpleTextOutputInterface,
Status};

#[macro_use]
extern crate alloc;
extern crate compiler_builtins;
extern crate uefi;
extern crate uefi_alloc;
extern crate utf16_literal;

#[macro_use]
Expand All @@ -30,6 +37,7 @@ pub mod panic;
mod boot2snow;
mod conf;
mod io;
mod string;

#[path="../../../../share/elf.rs"]
mod elf;
Expand All @@ -40,6 +48,9 @@ mod kernel_proto;
#[path="../../../../share/color.rs"]
mod color;

#[global_allocator]
static ALLOCATOR: uefi_alloc::Allocator = uefi_alloc::Allocator;

static PATH_CONFIG: &'static [u16] = utf16!("boot2snow\\boot2snow.conf\0");
static PATH_FALLBACK_KERNEL: &'static [u16] = utf16!("boot2snow\\kernel.bin\0");

Expand Down Expand Up @@ -124,6 +135,8 @@ pub extern "win64" fn _start(image_handle: Handle, system_table: &SystemTable) -
S_IMAGE_HANDLE = image_handle;
S_BOOT_SERVICES = system_table.boot_services;
S_RUNTIME_SERVICES = system_table.runtime_services;

uefi_alloc::init(::core::mem::transmute(&mut get_boot_services()));
}

let gop = GraphicsOutput::new(get_boot_services()).unwrap();
Expand All @@ -148,23 +161,3 @@ pub extern "win64" fn _start(image_handle: Handle, system_table: &SystemTable) -
SUCCESS
}

#[no_mangle]
pub extern "C" fn memcpy(dst: *mut u8, src: *const u8, count: usize) {
unsafe {
asm!("rep movsb" : : "{rcx}" (count), "{rdi}" (dst), "{rsi}" (src) : "rcx", "rsi", "rdi" : "volatile");
}
}
#[no_mangle]
pub extern "C" fn memset(dst: *mut u8, val: u8, count: usize) {
unsafe {
asm!("rep stosb" : : "{rcx}" (count), "{rdi}" (dst), "{al}" (val) : "rcx", "rdi" : "volatile");
}
}
#[no_mangle]
pub extern "C" fn memcmp(dst: *mut u8, src: *const u8, count: usize) -> isize {
unsafe {
let rv: isize;
asm!("repnz cmpsb ; movq $$0, $0 ; ja 1f; jb 2f; jmp 3f; 1: inc $0 ; jmp 3f; 2: dec $0; 3:" : "=r" (rv) : "{rcx}" (count), "{rdi}" (dst), "{rsi}" (src) : "rcx", "rsi", "rdi" : "volatile");
rv
}
}
30 changes: 30 additions & 0 deletions arch/x86_64/boot2snow/src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use alloc::{String, Vec};
use core::char;

pub fn wstr(string: &str) -> Vec<u16> {
let mut wstring = vec![];

for c in string.chars() {
wstring.push(c as u16);
}
wstring.push(0);

wstring
}

pub fn nstr(wstring: *const u16) -> String {
let mut string = String::new();

let mut i = 0;
loop {
let w = unsafe { *wstring.offset(i) };
i += 1;
if w == 0 {
break;
}
let c = unsafe { char::from_u32_unchecked(w as u32) };
string.push(c);
}

string
}
2 changes: 1 addition & 1 deletion arch/x86_64/boot2snow/x86_64-boot2snow.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
"eliminate-frame-pointer": false,
"linker-flavor": "ld",
"morestack": false
}
}
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ volatile = "0.1.0"
spin = "0.4.5"
x86_64 = "0.1.2"
bitflags = "1.0.1"
uefi = {path = "../share/libuefi"}
uefi = {path = "../share/uefi"}
11 changes: 6 additions & 5 deletions kernel/src/kmain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ extern {
#[no_mangle]
pub extern fn start_uefi() {
let magic = unsafe { _magic };
let info = unsafe { _info };
let info = unsafe { &*_info };
let video_info = unsafe { &*(*info).video_info };

let resolutin_w = unsafe { (*info).width };
let resolutin_h = unsafe { (*info).height };
let resolutin_w = video_info.xresolution;
let resolutin_h = video_info.yresolution;
let AREA = resolutin_w * resolutin_h;

let vid_addr = unsafe { (*info).vid_addr };
let vid_addr = video_info.physbaseptr;
let mut display = Display::new(vid_addr, resolutin_w, resolutin_h);
let mut console = Console::new(&mut display);
let map = unsafe { (*info).map_addr } as *const MemoryDescriptor;
let map = info.map_addr as *const MemoryDescriptor;
set_console(&mut console);

enable_nxe_bit();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions share/uefi_alloc
Submodule uefi_alloc added at 6a3fb5
14 changes: 9 additions & 5 deletions share/uefi_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ mod color;
use color::*;

#[repr(C)]
pub struct Info
{
pub struct Info {
pub runtime_services: *const (),

pub cmdline_ptr: *const u8,
Expand All @@ -13,9 +12,14 @@ pub struct Info
pub map_entnum: u32,
pub map_entsz: u32,

pub vid_addr: *mut Color,
pub width: u32,
pub height: u32
pub video_info: *const VideoInfo,
}

#[repr(C)]
pub struct VideoInfo {
pub physbaseptr: *mut Color,
pub xresolution: u32,
pub yresolution: u32
}

// TODO: Grab this from libuefi
Expand Down
6 changes: 0 additions & 6 deletions share/utf16_literal/Cargo.toml

This file was deleted.

99 changes: 0 additions & 99 deletions share/utf16_literal/src/lib.rs

This file was deleted.

0 comments on commit 912245c

Please sign in to comment.