Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshal alternative GPT header #14

Merged
merged 1 commit into from Feb 15, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/gpt.ml
Expand Up @@ -373,9 +373,19 @@ let unmarshal buf ~sector_size =
partitions_crc32;
})

let marshal_header ~sector_size (buf : Cstruct.t) t =
let marshal_header ~sector_size ~primary (buf : Cstruct.t) t =
if Cstruct.length buf < sector_size || Cstruct.length buf < sizeof then
invalid_arg "Gpt.marshal_header";
let t =
if primary then
t
else
(* The backup header has the current and backup LBAs swapped *)
let t =
{ t with current_lba = t.backup_lba; backup_lba = t.current_lba }
in
{ t with header_crc32 = Optint.to_int32 (calculate_header_crc32 t) }
in
Cstruct.blit_from_string signature 0 buf signature_offset revision_offset;
Cstruct.LE.set_uint32 buf revision_offset t.revision;
Cstruct.LE.set_uint32 buf header_size_offset t.header_size;
Expand Down
2 changes: 1 addition & 1 deletion lib/gpt.mli
Expand Up @@ -89,7 +89,7 @@ val unmarshal : Cstruct.t -> sector_size:int ->
@raise Invalid_argument when [buf] is too small to contain a GPT header
*)

val marshal_header : sector_size:int -> Cstruct.t -> t -> unit
val marshal_header : sector_size:int -> primary:bool -> Cstruct.t -> t -> unit
(** [marshal_header ~sector_size buf t] serializes the GPT header to [buf].
The caller is expected to write the contents of [buf] to [t.current_lba]
and [t.backup_lba]. *)
Expand Down
2 changes: 1 addition & 1 deletion test/test_gpt.ml
Expand Up @@ -167,7 +167,7 @@ let test_marshal_unmarshal () =
512 * ((len + 511) / 512)
in
let buf_partition_table = Cstruct.create partition_table_len in
Gpt.marshal_header ~sector_size:512 buf_header morig;
Gpt.marshal_header ~sector_size:512 ~primary:true buf_header morig;
Gpt.marshal_partition_table ~sector_size:512 buf_partition_table morig;
match Gpt.unmarshal ~sector_size:512 buf_header with
| Error e -> Alcotest.failf "Failed to parse marshalled gpt header: %s" e
Expand Down