Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ jobs:
run: |
make

- name: Run cargo
run: |
cd degpt
cargo build
1 change: 1 addition & 0 deletions degpt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
88 changes: 88 additions & 0 deletions degpt/Cargo.lock

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

10 changes: 10 additions & 0 deletions degpt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "degpt"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
gpt = "3.1"
uuid = "1.8"
47 changes: 47 additions & 0 deletions degpt/LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
MIT License

Copyright (c) 2024 Linaro Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

------------------------------------------------------------------------

The GPT implementation is based on the gpt crate:

MIT License

Copyright (c) 2017 Quyzi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions degpt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 Linaro Ltd.

pub mod mygpt;
75 changes: 75 additions & 0 deletions degpt/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 Linaro Ltd.

use std::env;
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::Write;

use degpt::mygpt::*;

fn main() -> Result<(), Box<dyn Error>> {
let mut args = env::args().skip(1);

let mut xml;
if let Some(name) = args.next() {
xml = File::create(name)?;
} else {
return Err(format!(
"usage: {} partition.xml gpt_both*.bin",
env::args().next().unwrap()
)
.into());
}

xml.write_all(
concat!(
"<?xml version=\"1.0\"?>\n",
"<configuration>\n",
" <parser_instructions>\n",
" WRITE_PROTECT_BOUNDARY_IN_KB=0\n",
" SECTOR_SIZE_IN_BYTES = 4096\n",
" GROW_LAST_PARTITION_TO_FILL_DISK=true\n",
" </parser_instructions>\n",
"\n",
)
.as_bytes(),
)?;

for gpt in args {
let bytes = fs::read(&gpt)?;
let hdr = Header::try_from(&bytes[0x1000..0x2000])?;

println!("{gpt}");
println!("{hdr:#}");

xml.write_all(" <physical_partition>\n".as_bytes())?;
for i in 0..hdr.num_parts {
let offset = 0x2000 + i as usize * 0x80;
let part = Partition::try_from(&bytes[offset..offset + 0x80])?;
if part.part_guid.is_nil() {
continue;
}
println!("{part:#}");
let size = (part.last_lba + 1 - part.first_lba) * 4096 / 1024;
let readonly: bool = part.flags & (1 << 60) != 0;
xml.write_all(
format!(
" <partition label=\"{}\" size_in_kb=\"{}\" type=\"{}\" bootable=\"false\" readonly=\"{}\" filename=\"\" />\n",
part.name,
size,
part.part_type_guid.guid.to_string().to_uppercase(),
readonly,
)
.as_bytes(),
)?;
}

xml.write_all(" </physical_partition>\n\n".as_bytes())?;
}

xml.write_all("</configuration>\n".as_bytes())?;

Ok(())
}
9 changes: 9 additions & 0 deletions degpt/src/mygpt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 Linaro Ltd.

pub mod header;
pub mod partition;

pub use header::Header;
pub use partition::Partition;
pub use partition::Type;
75 changes: 75 additions & 0 deletions degpt/src/mygpt/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2017 Quyzi
// Copyright (c) 2024 Linaro Ltd.

use std::error::Error;
use std::fmt;
use std::io::Cursor;

use gpt::header::parse_uuid;

/// Header describing a GPT disk.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Header {
/// GPT header magic signature, hardcoded to "EFI PART".
pub signature: String, // Offset 0. "EFI PART", 45h 46h 49h 20h 50h 41h 52h 54h
/// 00 00 01 00
pub revision: u32, // Offset 8
/// little endian
pub header_size_le: u32, // Offset 12
/// CRC32 of the header with crc32 section zeroed
pub crc32: u32, // Offset 16
/// must be 0
pub reserved: u32, // Offset 20
/// For main header, 1
pub current_lba: u64, // Offset 24
/// LBA for backup header
pub backup_lba: u64, // Offset 32
/// First usable LBA for partitions (primary table last LBA + 1)
pub first_usable: u64, // Offset 40
/// Last usable LBA (secondary partition table first LBA - 1)
pub last_usable: u64, // Offset 48
/// UUID of the disk
pub disk_guid: uuid::Uuid, // Offset 56
/// Starting LBA of partition entries
pub part_start: u64, // Offset 72
/// Number of partition entries
pub num_parts: u32, // Offset 80
/// Size of a partition entry, usually 128
pub part_size: u32, // Offset 84
/// CRC32 of the partition table
pub crc32_parts: u32, // Offset 88
}

impl fmt::Display for Header {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Disk:\t\t{}\nCRC32:\t\t{}\nTable CRC:\t{}",
self.disk_guid, self.crc32, self.crc32_parts
)
}
}

impl TryFrom<&[u8]> for Header {
type Error = Box<dyn Error>;

fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
Ok(Header {
signature: String::from_utf8(buf[0..8].into())?,
revision: u32::from_le_bytes(buf[8..12].try_into()?),
header_size_le: u32::from_le_bytes(buf[12..16].try_into()?),
crc32: u32::from_le_bytes(buf[16..20].try_into()?),
reserved: u32::from_le_bytes(buf[20..24].try_into()?),
current_lba: u64::from_le_bytes(buf[24..32].try_into()?),
backup_lba: u64::from_le_bytes(buf[32..40].try_into()?),
first_usable: u64::from_le_bytes(buf[40..48].try_into()?),
last_usable: u64::from_le_bytes(buf[48..56].try_into()?),
disk_guid: parse_uuid(&mut Cursor::new(&buf[56..72]))?,
part_start: u64::from_le_bytes(buf[72..80].try_into()?),
num_parts: u32::from_le_bytes(buf[80..84].try_into()?),
part_size: u32::from_le_bytes(buf[84..88].try_into()?),
crc32_parts: u32::from_le_bytes(buf[88..92].try_into()?),
})
}
}
Loading
Loading