1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#![allow(dead_code)]
#![no_std]

#[repr(C)]
struct Handle(*mut usize);

#[repr(C)]
struct Status(pub u32);

#[repr(C)]
struct Uuid(u32, u16, u16, [u8; 4]);

#[repr(C)]
struct InputKey {
    scan_code: u16,
    unicode_char: u16,
}

#[repr(C)]
struct SimpleTextInputProtocol {
    reset: *const usize,
    read_key_stroke: *const usize,
    waitforkey: *const usize,
}

type TextReset = extern "win64" fn(*const SimpleTextOutputProtocol, u8) -> Status;
type TextOutputString = extern "win64" fn(*const SimpleTextOutputProtocol, *const u16) -> Status;

#[repr(C)]
pub struct SimpleTextOutputProtocol {
    reset: TextReset,
    output_string: TextOutputString,
    test_string: *const usize,
    query_mode: *const usize,
    set_mode: *const usize,
    set_attribute: *const usize,
    clear_screen: *const usize,
    set_cursor_position: *const usize,
    enable_cursor: *const usize,
    mode: *const usize,
}

#[repr(C)]
struct TableHeader {
    signature: u64,
    revision: u32,
    header_size: u32,
    crc32: u32,
    reserved: u32,
}

#[repr(C)]
pub struct SystemTable {
    header: TableHeader,
    vendor: *const u16,
    revision: u32,
    con_in_handle: Handle,
    con_in: *const SimpleTextInputProtocol,
    con_out_handle: Handle,
    con_out: *const SimpleTextOutputProtocol,
}

impl SystemTable {
    pub fn console(&self) -> Console {
        Console {
            input: self.con_in,
            output: self.con_out,
        }
    }
}

/// TODO: store internal copy here?
static mut SYSTEM_TABLE : *const SystemTable = 0 as *const SystemTable;

pub trait SimpleTextOutput {
    unsafe fn write_raw(&self, str: *const u16);

    /// # Examples
    ///
    /// ```ignore
    /// #[start]
    /// #[no_mangle]
    /// pub extern "win64" fn efimain(_: uefi::Handle, st: &'static uefi::SystemTable) -> uefi::Status {
    ///     st.console().write("Hello, world!\r\n");
    ///
    ///     let status = uefi::Status(0);
    ///     return status;
    /// }
    /// ```
    fn write(&self, str: &str) {
        let mut buf = [0u16; 64];
        let mut i = 0;

        if str.is_empty() {
            return
        }

        for c in str.chars() {
            buf[i] = c as u16;
            i += 1;
            if i > buf.len() - 1 {
                buf[i] = 0;
                unsafe {
                    self.write_raw(buf.as_ptr());
                }
            }
        }

        buf[i] = 0;
        unsafe {
            self.write_raw(buf.as_ptr());
        }
    }
}

pub struct Console {
    input: *const SimpleTextInputProtocol,
    output: *const SimpleTextOutputProtocol,
}

impl SimpleTextOutput for Console {
    unsafe fn write_raw(&self, str: *const u16) {
        ((*(*self).output).output_string)(self.output, str);
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn it_works() {
    }
}