home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / utilities-general / dumpdmi < prev    next >
Text File  |  2006-05-04  |  2KB  |  101 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # dumpdmi 0.2
  4. # Chip Salzenberg <chip@valinux.com>
  5. #
  6.  
  7. use strict;
  8.  
  9. (my $ME = $0) =~ s#.*/##;
  10.  
  11. my $DEBUG;
  12. if (@ARGV && $ARGV[0] eq '-d') {
  13.     ++$DEBUG;
  14.     shift;
  15. }
  16.  
  17. my $BIOS;
  18. open MEM, '/dev/mem'
  19.   or die "$ME: Can't open /dev/mem: $!\n";
  20. sysseek MEM, 0xE0000, 0;
  21. sysread MEM, $BIOS, 0x20000
  22.   or die "$ME: Can't read BIOS from /dev/mem?\n";
  23.  
  24. while ( $BIOS =~ /_DMI_/g
  25.     && (pos($BIOS) & 0x0F) != 5 )
  26.     {}
  27. die "$ME: Can't find DMI in BIOS\n"
  28.   unless pos($BIOS);
  29.  
  30. my $info = substr($BIOS, pos($BIOS) + 1, 10);
  31. my ($DSIZE, $DBASE, $DCOUNT, $DVER) = unpack('v V v C', $info);
  32. $DVER = ($DVER >> 4) . '.' . ($DVER & 0x0F);
  33.  
  34. print "DMI Version: $DVER\n";
  35.  
  36. printf "DMI table at 0x%X contains %d structures occupying %d bytes.\n",
  37.     $DBASE, $DCOUNT, $DSIZE
  38.     if $DEBUG;
  39.  
  40. sysseek MEM, $DBASE, 0;
  41. sysread MEM, $_, $DSIZE
  42.   or die "$ME: Can't read DMI table: $!\n";
  43.  
  44. my $n = 0;
  45. my $last_type = 0;
  46. while (my $header = substr($_, 0, 4, '')) {
  47.     my ($type, $dlen, $handle) = unpack('CCv', $header);
  48.     last if $type < $last_type || $dlen < 4;
  49.  
  50.     my $data = substr($_, 0, $dlen - 4, '');
  51.     my @data = unpack 'C*', $data;
  52.     my @str = m{ \G ([^\0]+) \0 }xg;
  53.  
  54.     if ($DEBUG) {
  55.     printf "DMI type=%02X data=%s\n",
  56.         $type,
  57.         join(':',  map { sprintf '%02X', $_ } @data);
  58.     if (my @s = @str) {
  59.         for (@s) { s/\\/\\\\/g; s/"/\\"/g; }
  60.         printf "    strings={ %s }\n",
  61.             join(', ', map { '"'.$_.'"' } @s);
  62.     }
  63.     }
  64.  
  65.     my $str = sub {
  66.     my $n = $data[+shift];
  67.     (defined($n) && $n > 0 && $n <= @str)
  68.         or return '<ERROR>';
  69.     local $_ = $str[$n - 1];
  70.     s/^\s+//;
  71.     s/\s+$//;
  72.     $_;
  73.     };
  74.  
  75.     if ($type == 0) {
  76.     print "BIOS Vendor: ",        $str->(0), "\n";
  77.     print "BIOS Version: ",        $str->(1), "\n";
  78.     print "BIOS Release: ",        $str->(4), "\n";
  79.     }
  80.     elsif ($type == 1) {
  81.     print "System Vendor: ",    $str->(0), "\n";
  82.     print "System Name: ",        $str->(1), "\n";
  83.     print "System Version: ",    $str->(2), "\n";
  84.     print "System Serial Number: ",    $str->(3), "\n";
  85.     }
  86.     elsif ($type == 2) {
  87.     print "Board Vendor: ",        $str->(0), "\n";
  88.     print "Board Name: ",        $str->(1), "\n";
  89.     print "Board Version: ",    $str->(2), "\n";
  90.     print "Board Serial Number: ",    $str->(3), "\n";
  91.     }
  92.     elsif ($type == 3) {
  93.     print "Asset Tag: ",        $str->(4), "\n";
  94.     }
  95.  
  96.     last unless s/^.*?\0\0//;
  97.     last if ++$n >= $DCOUNT;
  98.  
  99.     $last_type = $type;
  100. }
  101.