11use crate :: tag_type:: Tag ;
22use crate :: Reader ;
33use core:: slice;
4+ use derive_more:: Display ;
45
56/// The VBE Framebuffer information Tag.
67#[ derive( Debug , PartialEq , Eq ) ]
@@ -28,6 +29,17 @@ pub struct FramebufferTag<'a> {
2829 pub buffer_type : FramebufferType < ' a > ,
2930}
3031
32+ /// Helper struct for [`FramebufferType`].
33+ #[ derive( Debug , PartialEq , Eq ) ]
34+ #[ repr( u8 ) ]
35+ #[ allow( clippy:: upper_case_acronyms) ]
36+ pub enum FramebufferTypeId {
37+ Indexed = 0 ,
38+ RGB = 1 ,
39+ Text = 2 ,
40+ // spec says: there may be more variants in the future
41+ }
42+
3143/// The type of framebuffer.
3244#[ derive( Debug , PartialEq , Eq ) ]
3345pub enum FramebufferType < ' a > {
@@ -79,7 +91,16 @@ pub struct FramebufferColor {
7991 pub blue : u8 ,
8092}
8193
82- pub fn framebuffer_tag ( tag : & Tag ) -> FramebufferTag {
94+ /// Error when an unknown [`FramebufferTypeId`] is found.
95+ #[ derive( Debug , Copy , Clone , Display , PartialEq , Eq ) ]
96+ #[ display( fmt = "Unknown framebuffer type _0" ) ]
97+ pub struct UnknownFramebufferType ( u8 ) ;
98+
99+ #[ cfg( feature = "unstable" ) ]
100+ impl core:: error:: Error for UnknownFramebufferType { }
101+
102+ /// Transforms a [`Tag`] into a [`FramebufferTag`].
103+ pub fn framebuffer_tag ( tag : & Tag ) -> Result < FramebufferTag , UnknownFramebufferType > {
83104 let mut reader = Reader :: new ( tag as * const Tag ) ;
84105 reader. skip ( 8 ) ;
85106 let address = reader. read_u64 ( ) ;
@@ -88,20 +109,27 @@ pub fn framebuffer_tag(tag: &Tag) -> FramebufferTag {
88109 let height = reader. read_u32 ( ) ;
89110 let bpp = reader. read_u8 ( ) ;
90111 let type_no = reader. read_u8 ( ) ;
91- reader. skip ( 2 ) ; // In the multiboot spec, it has this listed as a u8 _NOT_ a u16.
92- // Reading the GRUB2 source code reveals it is in fact a u16.
93- let buffer_type = match type_no {
94- 0 => {
112+ // In the multiboot spec, it has this listed as a u8 _NOT_ a u16.
113+ // Reading the GRUB2 source code reveals it is in fact a u16.
114+ reader. skip ( 2 ) ;
115+ let buffer_type_id = match type_no {
116+ 0 => Ok ( FramebufferTypeId :: Indexed ) ,
117+ 1 => Ok ( FramebufferTypeId :: RGB ) ,
118+ 2 => Ok ( FramebufferTypeId :: Text ) ,
119+ id => Err ( UnknownFramebufferType ( id) ) ,
120+ } ?;
121+ let buffer_type = match buffer_type_id {
122+ FramebufferTypeId :: Indexed => {
95123 let num_colors = reader. read_u32 ( ) ;
96124 let palette = unsafe {
97125 slice:: from_raw_parts (
98126 reader. current_address ( ) as * const FramebufferColor ,
99127 num_colors as usize ,
100128 )
101- } as & ' static [ FramebufferColor ] ;
129+ } as & [ FramebufferColor ] ;
102130 FramebufferType :: Indexed { palette }
103131 }
104- 1 => {
132+ FramebufferTypeId :: RGB => {
105133 let red_pos = reader. read_u8 ( ) ; // These refer to the bit positions of the LSB of each field
106134 let red_mask = reader. read_u8 ( ) ; // And then the length of the field from LSB to MSB
107135 let green_pos = reader. read_u8 ( ) ;
@@ -123,16 +151,15 @@ pub fn framebuffer_tag(tag: &Tag) -> FramebufferTag {
123151 } ,
124152 }
125153 }
126- 2 => FramebufferType :: Text ,
127- _ => panic ! ( "Unknown framebuffer type: {}" , type_no) ,
154+ FramebufferTypeId :: Text => FramebufferType :: Text ,
128155 } ;
129156
130- FramebufferTag {
157+ Ok ( FramebufferTag {
131158 address,
132159 pitch,
133160 width,
134161 height,
135162 bpp,
136163 buffer_type,
137- }
164+ } )
138165}
0 commit comments