Skip to content

Commit d2b0120

Browse files
committed
init
0 parents  commit d2b0120

File tree

9 files changed

+319
-0
lines changed

9 files changed

+319
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cargo.lock
2+
target

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "uefi_std"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
uefi = { git = "https://gitlab.redox-os.org/redox-os/uefi.git" }
8+
uefi_alloc = { git = "https://gitlab.redox-os.org/redox-os/uefi_alloc.git" }

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Jeremy Soller
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# uefi_std
2+
3+
UEFI standard library

src/io.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core::fmt::{self, Write};
2+
3+
use crate::UEFI;
4+
5+
pub struct Stdout;
6+
7+
impl Write for Stdout {
8+
fn write_str(&mut self, string: &str) -> Result<(), fmt::Error> {
9+
let uefi = unsafe { &mut *UEFI };
10+
11+
for c in string.chars() {
12+
let _ = (uefi.ConsoleOut.OutputString)(uefi.ConsoleOut, [c as u16, 0].as_ptr());
13+
if c == '\n' {
14+
let _ = (uefi.ConsoleOut.OutputString)(uefi.ConsoleOut, ['\r' as u16, 0].as_ptr());
15+
}
16+
}
17+
18+
Ok(())
19+
}
20+
}
21+
22+
pub fn _print(args: fmt::Arguments) {
23+
Stdout.write_fmt(args).unwrap();
24+
}

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![no_std]
2+
3+
#[macro_use]
4+
mod macros;
5+
6+
pub mod io;
7+
pub mod math;
8+
pub mod rt;
9+
10+
#[global_allocator]
11+
static ALLOCATOR: uefi_alloc::Allocator = uefi_alloc::Allocator;
12+
13+
pub static mut HANDLE: uefi::Handle = uefi::Handle(0);
14+
pub static mut UEFI: *mut uefi::system::SystemTable = 0 as *mut uefi::system::SystemTable;

src/macros.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[macro_export]
2+
macro_rules! print {
3+
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
4+
}
5+
6+
#[macro_export]
7+
macro_rules! println {
8+
() => (print!("\n"));
9+
($fmt:expr) => (print!(concat!($fmt, "\n")));
10+
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
11+
}

