reformat hex dump in 4-bytes separated by space
[perl-Mifare-MAD.git] / mifare-mad.pl
index b3008ed..2472cfc 100755 (executable)
@@ -76,11 +76,19 @@ foreach my $i ( 0 .. 15 ) {
                        , unpack('H*',substr($card,4,1))
                        , unpack('H*',substr($card,5,11))
                        ;
+               # General purpose byte (GPB)
+               my $gdp = ord(substr($card,0x39,1));
+               printf "ADV (MAD version code): %d\n", $gdp & 0b00000011;
+               printf "MA (multiapplication): %s\n",  $gdp & 0b01000000 ? 'yes' : 'monoaplication';
+               printf "DA (MAD available): %s\n",     $gdp & 0b10000000 ? 'yes' : 'no';
+
+               printf "Info byte (publisher sector): %x\n", ord(substr($card,0x11,1));
        } else {
-               my $v = unpack('v',(substr($card, 0x10 + ( $i * 2 ), 2)));
+               my $mad_offset = 0x10 + ( $i * 2 );
+               my $v = unpack('v',(substr($card, $mad_offset, 2)));
                my $cluster_id = unpack('HH', (( $v & 0xff00 ) >> 8) );
                my $full_id = sprintf "%04x",$v;
-               printf "MAD sector %-2d %04x [%s]\n%s\n", $i, $v
+               printf "MAD sector %-2d@%x %04x [%s]\n%s\n", $i, $mad_offset, $v
                        , $function_clusters->{ $cluster_id }
                        , $mad_id->{$full_id} || "FIXME: add $full_id from MAD_overview.pdf to __DATA__ at end of $0"
                        ;
@@ -125,16 +133,20 @@ foreach my $i ( 0 .. 15 ) {
                        ;
                $cond >>= $j;
 
-               printf "%04x %s %03b %s\n", $offset, unpack('H*',$block)
+               my $hex = unpack('H*',$block);
+               $hex =~ s/(....)/$1 /g;
+
+               printf "%04x  %s %03b %s\n", $offset, $hex
                        , $cond
                        , $j < 3 ? $access_condition_data->{$cond} : $access_condition_trailer->{$cond}
                        ;
        }
 
 
-       printf "KEY A:%s | %s | B:%s\n"
+       printf "KEY A:%s | %s GDP: %s | B:%s\n"
                ,unpack('H*',substr($card,$pos+0x30   ,6))
-               ,unpack('H*',substr($card,$pos+0x30+6 ,4))
+               ,unpack('H*',substr($card,$pos+0x30+6 ,3))
+               ,unpack('H*',substr($card,$pos+0x30+9 ,1))
                ,unpack('H*',substr($card,$pos+0x30+10,6))
                ;
 
@@ -194,11 +206,12 @@ E8    computer
 F0    mail
 F8-FF miscellaneous applications
 
-0000   sector free
-0001   sector defective
-0002   sector reserved
-0003   DIR continuted
-0004   card holder
+0000   sector is free
+0001   sector is defect, e.g. access keys are destroyed or unknown
+0002   sector is reserved
+0003   sector contains additional directory info (useful only for future cards)
+0004   sector contains card holder information in ASCII format.
+0005   sector not applicable (above memory size)
 
 0015 - card administration MIKROELEKTRONIKA spol.s.v.MIKROELEKTRONIKA spol.s.v.o. worldwide 1 01.02.2007 Card publisher info
 0016 - card administration Mikroelektronika spol.s.r.o., Kpt.Mikroelektronika spol.s.r.o., Kpt. PoEurope    1 10.10.2007 Issuer information