-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathloader.rs
More file actions
340 lines (299 loc) · 11.1 KB
/
loader.rs
File metadata and controls
340 lines (299 loc) · 11.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2019 Intel Corporation
use crate::{
block::SectorBuf,
bootinfo,
bzimage::{self, Kernel},
common::ascii_strip,
fat::{self, Read},
};
const ENTRY_DIRECTORY: &str = "/loader/entries";
pub struct LoaderConfig {
pub bzimage_path: [u8; 260],
pub initrd_path: [u8; 260],
pub cmdline: [u8; 4096],
}
impl Default for LoaderConfig {
fn default() -> Self {
Self {
bzimage_path: [0; 260],
initrd_path: [0; 260],
cmdline: [0; 4096],
}
}
}
#[allow(dead_code)]
#[derive(Debug)]
pub enum Error {
File(fat::Error),
BzImage(bzimage::Error),
UnterminatedString,
InvalidPattern,
}
impl From<fat::Error> for Error {
fn from(e: fat::Error) -> Error {
Error::File(e)
}
}
impl From<bzimage::Error> for Error {
fn from(e: bzimage::Error) -> Error {
Error::BzImage(e)
}
}
/// Given a `loader.conf` file, find the `default` option value.
fn default_entry_pattern(f: &mut fat::File) -> Result<[u8; 260], fat::Error> {
let mut data = [0; 4096];
assert!(f.get_size() as usize <= data.len());
assert!(data.len() >= SectorBuf::len());
let mut entry_pattern = [0; 260];
let mut offset = 0;
loop {
match f.read(&mut data[offset..offset + SectorBuf::len()]) {
Err(fat::Error::EndOfFile) => break,
Err(e) => return Err(e),
Ok(_) => {
offset += SectorBuf::len();
}
}
}
let conf = unsafe { core::str::from_utf8_unchecked(&data) };
for line in conf.lines() {
if let Some(mut pattern) = line.strip_prefix("default") {
pattern = pattern.trim();
entry_pattern[0..pattern.len()].copy_from_slice(pattern.as_bytes());
}
}
Ok(entry_pattern)
}
/// Given a glob-like pattern, select a boot entry from `/loader/entries/`,
/// falling back to the first entry encountered if no match is found.
fn find_entry(fs: &fat::Filesystem, pattern: &[u8]) -> Result<[u8; 255], Error> {
let mut dir: fat::Directory = fs.open(ENTRY_DIRECTORY)?.try_into()?;
let mut fallback = None;
loop {
match dir.next_entry() {
Ok(de) => {
if !de.is_file() {
continue;
}
let file_name = de.long_name();
// return the first matching file name
if compare_entry(&file_name, pattern)? {
return Ok(file_name);
}
// only fallback to entry files ending with `.conf`
if fallback.is_none() && compare_entry(&file_name, b"*.conf\0")? {
fallback = Some(file_name);
}
}
Err(fat::Error::EndOfFile) => break,
Err(err) => return Err(err.into()),
}
}
fallback.ok_or_else(|| fat::Error::NotFound.into())
}
/// Attempt to match a file name with a glob-like pattern.
/// An error is returned if either `file_name` or `pattern` are not `\0`
/// terminated.
fn compare_entry(file_name: &[u8], pattern: &[u8]) -> Result<bool, Error> {
fn compare_entry_inner<I>(
mut name_iter: core::iter::Peekable<I>,
pattern: &[u8],
max_depth: usize,
) -> Result<bool, Error>
where
I: Iterator<Item = u8> + Clone,
{
if max_depth == 0 {
return Ok(false);
}
let mut idx = 0;
while let Some(p) = pattern.get(idx) {
idx += 1;
let f = name_iter.peek().ok_or(Error::UnterminatedString)?;
#[cfg(test)]
println!("{} ~ {}", *p as char, *f as char);
match p {
b'\0' => return Ok(*f == b'\0'),
b'\\' => {
match pattern.get(idx) {
// trailing escape
Some(b'\0') | None => return Ok(false),
// no match
Some(p) if p != f => return Ok(false),
// continue
_ => (),
}
idx += 1;
}
b'?' => {
if *f == b'\0' {
return Ok(false);
}
}
b'*' => {
while name_iter.peek().is_some() {
if compare_entry_inner(
name_iter.clone(),
pattern.get(idx..).ok_or(Error::InvalidPattern)?,
max_depth - 1,
)? {
return Ok(true);
}
name_iter.next().ok_or(Error::UnterminatedString)?;
}
return Ok(*pattern.get(idx).ok_or(Error::UnterminatedString)? == b'\0');
}
// TODO
b'[' => todo!("patterns containing `[...]` sets are not supported"),
_ if *p != *f => return Ok(false),
_ => (),
}
name_iter.next().ok_or(Error::UnterminatedString)?;
}
Ok(false)
}
let name_iter = file_name.iter().copied().peekable();
compare_entry_inner(name_iter, pattern, 32)
}
fn parse_entry(f: &mut fat::File) -> Result<LoaderConfig, fat::Error> {
let mut data = [0; 4096];
assert!(f.get_size() as usize <= data.len());
assert!(data.len() >= SectorBuf::len());
let mut loader_config = LoaderConfig::default();
let mut offset = 0;
loop {
match f.read(&mut data[offset..offset + SectorBuf::len()]) {
Err(fat::Error::EndOfFile) => break,
Err(e) => return Err(e),
Ok(_) => {
offset += SectorBuf::len();
}
}
}
let conf = unsafe { core::str::from_utf8_unchecked(&data) };
for line in conf.lines() {
if let Some(entry) = line.strip_prefix("linux") {
let entry = entry.trim();
loader_config.bzimage_path[0..entry.len()].copy_from_slice(entry.as_bytes());
}
if let Some(entry) = line.strip_prefix("options") {
let entry = entry.trim();
loader_config.cmdline[0..entry.len()].copy_from_slice(entry.as_bytes());
}
if let Some(entry) = line.strip_prefix("initrd") {
let entry = entry.trim();
loader_config.initrd_path[0..entry.len()].copy_from_slice(entry.as_bytes());
}
}
Ok(loader_config)
}
fn default_entry_path(fs: &fat::Filesystem) -> Result<[u8; 260], Error> {
let mut f = match fs.open("/loader/loader.conf")? {
fat::Node::File(f) => f,
_ => return Err(fat::Error::NotFound.into()),
};
let default_entry_pattern = default_entry_pattern(&mut f)?;
let default_entry = find_entry(fs, &default_entry_pattern)?;
let default_entry = ascii_strip(&default_entry);
let mut entry_path = [0u8; 260];
entry_path[0..ENTRY_DIRECTORY.len()].copy_from_slice(ENTRY_DIRECTORY.as_bytes());
entry_path[ENTRY_DIRECTORY.len()] = b'/';
entry_path[ENTRY_DIRECTORY.len() + 1..ENTRY_DIRECTORY.len() + default_entry.len() + 1]
.copy_from_slice(default_entry.as_bytes());
Ok(entry_path)
}
pub fn load_default_entry(
fs: &fat::Filesystem,
info: &dyn bootinfo::Info,
) -> Result<Kernel, Error> {
let default_entry_path = default_entry_path(fs)?;
let default_entry_path = ascii_strip(&default_entry_path);
let mut f = match fs.open(default_entry_path)? {
fat::Node::File(f) => f,
_ => return Err(Error::File(fat::Error::NotFound)),
};
let entry = parse_entry(&mut f)?;
let bzimage_path = ascii_strip(&entry.bzimage_path);
let initrd_path = ascii_strip(&entry.initrd_path);
let cmdline = ascii_strip(&entry.cmdline);
let mut kernel = Kernel::new(info);
let mut bzimage_file = fs.open(bzimage_path)?;
kernel.load_kernel(info, &mut bzimage_file)?;
if !initrd_path.is_empty() {
let mut initrd_file = fs.open(initrd_path)?;
kernel.load_initrd(&mut initrd_file)?;
}
kernel.append_cmdline(info.cmdline());
kernel.append_cmdline(cmdline.as_bytes());
Ok(kernel)
}
#[cfg(test)]
mod tests {
use crate::fat::Read;
use crate::part::tests::*;
use core::convert::TryInto;
#[test]
fn test_default_entry() {
let d = FakeDisk::new(&clear_disk_path());
let (start, end) = crate::part::find_efi_partition(&d).unwrap();
let mut fs = crate::fat::Filesystem::new(&d, start, end);
fs.init().expect("Error initialising filesystem");
let mut f: crate::fat::File = fs.open("/loader/loader.conf").unwrap().try_into().unwrap();
let s = super::default_entry_pattern(&mut f).unwrap();
let s = super::ascii_strip(&s);
assert_eq!(s, "Clear-linux-kvm-5.0.6-318");
let default_entry_path = super::default_entry_path(&fs).unwrap();
let default_entry_path = super::ascii_strip(&default_entry_path);
assert_eq!(
default_entry_path,
format!("/loader/entries/{s}.conf").as_str()
);
let mut f: crate::fat::File = fs.open(default_entry_path).unwrap().try_into().unwrap();
let entry = super::parse_entry(&mut f).unwrap();
let s = super::ascii_strip(&entry.bzimage_path);
assert_eq!(s, "/EFI/org.clearlinux/kernel-org.clearlinux.kvm.5.0.6-318");
let s = super::ascii_strip(&entry.cmdline);
let s = s.trim_matches(char::from(0));
assert_eq!(
s,
"root=PARTUUID=ae06d187-e9fc-4d3b-9e5b-8e6ff28e894f console=tty0 console=ttyS0,115200n8 console=hvc0 quiet init=/usr/lib/systemd/systemd-bootchart initcall_debug tsc=reliable no_timer_check noreplace-smp cryptomgr.notests rootfstype=ext4,btrfs,xfs kvm-intel.nested=1 rw"
);
}
macro_rules! entry_pattern_matches {
(match $entry:literal with {
$(
$( #[$attr:meta] )*
$id:ident: $pat:literal => $result:literal
),* $(,)?
} ) => {
mod entry_pattern {
$(
#[test]
$( #[$attr] )*
fn $id() {
assert_eq!(super::super::compare_entry($entry, $pat).unwrap(), $result);
}
)*
}
}
}
entry_pattern_matches! {
match b"foobar.conf\0" with {
empty: b"\0" => false,
exact: b"foobar.conf\0" => true,
inexact: b"barfoo.conf\0" => false,
wildcard: b"*\0" => true,
leading_wildcard: b"*.conf\0" => true,
internal_wildcard: b"foo*.conf\0" => true,
trailing_wildcard: b"foob*\0" => true,
mismatched_wildcard: b"bar*\0" => false,
wildcard_backtrack: b"*obar.conf\0" => true,
single_wildcard: b"fo?bar.conf\0" => true,
mismatched_single_wildcard: b"foo?bar.conf\0" => false,
escaped_regular_char: b"foo\\bar.conf\0" => true,
escaped_special_char: b"foo\\?ar.conf\0" => false,
trailing_escape: b"foobar.conf\\\0" => false,
}
}
}