rpi-loader 0.5.0

Host-side CLI for the rpi-loader UART command agent — upload firmware to a Raspberry Pi over serial and read/write its SD card, without touching the card
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! `rpi-loader bundle` against real files on disk.
//!
//! These run the real binary over a real directory tree, because almost
//! everything the subcommand does is about the filesystem: which files a
//! directory source picks up, what they are called once they are in the
//! bundle, and in what order. None of that is visible from a unit test of
//! the manifest types.
//!
//! What the bundle *contains* is then checked by parsing it with
//! `rpi-loader-ota` — the same code the firmware runs — so a test failing
//! here means the packer and the installer disagree, which is the one
//! failure this whole crate exists to make impossible.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

use rpi_loader_ota::{Bundle, Entry, Format, Role};

/// A scratch directory that cleans up after itself.
struct Project {
    root: PathBuf,
}

impl Project {
    /// Creates one named after the test using it, so a leftover directory
    /// says which test left it.
    fn new(name: &str) -> Project {
        let root = std::env::temp_dir().join(format!("rpi-loader-bundle-{name}"));
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&root).expect("creating the scratch directory");
        Project { root }
    }

    /// Writes a file, creating the directories above it.
    fn write(&self, relative: &str, contents: &[u8]) -> &Project {
        let path = self.root.join(relative);
        fs::create_dir_all(path.parent().expect("a parent")).expect("creating a directory");
        fs::write(path, contents).expect("writing a file");
        self
    }

    /// Runs `rpi-loader bundle` in this project.
    fn pack(&self, arguments: &[&str]) -> Output {
        Command::new(env!("CARGO_BIN_EXE_rpi-loader"))
            .arg("bundle")
            .arg(self.root.join("bundle.toml"))
            .args(arguments)
            .output()
            .expect("running the packer")
    }

    /// The bundle the default output path names.
    fn output(&self, name: &str) -> PathBuf {
        self.root.join("target").join(format!("{name}.bundle"))
    }
}

impl Drop for Project {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.root);
    }
}

/// Parses a packed bundle the way a device would, and hands back what a
/// device would see.
fn entries(path: &Path, magic: &[u8; 4]) -> Vec<(Role, String, Vec<u8>)> {
    let bytes = fs::read(path).expect("reading the packed bundle");
    let format = Format {
        magic: *magic,
        max_entries: 32,
    };
    let bundle = Bundle::parse(&format, &bytes).expect("the packed bundle should parse");
    bundle
        .iter()
        .map(|Entry { role, path, data }| (role, path.to_owned(), data.to_vec()))
        .collect()
}

/// The message a failed run printed, for an assertion to name.
fn stderr(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).into_owned()
}

#[test]
fn packs_a_whole_project() {
    let project = Project::new("whole");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [kernel]
                source = "target/kernel7.img"
                dest   = "KERNEL7.IMG"

                [[files]]
                source = "www"
                dest   = "WWW"

                [[files]]
                source = "vendor/start.elf"
                dest   = "START.ELF"
                role   = "firmware"

                [[files]]
                source = "vendor/fixup.dat"
                dest   = "FIXUP.DAT"
                role   = "firmware"

                [[files]]
                source = "config.txt"
                dest   = "CONFIG.TXT"
                role   = "config"
            "#,
        )
        .write("target/kernel7.img", b"kernel")
        .write("www/index.htm", b"<html>")
        .write("www/css/site.css", b"body{}")
        .write("www/.hidden", b"not this one")
        .write("vendor/start.elf", b"start")
        .write("vendor/fixup.dat", b"fixup")
        .write("config.txt", b"arm_64bit=0\n");

    let output = project.pack(&[]);
    assert!(output.status.success(), "{}", stderr(&output));

    assert_eq!(
        entries(&project.output("water"), b"WATR"),
        vec![
            (Role::Kernel, "KERNEL7.IMG".into(), b"kernel".to_vec()),
            // Nested, because a bundle can carry a path now — and sorted,
            // so the same tree always packs to the same bytes.
            (Role::File, "WWW/css/site.css".into(), b"body{}".to_vec()),
            (Role::File, "WWW/index.htm".into(), b"<html>".to_vec()),
            (Role::Firmware, "START.ELF".into(), b"start".to_vec()),
            (Role::Firmware, "FIXUP.DAT".into(), b"fixup".to_vec()),
            (Role::Config, "CONFIG.TXT".into(), b"arm_64bit=0\n".to_vec()),
        ],
        "a dotfile was packed, or the order was not what the tree says"
    );
}

#[test]
fn a_bundle_needs_no_kernel() {
    let project = Project::new("kernelless");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "BSMT"
                name  = "basement"

                [[files]]
                source = "settings.cfg"
                dest   = "SETTINGS.CFG"
            "#,
        )
        .write("settings.cfg", b"zone=1");

    let output = project.pack(&[]);
    assert!(output.status.success(), "{}", stderr(&output));
    assert_eq!(
        entries(&project.output("basement"), b"BSMT"),
        vec![(Role::File, "SETTINGS.CFG".into(), b"zone=1".to_vec())]
    );
}

