Skip to content

Instantly share code, notes, and snippets.

@plobsing
Last active August 7, 2024 05:45

Revisions

  1. plobsing revised this gist Aug 7, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions log2phys
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ foreach (@ARGV) {
    my $sz = $stat[7];

    my $pos = 0;
    do {
    while ($pos < $sz) {
    # from fcntl(2):
    #
    # struct log2phys {
    @@ -32,7 +32,7 @@ foreach (@ARGV) {
    printf "logaddr: % 20d flags: %08x len: % 20d physaddr: % 20d\n", $pos, $l2p_flags, $l2p_contigbytes, $l2p_devoffset;

    $pos += $l2p_contigbytes;
    } while ($pos < $sz);
    }

    say "";
    }
  2. plobsing created this gist Aug 7, 2024.
    38 changes: 38 additions & 0 deletions log2phys
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/usr/bin/perl

    use v5.10;
    use strict;

    # https://github.com/apple-oss-distributions/xnu/blob/94d3b452840153a99b38a3a9659680b2a006908e/bsd/sys/fcntl.h#L326C9-L326C23
    use constant F_LOG2PHYS_EXT => 65;

    foreach (@ARGV) {
    say "==== $_ ====";
    open my $fh, "<", $_ or die "open: $!";

    my @stat = stat $fh or die "fstat: $!";
    printf "st_dev: % 20d st_ino: % 20d\n", $stat[0], $stat[1];
    my $sz = $stat[7];

    my $pos = 0;
    do {
    # from fcntl(2):
    #
    # struct log2phys {
    # u_int32_t l2p_flags; /* unused so far */
    # off_t l2p_contigbytes; /* IN: number of bytes to be queried;
    # OUT: number of contiguous bytes allocated at this position */
    # off_t l2p_devoffset; /* IN: bytes into file;
    # OUT: bytes into device */
    # };
    my $log2phys = pack("LQQ", 0, $sz - $pos, $pos);
    fcntl $fh, F_LOG2PHYS_EXT, $log2phys or die "fcntl: $!";
    my ($l2p_flags, $l2p_contigbytes, $l2p_devoffset) = unpack "LQQ", $log2phys;

    printf "logaddr: % 20d flags: %08x len: % 20d physaddr: % 20d\n", $pos, $l2p_flags, $l2p_contigbytes, $l2p_devoffset;

    $pos += $l2p_contigbytes;
    } while ($pos < $sz);

    say "";
    }