Export/ISO9660Hybrid.pm

filter: Statementstmt 80.7% 109 / 135 Branchbran 32.0% 16 / 50 Conditioncond 30.0% 3 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.3% 19 / 22 Podpod 100% 11 / 11 totaltotal 67.5% 158 / 234 CC 33 SCAR 27.6
CC
33
Cov
66%
finish
35.3
mbr_boot_code
17.8
new
6.2
CRAP 15.8
« Export/GPT.pm ↑ Summary Export.pm »
1 package Sys::Export::ISO9660Hybrid;
2  
3 1 our $VERSION = '0.006'; # VERSION
4 # ABSTRACT: Write ISO9660 filesystem overlaid on MBR+GPT partition EFI filesystem
5  
6 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Sys::Export qw( isa_hash isa_handle isa_array write_file_extent expand_stat_shorthand round_up_to_multiple S_ISDIR );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sys::Export::LogAny '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sys::Export::GPT;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Sys::Export::ISO9660 qw( BOOT_EFI );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Sys::Export::VFAT;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ISO_SECTOR_SIZE => Sys::Export::ISO9660::LBA_SECTOR_SIZE,
17 GPT_TYPE_ESP => 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B',
18 GPT_TYPE_GRUB => '21686148-6449-6E6F-744E-656564454649', # 'Hah!IdontNeedEFI'
19 1 };
20 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our @EXPORT_OK= qw( GPT_TYPE_ESP GPT_TYPE_GRUB );
22  
23  
24 2 ▶sub new($class, @attrs) {
Subroutine new: called CC=5 SCAR=18.2
Pod documented
25 my %attrs= @attrs != 1? @attrs
26 0 ▶ : isa_hash $attrs[0]? %{$attrs[0]}
Branch: 3 branches
BranchTrueFalseTotal
@attrs != 1 ? : 0 2 2
&isa_hash($attrs[0]) ? : 0 2 2
&isa_handle($attrs[0]) ? : 2 0 2
27 : isa_handle $attrs[0]? ( filehandle => $attrs[0] )
28 : ( filename => $attrs[0] );
29 2 my $self= bless {
30 iso => Sys::Export::ISO9660->new,
31 esp => Sys::Export::VFAT->new,
32 gpt => Sys::Export::GPT->new(
33 block_size => 4096,
34 partitions => [
35 { type => GPT_TYPE_ESP, name => 'UEFI System Partition' },
36 ],
37 ),
38 dual_files => [],
39 }, $class;
40 # apply other attributes
41 2 $self->$_($attrs{$_}) for keys %attrs;
42 2 $self;
43 }
44  
45  
46 0 ▶sub filename { @_ > 1? ($_[0]{filename}= $_[1]) : $_[0]{filename} }
Subroutine filename: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
47 4 ▶sub filehandle { @_ > 1? ($_[0]{filehandle}= $_[1]) : $_[0]{filehandle} }
Subroutine filehandle: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 2 2 4
48  
49  
50 3 ▶sub iso($self) { $self->{iso} }
Subroutine iso: called CC=1 SCAR=0.0
Pod documented
51 3 ▶sub esp($self) { $self->{esp} }
Subroutine esp: called CC=1 SCAR=0.0
Pod documented
52 4 ▶sub gpt($self) { $self->{gpt} }
Subroutine gpt: called CC=1 SCAR=0.0
Pod documented
53  
54  
55 0 ▶sub volume_label {
Subroutine volume_label: not called CC=2 SCAR=17.9
Pod documented
56 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
57 0 $_[0]->iso->volume_label($_[1]);
58 0 $_[0]->esp->volume_label($_[1]);
59 }
60 0 $_[0]->iso->volume_label;
61 }
62  
63  
64 2 ▶sub mbr_boot_code($self, @v) {
Subroutine mbr_boot_code: called CC=5 SCAR=28.8
Pod documented
65 2 ▶ if (@v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v) 0 2 2
66 0 ▶ my $data= ref $v[0]? $v[0] : \$v[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $v[0] ? : 0 0 0
67 0 my $sector0= substr($$data, 0, 512);
68 0 ▶ carp "boot_code contains nonzero bytes in partition table area"
Branch: 1 branch
BranchTrueFalseTotal
unless length $sector0 <= 446 or substr($sector0, 446, 64) =~ /^\0+\z/u 0 0 0
Condition0%0/3
length $sector0 <= 446 or substr($sector0, 446, 64) =~ /^\0+\z/u
l!l&&r!l&&!r
000
MC/DC0%0/2
length $sector0 <= 446 or substr($sector0, 446, 64) =~ /^\0+\z/u
length $sector0 <= 446 substr($sector0, 446, 64) =~ /^\0+\z/u
Truth table0%0/3
A or B
A= length $sector0 <= 446B= substr($sector0, 446, 64) =~ /^\0+\z/u
ABresult
1X1
011
000
69 unless length($sector0) <= 446 || substr($sector0, 446, 64) =~ /^\0+\z/;
70 0 $self->{mbr_boot_code}= \$sector0;
71 }
72 2 $self->{mbr_boot_code};
73 }
74  
75  
76 0 ▶sub partitions { shift->gpt->partitions(@_) }
Subroutine partitions: not called CC=1 SCAR=6.9
Pod documented
77  
78  
79 1 ▶sub add($self, $fileinfo) {
Subroutine add: called CC=4 SCAR=15.0
Pod documented
80 1 ▶ $fileinfo= { expand_stat_shorthand($fileinfo) }
Branch: 1 branch
BranchTrueFalseTotal
if &isa_array($fileinfo) 1 0 1
81 if isa_array $fileinfo;
82 1 ▶ my $is_dir= S_ISDIR($fileinfo->{mode}||0);
Condition50%1/2
$fileinfo->{'mode'} || 0
l!l
10
MC/DC0%0/1
$fileinfo->{'mode'} || 0
$fileinfo->{'mode'}
Truth table50%1/2
A
A= $fileinfo->{'mode'}
Aresult
11
00
83 1 ▶ if ($is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) { } 0 1 1
84 0 $self->iso->add($fileinfo);
85 0 return $self->esp->add($fileinfo);
86 } else {
87 1 my $vfile= $self->esp->add({ %$fileinfo, device_align => ISO_SECTOR_SIZE });
88 # prevent ISO9660 from assigning a LBA to this file.
89 1 my $ifile= $self->iso->add({ %$fileinfo, data => undef, device_offset => -1 });
90 1 push @{$self->{dual_files}}, [ $vfile, $ifile ];
91 1 return $vfile;
92 }
93 }
94  
95  
96 2 ▶sub finish($self) {
Subroutine finish: called CC=19 SCAR=35.7
Pod documented
97 2 my $fh= $self->filehandle;
98 2 ▶ if (!$fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 2 2
99 0 ▶ defined $self->filename or croak "Must set filename or filehandle attributes";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->filename or croak("Must set filename or filehandle attributes") 0 0 0
100 0 ▶ open $fh, '+>', $self->filename
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "+>", $self->filename or croak("open: $!") 0 0 0
101 or croak "open: $!";
102 }
103 2 my ($iso, $esp, $gpt4k)= ($self->iso, $self->esp, $self->gpt);
104 2 $iso->filehandle($fh);
105 2 $esp->filehandle($fh);
106 # Add the El Torrito ESP entry, but we don't know the offset yet
107 # This lets ISO9660 know that it needs to reserve a boot catalog.
108 2 my $esp_catalog_entry= $iso->add_boot_catalog_entry(
109 platform => BOOT_EFI,
110 device_offset => 0,
111 size => 0,
112 );
113 # Choose LBA extents for all the directory structures and files other than the shared ones
114 # we marked as device_offset => -1
115 2 $iso->allocate_extents;
116 # Now we know how much space the ISO structures and filesystem occupy, and can place all
117 # partitions after that.
118 2 my $ofs= $iso->volume_size;
119 # ISO9660 leaves the first 16 2K blocks empty. We need to fit a GPT512 header @512, a GPT4K
120 # header @4K, and a partition table for each from the remaining 24K, so 12K each, 24 entries
121 # with the default entry_size.
122 2 my $gpt512= Sys::Export::GPT->new(
123 %$gpt4k,
124 block_size => 512,
125 partitions => [ map +{ %$_, block_size => 512 }, $gpt4k->partitions->@* ]
126 );
127 2 my $table_bytes_needed= $gpt4k->entry_size * $gpt4k->partitions->@*;
128 2 ▶ if ($table_bytes_needed <= 3*1024) {
Branch: 2 branches
BranchTrueFalseTotal
if ($table_bytes_needed <= 3072) { } 2 0 2
elsif ($table_bytes_needed <= 12288) { } 0 0 0
129 2 $gpt512->entry_table_lba(2); # block immediately after the header, ofs=1K
130 2 $gpt4k->entry_table_lba(2); # block immediately after the header, ofs=8K
131 } elsif ($table_bytes_needed <= 12*1024) {
132 0 $gpt512->entry_table_lba(16); # block following 4K header, ofs=8K
133 0 $gpt4k->entry_table_lba(5); # block following 512table, ofs=20K
134 } else { # no room, so tables need to go after the ISO data
135 0 $gpt512->entry_table_lba($ofs / 512);
136 0 $ofs= round_up_to_multiple($ofs + $table_bytes_needed, 4096);
137 0 $gpt4k->entry_table_lba($ofs / 4096);
138 0 $ofs+= $table_bytes_needed;
139 }
140  
141 # Now choose locations for all the partitions
142 2 $ofs= round_up_to_multiple($ofs, 4096);
143 2 $gpt512->first_block($ofs/512);
144 2 $gpt4k->first_block($ofs/4096);
145 2 for my $i (0 .. $#{$gpt4k->partitions}) {
146 2 my $p= $gpt4k->partitions->[$i];
147 2 ▶ $ofs= round_up_to_multiple($ofs, $gpt4k->partition_align // 4096);
Condition50%1/2
$gpt4k->partition_align // 4096
l!l
20
MC/DC0%0/1
$gpt4k->partition_align // 4096
$gpt4k->partition_align
Truth table50%1/2
A
A= $gpt4k->partition_align
Aresult
11
00
148 # This algorithm doesn't currently account for partitons that the user
149 # placed manually.
150 2 ▶ croak "partion $i already has start_lba defined" if defined $p->start_lba;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->start_lba 0 2 2
151 2 $p->device_offset($ofs);
152 2 ▶ if ($p->type eq GPT_TYPE_ESP && !$esp->volume_offset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->type eq 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B' and not $esp->volume_offset) { } 2 0 2
elsif (not $p->size) { } 0 0 0
Condition33%1/3
$p->type eq "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" and not $esp->volume_offset
!ll&&!rl&&r
002
MC/DC0%0/2
$p->type eq "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" and not $esp->volume_offset
$p->type eq "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" not $esp->volume_offset
Truth table33%1/3
A and not(B)
A= $p->type eq "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"B= not $esp->volume_offset
ABresult
0X0
100
111
153 # Now we know the device offset for the partition containing VFAT
154 2 $esp->volume_offset($p->device_offset);
155 # Now we can write the VFAT and get device addresses for all its files
156 2 $esp->finish;
157 2 $p->size(round_up_to_multiple($esp->geometry->total_size, 4096));
158 2 $esp_catalog_entry->{extent}->device_offset($p->device_offset);
159 2 $esp_catalog_entry->{extent}->size($p->size);
160 } elsif (!$p->size) {
161 # was data supplied?
162 0 ▶ if ($p->data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->data) { } 0 0 0
163 0 $p->size(round_up_to_multiple(length ${$p->data}, 4096));
164 } else {
165 0 ▶ croak "Partiton $i lacks size" unless $p->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $p->size 0 0 0
166 }
167 }
168 2 $gpt512->partitions->[$i]->device_offset($p->device_offset);
169 2 $gpt512->partitions->[$i]->size($p->size);
170 2 $ofs= $p->device_offset + $p->size;
171 }
172 2 $ofs= round_up_to_multiple($ofs, 4096);
173 # Is the file already sized larger than the amount of remaining space we need?
174 2 my $table_size_in_4k= round_up_to_multiple($table_bytes_needed, 4096);
175 2 my $need_size= $ofs + $table_size_in_4k * 2 + 4096;
176 2 my $size= -s $fh;
177 2 ▶ if ($size < $need_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < $need_size) 2 0 2
178 2 ▶ truncate($fh, $need_size) || croak "Can't resize file";
Branch: 1 branch
BranchTrueFalseTotal
truncate $fh, $need_size or croak("Can't resize file") 2 0 2
179 2 $size= $need_size;
180 }
181 # work backward from end of device
182 2 $gpt512->backup_header_lba(int($size / 512) - 1);
183 2 $gpt4k->backup_header_lba(int($size / 4096) - 1);
184 2 $ofs= $gpt4k->backup_header_lba * 4096 - $table_size_in_4k;
185 2 $gpt4k->backup_table_lba($ofs / 4096);
186 2 $ofs -= $table_size_in_4k;
187 2 $gpt512->backup_table_lba($ofs / 512);
188 2 $gpt512->last_block($ofs / 512 - 1);
189 2 $gpt4k->last_block($ofs / 4096 - 1);
190  
191 # Now point all the ISO and VFAT files to the same extents
192 2 for ($self->{dual_files}->@*) {
193 1 my ($vfile, $ifile)= @$_;
194 1 ▶ die "BUG: unaligned file" if $vfile->device_offset % ISO_SECTOR_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $vfile->device_offset % 2048 0 1 1
195 1 $ifile->size($vfile->size);
196 1 $ifile->device_offset($vfile->device_offset);
197 }
198 # Now we can write the ISO9660
199 2 $iso->finish;
200 # Now write the partition tables. Write the 4K one first because the 512 will overwrite
201 # the tail of 4k's backup header.
202 2 $gpt4k->write_to_file($fh);
203 2 $gpt512->write_to_file($fh);
204 # Last, write the 440 bytes of boot loader if supplied by the user
205 2 ▶ if ($self->mbr_boot_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mbr_boot_code) 0 2 2
206 0 write_file_extent($fh, 0, 440, $self->mbr_boot_code, 0, 'Boot Code');
207 }
208 }
209  
210 # Avoiding dependency on namespace::clean
211 1 delete @{Sys::Export::ISO9660Hybrid::}{qw(
212 carp croak confess write_file_extent expand_stat_shorthand round_up_to_multiple
213 isa_hash isa_handle isa_array S_ISDIR BOOT_EFI
214 )};
215 1 1;
216  
217 __END__
« Export/GPT.pm ↑ Summary Export.pm »