#[test]
fn a_destination_defaults_to_the_source_file_name() {
    let project = Project::new("default-dest");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [[files]]
                source = "vendor/CA.PEM"
            "#,
        )
        .write("vendor/CA.PEM", b"-----BEGIN");

    let output = project.pack(&[]);
    assert!(output.status.success(), "{}", stderr(&output));
    assert_eq!(
        entries(&project.output("water"), b"WATR")[0].1,
        "CA.PEM".to_owned()
    );
}

#[test]
fn output_can_be_named() {
    let project = Project::new("named-output");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [[files]]
                source = "a.txt"
                dest   = "A.TXT"
            "#,
        )
        .write("a.txt", b"a");

    let elsewhere = project.root.join("build/custom.bin");
    let output = project.pack(&["--output", elsewhere.to_str().expect("a path")]);
    assert!(output.status.success(), "{}", stderr(&output));
    // The directory did not exist: a packer that made the caller create it
    // first would be a step in every Makefile that uses one.
    assert!(elsewhere.is_file());
}

#[test]
fn the_magic_must_be_four_characters() {
    let project = Project::new("short-magic");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WAT"
                name  = "water"

                [[files]]
                source = "a.txt"
                dest   = "A.TXT"
            "#,
        )
        .write("a.txt", b"a");

    let output = project.pack(&[]);
    assert!(!output.status.success());
    assert!(
        stderr(&output).contains("four ASCII characters"),
        "{}",
        stderr(&output)
    );
}

#[test]
fn a_kernel_role_in_files_is_refused() {
    // Two ways to name the boot image is two ways to name two of them, and
    // the container allows only one.
    let project = Project::new("kernel-role");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [[files]]
                source = "target/kernel7.img"
                dest   = "KERNEL7.IMG"
                role   = "kernel"
            "#,
        )
        .write("target/kernel7.img", b"kernel");

    let output = project.pack(&[]);
    assert!(!output.status.success());
    assert!(
        stderr(&output).contains("[kernel] table"),
        "{}",
        stderr(&output)
    );
}

#[test]
fn the_containers_rules_are_enforced_while_packing() {
    // The point of sharing the crate: this is `rpi-loader-ota` refusing,
    // reported by the packer, rather than a board discovering it later.
    let project = Project::new("unpaired");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [[files]]
                source = "vendor/start.elf"
                dest   = "START.ELF"
                role   = "firmware"
            "#,
        )
        .write("vendor/start.elf", b"start");

    let output = project.pack(&[]);
    assert!(!output.status.success());
    assert!(
        stderr(&output).contains("firmware file without its pair"),
        "{}",
        stderr(&output)
    );
}

#[test]
fn an_unknown_manifest_key_is_refused() {
    // Silently ignoring one means a file a project believed it was
    // shipping is simply absent from every update.
    let project = Project::new("unknown-key");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"
                sauce = "extra"
            "#,
        )
        .write("a.txt", b"a");

    let output = project.pack(&[]);
    assert!(!output.status.success());
    assert!(stderr(&output).contains("sauce"), "{}", stderr(&output));
}

#[test]
fn sdcard_writes_what_the_bundle_carries() {
    let project = Project::new("sdcard");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [kernel]
                source = "target/kernel7.img"
                dest   = "KERNEL7.IMG"

                [[files]]
                source = "www"
                dest   = "WWW"
            "#,
        )
        .write("target/kernel7.img", b"kernel")
        .write("www/index.htm", b"<html>")
        .write("www/css/site.css", b"body{}");

    // Stands in for a mounted card, and has to exist already — see below.
    let card = project.root.join("card");
    fs::create_dir_all(&card).expect("creating the card directory");

    let output = project.pack(&["--sdcard", card.to_str().expect("a path")]);
    assert!(output.status.success(), "{}", stderr(&output));

    assert_eq!(fs::read(card.join("KERNEL7.IMG")).unwrap(), b"kernel");
    assert_eq!(fs::read(card.join("WWW/index.htm")).unwrap(), b"<html>");
    // Nested destinations mean directories the card did not have.
    assert_eq!(fs::read(card.join("WWW/css/site.css")).unwrap(), b"body{}");

    // The bundle file is written too: the card and a later upload should be
    // able to carry provably the same bytes.
    assert!(project.output("water").is_file());
}

#[test]
fn sdcard_refuses_a_directory_that_is_not_there() {
    // A mount point exists; a typo does not. Creating one silently produces
    // a card that looks written and files nobody will find again.
    let project = Project::new("sdcard-missing");
    project
        .write(
            "bundle.toml",
            br#"
                magic = "WATR"
                name  = "water"

                [[files]]
                source = "a.txt"
                dest   = "A.TXT"
            "#,
        )
        .write("a.txt", b"a");

    let absent = project.root.join("not-mounted");
    let output = project.pack(&["--sdcard", absent.to_str().expect("a path")]);
    assert!(!output.status.success());
    assert!(
        stderr(&output).contains("is the card mounted?"),
        "{}",
        stderr(&output)
    );
    assert!(!absent.exists(), "the directory was created anyway");
}

#[test]
fn a_missing_source_names_itself() {
    let project = Project::new("missing-source");
    project.write(
        "bundle.toml",
        br#"
            magic = "WATR"
            name  = "water"

            [kernel]
            source = "target/kernel7.img"
        "#,
    );

    let output = project.pack(&[]);
    assert!(!output.status.success());
    assert!(
        stderr(&output).contains("kernel7.img"),
        "{}",
        stderr(&output)
    );
}