@@ -53,16 +53,15 @@ struct SectionHeader {
53
53
number_of_line_numbers : U16 ,
54
54
characteristics : U32 ,
55
55
}
56
- const OSREL_SECTION : [ u8 ; 8 ] = * b".osrel\0 \0 " ;
57
56
58
57
#[ derive( Debug , Error , PartialEq ) ]
59
58
pub enum UkiError {
60
59
#[ error( "UKI is not valid EFI executable" ) ]
61
60
PortableExecutableError ,
62
- #[ error( "UKI doesn't contain a '.osrel ' section" ) ]
63
- MissingOsrelSection ,
64
- #[ error( ".osrel section is not UTF-8" ) ]
65
- UnicodeError ,
61
+ #[ error( "UKI doesn't contain a '{0} ' section" ) ]
62
+ MissingSection ( & ' static str ) ,
63
+ #[ error( "UKI section '{0}' is not UTF-8" ) ]
64
+ UnicodeError ( & ' static str ) ,
66
65
#[ error( "No name information found in .osrel section" ) ]
67
66
NoName ,
68
67
}
@@ -72,7 +71,15 @@ pub enum UkiError {
72
71
// - the error types returned from FromBytes can't be used with `?` because they try to return a
73
72
// reference to the data, which causes problems with lifetime rules
74
73
// - it saves us from having to type Err(UkiError::PortableExecutableError) everywhere
75
- fn get_osrel_section ( image : & [ u8 ] ) -> Option < Result < & str , UkiError > > {
74
+ fn get_text_section < ' a > (
75
+ image : & ' a [ u8 ] ,
76
+ section_name : & ' static str ,
77
+ ) -> Option < Result < & ' a str , UkiError > > {
78
+ // Turn the section_name ".osrel" into a section_key b".osrel\0\0".
79
+ // This will panic if section_name.len() > 8, which is what we want.
80
+ let mut section_key = [ 0u8 ; 8 ] ;
81
+ section_key[ ..section_name. len ( ) ] . copy_from_slice ( section_name. as_bytes ( ) ) ;
82
+
76
83
// Skip the DOS stub
77
84
let ( dos_stub, ..) = DosStub :: ref_from_prefix ( image) . ok ( ) ?;
78
85
let rest = image. get ( dos_stub. pe_offset . get ( ) as usize ..) ?;
@@ -91,15 +98,15 @@ fn get_osrel_section(image: &[u8]) -> Option<Result<&str, UkiError>> {
91
98
let ( sections, ..) = <[ SectionHeader ] >:: ref_from_prefix_with_elems ( rest, n_sections) . ok ( ) ?;
92
99
93
100
for section in sections {
94
- if section. name == OSREL_SECTION {
101
+ if section. name == section_key {
95
102
let bytes = image
96
103
. get ( section. pointer_to_raw_data . get ( ) as usize ..) ?
97
104
. get ( ..section. virtual_size . get ( ) as usize ) ?;
98
- return Some ( std:: str:: from_utf8 ( bytes) . or ( Err ( UkiError :: UnicodeError ) ) ) ;
105
+ return Some ( std:: str:: from_utf8 ( bytes) . or ( Err ( UkiError :: UnicodeError ( section_name ) ) ) ) ;
99
106
}
100
107
}
101
108
102
- Some ( Err ( UkiError :: MissingOsrelSection ) )
109
+ Some ( Err ( UkiError :: MissingSection ( section_name ) ) )
103
110
}
104
111
105
112
/// Gets an appropriate label for display in the boot menu for the given UKI image, according to
@@ -124,12 +131,17 @@ fn get_osrel_section(image: &[u8]) -> Option<Result<&str, UkiError>> {
124
131
/// If we couldn't parse the PE file or couldn't find an ".osrel" section then an error will be
125
132
/// returned.
126
133
pub fn get_boot_label ( image : & [ u8 ] ) -> Result < String , UkiError > {
127
- let osrel = get_osrel_section ( image) . ok_or ( UkiError :: PortableExecutableError ) ??;
134
+ let osrel = get_text_section ( image, ".osrel" ) . ok_or ( UkiError :: PortableExecutableError ) ??;
128
135
OsReleaseInfo :: parse ( osrel)
129
136
. get_boot_label ( )
130
137
. ok_or ( UkiError :: NoName )
131
138
}
132
139
140
+ /// Gets the contents of the .cmdline section of a UKI.
141
+ pub fn get_cmdline ( image : & [ u8 ] ) -> Result < & str , UkiError > {
142
+ get_text_section ( image, ".cmdline" ) . ok_or ( UkiError :: PortableExecutableError ) ?
143
+ }
144
+
133
145
#[ cfg( test) ]
134
146
mod test {
135
147
use core:: mem:: size_of;
@@ -180,7 +192,7 @@ mod test {
180
192
peify (
181
193
b"" ,
182
194
& [ SectionHeader {
183
- name : OSREL_SECTION ,
195
+ name : * b".osrel \0 \0 " ,
184
196
virtual_size : U32 :: new ( osrel. len ( ) as u32 ) ,
185
197
pointer_to_raw_data : U32 :: new ( osrel_offset as u32 ) ,
186
198
..Default :: default ( )
@@ -212,7 +224,7 @@ ID=pretty-os
212
224
assert_eq ! ( get_boot_label( img) , Err ( UkiError :: PortableExecutableError ) ) ;
213
225
}
214
226
fn no_sec ( img : & [ u8 ] ) {
215
- assert_eq ! ( get_boot_label( img) , Err ( UkiError :: MissingOsrelSection ) ) ;
227
+ assert_eq ! ( get_boot_label( img) , Err ( UkiError :: MissingSection ( ".osrel" ) ) ) ;
216
228
}
217
229
218
230
pe_err ( b"" ) ;
@@ -248,7 +260,7 @@ ID=pretty-os
248
260
pe_err ( & peify (
249
261
b"" ,
250
262
& [ SectionHeader {
251
- name : OSREL_SECTION ,
263
+ name : * b".osrel \0 \0 " ,
252
264
pointer_to_raw_data : U32 :: new ( 1234567 ) ,
253
265
..Default :: default ( )
254
266
} ] ,
0 commit comments