#partition #gpt #read-write #reader-writer #writer #reader #partition-table

gpt-partition

GptPartitionCursor implements the Read + Write + Seek + Debug. It's used for backing up or restoring partition images, such as in embedded upgrades.

3 releases

0.1.2 Oct 25, 2023
0.1.1 Jun 14, 2023
0.1.0 Jun 13, 2023

#305 in Embedded development

Download history 31/week @ 2024-02-26 3/week @ 2024-03-11 50/week @ 2024-04-01

53 downloads per month

LGPL-3.0

19KB
320 lines

GptPartitionCursor implements the Read + Write + Seek + Debug.

It's used for backing up or restoring partition images, such as in embedded upgrades.

use gpt_partition::*;
use std::io::{Seek, Write};

fn main() -> Result<(), Box<dyn std::error::Error>>{
    let path = "/dev/mmcblk0";

    // Write the new GPT table to /dev/mmcblk0
    write_dev_with_gpt_img(&mut std::fs::File::open("./gpt.img")?, path).unwrap();

    //
    // Optional, fix GPT header and backup area.
    // (cargo add gpt-partition -F gpt_header_fixup)
    //
    gpt_header_fixup("/dev/mmcblk0").unwrap();

    let mut pt_boot = GptPartitionCursor::new(path, "boot").unwrap();
    println!("{:#?}", pt_boot);

    // Read the boot partition to boot.bin
    let mut fout = std::io::BufWriter::new(std::fs::File::create("boot.bin")?);
    std::io::copy(&mut pt_boot, &mut fout).unwrap();

    // Write the boot.bin to /dev/mmcblk0p1
    let mut fin = std::io::BufReader::new(std::fs::File::open("boot.bin")?);
    pt_boot.seek(std::io::SeekFrom::Start(0)).unwrap();
    let size = std::io::copy(&mut fin, &mut pt_boot).unwrap();
    pt_boot.flush().unwrap();
    println!("{:#?}", size == pt_boot.size);

    //
    // Check:
    // hexdump -C /dev/mmcblk0p1 | head
    //

    Ok(())
}

Dependencies

~650KB
~12K SLoC