+(* Fix the bootloader if we aligned the first partition. *)
+let () =
+ if align_first_partition_and_fix_bootloader then (
+ (* See can_fix_boot_loader above. *)
+ match partitions with
+ | { p_type = ContentFS ("ntfs", _); p_bootable = true;
+ p_target_partnum = partnum; p_target_start = start } :: _ ->
+ (* If the first partition is NTFS and bootable, set the "Number of
+ * Hidden Sectors" field in the NTFS Boot Record so that the
+ * filesystem is still bootable.
+ *)
+
+ (* Should always be /dev/sdb1? *)
+ let target = sprintf "/dev/sdb%d" partnum in
+
+ (* Sanity check: it contains the NTFS magic. *)
+ let magic = g#pread_device target 8 3L in
+ if magic <> "NTFS " then
+ eprintf "warning: first partition is NTFS but does not contain NTFS boot loader magic\n%!"
+ else (
+ if not quiet then
+ printf "Fixing first NTFS partition boot record ...\n%!";
+
+ if debug then (
+ let old_hidden = int_of_le32 (g#pread_device target 4 0x1c_L) in
+ eprintf "old hidden sectors value: 0x%Lx\n%!" old_hidden
+ );
+
+ let new_hidden = le32_of_int start in
+ ignore (g#pwrite_device target new_hidden 0x1c_L)
+ )