Pārlūkot izejas kodu

Merge pull request #46 from tuomas56/master

Add Framebuffer Tag
Isaac Woods 6 gadi atpakaļ
vecāks
revīzija
663c6ffa9b
2 mainītis faili ar 229 papildinājumiem un 0 dzēšanām
  1. 86 0
      src/framebuffer.rs
  2. 143 0
      src/lib.rs

+ 86 - 0
src/framebuffer.rs

@@ -0,0 +1,86 @@
+use header::Tag;
+use ::Reader;
+use core::slice;
+
+#[derive(Debug, PartialEq)]
+pub struct FramebufferTag<'a> {
+    pub address: u64,
+    pub pitch: u32,
+    pub width: u32,
+    pub height: u32,
+    pub bpp: u8,
+    pub buffer_type: FramebufferType<'a>
+}
+
+#[derive(Debug, PartialEq)]
+pub enum FramebufferType<'a> {
+    Indexed {
+        palette: &'a [FramebufferColor]   
+    },
+    RGB {
+        red: FramebufferField,
+        green: FramebufferField,
+        blue: FramebufferField
+    },
+    Text
+}
+
+#[derive(Debug, PartialEq)]
+pub struct FramebufferField {
+    pub position: u8,
+    pub size: u8
+}
+
+#[derive(Debug, PartialEq)]
+#[repr(C, packed)]
+pub struct FramebufferColor {
+    pub red: u8,
+    pub green: u8,
+    pub blue: u8
+}
+
+pub fn framebuffer_tag<'a>(tag: &'a Tag) -> FramebufferTag<'a> {
+    let mut reader = Reader::new(tag as *const Tag);
+    reader.skip(8);
+    let address = reader.read_u64();
+    let pitch = reader.read_u32();
+    let width = reader.read_u32();
+    let height = reader.read_u32();
+    let bpp = reader.read_u8();
+    let type_no = reader.read_u8();
+    reader.skip(2); // In the multiboot spec, it has this listed as a u8 _NOT_ a u16.
+                    // Reading the GRUB2 source code reveals it is in fact a u16.
+    let buffer_type = match type_no {
+        0 =>  {
+            let num_colors = reader.read_u32();
+            let palette = unsafe {
+                slice::from_raw_parts(reader.read_u32() as usize as *const FramebufferColor, num_colors as usize)
+            } as &'static [FramebufferColor];
+            FramebufferType::Indexed { palette }
+        },
+        1 => {
+            let red_pos = reader.read_u8();     // These refer to the bit positions of the MSB of each field
+            let red_mask = reader.read_u8();    // And then the length of the field from MSB to LSB
+            let green_pos = reader.read_u8();   
+            let green_mask = reader.read_u8();  
+            let blue_pos = reader.read_u8();
+            let blue_mask = reader.read_u8();
+            FramebufferType::RGB {
+                red: FramebufferField { position: red_pos, size: red_mask },
+                green: FramebufferField { position: green_pos, size: green_mask },
+                blue: FramebufferField { position: blue_pos, size: blue_mask }
+            }
+        },
+        2 => FramebufferType::Text,
+        _ => panic!("Unknown framebuffer type: {}", type_no)
+    };
+
+    FramebufferTag {
+        address,
+        pitch,
+        width,
+        height,
+        bpp,
+        buffer_type
+    }
+}

+ 143 - 0
src/lib.rs

@@ -7,6 +7,7 @@ use core::fmt;
 use header::{Tag, TagIter};
 pub use boot_loader_name::BootLoaderNameTag;
 pub use elf_sections::{ElfSectionsTag, ElfSection, ElfSectionIter, ElfSectionType, ElfSectionFlags};
+pub use framebuffer::{FramebufferTag, FramebufferType, FramebufferField, FramebufferColor};
 pub use memory_map::{MemoryMapTag, MemoryArea, MemoryAreaIter};
 pub use module::{ModuleTag, ModuleIter};
 pub use command_line::CommandLineTag;
@@ -22,6 +23,7 @@ mod memory_map;
 mod module;
 mod command_line;
 mod rsdp;
+mod framebuffer;
 
 pub unsafe fn load(address: usize) -> BootInformation {
     if !cfg!(test) {
@@ -76,6 +78,10 @@ impl BootInformation {
         self.get_tag(1).map(|tag| unsafe { &*(tag as *const Tag as *const CommandLineTag) })
     }
 
+    pub fn framebuffer_tag(&self) -> Option<FramebufferTag<'static>> {
+        self.get_tag(8).map(|tag| framebuffer::framebuffer_tag(tag))
+    }
+
     pub fn rsdp_v1_tag(&self) -> Option<&'static RsdpV1Tag> {
         self.get_tag(14).map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV1Tag) })
     }
@@ -151,10 +157,48 @@ impl fmt::Debug for BootInformation {
     }
 }
 
