From a7d2f63fff15926a4a61a7c6e941e195007f1c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 16 Sep 2022 11:09:18 +0200 Subject: [PATCH 1/2] Check constant 0x0000 and copy protection --- lib/mbr.ml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/mbr.ml b/lib/mbr.ml index 77f8ef6..a970c5d 100644 --- a/lib/mbr.ml +++ b/lib/mbr.ml @@ -176,14 +176,14 @@ let make partitions = [%%cstruct type mbr = { bootstrap_code1 : uint8_t; [@len 218] - _zeroes_1 : uint8_t; [@len 2] + zeroes_1 : uint16_t; original_physical_drive : uint8_t; seconds : uint8_t; minutes : uint8_t; hours : uint8_t; bootstrap_code2 : uint8_t; [@len 216] disk_signature : uint32_t; - _zeroes_2 : uint8_t; [@len 2] + copy_protected : uint16_t; partitions : uint8_t; [@len 64] signature1 : uint8_t; (* 0x55 *) signature2 : uint8_t; (* 0xaa *) @@ -206,6 +206,16 @@ let unmarshal (buf : Cstruct.t) : (t, string) result = (Printf.sprintf "Invalid signature: %02x %02x <> 0x55 0xaa" signature1 signature2)) >>= fun () -> + let zeroes = get_mbr_zeroes buf in + (if zeroes = 0 then Ok () + else Error (Printf.sprintf "Expected 0x0000, found 0x%04x" zeroes)) + >>= fun () -> + let copy_protected = get_mbr_copy_protected buf in + (match copy_protected with + | 0 -> Ok (copy_protected = 0x5a5a) + | _ -> + Error (Printf.sprintf "Invalid copy protection value %d" copy_protected)) + >>= fun () -> let bootstrap_code = Cstruct.append (get_mbr_bootstrap_code1 buf) (get_mbr_bootstrap_code2 buf) in From 4a0496ef90def6f28067291542390b28fc11c3ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Mon, 20 Feb 2023 12:55:23 +0000 Subject: [PATCH 2/2] Allow copy_protected to be either 0 or 0x5a5a --- lib/mbr.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mbr.ml b/lib/mbr.ml index a970c5d..f10c05e 100644 --- a/lib/mbr.ml +++ b/lib/mbr.ml @@ -212,7 +212,7 @@ let unmarshal (buf : Cstruct.t) : (t, string) result = >>= fun () -> let copy_protected = get_mbr_copy_protected buf in (match copy_protected with - | 0 -> Ok (copy_protected = 0x5a5a) + | 0 | 0x5a5a -> Ok (copy_protected = 0x5a5a) | _ -> Error (Printf.sprintf "Invalid copy protection value %d" copy_protected)) >>= fun () ->