1
1
use crate :: tag_type:: Tag ;
2
2
use crate :: Reader ;
3
3
use core:: slice;
4
+ use derive_more:: Display ;
4
5
5
6
/// The VBE Framebuffer information Tag.
6
7
#[ derive( Debug , PartialEq , Eq ) ]
@@ -28,6 +29,17 @@ pub struct FramebufferTag<'a> {
28
29
pub buffer_type : FramebufferType < ' a > ,
29
30
}
30
31
32
+ /// Helper struct for [`FramebufferType`].
33
+ #[ derive( Debug , PartialEq , Eq ) ]
34
+ #[ repr( u8 ) ]
35
+ pub enum FramebufferTypeId {
36
+ Indexed = 0 ,
37
+ #[ allow( clippy:: upper_case_acronyms) ]
38
+ RGB = 1 ,
39
+ Text = 2 ,
40
+ // spec says: there may be more variants in the future
41
+ }
42
+
31
43
/// The type of framebuffer.
32
44
#[ derive( Debug , PartialEq , Eq ) ]
33
45
pub enum FramebufferType < ' a > {
@@ -79,7 +91,16 @@ pub struct FramebufferColor {
79
91
pub blue : u8 ,
80
92
}
81
93
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 > {
83
104
let mut reader = Reader :: new ( tag as * const Tag ) ;
84
105
reader. skip ( 8 ) ;
85
106
let address = reader. read_u64 ( ) ;
@@ -88,20 +109,27 @@ pub fn framebuffer_tag(tag: &Tag) -> FramebufferTag {
88
109
let height = reader. read_u32 ( ) ;
89
110
let bpp = reader. read_u8 ( ) ;
90
111
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 => {
95
123
let num_colors = reader. read_u32 ( ) ;
96
124
let palette = unsafe {
97
125
slice:: from_raw_parts (
98
126
reader. current_address ( ) as * const FramebufferColor ,
99
127
num_colors as usize ,
100
128
)
101
- } as & ' static [ FramebufferColor ] ;
129
+ } as & [ FramebufferColor ] ;
102
130
FramebufferType :: Indexed { palette }
103
131
}
104
- 1 => {
132
+ FramebufferTypeId :: RGB => {
105
133
let red_pos = reader. read_u8 ( ) ; // These refer to the bit positions of the LSB of each field
106
134
let red_mask = reader. read_u8 ( ) ; // And then the length of the field from LSB to MSB
107
135
let green_pos = reader. read_u8 ( ) ;
@@ -123,16 +151,15 @@ pub fn framebuffer_tag(tag: &Tag) -> FramebufferTag {
123
151
} ,
124
152
}
125
153
}
126
- 2 => FramebufferType :: Text ,
127
- _ => panic ! ( "Unknown framebuffer type: {}" , type_no) ,
154
+ FramebufferTypeId :: Text => FramebufferType :: Text ,
128
155
} ;
129
156
130
- FramebufferTag {
157
+ Ok ( FramebufferTag {
131
158
address,
132
159
pitch,
133
160
width,
134
161
height,
135
162
bpp,
136
163
buffer_type,
137
- }
164
+ } )
138
165
}
0 commit comments