Skip to content

Commit

Permalink
sdcard: Add auto-retry for opening microSD card device(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
CFSworks committed Feb 2, 2024
1 parent c3a293e commit 723b595
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ howudoin = {version="0.1.*", features=["term-line"]}
i2c-linux = "0.1.*"
income = "0.1.*"
nix = "0.26.*"
retry = "2.0.0"
13 changes: 10 additions & 3 deletions src/bin/sdcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use nix::errno::Errno;
use nix::mount::{mount, MsFlags};

use retry::{delay::Fixed, retry};

use std::{
fs,
io::{self, Read, Seek},
Expand Down Expand Up @@ -253,7 +255,6 @@ fn rtl8370mb_led_thread(rx: mpsc::Receiver<[bool; 4]>) {
}
}


/// This runs in a thread and manages the LED blinking.
///
/// Send new blink patterns through the MPSC channel to change the active pattern.
Expand Down Expand Up @@ -464,10 +465,16 @@ fn main() -> ! {
let nand_ubi = MtdNand::open_named("ubi").unwrap_or_else(|e| init_error(e));

// Locate the rootfs and bootloader to be written
let mut rootfs = fs::File::open(ROOTFS_PATH).unwrap_or_else(|e| init_error(e.into()));
let mut rootfs = retry(Fixed::from_millis(100).take(10), || {
fs::File::open(ROOTFS_PATH)
})
.unwrap_or_else(|e| init_error(e.into()));
let rootfs_size = image::erofs_size(&mut rootfs).unwrap_or_else(|e| init_error(e));

let mut bootloader = fs::File::open(BOOTLOADER_PATH).unwrap_or_else(|e| init_error(e.into()));
let mut bootloader = retry(Fixed::from_millis(100).take(10), || {
fs::File::open(BOOTLOADER_PATH)
})
.unwrap_or_else(|e| init_error(e.into()));
bootloader
.seek(io::SeekFrom::Start(BOOTLOADER_OFFSET))
.unwrap_or_else(|e| init_error(e.into()));
Expand Down

0 comments on commit 723b595

Please sign in to comment.