| @@ -1,114 +1,114 @@ | | | @@ -1,114 +1,114 @@ |
1 | $NetBSD: patch-decode-dimms,v 1.18 2020/09/07 14:50:25 pgoyette Exp $ | | 1 | $NetBSD: patch-decode-dimms,v 1.19 2021/10/28 20:18:21 pgoyette Exp $ |
2 | | | 2 | |
3 | Add NetBSD-specific ability to use spdmem(4)'s sysctl values as | | 3 | Add NetBSD-specific ability to use spdmem(4)'s sysctl values as |
4 | input. Remove attempts to use linux-specific data sources. | | 4 | input. Remove attempts to use linux-specific data sources. |
5 | | | 5 | |
6 | --- eeprom/decode-dimms.orig 2020-09-07 07:41:27.782730720 -0700 | | 6 | --- eeprom/decode-dimms.orig 2021-07-22 04:10:34.000000000 -0700 |
7 | +++ eeprom/decode-dimms 2020-09-07 07:42:58.255264706 -0700 | | 7 | +++ eeprom/decode-dimms 2021-10-28 13:01:32.530284359 -0700 |
8 | @@ -45,8 +45,9 @@ | | 8 | @@ -45,8 +45,9 @@ |
9 | use Fcntl qw(:DEFAULT :seek); | | 9 | use Fcntl qw(:DEFAULT :seek); |
10 | use File::Basename; | | 10 | use File::Basename; |
11 | use vars qw($opt_html $opt_bodyonly $opt_side_by_side $opt_merge | | 11 | use vars qw($opt_html $opt_bodyonly $opt_side_by_side $opt_merge |
12 | - $opt_igncheck $use_sysfs $use_hexdump $sbs_col_width | | 12 | - $opt_igncheck $use_sysfs $use_hexdump $sbs_col_width |
13 | - @vendors %decode_callback @dimm $current %hexdump_cache); | | 13 | - @vendors %decode_callback @dimm $current %hexdump_cache); |
14 | + $opt_igncheck $use_sysctl $use_hexdump $sbs_col_width | | 14 | + $opt_igncheck $use_sysctl $use_hexdump $sbs_col_width |
15 | + @vendors %decode_callback @dimm $current %hexdump_cache | | 15 | + @vendors %decode_callback @dimm $current %hexdump_cache |
16 | + %sysctl_cache); | | 16 | + %sysctl_cache); |
17 | | | 17 | |
18 | use constant LITTLEENDIAN => "little-endian"; | | 18 | use constant LITTLEENDIAN => "little-endian"; |
19 | use constant BIGENDIAN => "big-endian"; | | 19 | use constant BIGENDIAN => "big-endian"; |
20 | @@ -497,7 +498,7 @@ | | 20 | @@ -497,7 +498,7 @@ |
21 | die "Unexpected number of vendor names in page $page" unless @{$vendors[$page]} == 126; | | 21 | die "Unexpected number of vendor names in page $page" unless @{$vendors[$page]} == 126; |
22 | } | | 22 | } |
23 | | | 23 | |
24 | -$use_sysfs = -d '/sys/bus'; | | 24 | -$use_sysfs = -d '/sys/bus'; |
25 | +$use_sysctl = 0; | | 25 | +$use_sysctl = 0; |
26 | | | 26 | |
27 | # We consider that no data was written to this area of the SPD EEPROM if | | 27 | # We consider that no data was written to this area of the SPD EEPROM if |
28 | # all bytes read 0x00 or all bytes read 0xff | | 28 | # all bytes read 0x00 or all bytes read 0xff |
29 | @@ -2436,6 +2437,26 @@ | | 29 | @@ -2437,6 +2438,26 @@ |
30 | } | | 30 | } |
31 | } | | 31 | } |
32 | | | 32 | |
33 | +# read data from a NetBSD (or equivalent) sysctl variable | | 33 | +# read data from a NetBSD (or equivalent) sysctl variable |
34 | + | | 34 | + |
35 | +sub read_sysctl($) | | 35 | +sub read_sysctl($) |
36 | +{ | | 36 | +{ |
37 | + | | 37 | + |
38 | + # Look in the cache first | | 38 | + # Look in the cache first |
39 | + return @{$sysctl_cache{$_[0]}} if exists $sysctl_cache{$_[0]}; | | 39 | + return @{$sysctl_cache{$_[0]}} if exists $sysctl_cache{$_[0]}; |
40 | + | | 40 | + |
41 | + my $sysctl_var = sprintf("hw.%s.spd_data", $_[0]); | | 41 | + my $sysctl_var = sprintf("hw.%s.spd_data", $_[0]); |
42 | + open(PIPE, "-|", "sysctl -r $sysctl_var") | | 42 | + open(PIPE, "-|", "sysctl -r $sysctl_var") |
43 | + or die "Cannot read sysctl variable $sysctl_var"; | | 43 | + or die "Cannot read sysctl variable $sysctl_var"; |
44 | + sysread(PIPE, my $eeprom, 512); # XXX Assumed maximum size! XXX | | 44 | + sysread(PIPE, my $eeprom, 512); # XXX Assumed maximum size! XXX |
45 | + close PIPE or die "sysctl returned $?"; | | 45 | + close PIPE or die "sysctl returned $?"; |
46 | + my @bytes = unpack("C*", $eeprom); | | 46 | + my @bytes = unpack("C*", $eeprom); |
47 | + | | 47 | + |
48 | + # Cache the data for later use | | 48 | + # Cache the data for later use |
49 | + $sysctl_cache{$_[0]} = \@bytes; | | 49 | + $sysctl_cache{$_[0]} = \@bytes; |
50 | + return @bytes; | | 50 | + return @bytes; |
51 | +} | | 51 | +} |
52 | + | | 52 | + |
53 | # Read bytes from SPD-EEPROM | | 53 | # Read bytes from SPD-EEPROM |
54 | # Note: offset must be a multiple of 16! | | 54 | # Note: offset must be a multiple of 16! |
55 | sub readspd($$$) | | 55 | sub readspd($$$) |
56 | @@ -2449,20 +2470,13 @@ | | 56 | @@ -2450,20 +2471,13 @@ |
57 | $size = @bytes - $offset; | | 57 | $size = @bytes - $offset; |
58 | } | | 58 | } |
59 | return @bytes[$offset..($offset + $size - 1)]; | | 59 | return @bytes[$offset..($offset + $size - 1)]; |
60 | - } elsif ($use_sysfs) { | | 60 | - } elsif ($use_sysfs) { |
61 | - # Kernel 2.6 with sysfs | | 61 | - # Kernel 2.6 with sysfs |
62 | - sysopen(HANDLE, "$dimm_i/eeprom", O_RDONLY) | | 62 | - sysopen(HANDLE, "$dimm_i/eeprom", O_RDONLY) |
63 | - or die "Cannot open $dimm_i/eeprom"; | | 63 | - or die "Cannot open $dimm_i/eeprom"; |
64 | - binmode HANDLE; | | 64 | - binmode HANDLE; |
65 | - sysseek(HANDLE, $offset, SEEK_SET) | | 65 | - sysseek(HANDLE, $offset, SEEK_SET) |
66 | - or die "Cannot seek $dimm_i/eeprom"; | | 66 | - or die "Cannot seek $dimm_i/eeprom"; |
67 | - $read = sysread(HANDLE, my $eeprom, $size) | | 67 | - $read = sysread(HANDLE, my $eeprom, $size) |
68 | - or die "Cannot read $dimm_i/eeprom"; | | 68 | - or die "Cannot read $dimm_i/eeprom"; |
69 | - close HANDLE; | | 69 | - close HANDLE; |
70 | - if ($read < $size) { | | 70 | - if ($read < $size) { |
71 | - print STDERR "WARNING: $dimm_i/eeprom is smaller than expected\n"; | | 71 | - print STDERR "WARNING: $dimm_i/eeprom is smaller than expected\n"; |
72 | + } elsif ($use_sysctl) { | | 72 | + } elsif ($use_sysctl) { |
73 | + @bytes = read_sysctl($dimm_i); | | 73 | + @bytes = read_sysctl($dimm_i); |
74 | + if (@bytes < $offset + $size) { | | 74 | + if (@bytes < $offset + $size) { |
75 | + print STDERR "WARNING: sysctl for $dimm_i is truncated\n"; | | 75 | + print STDERR "WARNING: sysctl for $dimm_i is truncated\n"; |
76 | + $size = @bytes - $offset; | | 76 | + $size = @bytes - $offset; |
77 | } | | 77 | } |
78 | - @bytes = unpack("C*", $eeprom); | | 78 | - @bytes = unpack("C*", $eeprom); |
79 | + return return @bytes[$offset..($offset + $size - 1)]; | | 79 | + return return @bytes[$offset..($offset + $size - 1)]; |
80 | } else { | | 80 | } else { |
81 | # Kernel 2.4 with procfs | | 81 | # Kernel 2.4 with procfs |
82 | for my $i (0 .. ($size-1)/16) { | | 82 | for my $i (0 .. ($size-1)/16) { |
83 | @@ -2528,7 +2542,7 @@ | | 83 | @@ -2529,7 +2543,7 @@ |
84 | # Parse command-line | | 84 | # Parse command-line |
85 | foreach (@ARGV) { | | 85 | foreach (@ARGV) { |
86 | if ($_ eq '-h' || $_ eq '--help') { | | 86 | if ($_ eq '-h' || $_ eq '--help') { |
87 | - print "Usage: $0 [-c] [-f [-b]] [-x|-X file [files..]]\n", | | 87 | - print "Usage: $0 [-c] [-f [-b]] [-x|-X file [files..]]\n", |
88 | + print "Usage: $0 [-c] [-f [-b]] [-x|-X|-s file [files..]]\n", | | 88 | + print "Usage: $0 [-c] [-f [-b]] [-x|-X|-s file [files..]]\n", |
89 | " $0 -h\n\n", | | 89 | " $0 -h\n\n", |
90 | " -f, --format Print nice html output\n", | | 90 | " -f, --format Print nice html output\n", |
91 | " -b, --bodyonly Don't print html header\n", | | 91 | " -b, --bodyonly Don't print html header\n", |
92 | @@ -2542,6 +2556,8 @@ | | 92 | @@ -2543,6 +2557,8 @@ |
93 | " -x, Read data from hexdump files\n", | | 93 | " -x, Read data from hexdump files\n", |
94 | " -X, Same as -x except treat multibyte hex\n", | | 94 | " -X, Same as -x except treat multibyte hex\n", |
95 | " data as little endian\n", | | 95 | " data as little endian\n", |
96 | + " -s, Use NetBSD-compatible sysctl(8) to obtain\n", | | 96 | + " -s, Use NetBSD-compatible sysctl(8) to obtain\n", |
97 | + " EEPROM data\n", | | 97 | + " EEPROM data\n", |
98 | " -h, --help Display this usage summary\n"; | | 98 | " -h, --help Display this usage summary\n"; |
99 | print <<"EOF"; | | 99 | print <<"EOF"; |
100 | | | 100 | |
101 | @@ -2586,85 +2602,25 @@ | | 101 | @@ -2587,85 +2603,25 @@ |
102 | $use_hexdump = LITTLEENDIAN; | | 102 | $use_hexdump = LITTLEENDIAN; |
103 | next; | | 103 | next; |
104 | } | | 104 | } |
105 | + if ($_ eq '-s') { | | 105 | + if ($_ eq '-s') { |
106 | + if (-x "/sbin/sysctl") { | | 106 | + if (-x "/sbin/sysctl") { |
107 | + $use_sysctl = 1; | | 107 | + $use_sysctl = 1; |
108 | + } else { die "No /sbin/sysctl available for -s"; } | | 108 | + } else { die "No /sbin/sysctl available for -s"; } |
109 | + next; | | 109 | + next; |
110 | + } | | 110 | + } |
111 | | | 111 | |
112 | if (m/^-/) { | | 112 | if (m/^-/) { |
113 | print STDERR "Unrecognized option $_\n"; | | 113 | print STDERR "Unrecognized option $_\n"; |
114 | exit; | | 114 | exit; |
| @@ -182,30 +182,30 @@ input. Remove attempts to use linux-spe | | | @@ -182,30 +182,30 @@ input. Remove attempts to use linux-spe |
182 | - } | | 182 | - } |
183 | - | | 183 | - |
184 | - if (!$opened) { | | 184 | - if (!$opened) { |
185 | - print STDERR "No EEPROM found, try loading the eeprom, at24 or ee1004 module\n"; | | 185 | - print STDERR "No EEPROM found, try loading the eeprom, at24 or ee1004 module\n"; |
186 | - exit; | | 186 | - exit; |
187 | - } | | 187 | - } |
188 | - | | 188 | - |
189 | - return sort { $a->{file} cmp $b->{file} } @files; | | 189 | - return sort { $a->{file} cmp $b->{file} } @files; |
190 | -} | | 190 | -} |
191 | - | | 191 | - |
192 | # @dimm is a list of hashes. There's one hash for each EEPROM we found. | | 192 | # @dimm is a list of hashes. There's one hash for each EEPROM we found. |
193 | # Each hash has the following keys: | | 193 | # Each hash has the following keys: |
194 | # * eeprom: Name of the eeprom data file | | 194 | # * eeprom: Name of the eeprom data file |
195 | @@ -2677,7 +2633,6 @@ | | 195 | @@ -2678,7 +2634,6 @@ |
196 | # * chk_spd: The checksum or CRC value found in the EEPROM | | 196 | # * chk_spd: The checksum or CRC value found in the EEPROM |
197 | # * chk_calc: The checksum or CRC computed from the EEPROM data | | 197 | # * chk_calc: The checksum or CRC computed from the EEPROM data |
198 | # Keys are added over time. | | 198 | # Keys are added over time. |
199 | -@dimm = get_dimm_list() unless $use_hexdump; | | 199 | -@dimm = get_dimm_list() unless $use_hexdump; |
200 | | | 200 | |
201 | for my $i (0 .. $#dimm) { | | 201 | for my $i (0 .. $#dimm) { |
202 | my @bytes = readspd(0, 128, $dimm[$i]->{file}); | | 202 | my @bytes = readspd(0, 128, $dimm[$i]->{file}); |
203 | @@ -2729,7 +2684,7 @@ | | 203 | @@ -2730,7 +2685,7 @@ |
204 | printl("Decoding EEPROM", $dimm[$current]->{eeprom}); | | 204 | printl("Decoding EEPROM", $dimm[$current]->{eeprom}); |
205 | } | | 205 | } |
206 | | | 206 | |
207 | - if (!$use_hexdump) { | | 207 | - if (!$use_hexdump) { |
208 | + if (!$use_hexdump && !$use_sysctl) { | | 208 | + if (!$use_hexdump && !$use_sysctl) { |
209 | if ($dimm[$current]->{file} =~ /-([\da-f]+)$/i) { | | 209 | if ($dimm[$current]->{file} =~ /-([\da-f]+)$/i) { |
210 | my $dimm_num = hex($1) - 0x50 + 1; | | 210 | my $dimm_num = hex($1) - 0x50 + 1; |
211 | if ($dimm_num >= 1 && $dimm_num <= 8) { | | 211 | if ($dimm_num >= 1 && $dimm_num <= 8) { |