| | 83 | # Abstract reads so that other functions don't have to care wether |
| | 84 | # we need to use procfs or sysfs |
| | 85 | sub read_eeprom_bytes |
| | 86 | { |
| | 87 | my ($bus, $addr, $offset, $length) = @_; |
| | 88 | my $filename; |
| | 89 | |
| | 90 | if ($sysfs) |
| | 91 | { |
| | 92 | $filename = "/sys/bus/i2c/devices/$bus-00$addr/eeprom"; |
| | 93 | sysopen(FH, $filename, O_RDONLY) |
| | 94 | or die "Can't open $filename"; |
| | 95 | sysseek(FH, $offset, SEEK_SET) |
| | 96 | or die "Can't seek in $filename"; |
| | 97 | |
| | 98 | my ($r, $bytes); |
| | 99 | $bytes = ''; |
| | 100 | $offset = 0; |
| | 101 | while($length) |
| | 102 | { |
| | 103 | $r = sysread(FH, $bytes, $length, $offset); |
| | 104 | die "Can't read $filename" |
| | 105 | unless defined($r); |
| | 106 | die "Unexpected EOF in $filename" |
| | 107 | unless $r; |
| | 108 | $offset += $r; |
| | 109 | $length -= $r; |
| | 110 | } |
| | 111 | close(FH); |
| | 112 | |
| | 113 | return $bytes; |
| | 114 | } |
| | 115 | else |
| | 116 | { |
| | 117 | my $base = $offset & 0xf0; |
| | 118 | $offset -= $base; |
| | 119 | my $values = ''; |
| | 120 | my $remains = $length + $offset; |
| | 121 | |
| | 122 | # Get all lines in a single string |
| | 123 | while ($remains > 0) |
| | 124 | { |
| | 125 | $filename = "/proc/sys/dev/sensors/eeprom-i2c-$bus-$addr/" |
| | 126 | . sprintf('%02x', $base); |
| | 127 | open(FH, $filename) |
| | 128 | or die "Can't open $filename"; |
| | 129 | $values .= <FH>; |
| | 130 | close(FH); |
| | 131 | $remains -= 16; |
| | 132 | $base += 16; |
| | 133 | } |
| | 134 | |
| | 135 | # Store the useful part in an array |
| | 136 | my @bytes = split(/[ \n]/, $values); |
| | 137 | @bytes = @bytes[$offset..$offset+$length-1]; |
| | 138 | |
| | 139 | # Back to a binary string |
| | 140 | return pack('C*', @bytes); |
| | 141 | } |
| | 142 | } |
| | 143 | |
| 79 | | my ($bus,$addr,$base,$offset,$length) = @_; |
| 80 | | |
| 81 | | my $line=''; |
| 82 | | my $remains=$length+$offset; |
| 83 | | while($remains>0) |
| 84 | | { |
| 85 | | my $filename="/proc/sys/dev/sensors/eeprom-i2c-$bus-$addr/".sprintf('%02x',$base); |
| 86 | | open(FH,$filename) || die "Can't open $filename"; |
| 87 | | $line.=<FH>; |
| 88 | | close(FH); |
| 89 | | $remains-=16; |
| 90 | | $base+=16; |
| 91 | | } |
| 92 | | |
| 93 | | my @bytes=split(/[ \n]/,$line); |
| 94 | | @bytes=@bytes[$offset..$offset+$length-1]; |
| | 146 | my ($bus, $addr, $offset, $length) = @_; |
| | 147 | |
| | 148 | my $string = read_eeprom_bytes($bus, $addr, $offset, $length); |
| | 149 | $string =~ s/\x00.*$//; |
| | 150 | |
| | 151 | return($string); |
| | 152 | } |
| | 153 | |
| | 154 | sub decode_uuid |
| | 155 | { |
| | 156 | my ($bus,$addr,$base) = @_; |
| | 157 | |
| | 158 | my @bytes = unpack('C16', read_eeprom_bytes($bus, $addr, $base, 16)); |
| 134 | | print_item('Machine Name',decode_string($bus,$addr,128,0,32)); |
| 135 | | print_item('Serial Number',decode_string($bus,$addr,192,0,32)); |
| 136 | | print_item('UUID',decode_uuid($bus,$addr,16)); |
| 137 | | print_item('Revision',decode_string($bus,$addr,160,0,10)); |
| 138 | | print_item('Model Name','PCG-'.decode_string($bus,$addr,160,10,4)); |
| 139 | | print_item('OEM Data',decode_string($bus,$addr,32,0,16)); |
| 140 | | print_item('Timestamp',decode_string($bus,$addr,224,0,32)); |
| | 177 | print_item('Machine Name', decode_string($bus, $addr, 128, 32)); |
| | 178 | print_item('Serial Number', decode_string($bus, $addr, 192, 32)); |
| | 179 | print_item('UUID', decode_uuid($bus, $addr, 16)); |
| | 180 | print_item('Revision', decode_string($bus, $addr, 160, 10)); |
| | 181 | print_item('Model Name', 'PCG-'.decode_string($bus, $addr, 170, 4)); |
| | 182 | print_item('OEM Data', decode_string($bus, $addr, 32, 16)); |
| | 183 | print_item('Timestamp', decode_string($bus, $addr, 224, 32)); |