forked from pengutronix/rsinit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdline.rs
More file actions
324 lines (276 loc) · 9.66 KB
/
cmdline.rs
File metadata and controls
324 lines (276 loc) · 9.66 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
// SPDX-FileCopyrightText: 2024 The rsinit Authors
// SPDX-License-Identifier: GPL-2.0-only
use std::fmt::Debug;
use nix::mount::MsFlags;
use crate::util::{read_file, Result};
pub fn ensure_value<'a>(key: &str, value: Option<&'a str>) -> Result<&'a str> {
value.ok_or(format!("Cmdline option '{key}' must have an argument!").into())
}
#[derive(Debug, PartialEq)]
pub struct CmdlineOptions {
pub root: Option<String>,
pub rootfstype: Option<String>,
pub rootflags: Option<String>,
pub rootfsflags: MsFlags,
pub verity_root: Option<String>,
pub nfsroot: Option<String>,
pub init: String,
pub cleanup: bool,
}
impl Default for CmdlineOptions {
fn default() -> CmdlineOptions {
CmdlineOptions {
root: None,
rootfstype: None,
rootflags: None,
rootfsflags: MsFlags::MS_RDONLY,
verity_root: None,
nfsroot: None,
init: "/sbin/init".into(),
cleanup: true,
}
}
}
impl CmdlineOptions {
fn parse_option<'a>(
&mut self,
key: &str,
value: Option<&str>,
callbacks: &mut [Box<CmdlineCallback<'a>>],
) -> Result<()> {
match key {
"root" => self.root = Some(ensure_value(key, value)?.to_string()),
"rootfstype" => self.rootfstype = Some(ensure_value(key, value)?.to_string()),
"rootflags" => self.rootflags = value.map(str::to_string),
"ro" => self.rootfsflags.insert(MsFlags::MS_RDONLY),
"rw" => self.rootfsflags.remove(MsFlags::MS_RDONLY),
"rsinit.verity_root" => self.verity_root = Some(ensure_value(key, value)?.to_string()),
"nfsroot" => self.nfsroot = Some(ensure_value(key, value)?.to_string()),
"init" => self.init = ensure_value(key, value)?.into(),
_ => {
for cb in callbacks {
cb(key, value)?
}
}
}
Ok(())
}
fn parse_nfsroot(&mut self) -> Result<()> {
if self.root.as_deref() != Some("/dev/nfs") && self.rootfstype.as_deref() != Some("nfs") {
return Ok(());
}
let nfsroot_option = self
.nfsroot
.as_ref()
.ok_or("Missing nfsroot command-line option!")?;
let mut rootflags = String::from("nolock");
let mut nfsroot = match nfsroot_option.split_once(',') {
None => nfsroot_option.to_string(),
Some((root, flags)) => {
rootflags.push(',');
rootflags.push_str(flags);
root.to_string()
}
};
rootflags.push_str(",addr=");
if !nfsroot.contains(':') {
let pnp = read_file("/proc/net/pnp")?;
for line in pnp.lines() {
match line.split_once(' ') {
None => continue,
Some((key, value)) => {
if key == "bootserver" {
nfsroot = value.to_owned() + ":" + &nfsroot;
rootflags.push_str(value);
break;
}
}
}
}
} else {
let (bootserver, _) = nfsroot
.split_once(':')
.ok_or("Failed to split out path from nfsroot parameter")?;
rootflags.push_str(bootserver);
}
self.root = Some(nfsroot.to_string());
self.rootflags = Some(rootflags);
self.rootfstype = Some("nfs".to_string());
Ok(())
}
}
pub type CmdlineCallback<'a> = dyn FnMut(&str, Option<&str>) -> Result<()> + 'a;
#[derive(Default)]
pub struct CmdlineOptionsParser<'a> {
callbacks: Vec<Box<CmdlineCallback<'a>>>,
}
impl<'a> CmdlineOptionsParser<'a> {
pub fn new() -> Self {
Self::default()
}
pub fn add_callback(&mut self, cb: Box<CmdlineCallback<'a>>) {
self.callbacks.push(cb);
}
pub fn parse_file(&mut self, path: &str) -> Result<CmdlineOptions> {
let cmdline = read_file(path)?;
self.parse_string(&cmdline)
}
pub fn parse_string(&mut self, cmdline: &str) -> Result<CmdlineOptions> {
let mut options = CmdlineOptions::default();
let mut have_value = false;
let mut quoted = false;
let mut key = &cmdline[0..0];
let mut start = 0;
for (i, c) in cmdline.char_indices() {
let mut skip = false;
match c {
'=' => {
if !have_value {
skip = true;
key = &cmdline[start..i];
start = i;
}
have_value = true;
}
'"' => {
quoted = !quoted;
skip = true;
}
' ' | '\n' => {
if !quoted {
if !have_value {
key = &cmdline[start..i];
}
if !key.is_empty() {
options.parse_option(
key,
if have_value {
Some(&cmdline[start..i])
} else {
None
},
&mut self.callbacks,
)?;
}
key = &cmdline[0..0];
have_value = false;
skip = true;
}
}
_ => {}
}
if skip {
start = i + 1;
}
}
options.parse_nfsroot()?;
Ok(options)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_regular() {
let cmdline = "root=/dev/mmcblk0p1 rw\n";
let expected = CmdlineOptions {
root: Some("/dev/mmcblk0p1".into()),
rootfsflags: MsFlags::empty(),
..Default::default()
};
let options = CmdlineOptionsParser::new()
.parse_string(cmdline)
.expect("failed");
assert_eq!(options, expected);
}
#[test]
fn test_nfs() {
let cmdline = "root=/dev/nfs nfsroot=192.168.42.23:/path/to/nfsroot,v3,tcp ip=dhcp console=ttymxc1,115200n8 rootwait ro\n";
let expected = CmdlineOptions {
root: Some("192.168.42.23:/path/to/nfsroot".into()),
rootflags: Some("nolock,v3,tcp,addr=192.168.42.23".into()),
rootfsflags: MsFlags::MS_RDONLY,
nfsroot: Some("192.168.42.23:/path/to/nfsroot,v3,tcp".into()),
rootfstype: Some("nfs".into()),
..Default::default()
};
let options = CmdlineOptionsParser::new()
.parse_string(cmdline)
.expect("failed");
assert_eq!(options, expected);
}
#[test]
fn test_9p_qemu() {
let cmdline =
"root=/dev/root rootfstype=9p rootflags=trans=virtio console=ttyAMA0,115200\n";
let expected = CmdlineOptions {
root: Some("/dev/root".into()),
rootfstype: Some("9p".into()),
rootflags: Some("trans=virtio".into()),
..Default::default()
};
let options = CmdlineOptionsParser::new()
.parse_string(cmdline)
.expect("failed");
assert_eq!(options, expected);
}
#[test]
fn test_9p_usbg() {
let cmdline = "root=rootdev rootfstype=9p rootflags=trans=usbg,cache=loose,uname=root,dfltuid=0,dfltgid=0,aname=/path/to/9pfsroot rw\n";
let expected = CmdlineOptions {
root: Some("rootdev".into()),
rootfstype: Some("9p".into()),
rootflags: Some(
"trans=usbg,cache=loose,uname=root,dfltuid=0,dfltgid=0,aname=/path/to/9pfsroot"
.into(),
),
rootfsflags: MsFlags::empty(),
..Default::default()
};
let options = CmdlineOptionsParser::new()
.parse_string(cmdline)
.expect("failed");
assert_eq!(options, expected);
}
#[test]
fn test_init() {
let cmdline = "root=/dev/mmcblk0p1 init=/bin/sh\n";
let expected = CmdlineOptions {
root: Some("/dev/mmcblk0p1".into()),
init: "/bin/sh".into(),
..Default::default()
};
let options = CmdlineOptionsParser::new()
.parse_string(cmdline)
.expect("failed");
assert_eq!(options, expected);
}
#[test]
fn test_custom_option() {
let cmdline = "root=/dev/mmcblk0p1 rsinit.custom=xyz\n";
let custom_option = std::cell::RefCell::new(String::new());
let cb = Box::new(|key: &str, value: Option<&str>| {
if key == "rsinit.custom" {
*custom_option.borrow_mut() = ensure_value(key, value)?.to_owned();
}
Ok(())
});
let mut parser = CmdlineOptionsParser::new();
parser.add_callback(cb);
let _ = parser.parse_string(cmdline).expect("failed");
assert_eq!(&*custom_option.borrow(), "xyz");
}
#[test]
fn test_verity() {
let cmdline = "rsinit.verity_root=/dev/mmcblk0p1 rootfstype=ext4\n";
let expected = CmdlineOptions {
verity_root: Some("/dev/mmcblk0p1".into()),
rootfstype: Some("ext4".into()),
..Default::default()
};
let options = CmdlineOptionsParser::new()
.parse_string(cmdline)
.expect("failed");
assert_eq!(options, expected);
}
}