Skip to content

Commit

Permalink
Merge pull request #14 from mirage/agpt
Browse files Browse the repository at this point in the history
Marshal alternative GPT header
  • Loading branch information
reynir committed Feb 15, 2024
2 parents 21634aa + b6d4f43 commit 1831715
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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

0 comments on commit 1831715

Please sign in to comment.