| 1 | package Sys::Export::GPT; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.006'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Implementation of GUID Partition Table | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use v5.28; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use List::Util qw( min max ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Encode qw( encode ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Sys::Export::LogAny '$log'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Sys::Export qw( write_file_extent isa_array isa_pow2 isa_handle round_up_to_multiple _pack ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Sys::Export::GPT::Partition; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | sub new($class, %attrs) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | my $self= bless { | |||||||||||||||||||||||||||||||||||||||||
| 20 | block_size => 512, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | entry_size => 128, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | entry_table_lba => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | partitions => [], | ||||||||||||||||||||||||||||||||||||||||||
| 24 | partition_align => 4096, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # Some fields need to be initialized in a specific order: | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | ▶ | $self->block_size(delete $attrs{block_size}) if defined $attrs{block_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | # The rest have no interdependencies | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | for (keys %attrs) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 11 | ▶ | my $m= $self->can($_) or croak "Unknown attribute '$_'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | $m->($self, $attrs{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 31 | ▶ | sub block_size($self, @v) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
block_size: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 31 | ▶ | if (@v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | croak "Not a power of 2" unless isa_pow2 $v[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | $self->{block_size}= $v[0]; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | $_->block_size($v[0]) for @{ $self->{partitions} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"partitions"} // []
MC/DC0%0/1
$self->{"partitions"} // []
$self->{"partitions"}
Truth table50%1/2
A
A= $self->{"partitions"}
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 31 | $self->{block_size}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub device_size($self, @v) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
device_size: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (@v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | croak "Not a multiple of block_size" if $v[0] & ($self->block_size - 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $self->{device_size}= $v[0]; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | $self->{device_size} | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 220 | ▶ | sub entry_size($self, @v) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
entry_size: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 220 | ▶ | if (@v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | croak "Not a power of 2 >= 128" unless isa_pow2 $v[0] && $v[0] >= 128; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&isa_pow2($v[0]) and $v[0] >= 128
MC/DC0%0/2
&isa_pow2($v[0]) and $v[0] >= 128
&isa_pow2($v[0])
$v[0] >= 128
Truth table33%1/3
A and B
A= &isa_pow2($v[0])B= $v[0] >= 128
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | $self->{entry_size}= $v[0]; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 220 | $self->{entry_size}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 28 | ▶ | sub partitions($self, @v) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
partitions: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 28 | ▶ | if (@v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | croak "Not an arrayref" unless isa_array $v[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | $self->{partitions}= [ map $_ && Sys::Export::GPT::Partition->coerce($_), @{$v[0]} ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_ && 'Sys::Export::GPT::Partition'->coerce($_)
MC/DC50%1/2
$_ && 'Sys::Export::GPT::Partition'->coerce($_)
$_
'Sys::Export::GPT::Partition'->coerce($_)
Truth table66%2/3
A && B
A= $_B= 'Sys::Export::GPT::Partition'->coerce($_)
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | $_->block_size($self->block_size) for grep defined, $self->{partitions}->@*; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 28 | ▶ | $self->{partitions} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"partitions"} // []
MC/DC0%0/1
$self->{"partitions"} // []
$self->{"partitions"}
Truth table50%1/2
A
A= $self->{"partitions"}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | ▶ | sub partition_align { @_ > 1? ($_[0]{partition_align}= $_[1]) : $_[0]{partition_align} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
partition_align: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 20 | ▶ | sub guid { @_ > 1? ($_[0]{guid}= $_[1]) : $_[0]{guid} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guid: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 22 | ▶ | sub entry_table_lba { @_ > 1? ($_[0]{entry_table_lba}= $_[1]) : $_[0]{entry_table_lba} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
entry_table_lba: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 33 | ▶ | sub backup_header_lba { @_ > 1? ($_[0]{backup_header_lba}= $_[1]) : $_[0]{backup_header_lba} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
backup_header_lba: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 25 | ▶ | sub backup_table_lba { @_ > 1? ($_[0]{backup_table_lba}= $_[1]) : $_[0]{backup_table_lba} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
backup_table_lba: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 40 | ▶ | sub first_block { @_ > 1? ($_[0]{first_block}= $_[1]) : $_[0]{first_block} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
first_block: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 30 | ▶ | sub last_block { @_ > 1? ($_[0]{last_block}= $_[1]) : $_[0]{last_block} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_block: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # Generates a random GUID in the format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # Tries to read from /dev/urandom first (or /dev/random as fallback), then falls back to | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # Perl's rand() if those aren't available (e.g., on Windows). | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 9 | ▶ | sub _generate_guid { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_guid: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 9 | my @bytes; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # Try /dev/urandom first (non-blocking), then /dev/random | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 9 | for my $dev ('/dev/urandom', '/dev/random') { | |||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | ▶ | if (open my $fh, '<:raw', $dev) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 9 | ▶ | if (read($fh, my $bytes, 16) == 16) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 9 | @bytes= unpack 'C*', $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 9 | last; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # Fallback to rand() if /dev/random not available (Windows, etc) | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 9 | ▶ | @bytes= map int(rand 256), 1..16 unless @bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Set version (4) and variant (RFC 4122) bits | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 9 | $bytes[6] = ($bytes[6] & 0x0f) | 0x40; # Version 4 | |||||||||||||||||||||||||||||||||||||||||
| 105 | 9 | $bytes[8] = ($bytes[8] & 0x3f) | 0x80; # Variant 10xx | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # Format as GUID string | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | sprintf '%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x', @bytes; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | ▶ | sub choose_missing_geometry($self, $device_size= undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose_missing_geometry: called CC=30 SCAR=41.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | ▶ | $device_size //= $self->device_size; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$device_size //= $self->device_size
MC/DC0%0/2
$device_size //= $self->device_size
$device_size
$self->device_size
Truth table33%1/3
A //= B
A= $device_sizeB= $self->device_size
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | my $pttn= $self->{partitions}; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | my $bs= $self->block_size; | |||||||||||||||||||||||||||||||||||||||||
| 116 | # Determine number of partition entries. 128 ought to be the defaut, but Sys::Export is | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # all about generating minimal images, so just round up to a whole block. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | my $n_entries= max( scalar @$pttn, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | my $entries_per_block= $bs / $self->entry_size; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 5 | ▶ | $n_entries= round_up_to_multiple($n_entries, $entries_per_block) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | if $entries_per_block > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 5 | $#$pttn= $n_entries - 1; # update array length to match | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 5 | my $n_table_blocks= $n_entries / $entries_per_block; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 5 | my $end_lba= int($device_size / $bs) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # header is located at 1, so entries can begin at 2. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | ▶ | $self->{entry_table_lba} //= 2; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"entry_table_lba"} //= 2
MC/DC0%0/1
$self->{"entry_table_lba"} //= 2
$self->{"entry_table_lba"}
Truth table50%1/2
A
A= $self->{"entry_table_lba"}
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # first_block can start right after that | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | ▶ | $self->{first_block} //= $self->{entry_table_lba} + $n_table_blocks; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"first_block"} //= $self->{"entry_table_lba"} + $n_table_blocks
MC/DC100%1/1
$self->{"first_block"} //= $self->{"entry_table_lba"} + $n_table_blocks
$self->{"first_block"}
Truth table100%2/2
A
A= $self->{"first_block"}
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # Assign position for any partition not yet defined | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 5 | my $lba_pos= $self->first_block; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 5 | my $block_align= $self->partition_align / $bs; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 5 | for (grep defined, @$pttn) { | |||||||||||||||||||||||||||||||||||||||||
| 138 | 5 | ▶ | round_up_to_multiple($lba_pos, $block_align) if $block_align > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | $_->block_size($self->block_size); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 5 | ▶ | if (defined $_->start_lba) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 5 | ▶ | croak "Partition ".$_->name." start_lba ".$_->start_lba." < first_block ".$self->first_block | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | if $_->start_lba < $self->first_block; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $_->start_lba($lba_pos); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 5 | ▶ | unless (defined $_->end_lba) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | # Choose based on data size, if available | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($_->data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $s= blessed($_->data)? $_->data->size : length ${$_->data}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $_->size(round_up_to_multiple($s, $self->block_size)); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $log->debugf("set partition '%s' size to %d based on data length %d", | |||||||||||||||||||||||||||||||||||||||||
| 152 | $_->name, $_->size, $s); | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | croak "No end_lba for partition ".$_->name | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 5 | $log->debugf("partition '%s' start=0x%X end=0x%X", $_->name, $_->start_lba, $_->end_lba); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | $lba_pos= $_->end_lba + 1 if $_->end_lba >= $lba_pos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 5 | my $max_part_lba= $lba_pos-1; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # If building a minimal image, last_block ends at the maximum partition extent. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # But if the handle is an actual block device, or a file larger than max partition, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # then count backward from the actual end. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 5 | ▶ | my $min_end_lba= max($max_part_lba, $self->{last_block}//0) + 1 + $n_table_blocks; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"last_block"} // 0
MC/DC100%1/1
$self->{"last_block"} // 0
$self->{"last_block"}
Truth table100%2/2
A
A= $self->{"last_block"}
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | $log->debugf("end_lba = %s, max_part_lba = %s, last_block = %s, need end_lba >= %s", | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 5 | $end_lba, $max_part_lba, $self->{last_block}, $min_end_lba); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | ▶ | if ($end_lba <= $min_end_lba) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | ▶ | $self->{last_block} //= $max_part_lba; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"last_block"} //= $max_part_lba
MC/DC0%0/2
$self->{"last_block"} //= $max_part_lba
$self->{"last_block"}
$max_part_lba
Truth table33%1/3
A //= B
A= $self->{"last_block"}B= $max_part_lba
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | $self->{backup_table_lba} //= $self->{last_block} + 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"backup_table_lba"} //= $self->{"last_block"} + 1
MC/DC0%0/1
$self->{"backup_table_lba"} //= $self->{"last_block"} + 1
$self->{"backup_table_lba"}
Truth table50%1/2
A
A= $self->{"backup_table_lba"}
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | $self->{backup_header_lba} //= $self->{backup_table_lba} + $n_table_blocks; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"backup_header_lba"} //= $self->{"backup_table_lba"} + $n_table_blocks
MC/DC0%0/1
$self->{"backup_header_lba"} //= $self->{"backup_table_lba"} + $n_table_blocks
$self->{"backup_header_lba"}
Truth table50%1/2
A
A= $self->{"backup_header_lba"}
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # work backward from end_lba | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 4 | ▶ | $self->{backup_header_lba} //= $end_lba; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"backup_header_lba"} //= $end_lba
MC/DC0%0/2
$self->{"backup_header_lba"} //= $end_lba
$self->{"backup_header_lba"}
$end_lba
Truth table33%1/3
A //= B
A= $self->{"backup_header_lba"}B= $end_lba
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 4 | ▶ | $self->{backup_table_lba} //= $self->{backup_header_lba} - $n_table_blocks; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"backup_table_lba"} //= $self->{"backup_header_lba"} - $n_table_blocks
MC/DC0%0/1
$self->{"backup_table_lba"} //= $self->{"backup_header_lba"} - $n_table_blocks
$self->{"backup_table_lba"}
Truth table50%1/2
A
A= $self->{"backup_table_lba"}
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | $self->{last_block} //= $self->{backup_table_lba} - 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"last_block"} //= $self->{"backup_table_lba"} - 1
MC/DC0%0/1
$self->{"last_block"} //= $self->{"backup_table_lba"} - 1
$self->{"last_block"}
Truth table50%1/2
A
A= $self->{"last_block"}
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | $end_lba= max($end_lba, $self->{backup_header_lba}); | |||||||||||||||||||||||||||||||||||||||||
| 179 | $log->debugf("entry_table = %s, first_block = %s, last_block = %s, backup_table = %s, backup_header = %s, end_lba = %s", | ||||||||||||||||||||||||||||||||||||||||||
| 180 | $self->{entry_table_lba}, $self->{first_block}, $self->{last_block}, $self->{backup_table_lba}, | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 5 | $self->{backup_header_lba}, $end_lba); | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # Sanity checks | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 5 | ▶ | $self->entry_table_lba + $n_table_blocks <= $self->first_block | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | or croak "entry array collides with partition area"; | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | ▶ | $self->last_block >= $self->first_block | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | or croak "last_block less than first_block"; | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | !defined $pttn->[$_] | ||||||||||||||||||||||||||||||||||||||||||
| 191 | || $self->first_block <= $pttn->[$_]->start_lba && $pttn->[$_]->end_lba <= $self->last_block | ||||||||||||||||||||||||||||||||||||||||||
| 192 | || croak "partition $_ exceeds range of [first_block, last_block]" | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 5 | ▶ | for 0..$#$pttn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $pttn->[$_] or $self->first_block <= $pttn->[$_]->start_lba and $pttn->[$_]->end_lba <= $self->last_block
$self->first_block <= $pttn->[$_]->start_lba and $pttn->[$_]->end_lba <= $self->last_block
MC/DC0%0/3
not defined $pttn->[$_] or $self->first_block <= $pttn->[$_]->start_lba and $pttn->[$_]->end_lba <= $self->last_block
not defined $pttn->[$_]
$self->first_block <= $pttn->[$_]->start_lba
$pttn->[$_]->end_lba <= $self->last_block
Truth table50%2/4
not(A) or B and C
A= not defined $pttn->[$_]B= $self->first_block <= $pttn->[$_]->start_lbaC= $pttn->[$_]->end_lba <= $self->last_block
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | ▶ | $self->last_block < $self->backup_table_lba | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | or croak "partition area collides with backup partition entry array"; | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 5 | ▶ | $self->backup_table_lba + $n_table_blocks <= $self->backup_header_lba | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | or croak "backup_header_lba at lower LBA than end of backup partition entry array"; | ||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 5 | ▶ | sub write_to_file($self, $fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_to_file: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | ▶ | croak "No filehandle provided" unless isa_handle $fh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | my $partitions= $self->{partitions}; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | my $bs= $self->block_size; | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | # choose a GUID if not set | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | ▶ | defined $self->guid or $self->guid(_generate_guid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 5 | my $size= -s $fh; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | $self->choose_missing_geometry($size); | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 5 | my $max_lba= $self->backup_header_lba; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 5 | my $need_size= ($max_lba + 1) * $bs; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 5 | ▶ | if ($size < $need_size) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | ▶ | truncate($fh, $need_size) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | or croak "Can't extend file handle to $need_size bytes"; | ||||||||||||||||||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 4 | $max_lba= int($size / $bs) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # Encode partition entries | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 5 | my $entries_data= join '', map $self->_pack_partition_entry($_), @$partitions; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # Calculate partition entries CRC | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | my $entries_crc= _crc32($entries_data); | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | # Write protective MBR at LBA 0, but don't alter first 446 bytes of boot loader | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 5 | write_file_extent($fh, 446, $bs-446, | |||||||||||||||||||||||||||||||||||||||||
| 231 | \$self->_pack_protective_mbr($max_lba), 446, 'Protective MBR'); | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | # Write primary GPT header at LBA 1 | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | write_file_extent($fh, 1 * $bs, $bs, | |||||||||||||||||||||||||||||||||||||||||
| 235 | \$self->_pack_header(0, $entries_crc), 0, 'Primary GPT header'); | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | # Write primary partition entries (probably at LBA 2) | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 5 | write_file_extent($fh, $self->entry_table_lba * $bs, length $entries_data, | |||||||||||||||||||||||||||||||||||||||||
| 239 | \$entries_data, 0, 'Primary partition entries'); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | # If any partition defined data, write that | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 5 | ▶ | for (grep defined && defined $_->data, @$partitions) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && defined $_->data
MC/DC0%0/2
defined $_ && defined $_->data
defined $_
defined $_->data
Truth table66%2/3
A && B
A= defined $_B= defined $_->data
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | write_file_extent($fh, $_->start_lba * $bs, $_->size, | |||||||||||||||||||||||||||||||||||||||||
| 244 | $_->data, 0, 'Partition '.$_->name); | ||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # Write backup partition entries | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 5 | write_file_extent($fh, $self->backup_table_lba * $bs, length $entries_data, | |||||||||||||||||||||||||||||||||||||||||
| 249 | \$entries_data, 0, 'Backup partition entries'); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | # Write backup GPT header at last LBA | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 5 | write_file_extent($fh, $self->backup_header_lba * $bs, $bs, | |||||||||||||||||||||||||||||||||||||||||
| 253 | \$self->_pack_header(1, $entries_crc), 0, 'Backup GPT header'); | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | # Creates a protective MBR partiton that marks the entire disk as GPT. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # This gets written at offset 446 within the first block and overwrites all 4 partition entries. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # This also includes the final 2-byte boot signature. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 5 | ▶ | sub _pack_protective_mbr($self, $max_lba) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_protective_mbr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 5 | pack '@446 C C C C C C C C V V @510 v', | |||||||||||||||||||||||||||||||||||||||||
| 263 | # Boot code area and disk ID uses first 446 bytes | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # Partition entry 1: Protective GPT partition | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 0x00, # Status (inactive) | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 0x00, 0x02, 0x00, # CHS start (0/2/0) | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 0xEE, # Type (GPT protective) | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0xFF, 0xFF, 0xFF, # CHS end (max) | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 1, # LBA start | ||||||||||||||||||||||||||||||||||||||||||
| 270 | min($max_lba, 0xFFFFFFFF),# LBA end | ||||||||||||||||||||||||||||||||||||||||||
| 271 | # skip to 510, pack automatically adds nul bytes for next 3 partitions | ||||||||||||||||||||||||||||||||||||||||||
| 272 | # Boot signature | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 0xAA55; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | # Pure Perl implementation of CRC32 using the standard polynomial (0xEDB88320). | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 15 | ▶ | sub _crc32($data) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_crc32: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | state @table= map { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 15 | my $crc= $_; | |||||||||||||||||||||||||||||||||||||||||
| 280 | $crc= ($crc & 1)? (0xEDB88320 ^ ($crc >> 1)) : ($crc >> 1) | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 512 | ▶ | for 1..8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 512 | $crc | |||||||||||||||||||||||||||||||||||||||||
| 283 | } 0..255; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 15 | my $crc = 0xFFFFFFFF; | |||||||||||||||||||||||||||||||||||||||||
| 286 | $crc= $table[($crc ^ $_) & 0xFF] ^ ($crc >> 8) | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 15 | for unpack 'C*', $data; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 15 | return $crc ^ 0xFFFFFFFF; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Convert a hex-notation GUID to binary in the Microsoft encoding | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 20 | ▶ | sub _pack_guid($guid) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_guid: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | # Microsoft GUIDs are a LE 32-bit int, two LE 16 bit ints, 2 bytes and 6 bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # The 8 bytes can be processed as big-endian ints, making it appear mixed-endian. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 20 | ▶ | my @ints= (lc($guid) =~ /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})([0-9a-f]{8})\z/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | or croak "Invalid GUID format '$guid'"; | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 20 | pack 'VvvnnN', map hex, @ints; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | # Encodes a single partition entry as a 128-byte binary structure. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 200 | ▶ | sub _pack_partition_entry($self, $part) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_partition_entry: called CC=9 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 200 | ▶ | return "\0" x $self->entry_size unless defined $part; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Only encode if partition has required fields | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 5 | ▶ | defined $part->start_lba or croak "Undefined start LBA"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 5 | ▶ | defined $part->end_lba or croak "Undefined end LBA"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 5 | ▶ | defined $part->type or croak "Missing type attribute"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 5 | ▶ | defined $part->guid or $part->guid(_generate_guid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 5 | my $name_max= $self->entry_size - 56; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 5 | ▶ | my $name_utf16= encode('UTF-16LE', $part->name // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$part->name // ""
MC/DC0%0/1
$part->name // ""
$part->name
Truth table50%1/2
A
A= $part->name
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 5 | ▶ | carp "Partition name '".part->name."' was truncated" if length $name_utf16 > $name_max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 5 | $part->block_size($self->block_size); # ensure accurate LBA numbers | |||||||||||||||||||||||||||||||||||||||||
| 312 | 5 | ▶ | _pack 'a16 a16 Q< Q< Q< a'.$name_max, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$part->flags // 0
MC/DC0%0/1
$part->flags // 0
$part->flags
Truth table50%1/2
A
A= $part->flags
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | _pack_guid($part->type), _pack_guid($part->guid), $part->start_lba, $part->end_lba, | ||||||||||||||||||||||||||||||||||||||||||
| 314 | $part->flags // 0, $name_utf16 | ||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | # Encodes a GPT header (primary or backup) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 10 | ▶ | sub _pack_header($self, $is_backup, $entries_crc) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_header: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 10 | ▶ | my $header_lba = $is_backup ? $self->backup_header_lba : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 10 | ▶ | my $alt_header_lba = $is_backup ? 1 : $self->backup_header_lba; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 10 | ▶ | my $entries_lba = $is_backup ? $self->backup_table_lba : $self->entry_table_lba; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 10 | my $num_entries = scalar @{$self->partitions}; | |||||||||||||||||||||||||||||||||||||||||
| 323 | # Build header without CRC first | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 10 | my $header = _pack('a8 V V V V Q< Q< Q< Q< a16 Q< V V V', | |||||||||||||||||||||||||||||||||||||||||
| 325 | 'EFI PART', # Signature | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 0x00010000, # Revision 1.0 | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 92, # Header size | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 0, # CRC32 (placeholder) | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 0, # Reserved | ||||||||||||||||||||||||||||||||||||||||||
| 330 | $header_lba, # Current LBA | ||||||||||||||||||||||||||||||||||||||||||
| 331 | $alt_header_lba, # Alternate LBA | ||||||||||||||||||||||||||||||||||||||||||
| 332 | $self->first_block, # First usable LBA | ||||||||||||||||||||||||||||||||||||||||||
| 333 | $self->last_block, # Last usable LBA | ||||||||||||||||||||||||||||||||||||||||||
| 334 | _pack_guid($self->guid), # Disk GUID | ||||||||||||||||||||||||||||||||||||||||||
| 335 | $entries_lba, # Partition entries LBA | ||||||||||||||||||||||||||||||||||||||||||
| 336 | $num_entries, # Number of entries | ||||||||||||||||||||||||||||||||||||||||||
| 337 | $self->entry_size, # Size of entry | ||||||||||||||||||||||||||||||||||||||||||
| 338 | $entries_crc # Partition entries CRC32 | ||||||||||||||||||||||||||||||||||||||||||
| 339 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | # Calculate and insert header CRC | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 10 | my $header_crc = _crc32($header); | |||||||||||||||||||||||||||||||||||||||||
| 343 | 10 | substr($header, 16, 4) = pack('V', $header_crc); | |||||||||||||||||||||||||||||||||||||||||
| 344 | 10 | return $header; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | # Avoiding dependency on namespace::clean | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 2 | delete @{Sys::Export::GPT::}{qw( | |||||||||||||||||||||||||||||||||||||||||
| 349 | carp croak confess blessed min max encode write_file_extent round_up_to_multiple | ||||||||||||||||||||||||||||||||||||||||||
| 350 | isa_array isa_handle isa_pow2 _pack | ||||||||||||||||||||||||||||||||||||||||||
| 351 | )}; | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | __END__ | ||||||||||||||||||||||||||||||||||||||||||