+pub(crate) struct Reader {
+    pub(crate) ptr: *const u8,
+    pub(crate) off: usize
+}
+
+impl Reader {
+    pub(crate) fn new<T>(ptr: *const T) -> Reader {
+        Reader {
+            ptr: ptr as *const u8,
+            off: 0
+        }
+    }
+
+    pub(crate) fn read_u8(&mut self) -> u8 {
+        self.off += 1;
+        unsafe {
+            core::ptr::read(self.ptr.offset((self.off - 1) as isize))
+        }
+    }
+
+    pub(crate) fn read_u16(&mut self) -> u16 {
+        self.read_u8() as u16 | (self.read_u8() as u16) << 8
+    }
+
+    pub(crate) fn read_u32(&mut self) -> u32 {
+        self.read_u16() as u32 | (self.read_u16() as u32) << 16
+    }
+
+    pub(crate) fn read_u64(&mut self) -> u64 {
+        self.read_u32() as u64 | (self.read_u32() as u64) << 32
+    }
+
+    pub(crate) fn skip(&mut self, n: usize) {
+        self.off += n;
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::load;
     use super::{ElfSectionFlags, ElfSectionType};
+    use super::FramebufferType;
 
     #[test]
     fn no_tags() {
@@ -244,6 +288,96 @@ mod tests {
         assert!(bi.command_line_tag().is_none());
     }
 
+    #[test]
+    fn framebuffer_tag_rgb() {
+        // direct RGB mode test:
+        // taken from GRUB2 running in QEMU at 
+        // 1280x720 with 32bpp in BGRA format.
+        let bytes: [u8; 56] = [
+            56, 0, 0, 0,  // total size
+            0, 0, 0, 0,   // reserved
+            8, 0, 0, 0,   // framebuffer tag type
+            40, 0, 0, 0,  // framebuffer tag size
+            0, 0, 0, 253, // framebuffer low dword of address
+            0, 0, 0, 0,   // framebuffer high dword of address
+            0, 20, 0, 0,  // framebuffer pitch
+            0, 5, 0, 0,   // framebuffer width
+            208, 2, 0, 0, // framebuffer height
+            32, 1, 0, 0,  // framebuffer bpp, type, reserved word
+            16, 8, 8, 8,  // framebuffer red pos/size, green pos/size
+            0, 8, 0, 0,   // framebuffer blue pos/size, padding word
+            0, 0, 0, 0,   // end tag type
+            8, 0, 0, 0    // end tag size
+        ];
+        let addr = bytes.as_ptr() as usize;
+        let bi = unsafe { load(addr) };
+        assert_eq!(addr, bi.start_address());
+        assert_eq!(addr + bytes.len(), bi.end_address());
+        assert_eq!(bytes.len(), bi.total_size());
+        use framebuffer::{FramebufferTag, FramebufferField, FramebufferType};
+        assert_eq!(bi.framebuffer_tag(), Some(FramebufferTag {
+            address: 4244635648,
+            pitch: 5120,
+            width: 1280,
+            height: 720,
+            bpp: 32,
+            buffer_type: FramebufferType::RGB {
+                red: FramebufferField {
+                    position: 16, size: 8
+                },
+                green: FramebufferField {
+                    position: 8, size: 8
+                },
+                blue: FramebufferField {
+                    position: 0, size: 8
+                }
+            }
+        }))
+    }
+
+    #[test]
+    fn framebuffer_tag_indexed() {
+        // indexed mode test:
+        // this is synthetic, as I can't get QEMU
+        // to run in indexed color mode.
+        let bytes: [u8; 56] = [
+            56, 0, 0, 0,  // total size
+            0, 0, 0, 0,   // reserved
+            8, 0, 0, 0,   // framebuffer tag type
+            40, 0, 0, 0,  // framebuffer tag size
+            0, 0, 0, 253, // framebuffer low dword of address
+            0, 0, 0, 0,   // framebuffer high dword of address
+            0, 20, 0, 0,  // framebuffer pitch
+            0, 5, 0, 0,   // framebuffer width
+            208, 2, 0, 0, // framebuffer height
+            32, 0, 0, 0,  // framebuffer bpp, type, reserved word
+            0, 1, 0, 0,   // framebuffer palette length
+            0, 24, 1, 0,  // framebuffer palette address
+            0, 0, 0, 0,   // end tag type
+            8, 0, 0, 0    // end tag size
+        ];
+        let addr = bytes.as_ptr() as usize;
+        let bi = unsafe { load(addr) };
+        assert_eq!(addr, bi.start_address());
+        assert_eq!(addr + bytes.len(), bi.end_address());
+        assert_eq!(bytes.len(), bi.total_size());
+        use framebuffer::{FramebufferTag, FramebufferField, FramebufferType};
+        assert!(bi.framebuffer_tag().is_some());
+        let fbi = bi.framebuffer_tag().unwrap();
+        assert_eq!(fbi.address, 4244635648);
+        assert_eq!(fbi.pitch, 5120);
+        assert_eq!(fbi.width, 1280);
+        assert_eq!(fbi.height, 720);
+        assert_eq!(fbi.bpp, 32);
+        match fbi.buffer_type {
+            FramebufferType::Indexed { palette } => {
+                assert_eq!(palette.as_ptr() as usize, 71680);
+                assert_eq!(palette.len(), 256);
+            },
+            _ => panic!("Expected indexed framebuffer type.")
+        }
+    }
+
     #[test]
     fn grub2() {
         let mut bytes: [u8; 960] = [
@@ -590,5 +724,14 @@ mod tests {
         assert!(bi.module_tags().next().is_none());
         assert_eq!("GRUB 2.02~beta3-5", bi.boot_loader_name_tag().unwrap().name());
         assert_eq!("", bi.command_line_tag().unwrap().command_line());
+
+        // Test the Framebuffer tag
+        let fbi = bi.framebuffer_tag().unwrap();
+        assert_eq!(fbi.address, 753664);
+        assert_eq!(fbi.pitch, 160);
+        assert_eq!(fbi.width, 80);
+        assert_eq!(fbi.height, 25);
+        assert_eq!(fbi.bpp, 16);
+        assert_eq!(fbi.buffer_type, FramebufferType::Text);
     }
 }