src/math.rs

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
* Copyright (c) 2018 Jorge Aparicio
3+
*
4+
* Permission is hereby granted, free of charge, to any
5+
* person obtaining a copy of this software and associated
6+
* documentation files (the "Software"), to deal in the
7+
* Software without restriction, including without
8+
* limitation the rights to use, copy, modify, merge,
9+
* publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software
11+
* is furnished to do so, subject to the following
12+
* conditions:
13+
*
14+
* The above copyright notice and this permission notice
15+
* shall be included in all copies or substantial portions
16+
* of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
19+
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
20+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
22+
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
25+
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
* DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
use core::f32;
30+
use core::u32;
31+
32+
#[no_mangle]
33+
pub fn fmodf(x: f32, y: f32) -> f32 {
34+
let mut uxi = x.to_bits();
35+
let mut uyi = y.to_bits();
36+
let mut ex = (uxi >> 23 & 0xff) as i32;
37+
let mut ey = (uyi >> 23 & 0xff) as i32;
38+
let sx = uxi & 0x80000000;
39+
let mut i;
40+
41+
if uyi << 1 == 0 || y.is_nan() || ex == 0xff {
42+
return (x * y) / (x * y);
43+
}
44+
45+
if uxi << 1 <= uyi << 1 {
46+
if uxi << 1 == uyi << 1 {
47+
return 0.0 * x;
48+
}
49+
50+
return x;
51+
}
52+
53+
/* normalize x and y */
54+
if ex == 0 {
55+
i = uxi << 9;
56+
while i >> 31 == 0 {
57+
ex -= 1;
58+
i <<= 1;
59+
}
60+
61+
uxi <<= -ex + 1;
62+
} else {
63+
uxi &= u32::MAX >> 9;
64+
uxi |= 1 << 23;
65+
}
66+
67+
if ey == 0 {
68+
i = uyi << 9;
69+
while i >> 31 == 0 {
70+
ey -= 1;
71+
i <<= 1;
72+
}
73+
74+
uyi <<= -ey + 1;
75+
} else {
76+
uyi &= u32::MAX >> 9;
77+
uyi |= 1 << 23;
78+
}
79+
80+
/* x mod y */
81+
while ex > ey {
82+
i = uxi - uyi;
83+
if i >> 31 == 0 {
84+
if i == 0 {
85+
return 0.0 * x;
86+
}
87+
uxi = i;
88+
}
89+
uxi <<= 1;
90+
91+
ex -= 1;
92+
}
93+
94+
i = uxi - uyi;
95+
if i >> 31 == 0 {
96+
if i == 0 {
97+
return 0.0 * x;
98+
}
99+
uxi = i;
100+
}
101+
102+
while uxi >> 23 == 0 {
103+
uxi <<= 1;
104+
ex -= 1;
105+
}
106+
107+
/* scale result up */
108+
if ex > 0 {
109+
uxi -= 1 << 23;
110+
uxi |= (ex as u32) << 23;
111+
} else {
112+
uxi >>= -ex + 1;
113+
}
114+
uxi |= sx;
115+
116+
f32::from_bits(uxi)
117+
}
118+
119+
/* origin: FreeBSD /usr/src/lib/msun/src/e_sqrtf.c */
120+
/*
121+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
122+
*/
123+
/*
124+
* ====================================================
125+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
126+
*
127+
* Developed at SunPro, a Sun Microsystems, Inc. business.
128+
* Permission to use, copy, modify, and distribute this
129+
* software is freely granted, provided that this notice
130+
* is preserved.
131+
* ====================================================
132+
*/
133+
134+
const TINY: f32 = 1.0e-30;
135+
136+
#[no_mangle]
137+
pub fn sqrtf(x: f32) -> f32 {
138+
let mut z: f32;
139+
let sign: i32 = 0x80000000u32 as i32;
140+
let mut ix: i32;
141+
let mut s: i32;
142+
let mut q: i32;
143+
let mut m: i32;
144+
let mut t: i32;
145+
let mut i: i32;
146+
let mut r: u32;
147+
148+
ix = x.to_bits() as i32;
149+
150+
/* take care of Inf and NaN */
151+
if (ix as u32 & 0x7f800000) == 0x7f800000 {
152+
return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */
153+
}
154+
155+
/* take care of zero */
156+
if ix <= 0 {
157+
if (ix & !sign) == 0 {
158+
return x; /* sqrt(+-0) = +-0 */
159+
}
160+
if ix < 0 {
161+
return (x - x) / (x - x); /* sqrt(-ve) = sNaN */
162+
}
163+
}
164+
165+
/* normalize x */
166+
m = ix >> 23;
167+
if m == 0 {
168+
/* subnormal x */
169+
i = 0;
170+
while ix & 0x00800000 == 0 {
171+
ix <<= 1;
172+
i = i + 1;
173+
}
174+
m -= i - 1;
175+
}
176+
m -= 127; /* unbias exponent */
177+
ix = (ix & 0x007fffff) | 0x00800000;
178+
if m & 1 == 1 {
179+
/* odd m, double x to make it even */
180+
ix += ix;
181+
}
182+
m >>= 1; /* m = [m/2] */
183+
184+
/* generate sqrt(x) bit by bit */
185+
ix += ix;
186+
q = 0;
187+
s = 0;
188+
r = 0x01000000; /* r = moving bit from right to left */
189+
190+
while r != 0 {
191+
t = s + r as i32;
192+
if t <= ix {
193+
s = t + r as i32;
194+
ix -= t;
195+
q += r as i32;
196+
}
197+
ix += ix;
198+
r >>= 1;
199+
}
200+
201+
/* use floating add to find out rounding direction */
202+
if ix != 0 {
203+
z = 1.0 - TINY; /* raise inexact flag */
204+
if z >= 1.0 {
205+
z = 1.0 + TINY;
206+
if z > 1.0 {
207+
q += 2;
208+
} else {
209+
q += q & 1;
210+
}
211+
}
212+
}
213+
214+
ix = (q >> 1) + 0x3f000000;
215+
ix += m << 23;
216+
f32::from_bits(ix as u32)
217+
}

src/rt.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use uefi::Handle;
2+
use uefi::status::Status;
3+
use uefi::system::SystemTable;
4+
5+
use crate::{HANDLE, UEFI};
6+
7+
#[no_mangle]
8+
pub unsafe extern "win64" fn _start(handle: Handle, uefi: &'static mut SystemTable) -> Status {
9+
extern "C" {
10+
fn main() -> Status;
11+
}
12+
13+
HANDLE = handle;
14+
UEFI = uefi;
15+
16+
uefi_alloc::init(::core::mem::transmute(&mut *UEFI));
17+
18+
main()
19+
}

0 commit comments

Comments
 (0)