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

Script for printing MBR headers to stdout #17

Merged
merged 15 commits into from
Mar 14, 2023
Merged

Conversation

PizieDust
Copy link
Contributor

@PizieDust PizieDust commented Mar 10, 2023

part of #14

  • Inspect a MBR header's content. For example printing out what partions it contains, their size and type etc.
  • Retrieve contents of partitions for example to stdout

Using this script:

./_build/default/bin/mbr_inspect.exe [file_name]

replace [file_name] with the name to any disk image you want to read the MBR Header.
This will print the headers of an MBR to stdout including information about the partitions it contains.
Sample output looks like this:
image

to test, create a disk image:

fallocate -l 2M test.img
parted --align none test.img mklabel msdos
parted --align none test.img mkpart primary 1s 33s
parted --align none test.img mkpart primary 34s 65s
parted --align none test.img mkpart primary 66s 97s

the commands above will create an MBR with three partitions

to make any of the partitions bootable, you can use the parted command.

parted test.img
set 1 boot on

cc @reynir

@PizieDust PizieDust marked this pull request as ready for review March 12, 2023 09:33
@PizieDust
Copy link
Contributor Author

The reason for moving the print_mbr_fields and read_mbr functions to the main mbr.ml file is so that these functions can be used in other use cases.
For instance, to resize partitions, we can use the read_mbr function to read the mbr, then resize the partitions as needed and then use the print_mbr_fields function to print the new MBR structure with the resized partition.
Including them in the main mbr.ml file ensures we can use the functions accros different implementations without needing to rewrite the same code in every file.

Copy link
Member

@reynir reynir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite pleased overall with the code. There are some minor issues with how some values are printed and I would like the read_mbr function moved back out of the mbr library. With those changes I would be happy to merge.

Another task on top of this could be to add flags to toggle printing the boot code for example. For this I like to use the library cmdliner.

lib/mbr.ml Outdated

let print_mbr_fields mbr =
Printf.printf "MBR fields:\n";
Printf.printf " bootstrap_code: %s\n" mbr.bootstrap_code;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbr.bootstrap_code contains binary (executable) data. In OCaml you can use the %S printf modifier to print it as an escaped string, but even better would be to print it as hexadecimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I will see how to implement the cmdliner library. It looks great with good documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reynir concerning this, I'll open a new Issue for it to keep as a reminder so that when cstruct 6.2.0 is released we can implement the command line flags to toggle display for the bootstrap_code. so if that's okay by you then we could merge .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! Please go ahead and open an issue.

lib/mbr.ml Outdated
Printf.printf " seconds: %d\n" mbr.seconds;
Printf.printf " minutes: %d\n" mbr.minutes;
Printf.printf " hours: %d\n" mbr.hours;
Printf.printf " disk_signature: %ld\n" mbr.disk_signature;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to print the disk signature as hexadecimal. This is what fdisk -l test.img does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this, I was exploring the Cstruct library and found a useful function to_hex_string, however this function exist only in v6.2.0 while the latest release of Cstruct on Opam is v6.1.1

The PR to publish the new release is still pending.
ocaml/opam-repository#23374

I am unsure if it's wise to wait or find some other way to convert the Cstruct.t value to a hex string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For disk_signature you can use %lx in printf.

Good question about bootstrap_code. There is a 'hex' library that can be used. That would introduce another dependency for the binary, but that is maybe alright. Another way forward could be to print "" since I think that's rarely what you're interested in. And then later when cstruct.6.2.0 is released we can add a flag to print the bootstrap code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the best way will be to print an empty string and update later when cstruct 6.2.0 is released.
I think adding another dependency (hex library) will be too much given that we are using it just for this purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-) Thanks for the %lx. It's just what I needed.

lib/mbr.ml Outdated Show resolved Hide resolved
lib/mbr.mli Outdated Show resolved Hide resolved
@reynir reynir merged commit 36db681 into mirage:master Mar 14, 2023
@reynir
Copy link
Member

reynir commented Mar 14, 2023

Thanks! I merged and pushed a commit that makes the usage message use the 'basename' of the command so as to not print the whole path to the command in the usage message.

@PizieDust
Copy link
Contributor Author

that's great. thank you. I'll go over it to learn how you did that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants