root/i2c-tools/trunk/eeprom/decode-dimms @ 5714

Revision 5714, 63.2 KB (checked in by khali, 4 years ago)

Be more robust against seek/read errors on sysfs.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2#
3# EEPROM data decoder for SDRAM DIMM modules
4#
5# Copyright 1998, 1999 Philip Edelbrock <phil@netroedge.com>
6# modified by Christian Zuckschwerdt <zany@triq.net>
7# modified by Burkart Lingner <burkart@bollchen.de>
8# Copyright (C) 2005-2009  Jean Delvare <khali@linux-fr.org>
9#
10#    This program is free software; you can redistribute it and/or modify
11#    it under the terms of the GNU General Public License as published by
12#    the Free Software Foundation; either version 2 of the License, or
13#    (at your option) any later version.
14#
15#    This program is distributed in the hope that it will be useful,
16#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18#    GNU General Public License for more details.
19#
20#    You should have received a copy of the GNU General Public License
21#    along with this program; if not, write to the Free Software
22#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23#    MA 02110-1301 USA.
24#
25#
26# The eeprom driver must be loaded (unless option -x is used). For kernels
27# older than 2.6.0, the eeprom driver can be found in the lm-sensors package.
28#
29# References:
30# PC SDRAM Serial Presence
31# Detect (SPD) Specification, Intel,
32# 1997,1999, Rev 1.2B
33#
34# Jedec Standards 4.1.x & 4.5.x
35# http://www.jedec.org
36#
37
38require 5.004;
39
40use strict;
41use POSIX qw(ceil);
42use Fcntl qw(:DEFAULT :seek);
43use vars qw($opt_html $opt_bodyonly $opt_side_by_side $opt_merge
44            $opt_igncheck $use_sysfs $use_hexdump $sbs_col_width
45            @vendors %decode_callback $revision @dimm $current %hexdump_cache);
46
47use constant LITTLEENDIAN       => "little-endian";
48use constant BIGENDIAN          => "big-endian";
49
50$revision = '$Revision$ ($Date$)';
51$revision =~ s/\$\w+: (.*?) \$/$1/g;
52$revision =~ s/ \([^()]*\)//;
53
54@vendors = (
55["AMD", "AMI", "Fairchild", "Fujitsu",
56 "GTE", "Harris", "Hitachi", "Inmos",
57 "Intel", "I.T.T.", "Intersil", "Monolithic Memories",
58 "Mostek", "Freescale (former Motorola)", "National", "NEC",
59 "RCA", "Raytheon", "Conexant (Rockwell)", "Seeq",
60 "NXP (former Signetics, Philips Semi.)", "Synertek", "Texas Instruments", "Toshiba",
61 "Xicor", "Zilog", "Eurotechnique", "Mitsubishi",
62 "Lucent (AT&T)", "Exel", "Atmel", "SGS/Thomson",
63 "Lattice Semi.", "NCR", "Wafer Scale Integration", "IBM",
64 "Tristar", "Visic", "Intl. CMOS Technology", "SSSI",
65 "MicrochipTechnology", "Ricoh Ltd.", "VLSI", "Micron Technology",
66 "Hyundai Electronics", "OKI Semiconductor", "ACTEL", "Sharp",
67 "Catalyst", "Panasonic", "IDT", "Cypress",
68 "DEC", "LSI Logic", "Zarlink (former Plessey)", "UTMC",
69 "Thinking Machine", "Thomson CSF", "Integrated CMOS (Vertex)", "Honeywell",
70 "Tektronix", "Sun Microsystems", "SST", "ProMos/Mosel Vitelic",
71 "Infineon (former Siemens)", "Macronix", "Xerox", "Plus Logic",
72 "SunDisk", "Elan Circuit Tech.", "European Silicon Str.", "Apple Computer",
73 "Xilinx", "Compaq", "Protocol Engines", "SCI",
74 "Seiko Instruments", "Samsung", "I3 Design System", "Klic",
75 "Crosspoint Solutions", "Alliance Semiconductor", "Tandem", "Hewlett-Packard",
76 "Intg. Silicon Solutions", "Brooktree", "New Media", "MHS Electronic",
77 "Performance Semi.", "Winbond Electronic", "Kawasaki Steel", "Bright Micro",
78 "TECMAR", "Exar", "PCMCIA", "LG Semi (former Goldstar)",
79 "Northern Telecom", "Sanyo", "Array Microsystems", "Crystal Semiconductor",
80 "Analog Devices", "PMC-Sierra", "Asparix", "Convex Computer",
81 "Quality Semiconductor", "Nimbus Technology", "Transwitch", "Micronas (ITT Intermetall)",
82 "Cannon", "Altera", "NEXCOM", "QUALCOMM",
83 "Sony", "Cray Research", "AMS(Austria Micro)", "Vitesse",
84 "Aster Electronics", "Bay Networks (Synoptic)", "Zentrum or ZMD", "TRW",
85 "Thesys", "Solbourne Computer", "Allied-Signal", "Dialog",
86 "Media Vision", "Level One Communication"],
87["Cirrus Logic", "National Instruments", "ILC Data Device", "Alcatel Mietec",
88 "Micro Linear", "Univ. of NC", "JTAG Technologies", "BAE Systems",
89 "Nchip", "Galileo Tech", "Bestlink Systems", "Graychip",
90 "GENNUM", "VideoLogic", "Robert Bosch", "Chip Express",
91 "DATARAM", "United Microelec Corp.", "TCSI", "Smart Modular",
92 "Hughes Aircraft", "Lanstar Semiconductor", "Qlogic", "Kingston",
93 "Music Semi", "Ericsson Components", "SpaSE", "Eon Silicon Devices",
94 "Programmable Micro Corp", "DoD", "Integ. Memories Tech.", "Corollary Inc.",
95 "Dallas Semiconductor", "Omnivision", "EIV(Switzerland)", "Novatel Wireless",
96 "Zarlink (former Mitel)", "Clearpoint", "Cabletron", "STEC (former Silicon Technology)",
97 "Vanguard", "Hagiwara Sys-Com", "Vantis", "Celestica",
98 "Century", "Hal Computers", "Rohm Company Ltd.", "Juniper Networks",
99 "Libit Signal Processing", "Mushkin Enhanced Memory", "Tundra Semiconductor", "Adaptec Inc.",
100 "LightSpeed Semi.", "ZSP Corp.", "AMIC Technology", "Adobe Systems",
101 "Dynachip", "PNY Electronics", "Newport Digital", "MMC Networks",
102 "T Square", "Seiko Epson", "Broadcom", "Viking Components",
103 "V3 Semiconductor", "Flextronics (former Orbit)", "Suwa Electronics", "Transmeta",
104 "Micron CMS", "American Computer & Digital Components Inc", "Enhance 3000 Inc", "Tower Semiconductor",
105 "CPU Design", "Price Point", "Maxim Integrated Product", "Tellabs",
106 "Centaur Technology", "Unigen Corporation", "Transcend Information", "Memory Card Technology",
107 "CKD Corporation Ltd.", "Capital Instruments, Inc.", "Aica Kogyo, Ltd.", "Linvex Technology",
108 "MSC Vertriebs GmbH", "AKM Company, Ltd.", "Dynamem, Inc.", "NERA ASA",
109 "GSI Technology", "Dane-Elec (C Memory)", "Acorn Computers", "Lara Technology",
110 "Oak Technology, Inc.", "Itec Memory", "Tanisys Technology", "Truevision",
111 "Wintec Industries", "Super PC Memory", "MGV Memory", "Galvantech",
112 "Gadzoox Nteworks", "Multi Dimensional Cons.", "GateField", "Integrated Memory System",
113 "Triscend", "XaQti", "Goldenram", "Clear Logic",
114 "Cimaron Communications", "Nippon Steel Semi. Corp.", "Advantage Memory", "AMCC",
115 "LeCroy", "Yamaha Corporation", "Digital Microwave", "NetLogic Microsystems",
116 "MIMOS Semiconductor", "Advanced Fibre", "BF Goodrich Data.", "Epigram",
117 "Acbel Polytech Inc.", "Apacer Technology", "Admor Memory", "FOXCONN",
118 "Quadratics Superconductor", "3COM"],
119["Camintonn Corporation", "ISOA Incorporated", "Agate Semiconductor", "ADMtek Incorporated",
120 "HYPERTEC", "Adhoc Technologies", "MOSAID Technologies", "Ardent Technologies",
121 "Switchcore", "Cisco Systems, Inc.", "Allayer Technologies", "WorkX AG",
122 "Oasis Semiconductor", "Novanet Semiconductor", "E-M Solutions", "Power General",
123 "Advanced Hardware Arch.", "Inova Semiconductors GmbH", "Telocity", "Delkin Devices",
124 "Symagery Microsystems", "C-Port Corporation", "SiberCore Technologies", "Southland Microsystems",
125 "Malleable Technologies", "Kendin Communications", "Great Technology Microcomputer", "Sanmina Corporation",
126 "HADCO Corporation", "Corsair", "Actrans System Inc.", "ALPHA Technologies",
127 "Silicon Laboratories, Inc. (Cygnal)", "Artesyn Technologies", "Align Manufacturing", "Peregrine Semiconductor",
128 "Chameleon Systems", "Aplus Flash Technology", "MIPS Technologies", "Chrysalis ITS",
129 "ADTEC Corporation", "Kentron Technologies", "Win Technologies", "Tachyon Semiconductor (former ASIC Designs Inc.)",
130 "Extreme Packet Devices", "RF Micro Devices", "Siemens AG", "Sarnoff Corporation",
131 "Itautec Philco SA", "Radiata Inc.", "Benchmark Elect. (AVEX)", "Legend",
132 "SpecTek Incorporated", "Hi/fn", "Enikia Incorporated", "SwitchOn Networks",
133 "AANetcom Incorporated", "Micro Memory Bank", "ESS Technology", "Virata Corporation",
134 "Excess Bandwidth", "West Bay Semiconductor", "DSP Group", "Newport Communications",
135 "Chip2Chip Incorporated", "Phobos Corporation", "Intellitech Corporation", "Nordic VLSI ASA",
136 "Ishoni Networks", "Silicon Spice", "Alchemy Semiconductor", "Agilent Technologies",
137 "Centillium Communications", "W.L. Gore", "HanBit Electronics", "GlobeSpan",
138 "Element 14", "Pycon", "Saifun Semiconductors", "Sibyte, Incorporated",
139 "MetaLink Technologies", "Feiya Technology", "I & C Technology", "Shikatronics",
140 "Elektrobit", "Megic", "Com-Tier", "Malaysia Micro Solutions",
141 "Hyperchip", "Gemstone Communications", "Anadigm (former Anadyne)", "3ParData",
142 "Mellanox Technologies", "Tenx Technologies", "Helix AG", "Domosys",
143 "Skyup Technology", "HiNT Corporation", "Chiaro", "MDT Technologies GmbH (former MCI Computer GMBH)",
144 "Exbit Technology A/S", "Integrated Technology Express", "AVED Memory", "Legerity",
145 "Jasmine Networks", "Caspian Networks", "nCUBE", "Silicon Access Networks",
146 "FDK Corporation", "High Bandwidth Access", "MultiLink Technology", "BRECIS",
147 "World Wide Packets", "APW", "Chicory Systems", "Xstream Logic",
148 "Fast-Chip", "Zucotto Wireless", "Realchip", "Galaxy Power",
149 "eSilicon", "Morphics Technology", "Accelerant Networks", "Silicon Wave",
150 "SandCraft", "Elpida"],
151["Solectron", "Optosys Technologies", "Buffalo (former Melco)", "TriMedia Technologies",
152 "Cyan Technologies", "Global Locate", "Optillion", "Terago Communications",
153 "Ikanos Communications", "Princeton Technology", "Nanya Technology", "Elite Flash Storage",
154 "Mysticom", "LightSand Communications", "ATI Technologies", "Agere Systems",
155 "NeoMagic", "AuroraNetics", "Golden Empire", "Mushkin",
156 "Tioga Technologies", "Netlist", "TeraLogic", "Cicada Semiconductor",
157 "Centon Electronics", "Tyco Electronics", "Magis Works", "Zettacom",
158 "Cogency Semiconductor", "Chipcon AS", "Aspex Technology", "F5 Networks",
159 "Programmable Silicon Solutions", "ChipWrights", "Acorn Networks", "Quicklogic",
160 "Kingmax Semiconductor", "BOPS", "Flasys", "BitBlitz Communications",
161 "eMemory Technology", "Procket Networks", "Purple Ray", "Trebia Networks",
162 "Delta Electronics", "Onex Communications", "Ample Communications", "Memory Experts Intl",
163 "Astute Networks", "Azanda Network Devices", "Dibcom", "Tekmos",
164 "API NetWorks", "Bay Microsystems", "Firecron Ltd", "Resonext Communications",
165 "Tachys Technologies", "Equator Technology", "Concept Computer", "SILCOM",
166 "3Dlabs", "c't Magazine", "Sanera Systems", "Silicon Packets",
167 "Viasystems Group", "Simtek", "Semicon Devices Singapore", "Satron Handelsges",
168 "Improv Systems", "INDUSYS GmbH", "Corrent", "Infrant Technologies",
169 "Ritek Corp", "empowerTel Networks", "Hypertec", "Cavium Networks",
170 "PLX Technology", "Massana Design", "Intrinsity", "Valence Semiconductor",
171 "Terawave Communications", "IceFyre Semiconductor", "Primarion", "Picochip Designs Ltd",
172 "Silverback Systems", "Jade Star Technologies", "Pijnenburg Securealink",
173 "TakeMS International AG", "Cambridge Silicon Radio",
174 "Swissbit", "Nazomi Communications", "eWave System",
175 "Rockwell Collins", "Picocel Co., Ltd.", "Alphamosaic Ltd", "Sandburst",
176 "SiCon Video", "NanoAmp Solutions", "Ericsson Technology", "PrairieComm",
177 "Mitac International", "Layer N Networks", "MtekVision", "Allegro Networks",
178 "Marvell Semiconductors", "Netergy Microelectronic", "NVIDIA", "Internet Machines",
179 "Peak Electronics", "Litchfield Communication", "Accton Technology", "Teradiant Networks",
180 "Europe Technologies", "Cortina Systems", "RAM Components", "Raqia Networks",
181 "ClearSpeed", "Matsushita Battery", "Xelerated", "SimpleTech",
182 "Utron Technology", "Astec International", "AVM gmbH", "Redux Communications",
183 "Dot Hill Systems", "TeraChip"],
184["T-RAM Incorporated", "Innovics Wireless", "Teknovus", "KeyEye Communications",
185 "Runcom Technologies", "RedSwitch", "Dotcast", "Silicon Mountain Memory",
186 "Signia Technologies", "Pixim", "Galazar Networks", "White Electronic Designs",
187 "Patriot Scientific", "Neoaxiom Corporation", "3Y Power Technology", "Europe Technologies",
188 "Potentia Power Systems", "C-guys Incorporated", "Digital Communications Technology Incorporated", "Silicon-Based Technology",
189 "Fulcrum Microsystems", "Positivo Informatica Ltd", "XIOtech Corporation", "PortalPlayer",
190 "Zhiying Software", "Direct2Data", "Phonex Broadband", "Skyworks Solutions",
191 "Entropic Communications", "Pacific Force Technology", "Zensys A/S", "Legend Silicon Corp.",
192 "sci-worx GmbH", "SMSC (former Oasis Silicon Systems)", "Renesas Technology", "Raza Microelectronics",
193 "Phyworks", "MediaTek", "Non-cents Productions", "US Modular",
194 "Wintegra Ltd", "Mathstar", "StarCore", "Oplus Technologies",
195 "Mindspeed", "Just Young Computer", "Radia Communications", "OCZ",
196 "Emuzed", "LOGIC Devices", "Inphi Corporation", "Quake Technologies",
197 "Vixel", "SolusTek", "Kongsberg Maritime", "Faraday Technology",
198 "Altium Ltd.", "Insyte", "ARM Ltd.", "DigiVision",
199 "Vativ Technologies", "Endicott Interconnect Technologies", "Pericom", "Bandspeed",
200 "LeWiz Communications", "CPU Technology", "Ramaxel Technology", "DSP Group",
201 "Axis Communications", "Legacy Electronics", "Chrontel", "Powerchip Semiconductor",
202 "MobilEye Technologies", "Excel Semiconductor", "A-DATA Technology", "VirtualDigm",
203 "G Skill Intl", "Quanta Computer", "Yield Microelectronics", "Afa Technologies",
204 "KINGBOX Technology Co. Ltd.", "Ceva", "iStor Networks", "Advance Modules",
205 "Microsoft", "Open-Silicon", "Goal Semiconductor", "ARC International",
206 "Simmtec", "Metanoia", "Key Stream", "Lowrance Electronics",
207 "Adimos", "SiGe Semiconductor", "Fodus Communications", "Credence Systems Corp.",
208 "Genesis Microchip Inc.", "Vihana, Inc.", "WIS Technologies", "GateChange Technologies",
209 "High Density Devices AS", "Synopsys", "Gigaram", "Enigma Semiconductor Inc.",
210 "Century Micro Inc.", "Icera Semiconductor", "Mediaworks Integrated Systems", "O'Neil Product Development",
211 "Supreme Top Technology Ltd.", "MicroDisplay Corporation", "Team Group Inc.", "Sinett Corporation",
212 "Toshiba Corporation", "Tensilica", "SiRF Technology", "Bacoc Inc.",
213 "SMaL Camera Technologies", "Thomson SC", "Airgo Networks", "Wisair Ltd.",
214 "SigmaTel", "Arkados", "Compete IT gmbH Co. KG", "Eudar Technology Inc.",
215 "Focus Enhancements", "Xyratex"],
216["Specular Networks", "Patriot Memory", "U-Chip Technology Corp.", "Silicon Optix",
217 "Greenfield Networks", "CompuRAM GmbH", "Stargen, Inc.", "NetCell Corporation",
218 "Excalibrus Technologies Ltd", "SCM Microsystems", "Xsigo Systems, Inc.", "CHIPS & Systems Inc",
219 "Tier 1 Multichip Solutions", "CWRL Labs", "Teradici", "Gigaram, Inc.",
220 "g2 Microsystems", "PowerFlash Semiconductor", "P.A. Semi, Inc.", "NovaTech Solutions, S.A.",
221 "c2 Microsystems, Inc.", "Level5 Networks", "COS Memory AG", "Innovasic Semiconductor",
222 "02IC Co. Ltd", "Tabula, Inc.", "Crucial Technology", "Chelsio Communications",
223 "Solarflare Communications", "Xambala Inc.", "EADS Astrium", "ATO Semicon Co. Ltd.",
224 "Imaging Works, Inc.", "Astute Networks, Inc.", "Tzero", "Emulex",
225 "Power-One", "Pulse~LINK Inc.", "Hon Hai Precision Industry", "White Rock Networks Inc.",
226 "Telegent Systems USA, Inc.", "Atrua Technologies, Inc.", "Acbel Polytech Inc.",
227 "eRide Inc.","ULi Electronics Inc.", "Magnum Semiconductor Inc.", "neoOne Technology, Inc.",
228 "Connex Technology, Inc.", "Stream Processors, Inc.", "Focus Enhancements", "Telecis Wireless, Inc.",
229 "uNav Microelectronics", "Tarari, Inc.", "Ambric, Inc.", "Newport Media, Inc.", "VMTS",
230 "Enuclia Semiconductor, Inc.", "Virtium Technology Inc.", "Solid State System Co., Ltd.", "Kian Tech LLC",
231 "Artimi", "Power Quotient International", "Avago Technologies", "ADTechnology", "Sigma Designs",
232 "SiCortex, Inc.", "Ventura Technology Group", "eASIC", "M.H.S. SAS", "Micro Star International",
233 "Rapport Inc.", "Makway International", "Broad Reach Engineering Co.",
234 "Semiconductor Mfg Intl Corp", "SiConnect", "FCI USA Inc.", "Validity Sensors",
235 "Coney Technology Co. Ltd.", "Spans Logic", "Neterion Inc.", "Qimonda",
236 "New Japan Radio Co. Ltd.", "Velogix", "Montalvo Systems", "iVivity Inc.", "Walton Chaintech",
237 "AENEON", "Lorom Industrial Co. Ltd.", "Radiospire Networks", "Sensio Technologies, Inc.",
238 "Nethra Imaging", "Hexon Technology Pte Ltd", "CompuStocx (CSX)", "Methode Electronics, Inc.",
239 "Connect One Ltd.", "Opulan Technologies", "Septentrio NV", "Goldenmars Technology Inc.",
240 "Kreton Corporation", "Cochlear Ltd.", "Altair Semiconductor", "NetEffect, Inc.",
241 "Spansion, Inc.", "Taiwan Semiconductor Mfg", "Emphany Systems Inc.",
242 "ApaceWave Technologies", "Mobilygen Corporation", "Tego", "Cswitch Corporation",
243 "Haier (Beijing) IC Design Co.", "MetaRAM", "Axel Electronics Co. Ltd.", "Tilera Corporation",
244 "Aquantia", "Vivace Semiconductor", "Redpine Signals", "Octalica", "InterDigital Communications",
245 "Avant Technology", "Asrock, Inc.", "Availink", "Quartics, Inc.", "Element CXI",
246 "Innovaciones Microelectronicas", "VeriSilicon Microelectronics", "W5 Networks"],
247["MOVEKING", "Mavrix Technology, Inc.", "CellGuide Ltd.", "Faraday Technology",
248 "Diablo Technologies, Inc.", "Jennic", "Octasic", "Molex Incorporated", "3Leaf Networks",
249 "Bright Micron Technology", "Netxen", "NextWave Broadband Inc.", "DisplayLink", "ZMOS Technology",
250 "Tec-Hill", "Multigig, Inc.", "Amimon", "Euphonic Technologies, Inc.", "BRN Phoenix",
251 "InSilica", "Ember Corporation", "Avexir Technologies Corporation", "Echelon Corporation",
252 "Edgewater Computer Systems", "XMOS Semiconductor Ltd.", "GENUSION, Inc.", "Memory Corp NV",
253 "SiliconBlue Technologies", "Rambus Inc."]);
254
255$use_sysfs = -d '/sys/bus';
256
257# We consider that no data was written to this area of the SPD EEPROM if
258# all bytes read 0x00 or all bytes read 0xff
259sub spd_written(@)
260{
261        my $all_00 = 1;
262        my $all_ff = 1;
263
264        foreach my $b (@_) {
265                $all_00 = 0 unless $b == 0x00;
266                $all_ff = 0 unless $b == 0xff;
267                return 1 unless $all_00 or $all_ff;
268        }
269
270        return 0;
271}
272
273sub parity($)
274{
275        my $n = shift;
276        my $parity = 0;
277
278        while ($n) {
279                $parity++ if ($n & 1);
280                $n >>= 1;
281        }
282
283        return ($parity & 1);
284}
285
286# New encoding format (as of DDR3) for manufacturer just has a count of
287# leading 0x7F rather than all the individual bytes.  The count bytes includes
288# parity!
289sub manufacturer_ddr3($$)
290{
291        my ($count, $code) = @_;
292        return "Invalid" if parity($count) != 1;
293        return "Invalid" if parity($code) != 1;
294        return (($code & 0x7F) - 1 > $vendors[$count & 0x7F]) ? "Unknown" :
295                $vendors[$count & 0x7F][($code & 0x7F) - 1];
296}
297
298sub manufacturer(@)
299{
300        my @bytes = @_;
301        my $ai = 0;
302        my $first;
303
304        return ("Undefined", []) unless spd_written(@bytes);
305
306        while (defined($first = shift(@bytes)) && $first == 0x7F) {
307                $ai++;
308        }
309
310        return ("Invalid", []) unless defined $first;
311        return ("Invalid", [$first, @bytes]) if parity($first) != 1;
312        if (parity($ai) == 0) {
313                $ai |= 0x80;
314        }
315        return (manufacturer_ddr3($ai, $first), \@bytes);
316}
317
318sub manufacturer_data(@)
319{
320        my $hex = "";
321        my $asc = "";
322
323        return unless spd_written(@_);
324
325        foreach my $byte (@_) {
326                $hex .= sprintf("\%02X ", $byte);
327                $asc .= ($byte >= 32 && $byte < 127) ? chr($byte) : '?';
328        }
329
330        return "$hex(\"$asc\")";
331}
332
333sub part_number(@)
334{
335        my $asc = "";
336        my $byte;
337
338        while (defined ($byte = shift) && $byte >= 32 && $byte < 127) {
339                $asc .= chr($byte);
340        }
341
342        return ($asc eq "") ? "Undefined" : $asc;
343}
344
345sub cas_latencies(@)
346{
347        return "None" unless @_;
348        return join ', ', map("${_}T", sort { $b <=> $a } @_);
349}
350
351# Real printing functions
352
353sub html_encode($)
354{
355        my $text = shift;
356        $text =~ s/</\&lt;/sg;
357        $text =~ s/>/\&gt;/sg;
358        $text =~ s/\n/<br>\n/sg;
359        return $text;
360}
361
362sub same_values(@)
363{
364        my $value = shift;
365        while (@_) {
366                return 0 unless $value eq shift;
367        }
368        return 1;
369}
370
371sub real_printl($$) # print a line w/ label and values
372{
373        my ($label, @values) = @_;
374        local $_;
375        my $same_values = same_values(@values);
376
377        # If all values are N/A, don't bother printing
378        return if $values[0] eq "N/A" and $same_values;
379
380        if ($opt_html) {
381                $label = html_encode($label);
382                @values = map { html_encode($_) } @values;
383                print "<tr><td valign=top>$label</td>";
384                if ($opt_merge && $same_values) {
385                        print "<td colspan=".(scalar @values).">$values[0]</td>";
386                } else {
387                        print "<td>$_</td>" foreach @values;
388                }
389                print "</tr>\n";
390        } else {
391                if ($opt_merge && $same_values) {
392                        splice(@values, 1);
393                }
394
395                my $format = "%-47s".((" %-".$sbs_col_width."s") x (scalar @values - 1))." %s\n";
396                my $maxl = 0; # Keep track of the max number of lines
397
398                # It's a bit tricky because each value may span over more than
399                # one line. We can easily extract the values per column, but
400                # we need them per line at printing time. So we have to
401                # prepare a 2D array with all the individual string fragments.
402                my ($col, @lines);
403                for ($col = 0; $col < @values; $col++) {
404                        my @cells = split /\n/, $values[$col];
405                        $maxl = @cells if @cells > $maxl;
406                        for (my $l = 0; $l < @cells; $l++) {
407                                $lines[$l]->[$col] = $cells[$l];
408                        }
409                }
410
411                # Also make sure there are no holes in the array
412                for (my $l = 0; $l < $maxl; $l++) {
413                        for ($col = 0; $col < @values; $col++) {
414                                $lines[$l]->[$col] = ""
415                                        if not defined $lines[$l]->[$col];
416                        }
417                }
418
419                printf $format, $label, @{shift @lines};
420                printf $format, "", @{$_} foreach (@lines);
421        }
422}
423
424sub printl2($$) # print a line w/ label and value (outside a table)
425{
426        my ($label, $value) = @_;
427        if ($opt_html) {
428                $label = html_encode($label);
429                $value = html_encode($value);
430        }
431        print "$label: $value\n";
432}
433
434sub real_prints($) # print separator w/ given text
435{
436        my ($label, $ncol) = @_;
437        $ncol = 1 unless $ncol;
438        if ($opt_html) {
439                $label = html_encode($label);
440                print "<tr><td align=center colspan=".(1+$ncol)."><b>$label</b></td></tr>\n";
441        } else {
442                print "\n---=== $label ===---\n";
443        }
444}
445
446sub printh($$) # print header w/ given text
447{
448        my ($header, $sub) = @_;
449        if ($opt_html) {
450                $header = html_encode($header);
451                $sub = html_encode($sub);
452                print "<h1>$header</h1>\n";
453                print "<p>$sub</p>\n";
454        } else {
455                print "\n$header\n$sub\n";
456        }
457}
458
459sub printc($) # print comment
460{
461        my ($comment) = @_;
462        if ($opt_html) {
463                $comment = html_encode($comment);
464                print "<!-- $comment -->\n";
465        } else {
466                print "# $comment\n";
467        }
468}
469
470# Fake printing functions
471# These don't actually print anything, instead they store the desired
472# output for later processing.
473
474sub printl($$) # print a line w/ label and value
475{
476        my @output = (\&real_printl, @_);
477        push @{$dimm[$current]->{output}}, \@output;
478}
479
480sub printl_cond($$$) # same as printl but conditional
481{
482        my ($cond, $label, $value) = @_;
483        return unless $cond || $opt_side_by_side;
484        printl($label, $cond ? $value : "N/A");
485}
486
487sub prints($) # print separator w/ given text
488{
489        my @output = (\&real_prints, @_);
490        push @{$dimm[$current]->{output}}, \@output;
491}
492
493# Helper functions
494
495sub tns($) # print a time in ns
496{
497        return sprintf("%3.2f ns", $_[0]);
498}
499
500sub tns3($) # print a time in ns, with 3 decimal digits
501{
502        return sprintf("%.3f ns", $_[0]);
503}
504
505sub value_or_undefined
506{
507        my ($value, $unit) = @_;
508        return "Undefined!" unless $value;
509        $value .= " $unit" if defined $unit;
510        return $value;
511}
512
513# Parameter: EEPROM bytes 0-127 (using 3-62)
514sub decode_sdr_sdram($)
515{
516        my $bytes = shift;
517        my $temp;
518
519# SPD revision
520        printl("SPD Revision", $bytes->[62]);
521
522#size computation
523
524        prints("Memory Characteristics");
525
526        my $k = 0;
527        my $ii = 0;
528
529        $ii = ($bytes->[3] & 0x0f) + ($bytes->[4] & 0x0f) - 17;
530        if (($bytes->[5] <= 8) && ($bytes->[17] <= 8)) {
531                 $k = $bytes->[5] * $bytes->[17];
532        }
533
534        if ($ii > 0 && $ii <= 12 && $k > 0) {
535                printl("Size", ((1 << $ii) * $k) . " MB");
536        } else {
537                printl("Size", "INVALID: " . $bytes->[3] . "," . $bytes->[4] . "," .
538                               $bytes->[5] . "," . $bytes->[17]);
539        }
540
541        my @cas;
542        for ($ii = 0; $ii < 7; $ii++) {
543                push(@cas, $ii + 1) if ($bytes->[18] & (1 << $ii));
544        }
545
546        my $trcd;
547        my $trp;
548        my $tras;
549        my $ctime = ($bytes->[9] >> 4) + ($bytes->[9] & 0xf) * 0.1;
550
551        $trcd = $bytes->[29];
552        $trp = $bytes->[27];;
553        $tras = $bytes->[30];
554
555        printl("tCL-tRCD-tRP-tRAS",
556                $cas[$#cas] . "-" .
557                ceil($trcd/$ctime) . "-" .
558                ceil($trp/$ctime) . "-" .
559                ceil($tras/$ctime));
560
561        if ($bytes->[3] == 0) { $temp = "Undefined!"; }
562        elsif ($bytes->[3] == 1) { $temp = "1/16"; }
563        elsif ($bytes->[3] == 2) { $temp = "2/17"; }
564        elsif ($bytes->[3] == 3) { $temp = "3/18"; }
565        else { $temp = $bytes->[3]; }
566        printl("Number of Row Address Bits", $temp);
567
568        if ($bytes->[4] == 0) { $temp = "Undefined!"; }
569        elsif ($bytes->[4] == 1) { $temp = "1/16"; }
570        elsif ($bytes->[4] == 2) { $temp = "2/17"; }
571        elsif ($bytes->[4] == 3) { $temp = "3/18"; }
572        else { $temp = $bytes->[4]; }
573        printl("Number of Col Address Bits", $temp);
574
575        printl("Number of Module Rows", value_or_undefined($bytes->[5]));
576
577        if ($bytes->[7] > 1) { $temp = "Undefined!"; }
578        else { $temp = ($bytes->[7] * 256) + $bytes->[6]; }
579        printl("Data Width", $temp);
580
581        if ($bytes->[8] == 0) { $temp = "5.0 Volt/TTL"; }
582        elsif ($bytes->[8] == 1) { $temp = "LVTTL"; }
583        elsif ($bytes->[8] == 2) { $temp = "HSTL 1.5"; }
584        elsif ($bytes->[8] == 3) { $temp = "SSTL 3.3"; }
585        elsif ($bytes->[8] == 4) { $temp = "SSTL 2.5"; }
586        elsif ($bytes->[8] == 255) { $temp = "New Table"; }
587        else { $temp = "Undefined!"; }
588        printl("Module Interface Signal Levels", $temp);
589
590        if ($bytes->[11] == 0) { $temp = "No Parity"; }
591        elsif ($bytes->[11] == 1) { $temp = "Parity"; }
592        elsif ($bytes->[11] == 2) { $temp = "ECC";  }
593        else { $temp = "Undefined!"; }
594        printl("Module Configuration Type", $temp);
595
596        if ($bytes->[12] & 0x80) { $temp = "Self Refreshing"; }
597        else { $temp = "Not Self Refreshing"; }
598        printl("Refresh Type", $temp);
599
600        $temp = $bytes->[12] & 0x7f;
601        if ($temp == 0) { $temp = "Normal (15.625 us)"; }
602        elsif ($temp == 1) { $temp = "Reduced (3.9 us)"; }
603        elsif ($temp == 2) { $temp = "Reduced (7.8 us)"; }
604        elsif ($temp == 3) { $temp = "Extended (31.3 us)"; }
605        elsif ($temp == 4) { $temp = "Extended (62.5 us)"; }
606        elsif ($temp == 5) { $temp = "Extended (125 us)"; }
607        else { $temp = "Undefined!"; }
608        printl("Refresh Rate", $temp);
609
610        if ($bytes->[13] & 0x80) { $temp = "Bank2 = 2 x Bank1"; }
611        else { $temp = "No Bank2 OR Bank2 = Bank1 width"; }
612        printl("Primary SDRAM Component Bank Config", $temp);
613        printl("Primary SDRAM Component Widths",
614               value_or_undefined($bytes->[13] & 0x7f));
615
616        if ($bytes->[14] & 0x80) { $temp = "Bank2 = 2 x Bank1"; }
617        else { $temp = "No Bank2 OR Bank2 = Bank1 width"; }
618        printl("Error Checking SDRAM Component Bank Config", $temp);
619        printl("Error Checking SDRAM Component Widths",
620               value_or_undefined($bytes->[14] & 0x7f));
621
622        printl("Min Clock Delay for Back to Back Random Access",
623               value_or_undefined($bytes->[15]));
624
625        my @array;
626        for ($ii = 0; $ii < 4; $ii++) {
627                push(@array, 1 << $ii) if ($bytes->[16] & (1 << $ii));
628        }
629        push(@array, "Page") if ($bytes->[16] & 128);
630        if (@array) { $temp = join ', ', @array; }
631        else { $temp = "None"; }
632        printl("Supported Burst Lengths", $temp);
633
634        printl("Number of Device Banks",
635               value_or_undefined($bytes->[17]));
636
637        printl("Supported CAS Latencies", cas_latencies(@cas));
638
639        @array = ();
640        for ($ii = 0; $ii < 7; $ii++) {
641                push(@array, $ii) if ($bytes->[19] & (1 << $ii));
642        }
643        if (@array) { $temp = join ', ', @array; }
644        else { $temp = "None"; }
645        printl("Supported CS Latencies", $temp);
646
647        @array = ();
648        for ($ii = 0; $ii < 7; $ii++) {
649                push(@array, $ii) if ($bytes->[20] & (1 << $ii));
650        }
651        if (@array) { $temp = join ', ', @array; }
652        else { $temp = "None"; }
653        printl("Supported WE Latencies", $temp);
654
655        my ($cycle_time, $access_time);
656
657        if (@cas >= 1) {
658                $cycle_time = "$ctime ns at CAS ".$cas[$#cas];
659
660                $temp = ($bytes->[10] >> 4) + ($bytes->[10] & 0xf) * 0.1;
661                $access_time = "$temp ns at CAS ".$cas[$#cas];
662        }
663
664        if (@cas >= 2 && spd_written(@$bytes[23..24])) {
665                $temp = $bytes->[23] >> 4;
666                if ($temp == 0) { $temp = "Undefined!"; }
667                else {
668                        $temp += 15 if $temp < 4;
669                        $temp += ($bytes->[23] & 0xf) * 0.1;
670                        $temp .= " ns";
671                }
672                $cycle_time .= "\n$temp ns at CAS ".$cas[$#cas-1];
673
674                $temp = $bytes->[24] >> 4;
675                if ($temp == 0) { $temp = "Undefined!"; }
676                else {
677                        $temp += 15 if $temp < 4;
678                        $temp += ($bytes->[24] & 0xf) * 0.1;
679                        $temp .= " ns";
680                }
681                $access_time .= "\n$temp ns at CAS ".$cas[$#cas-1];
682        }
683
684        if (@cas >= 3 && spd_written(@$bytes[25..26])) {
685                $temp = $bytes->[25] >> 2;
686                if ($temp == 0) { $temp = "Undefined!"; }
687                else {
688                        $temp += ($bytes->[25] & 0x3) * 0.25;
689                        $temp .= " ns";
690                }
691                $cycle_time .= "\n$temp ns at CAS ".$cas[$#cas-2];
692
693                $temp = $bytes->[26] >> 2;
694                if ($temp == 0) { $temp = "Undefined!"; }
695                else {
696                        $temp += ($bytes->[26] & 0x3) * 0.25;
697                        $temp .= " ns";
698                }
699                $access_time .= "\n$temp ns at CAS ".$cas[$#cas-2];
700        }
701
702        printl_cond(defined $cycle_time, "Cycle Time", $cycle_time);
703        printl_cond(defined $access_time, "Access Time", $access_time);
704
705        $temp = "";
706        if ($bytes->[21] & 1) { $temp .= "Buffered Address/Control Inputs\n"; }
707        if ($bytes->[21] & 2) { $temp .= "Registered Address/Control Inputs\n"; }
708        if ($bytes->[21] & 4) { $temp .= "On card PLL (clock)\n"; }
709        if ($bytes->[21] & 8) { $temp .= "Buffered DQMB Inputs\n"; }
710        if ($bytes->[21] & 16) { $temp .= "Registered DQMB Inputs\n"; }
711        if ($bytes->[21] & 32) { $temp .= "Differential Clock Input\n"; }
712        if ($bytes->[21] & 64) { $temp .= "Redundant Row Address\n"; }
713        if ($bytes->[21] & 128) { $temp .= "Undefined (bit 7)\n"; }
714        if ($bytes->[21] == 0) { $temp .= "(None Reported)\n"; }
715        printl("SDRAM Module Attributes", $temp);
716
717        $temp = "";
718        if ($bytes->[22] & 1) { $temp .= "Supports Early RAS# Recharge\n"; }
719        if ($bytes->[22] & 2) { $temp .= "Supports Auto-Precharge\n"; }
720        if ($bytes->[22] & 4) { $temp .= "Supports Precharge All\n"; }
721        if ($bytes->[22] & 8) { $temp .= "Supports Write1/Read Burst\n"; }
722        if ($bytes->[22] & 16) { $temp .= "Lower VCC Tolerance: 5%\n"; }
723        else { $temp .= "Lower VCC Tolerance: 10%\n"; }
724        if ($bytes->[22] & 32) { $temp .= "Upper VCC Tolerance: 5%\n"; }
725        else { $temp .= "Upper VCC Tolerance: 10%\n"; }
726        if ($bytes->[22] & 64) { $temp .= "Undefined (bit 6)\n"; }
727        if ($bytes->[22] & 128) { $temp .= "Undefined (bit 7)\n"; }
728        printl("SDRAM Device Attributes (General)", $temp);
729
730        printl("Minimum Row Precharge Time",
731               value_or_undefined($bytes->[27], "ns"));
732
733        printl("Row Active to Row Active Min",
734               value_or_undefined($bytes->[28], "ns"));
735
736        printl("RAS to CAS Delay",
737               value_or_undefined($bytes->[29], "ns"));
738
739        printl("Min RAS Pulse Width",
740               value_or_undefined($bytes->[30], "ns"));
741
742        $temp = "";
743        if ($bytes->[31] & 1) { $temp .= "4 MByte\n"; }
744        if ($bytes->[31] & 2) { $temp .= "8 MByte\n"; }
745        if ($bytes->[31] & 4) { $temp .= "16 MByte\n"; }
746        if ($bytes->[31] & 8) { $temp .= "32 MByte\n"; }
747        if ($bytes->[31] & 16) { $temp .= "64 MByte\n"; }
748        if ($bytes->[31] & 32) { $temp .= "128 MByte\n"; }
749        if ($bytes->[31] & 64) { $temp .= "256 MByte\n"; }
750        if ($bytes->[31] & 128) { $temp .= "512 MByte\n"; }
751        if ($bytes->[31] == 0) { $temp .= "(Undefined! -- None Reported!)\n"; }
752        printl("Row Densities", $temp);
753
754        $temp = (($bytes->[32] & 0x7f) >> 4) + ($bytes->[32] & 0xf) * 0.1;
755        printl_cond(($bytes->[32] & 0xf) <= 9,
756                    "Command and Address Signal Setup Time",
757                    (($bytes->[32] >> 7) ? -$temp : $temp) . " ns");
758
759        $temp = (($bytes->[33] & 0x7f) >> 4) + ($bytes->[33] & 0xf) * 0.1;
760        printl_cond(($bytes->[33] & 0xf) <= 9,
761                    "Command and Address Signal Hold Time",
762                    (($bytes->[33] >> 7) ? -$temp : $temp) . " ns");
763
764        $temp = (($bytes->[34] & 0x7f) >> 4) + ($bytes->[34] & 0xf) * 0.1;
765        printl_cond(($bytes->[34] & 0xf) <= 9, "Data Signal Setup Time",
766                    (($bytes->[34] >> 7) ? -$temp : $temp) . " ns");
767
768        $temp = (($bytes->[35] & 0x7f) >> 4) + ($bytes->[35] & 0xf) * 0.1;
769        printl_cond(($bytes->[35] & 0xf) <= 9, "Data Signal Hold Time",
770                    (($bytes->[35] >> 7) ? -$temp : $temp) . " ns");
771}
772
773# Parameter: EEPROM bytes 0-127 (using 3-62)
774sub decode_ddr_sdram($)
775{
776        my $bytes = shift;
777        my $temp;
778
779# SPD revision
780        printl_cond($bytes->[62] != 0xff, "SPD Revision",
781                    ($bytes->[62] >> 4) . "." . ($bytes->[62] & 0xf));
782
783# speed
784        prints("Memory Characteristics");
785
786        $temp = ($bytes->[9] >> 4) + ($bytes->[9] & 0xf) * 0.1;
787        my $ddrclk = 2 * (1000 / $temp);
788        my $tbits = ($bytes->[7] * 256) + $bytes->[6];
789        if (($bytes->[11] == 2) || ($bytes->[11] == 1)) { $tbits = $tbits - 8; }
790        my $pcclk = int ($ddrclk * $tbits / 8);
791        $pcclk += 100 if ($pcclk % 100) >= 50; # Round properly
792        $pcclk = $pcclk - ($pcclk % 100);
793        $ddrclk = int ($ddrclk);
794        printl("Maximum module speed", "${ddrclk}MHz (PC${pcclk})");
795
796#size computation
797        my $k = 0;
798        my $ii = 0;
799
800        $ii = ($bytes->[3] & 0x0f) + ($bytes->[4] & 0x0f) - 17;
801        if (($bytes->[5] <= 8) && ($bytes->[17] <= 8)) {
802                 $k = $bytes->[5] * $bytes->[17];
803        }
804
805        if ($ii > 0 && $ii <= 12 && $k > 0) {
806                printl("Size", ((1 << $ii) * $k) . " MB");
807        } else {
808                printl("Size", "INVALID: " . $bytes->[3] . ", " . $bytes->[4] . ", " .
809                               $bytes->[5] . ", " . $bytes->[17]);
810        }
811
812        my $highestCAS = 0;
813        my %cas;
814        for ($ii = 0; $ii < 7; $ii++) {
815                if ($bytes->[18] & (1 << $ii)) {
816                        $highestCAS = 1+$ii*0.5;
817                        $cas{$highestCAS}++;
818                }
819        }
820
821        my $trcd;
822        my $trp;
823        my $tras;
824        my $ctime = ($bytes->[9] >> 4) + ($bytes->[9] & 0xf) * 0.1;
825
826        $trcd = ($bytes->[29] >> 2) + (($bytes->[29] & 3) * 0.25);
827        $trp = ($bytes->[27] >> 2) + (($bytes->[27] & 3) * 0.25);
828        $tras = $bytes->[30];
829
830        printl("tCL-tRCD-tRP-tRAS",
831                $highestCAS . "-" .
832                ceil($trcd/$ctime) . "-" .
833                ceil($trp/$ctime) . "-" .
834                ceil($tras/$ctime));
835
836# latencies
837        printl("Supported CAS Latencies", cas_latencies(keys %cas));
838
839        my @array;
840        for ($ii = 0; $ii < 7; $ii++) {
841                push(@array, $ii) if ($bytes->[19] & (1 << $ii));
842        }
843        if (@array) { $temp = join ', ', @array; }
844        else { $temp = "None"; }
845        printl("Supported CS Latencies", $temp);
846
847        @array = ();
848        for ($ii = 0; $ii < 7; $ii++) {
849                push(@array, $ii) if ($bytes->[20] & (1 << $ii));
850        }
851        if (@array) { $temp = join ', ', @array; }
852        else { $temp = "None"; }
853        printl("Supported WE Latencies", $temp);
854
855# timings
856        my ($cycle_time, $access_time);
857
858        if (exists $cas{$highestCAS}) {
859                $cycle_time = "$ctime ns at CAS $highestCAS";
860                $access_time = (($bytes->[10] >> 4) * 0.1 + ($bytes->[10] & 0xf) * 0.01)
861                             . " ns at CAS $highestCAS";
862        }
863
864        if (exists $cas{$highestCAS-0.5} && spd_written(@$bytes[23..24])) {
865                $cycle_time .= "\n".(($bytes->[23] >> 4) + ($bytes->[23] & 0xf) * 0.1)
866                             . " ns at CAS ".($highestCAS-0.5);
867                $access_time .= "\n".(($bytes->[24] >> 4) * 0.1 + ($bytes->[24] & 0xf) * 0.01)
868                              . " ns at CAS ".($highestCAS-0.5);
869        }
870
871        if (exists $cas{$highestCAS-1} && spd_written(@$bytes[25..26])) {
872                $cycle_time .= "\n".(($bytes->[25] >> 4) + ($bytes->[25] & 0xf) * 0.1)
873                             . " ns at CAS ".($highestCAS-1);
874                $access_time .= "\n".(($bytes->[26] >> 4) * 0.1 + ($bytes->[26] & 0xf) * 0.01)
875                              . " ns at CAS ".($highestCAS-1);
876        }
877
878        printl_cond(defined $cycle_time, "Minimum Cycle Time", $cycle_time);
879        printl_cond(defined $access_time, "Maximum Access Time", $access_time);
880
881# module attributes
882        if ($bytes->[47] & 0x03) {
883                if (($bytes->[47] & 0x03) == 0x01) { $temp = "1.125\" to 1.25\""; }
884                elsif (($bytes->[47] & 0x03) == 0x02) { $temp = "1.7\""; }
885                elsif (($bytes->[47] & 0x03) == 0x03) { $temp = "Other"; }
886                printl("Module Height", $temp);
887        }
888}
889
890sub ddr2_sdram_ctime($)
891{
892        my $byte = shift;
893        my $ctime;
894
895        $ctime = $byte >> 4;
896        if (($byte & 0xf) <= 9) { $ctime += ($byte & 0xf) * 0.1; }
897        elsif (($byte & 0xf) == 10) { $ctime += 0.25; }
898        elsif (($byte & 0xf) == 11) { $ctime += 0.33; }
899        elsif (($byte & 0xf) == 12) { $ctime += 0.66; }
900        elsif (($byte & 0xf) == 13) { $ctime += 0.75; }
901
902        return $ctime;
903}
904
905sub ddr2_sdram_atime($)
906{
907        my $byte = shift;
908        my $atime;
909
910        $atime = ($byte >> 4) * 0.1 + ($byte & 0xf) * 0.01;
911
912        return $atime;
913}
914
915# Base, high-bit, 3-bit fraction code
916sub ddr2_sdram_rtime($$$)
917{
918        my ($rtime, $msb, $ext) = @_;
919        my @table = (0, .25, .33, .50, .66, .75);
920
921        return $rtime + $msb * 256 + $table[$ext];
922}
923
924sub ddr2_module_types($)
925{
926        my $byte = shift;
927        my @types = qw(RDIMM UDIMM SO-DIMM Micro-DIMM Mini-RDIMM Mini-UDIMM);
928        my @widths = (133.35, 133.25, 67.6, 45.5, 82.0, 82.0);
929        my @suptypes;
930        local $_;
931
932        foreach (0..5) {
933                push @suptypes, "$types[$_] ($widths[$_] mm)"
934                        if ($byte & (1 << $_));
935        }
936
937        return @suptypes;
938}
939
940sub ddr2_refresh_rate($)
941{
942        my $byte = shift;
943        my @refresh = qw(Normal Reduced Reduced Extended Extended Extended);
944        my @refresht = (15.625, 3.9, 7.8, 31.3, 62.5, 125);
945
946        return "$refresh[$byte & 0x7f] ($refresht[$byte & 0x7f] us)".
947               ($byte & 0x80 ? " - Self Refresh" : "");
948}
949
950# Parameter: EEPROM bytes 0-127 (using 3-62)
951sub decode_ddr2_sdram($)
952{
953        my $bytes = shift;
954        my $temp;
955        my $ctime;
956
957# SPD revision
958        if ($bytes->[62] != 0xff) {
959                printl("SPD Revision", ($bytes->[62] >> 4) . "." .
960                                       ($bytes->[62] & 0xf));
961        }
962
963# speed
964        prints("Memory Characteristics");
965
966        $ctime = ddr2_sdram_ctime($bytes->[9]);
967        my $ddrclk = 2 * (1000 / $ctime);
968        my $tbits = ($bytes->[7] * 256) + $bytes->[6];
969        if ($bytes->[11] & 0x03) { $tbits = $tbits - 8; }
970        my $pcclk = int ($ddrclk * $tbits / 8);
971        # Round down to comply with Jedec
972        $pcclk = $pcclk - ($pcclk % 100);
973        $ddrclk = int ($ddrclk);
974        printl("Maximum module speed", "${ddrclk}MHz (PC2-${pcclk})");
975
976#size computation
977        my $k = 0;
978        my $ii = 0;
979
980        $ii = ($bytes->[3] & 0x0f) + ($bytes->[4] & 0x0f) - 17;
981        $k = (($bytes->[5] & 0x7) + 1) * $bytes->[17];
982
983        if($ii > 0 && $ii <= 12 && $k > 0) {
984                printl("Size", ((1 << $ii) * $k) . " MB");
985        } else {
986                printl("Size", "INVALID: " . $bytes->[3] . "," . $bytes->[4] . "," .
987                               $bytes->[5] . "," . $bytes->[17]);
988        }
989
990        printl("Banks x Rows x Columns x Bits",
991               join(' x ', $bytes->[17], $bytes->[3], $bytes->[4], $bytes->[6]));
992        printl("Ranks", ($bytes->[5] & 7) + 1);
993
994        printl("SDRAM Device Width", $bytes->[13]." bits");
995
996        my @heights = ('< 25.4', '25.4', '25.4 - 30.0', '30.0', '30.5', '> 30.5');
997        printl("Module Height", $heights[$bytes->[5] >> 5]." mm");
998
999        my @suptypes = ddr2_module_types($bytes->[20]);
1000        printl("Module Type".(@suptypes > 1 ? 's' : ''), join(', ', @suptypes));
1001
1002        printl("DRAM Package", $bytes->[5] & 0x10 ? "Stack" : "Planar");
1003
1004        my @volts = ("TTL (5V Tolerant)", "LVTTL", "HSTL 1.5V",
1005                     "SSTL 3.3V", "SSTL 2.5V", "SSTL 1.8V", "TBD");
1006        printl("Voltage Interface Level", $volts[$bytes->[8]]);
1007
1008        printl("Refresh Rate", ddr2_refresh_rate($bytes->[12]));
1009
1010        my @burst;
1011        push @burst, 4 if ($bytes->[16] & 4);
1012        push @burst, 8 if ($bytes->[16] & 8);
1013        $burst[0] = 'None' if !@burst;
1014        printl("Supported Burst Lengths", join(', ', @burst));
1015
1016        my $highestCAS = 0;
1017        my %cas;
1018        for ($ii = 2; $ii < 7; $ii++) {
1019                if ($bytes->[18] & (1 << $ii)) {
1020                        $highestCAS = $ii;
1021                        $cas{$highestCAS}++;
1022                }
1023        }
1024
1025        my $trcd;
1026        my $trp;
1027        my $tras;
1028
1029        $trcd = ($bytes->[29] >> 2) + (($bytes->[29] & 3) * 0.25);
1030        $trp = ($bytes->[27] >> 2) + (($bytes->[27] & 3) * 0.25);
1031        $tras = $bytes->[30];
1032
1033        printl("tCL-tRCD-tRP-tRAS",
1034                $highestCAS . "-" .
1035                ceil($trcd/$ctime) . "-" .
1036                ceil($trp/$ctime) . "-" .
1037                ceil($tras/$ctime));
1038
1039# latencies
1040        printl("Supported CAS Latencies (tCL)", cas_latencies(keys %cas));
1041
1042# timings
1043        my ($cycle_time, $access_time);
1044
1045        if (exists $cas{$highestCAS}) {
1046                $cycle_time = tns($ctime) . " at CAS $highestCAS (tCK min)";
1047                $access_time = tns(ddr2_sdram_atime($bytes->[10]))
1048                             . " at CAS $highestCAS (tAC)";
1049        }
1050
1051        if (exists $cas{$highestCAS-1} && spd_written(@$bytes[23..24])) {
1052                $cycle_time .= "\n".tns(ddr2_sdram_ctime($bytes->[23]))
1053                             . " at CAS ".($highestCAS-1);
1054                $access_time .= "\n".tns(ddr2_sdram_atime($bytes->[24]))
1055                              . " at CAS ".($highestCAS-1);
1056        }
1057
1058        if (exists $cas{$highestCAS-2} && spd_written(@$bytes[25..26])) {
1059                $cycle_time .= "\n".tns(ddr2_sdram_ctime($bytes->[25]))
1060                             . " at CAS ".($highestCAS-2);
1061                $access_time .= "\n".tns(ddr2_sdram_atime($bytes->[26]))
1062                              . " at CAS ".($highestCAS-2);
1063        }
1064
1065        printl_cond(defined $cycle_time, "Minimum Cycle Time", $cycle_time);
1066        printl_cond(defined $access_time, "Maximum Access Time", $access_time);
1067
1068        printl("Maximum Cycle Time (tCK max)",
1069               tns(ddr2_sdram_ctime($bytes->[43])));
1070
1071# more timing information
1072        prints("Timing Parameters");
1073        printl("Address/Command Setup Time Before Clock (tIS)",
1074               tns(ddr2_sdram_atime($bytes->[32])));
1075        printl("Address/Command Hold Time After Clock (tIH)",
1076               tns(ddr2_sdram_atime($bytes->[33])));
1077        printl("Data Input Setup Time Before Strobe (tDS)",
1078               tns(ddr2_sdram_atime($bytes->[34])));
1079        printl("Data Input Hold Time After Strobe (tDH)",
1080               tns(ddr2_sdram_atime($bytes->[35])));
1081        printl("Minimum Row Precharge Delay (tRP)", tns($trp));
1082        printl("Minimum Row Active to Row Active Delay (tRRD)",
1083               tns($bytes->[28]/4));
1084        printl("Minimum RAS# to CAS# Delay (tRCD)", tns($trcd));
1085        printl("Minimum RAS# Pulse Width (tRAS)", tns($tras));
1086        printl("Write Recovery Time (tWR)", tns($bytes->[36]/4));
1087        printl("Minimum Write to Read CMD Delay (tWTR)", tns($bytes->[37]/4));
1088        printl("Minimum Read to Pre-charge CMD Delay (tRTP)", tns($bytes->[38]/4));
1089        printl("Minimum Active to Auto-refresh Delay (tRC)",
1090               tns(ddr2_sdram_rtime($bytes->[41], 0, ($bytes->[40] >> 4) & 7)));
1091        printl("Minimum Recovery Delay (tRFC)",
1092               tns(ddr2_sdram_rtime($bytes->[42], $bytes->[40] & 1,
1093                                    ($bytes->[40] >> 1) & 7)));
1094        printl("Maximum DQS to DQ Skew (tDQSQ)", tns($bytes->[44]/100));
1095        printl("Maximum Read Data Hold Skew (tQHS)", tns($bytes->[45]/100));
1096        printl("PLL Relock Time", $bytes->[46] . " us") if ($bytes->[46]);
1097}
1098
1099# Parameter: EEPROM bytes 0-127 (using 3-76)
1100sub decode_ddr3_sdram($)
1101{
1102        my $bytes = shift;
1103        my $temp;
1104        my $ctime;
1105
1106        my @module_types = ("Undefined", "RDIMM", "UDIMM", "SO-DIMM",
1107                            "Micro-DIMM", "Mini-RDIMM", "Mini-UDIMM");
1108
1109        printl("Module Type", ($bytes->[3] <= $#module_types) ?
1110                                        $module_types[$bytes->[3]] :
1111                                        sprint("Reserved (0x%.2X)", $bytes->[3]));
1112
1113# speed
1114        prints("Memory Characteristics");
1115
1116        my $dividend = ($bytes->[9] >> 4) & 15;
1117        my $divisor  = $bytes->[9] & 15;
1118        printl("Fine time base", sprintf("%.3f", $dividend / $divisor) . " ps");
1119
1120        $dividend = $bytes->[10];
1121        $divisor  = $bytes->[11];
1122        my $mtb = $dividend / $divisor;
1123        printl("Medium time base", tns3($mtb));
1124
1125        $ctime = $bytes->[12] * $mtb;
1126        my $ddrclk = 2 * (1000 / $ctime);
1127        my $tbits = 1 << (($bytes->[8] & 7) + 3);
1128        my $pcclk = int ($ddrclk * $tbits / 8);
1129        $ddrclk = int ($ddrclk);
1130        printl("Maximum module speed", "${ddrclk}MHz (PC3-${pcclk})");
1131
1132# Size computation
1133
1134        my $cap =  ($bytes->[4]       & 15) + 28;
1135        $cap   +=  ($bytes->[8]       & 7)  + 3;
1136        $cap   -=  ($bytes->[7]       & 7)  + 2;
1137        $cap   -= 20 + 3;
1138        my $k   = (($bytes->[7] >> 3) & 31) + 1;
1139        printl("Size", ((1 << $cap) * $k) . " MB");
1140
1141        printl("Banks x Rows x Columns x Bits",
1142               join(' x ', 1 << ((($bytes->[4] >> 4) &  7) +  3),
1143                           ((($bytes->[5] >> 3) & 31) + 12),
1144                           ( ($bytes->[5]       &  7) +  9),
1145                           ( 1 << (($bytes->[8] &  7) + 3)) ));
1146        printl("Ranks", $k);
1147
1148        printl("SDRAM Device Width", (1 << (($bytes->[7] & 7) + 2))." bits");
1149
1150        my $taa;
1151        my $trcd;
1152        my $trp;
1153        my $tras;
1154
1155        $taa  = int($bytes->[16] / $bytes->[12]);
1156        $trcd = int($bytes->[18] / $bytes->[12]);
1157        $trp  = int($bytes->[20] / $bytes->[12]);
1158        $tras = int((($bytes->[21] >> 4) * 256 + $bytes->[22]) / $bytes->[12]);
1159
1160        printl("tCL-tRCD-tRP-tRAS", join("-", $taa, $trcd, $trp, $tras));
1161
1162# latencies
1163        my $highestCAS = 0;
1164        my %cas;
1165        my $ii;
1166        my $cas_sup = ($bytes->[15] << 8) + $bytes->[14];
1167        for ($ii = 0; $ii < 15; $ii++) {
1168                if ($cas_sup & (1 << $ii)) {
1169                        $highestCAS = $ii + 4;
1170                        $cas{$highestCAS}++;
1171                }
1172        }
1173        printl("Supported CAS Latencies (tCL)", cas_latencies(keys %cas));
1174
1175# more timing information
1176        prints("Timing Parameters");
1177
1178        printl("Minimum Write Recovery time (tWR)", tns3($bytes->[17] * $mtb));
1179        printl("Minimum Row Active to Row Active Delay (tRRD)",
1180                tns3($bytes->[19] * $mtb));
1181        printl("Minimum Active to Auto-Refresh Delay (tRC)",
1182                tns3((((($bytes->[21] >> 4) & 15) << 8) + $bytes->[23]) * $mtb));
1183        printl("Minimum Recovery Delay (tRFC)",
1184                tns3((($bytes->[25] << 8) + $bytes->[24]) * $mtb));
1185        printl("Minimum Write to Read CMD Delay (tWTR)",
1186                tns3($bytes->[26] * $mtb));
1187        printl("Minimum Read to Pre-charge CMD Delay (tRTP)",
1188                tns3($bytes->[27] * $mtb));
1189        printl("Minimum Four Activate Window Delay (tFAW)",
1190                tns3(((($bytes->[28] & 15) << 8) + $bytes->[29]) * $mtb));
1191
1192# miscellaneous stuff
1193        prints("Optional Features");
1194
1195        my $volts = "1.5V";
1196        if ($bytes->[6] & 1) {
1197                $volts .= " tolerant";
1198        }
1199        if ($bytes->[6] & 2) {
1200                $volts .= ", 1.35V ";
1201        }
1202        if ($bytes->[6] & 4) {
1203                $volts .= ", 1.2X V";
1204        }
1205        printl("Operable voltages", $volts);
1206        printl("RZQ/6 supported?", ($bytes->[30] & 1) ? "Yes" : "No");
1207        printl("RZQ/7 supported?", ($bytes->[30] & 2) ? "Yes" : "No");
1208        printl("DLL-Off Mode supported?", ($bytes->[30] & 128) ? "Yes" : "No");
1209        printl("Operating temperature range", sprintf "0-%dC",
1210                ($bytes->[31] & 1) ? 95 : 85);
1211        printl("Refresh Rate in extended temp range",
1212                ($bytes->[31] & 2) ? "2X" : "1X");
1213        printl("Auto Self-Refresh?", ($bytes->[31] & 4) ? "Yes" : "No");
1214        printl("On-Die Thermal Sensor readout?",
1215                ($bytes->[31] & 8) ? "Yes" : "No");
1216        printl("Partial Array Self-Refresh?",
1217                ($bytes->[31] & 128) ? "Yes" : "No");
1218        printl("Thermal Sensor Accuracy",
1219                ($bytes->[32] & 128) ? sprintf($bytes->[32] & 127) :
1220                                        "Not implemented");
1221        printl("SDRAM Device Type",
1222                ($bytes->[33] & 128) ? sprintf($bytes->[33] & 127) :
1223                                        "Standard Monolithic");
1224        if ($bytes->[3] >= 1 && $bytes->[3] <= 6) {
1225
1226                prints("Physical Characteristics");
1227                printl("Module Height (mm)", ($bytes->[60] & 31) + 15);
1228                printl("Module Thickness (mm)", sprintf("%d front, %d back",
1229                                                ($bytes->[61] & 15) + 1,
1230                                                (($bytes->[61] >> 4) & 15) +1));
1231                printl("Module Width (mm)", ($bytes->[3] <= 2) ? 133.5 :
1232                                        ($bytes->[3] == 3) ? 67.6 : "TBD");
1233
1234                my $alphabet = "ABCDEFGHJKLMNPRTUVWY";
1235                my $ref = $bytes->[62] & 31;
1236                my $ref_card;
1237                if ($ref == 31) {
1238                        $ref_card = "ZZ";
1239                } else {
1240                        if ($bytes->[62] & 128) {
1241                                $ref += 31;
1242                        }
1243                        if ($ref < length $alphabet) {
1244                                $ref_card = substr $alphabet, $ref, 1;
1245                        } else {
1246                                my $ref1 = int($ref / (length $alphabet));
1247                                $ref -= (length $alphabet) * $ref1;
1248                                $ref_card = (substr $alphabet, $ref1, 1) .
1249                                            (substr $alphabet, $ref, 1);
1250                        }
1251                }
1252                printl("Module Reference Card", $ref_card);
1253        }
1254        if ($bytes->[3] == 1 || $bytes->[3] == 5) {
1255                prints("Registered DIMM");
1256
1257                my @rows = ("Undefined", 1, 2, 4);
1258                printl("# DRAM Rows", $rows[($bytes->[63] >> 2) & 3]);
1259                printl("# Registers", $rows[$bytes->[63] & 3]);
1260                printl("Register manufacturer",
1261                        manufacturer_ddr3($bytes->[65], $bytes->[66]));
1262                printl("Register device type",
1263                                (($bytes->[68] & 7) == 0) ? "SSTE32882" :
1264                                        "Undefined");
1265                printl("Register revision", sprintf("0x%.2X", $bytes->[67]));
1266                printl("Heat spreader characteristics",
1267                                ($bytes->[64] < 128) ? "Not incorporated" :
1268                                        sprintf("%.2X", ($bytes->[64] & 127)));
1269                my $regs;
1270                for (my $i = 0; $i < 8; $i++) {
1271                        $regs = sprintf("SSTE32882 RC%d/RC%d",
1272                                        $i * 2, $i * 2 + 1);
1273                        printl($regs, sprintf("%.2X", $bytes->[$i + 69]));
1274                }
1275        }
1276}
1277
1278# Parameter: EEPROM bytes 0-127 (using 4-5)
1279sub decode_direct_rambus($)
1280{
1281        my $bytes = shift;
1282
1283#size computation
1284        prints("Memory Characteristics");
1285
1286        my $ii;
1287
1288        $ii = ($bytes->[4] & 0x0f) + ($bytes->[4] >> 4) + ($bytes->[5] & 0x07) - 13;
1289
1290        if ($ii > 0 && $ii < 16) {
1291                printl("Size", (1 << $ii) . " MB");
1292        } else {
1293                printl("Size", sprintf("INVALID: 0x%02x, 0x%02x",
1294                                       $bytes->[4], $bytes->[5]));
1295        }
1296}
1297
1298# Parameter: EEPROM bytes 0-127 (using 3-5)
1299sub decode_rambus($)
1300{
1301        my $bytes = shift;
1302
1303#size computation
1304        prints("Memory Characteristics");
1305
1306        my $ii;
1307
1308        $ii = ($bytes->[3] & 0x0f) + ($bytes->[3] >> 4) + ($bytes->[5] & 0x07) - 13;
1309
1310        if ($ii > 0 && $ii < 16) {
1311                printl("Size", (1 << $ii) . " MB");
1312        } else {
1313                printl("Size", "INVALID: " . sprintf("0x%02x, 0x%02x",
1314                                               $bytes->[3], $bytes->[5]));
1315        }
1316}
1317
1318%decode_callback = (
1319        "SDR SDRAM"     => \&decode_sdr_sdram,
1320        "DDR SDRAM"     => \&decode_ddr_sdram,
1321        "DDR2 SDRAM"    => \&decode_ddr2_sdram,
1322        "DDR3 SDRAM"    => \&decode_ddr3_sdram,
1323        "Direct Rambus" => \&decode_direct_rambus,
1324        "Rambus"        => \&decode_rambus,
1325);
1326
1327# Parameter: Manufacturing year/week bytes
1328sub manufacture_date($$)
1329{
1330        my ($year, $week) = @_;
1331
1332        # In theory the year and week are in BCD format, but
1333        # this is not always true in practice :(
1334        if (($year & 0xf0) <= 0x90 && ($year & 0x0f) <= 0x09
1335         && ($week & 0xf0) <= 0x90 && ($week & 0x0f) <= 0x09) {
1336                # Note that this heuristic will break in year 2080
1337                return sprintf("%d%02X-W%02X",
1338                                $year >= 0x80 ? 19 : 20, $year, $week);
1339        # Fallback to binary format if it seems to make sense
1340        } elsif ($year <= 99 && $week >= 1 && $week <= 53) {
1341                return sprintf("%d%02d-W%02d",
1342                                $year >= 80 ? 19 : 20, $year, $week);
1343        } else {
1344                return sprintf("0x%02X%02X", $year, $week);
1345        }
1346}
1347
1348sub printl_mfg_location_code($)
1349{
1350        my $code = shift;
1351        my $letter = chr($code);
1352
1353        # Try the location code as ASCII first, as earlier specifications
1354        # suggested this. As newer specifications don't mention it anymore,
1355        # we still fall back to binary.
1356        printl_cond(spd_written($code), "Manufacturing Location Code",
1357                    $letter =~ m/^[\w\d]$/ ? $letter : sprintf("0x%.2X", $code));
1358}
1359
1360sub printl_mfg_assembly_serial(@)
1361{
1362        printl_cond(spd_written(@_), "Assembly Serial Number",
1363                    sprintf("0x%02X%02X%02X%02X", @_));
1364}
1365
1366# Parameter: EEPROM bytes 0-175 (using 117-149)
1367sub decode_ddr3_mfg_data($)
1368{
1369        my $bytes = shift;
1370
1371        prints("Manufacturer Data");
1372
1373        printl("Module Manufacturer",
1374               manufacturer_ddr3($bytes->[117], $bytes->[118]));
1375
1376        if (spd_written(@{$bytes}[148..149])) {
1377                printl("DRAM Manufacturer",
1378                       manufacturer_ddr3($bytes->[148], $bytes->[149]));
1379        }
1380
1381        printl_mfg_location_code($bytes->[119]);
1382
1383        if (spd_written(@{$bytes}[120..121])) {
1384                printl("Manufacturing Date",
1385                       manufacture_date($bytes->[120], $bytes->[121]));
1386        }
1387
1388        printl_mfg_assembly_serial(@{$bytes}[122..125]);
1389
1390        printl("Part Number", part_number(@{$bytes}[128..145]));
1391
1392        if (spd_written(@{$bytes}[146..147])) {
1393                printl("Revision Code",
1394                       sprintf("0x%02X%02X", $bytes->[146], $bytes->[147]));
1395        }
1396}
1397
1398# Parameter: EEPROM bytes 0-127 (using 64-98)
1399sub decode_manufacturing_information($)
1400{
1401        my $bytes = shift;
1402        my ($temp, $extra);
1403
1404        prints("Manufacturing Information");
1405
1406        # $extra is a reference to an array containing up to
1407        # 7 extra bytes from the Manufacturer field. Sometimes
1408        # these bytes are filled with interesting data.
1409        ($temp, $extra) = manufacturer(@{$bytes}[64..71]);
1410        printl("Manufacturer", $temp);
1411        $temp = manufacturer_data(@{$extra});
1412        printl_cond(defined $temp, "Custom Manufacturer Data", $temp);
1413
1414        printl_mfg_location_code($bytes->[72]);
1415
1416        printl("Part Number", part_number(@{$bytes}[73..90]));
1417
1418        printl_cond(spd_written(@{$bytes}[91..92]), "Revision Code",
1419                    sprintf("0x%02X%02X", @{$bytes}[91..92]));
1420
1421        printl_cond(spd_written(@{$bytes}[93..94]), "Manufacturing Date",
1422               manufacture_date($bytes->[93], $bytes->[94]));
1423
1424        printl_mfg_assembly_serial(@{$bytes}[95..98]);
1425}
1426
1427# Parameter: EEPROM bytes 0-127 (using 126-127)
1428sub decode_intel_spec_freq($)
1429{
1430        my $bytes = shift;
1431        my $temp;
1432
1433        prints("Intel Specification");
1434
1435        if ($bytes->[126] == 0x66) { $temp = "66MHz"; }
1436        elsif ($bytes->[126] == 100) { $temp = "100MHz or 133MHz"; }
1437        elsif ($bytes->[126] == 133) { $temp = "133MHz"; }
1438        else { $temp = "Undefined!"; }
1439        printl("Frequency", $temp);
1440
1441        $temp = "";
1442        if ($bytes->[127] & 1) { $temp .= "Intel Concurrent Auto-precharge\n"; }
1443        if ($bytes->[127] & 2) { $temp .= "CAS Latency = 2\n"; }
1444        if ($bytes->[127] & 4) { $temp .= "CAS Latency = 3\n"; }
1445        if ($bytes->[127] & 8) { $temp .= "Junction Temp A (100 degrees C)\n"; }
1446        else { $temp .= "Junction Temp B (90 degrees C)\n"; }
1447        if ($bytes->[127] & 16) { $temp .= "CLK 3 Connected\n"; }
1448        if ($bytes->[127] & 32) { $temp .= "CLK 2 Connected\n"; }
1449        if ($bytes->[127] & 64) { $temp .= "CLK 1 Connected\n"; }
1450        if ($bytes->[127] & 128) { $temp .= "CLK 0 Connected\n"; }
1451        if (($bytes->[127] & 192) == 192) { $temp .= "Double-sided DIMM\n"; }
1452        elsif (($bytes->[127] & 192) != 0) { $temp .= "Single-sided DIMM\n"; }
1453        printl("Details for 100MHz Support", $temp);
1454}
1455
1456# Read various hex dump style formats: hexdump, hexdump -C, i2cdump, eeprog
1457# note that normal 'hexdump' format on a little-endian system byte-swaps
1458# words, using hexdump -C is better.
1459sub read_hexdump($)
1460{
1461        my $addr = 0;
1462        my $repstart = 0;
1463        my @bytes;
1464        my $header = 1;
1465        my $word = 0;
1466
1467        # Look in the cache first
1468        return @{$hexdump_cache{$_[0]}} if exists $hexdump_cache{$_[0]};
1469
1470        open F, '<', $_[0] or die "Unable to open: $_[0]";
1471        while (<F>) {
1472                chomp;
1473                if (/^\*$/) {
1474                        $repstart = $addr;
1475                        next;
1476                }
1477                /^(?:0000 )?([a-f\d]{2,8}):?\s+((:?[a-f\d]{4}\s*){8}|(:?[a-f\d]{2}\s*){16})/i ||
1478                /^(?:0000 )?([a-f\d]{2,8}):?\s*$/i;
1479                next if (!defined $1 && $header);               # skip leading unparsed lines
1480
1481                defined $1 or die "Unable to parse input";
1482                $header = 0;
1483
1484                $addr = hex $1;
1485                if ($repstart) {
1486                        @bytes[$repstart .. ($addr-1)] =
1487                                (@bytes[($repstart-16)..($repstart-1)]) x (($addr-$repstart)/16);
1488                        $repstart = 0;
1489                }
1490                last unless defined $2;
1491                foreach (split(/\s+/, $2)) {
1492                        if (/^(..)(..)$/) {
1493                                $word |= 1;
1494                                if ($use_hexdump eq LITTLEENDIAN) {
1495                                        $bytes[$addr++] = hex($2);
1496                                        $bytes[$addr++] = hex($1);
1497                                } else {
1498                                        $bytes[$addr++] = hex($1);
1499                                        $bytes[$addr++] = hex($2);
1500                                }
1501                        } else {
1502                                $bytes[$addr++] = hex($_);
1503                        }
1504                }
1505        }
1506        close F;
1507        $header and die "Unable to parse any data from hexdump '$_[0]'";
1508        $word and printc("Using $use_hexdump 16-bit hex dump");
1509
1510        # Cache the data for later use
1511        $hexdump_cache{$_[0]} = \@bytes;
1512        return @bytes;
1513}
1514
1515# Returns the (total, used) number of bytes in the EEPROM,
1516# assuming it is a non-Rambus SPD EEPROM.
1517sub spd_sizes($)
1518{
1519        my $bytes = shift;
1520
1521        if ($bytes->[2] >= 9) {
1522                # For FB-DIMM and newer, decode number of bytes written
1523                my $spd_len = ($bytes->[0] >> 4) & 7;
1524                my $size = 64 << ($bytes->[0] & 15);
1525                if ($spd_len == 0) {
1526                        return ($size, 128);
1527                } elsif ($spd_len == 1) {
1528                        return ($size, 176);
1529                } elsif ($spd_len == 2) {
1530                        return ($size, 256);
1531                } else {
1532                        return (64, 64);
1533                }
1534        } else {
1535                my $size;
1536                if ($bytes->[1] <= 14) {
1537                        $size = 1 << $bytes->[1];
1538                } elsif ($bytes->[1] == 0) {
1539                        $size = "RFU";
1540                } else { $size = "ERROR!" }
1541
1542                return ($size, ($bytes->[0] < 64) ? 64 : $bytes->[0]);
1543        }
1544}
1545
1546# Read bytes from SPD-EEPROM
1547# Note: offset must be a multiple of 16!
1548sub readspd($$$)
1549{
1550        my ($offset, $size, $dimm_i) = @_;
1551        my @bytes;
1552        if ($use_hexdump) {
1553                @bytes = read_hexdump($dimm_i);
1554                return @bytes[$offset..($offset + $size - 1)];
1555        } elsif ($use_sysfs) {
1556                # Kernel 2.6 with sysfs
1557                sysopen(HANDLE, "$dimm_i/eeprom", O_RDONLY)
1558                        or die "Cannot open $dimm_i/eeprom";
1559                binmode HANDLE;
1560                sysseek(HANDLE, $offset, SEEK_SET)
1561                        or die "Cannot seek $dimm_i/eeprom";
1562                sysread(HANDLE, my $eeprom, $size)
1563                        or die "Cannot read $dimm_i/eeprom";
1564                close HANDLE;
1565                @bytes = unpack("C*", $eeprom);
1566        } else {
1567                # Kernel 2.4 with procfs
1568                for my $i (0 .. ($size-1)/16) {
1569                        my $hexoff = sprintf('%02x', $offset + $i * 16);
1570                        push @bytes, split(" ", `cat $dimm_i/$hexoff`);
1571                }
1572        }
1573        return @bytes;
1574}
1575
1576# Calculate and verify checksum of first 63 bytes
1577sub checksum($)
1578{
1579        my $bytes = shift;
1580        my $dimm_checksum = 0;
1581        local $_;
1582
1583        $dimm_checksum += $bytes->[$_] foreach (0 .. 62);
1584        $dimm_checksum &= 0xff;
1585
1586        return ("EEPROM Checksum of bytes 0-62",
1587                ($bytes->[63] == $dimm_checksum) ? 1 : 0,
1588                sprintf('0x%02X', $bytes->[63]),
1589                sprintf('0x%02X', $dimm_checksum));
1590}
1591
1592# Calculate and verify CRC
1593sub check_crc($)
1594{
1595        my $bytes = shift;
1596        my $crc = 0;
1597        my $crc_cover = $bytes->[0] & 0x80 ? 116 : 125;
1598        my $crc_ptr = 0;
1599        my $crc_bit;
1600
1601        while ($crc_ptr <= $crc_cover) {
1602                $crc = $crc ^ ($bytes->[$crc_ptr] << 8);
1603                for ($crc_bit = 0; $crc_bit < 8; $crc_bit++) {
1604                        if ($crc & 0x8000) {
1605                                $crc = ($crc << 1) ^ 0x1021;
1606                        } else {
1607                                $crc = $crc << 1
1608                        }
1609                }
1610                $crc_ptr++;
1611        }
1612        $crc &= 0xffff;
1613
1614        my $dimm_crc = ($bytes->[127] << 8) | $bytes->[126];
1615        return ("EEPROM CRC of bytes 0-$crc_cover",
1616                ($dimm_crc == $crc) ? 1 : 0,
1617                sprintf("0x%04X", $dimm_crc),
1618                sprintf("0x%04X", $crc));
1619}
1620
1621# Parse command-line
1622foreach (@ARGV) {
1623        if ($_ eq '-h' || $_ eq '--help') {
1624                print "Usage: $0 [-c] [-f [-b]] [-x|-X file [files..]]\n",
1625                        "       $0 -h\n\n",
1626                        "  -f, --format            Print nice html output\n",
1627                        "  -b, --bodyonly          Don't print html header\n",
1628                        "                          (useful for postprocessing the output)\n",
1629                        "      --side-by-side      Display all DIMMs side-by-side if possible\n",
1630                        "      --merge-cells       Merge neighbour cells with identical values\n",
1631                        "                          (side-by-side output only)\n",
1632                        "  -c, --checksum          Decode completely even if checksum fails\n",
1633                        "  -x,                     Read data from hexdump files\n",
1634                        "  -X,                     Same as -x except treat multibyte hex\n",
1635                        "                          data as little endian\n",
1636                        "  -h, --help              Display this usage summary\n";
1637                print <<"EOF";
1638
1639Hexdumps can be the output from hexdump, hexdump -C, i2cdump, eeprog and
1640likely many other progams producing hex dumps of one kind or another.  Note
1641that the default output of "hexdump" will be byte-swapped on little-endian
1642systems and you must use -X instead of -x, otherwise the dump will not be
1643parsed correctly.  It is better to use "hexdump -C", which is not ambiguous.
1644EOF
1645                exit;
1646        }
1647
1648        if ($_ eq '-f' || $_ eq '--format') {
1649                $opt_html = 1;
1650                next;
1651        }
1652        if ($_ eq '-b' || $_ eq '--bodyonly') {
1653                $opt_bodyonly = 1;
1654                next;
1655        }
1656        if ($_ eq '--side-by-side') {
1657                $opt_side_by_side = 1;
1658                next;
1659        }
1660        if ($_ eq '--merge-cells') {
1661                $opt_merge = 1;
1662                next;
1663        }
1664        if ($_ eq '-c' || $_ eq '--checksum') {
1665                $opt_igncheck = 1;
1666                next;
1667        }
1668        if ($_ eq '-x') {
1669                $use_hexdump = BIGENDIAN;
1670                next;
1671        }
1672        if ($_ eq '-X') {
1673                $use_hexdump = LITTLEENDIAN;
1674                next;
1675        }
1676
1677        if (m/^-/) {
1678                print STDERR "Unrecognized option $_\n";
1679                exit;
1680        }
1681
1682        push @dimm, { eeprom => $_, file => $_ } if $use_hexdump;
1683}
1684
1685if ($opt_html && !$opt_bodyonly) {
1686        print "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n",
1687              "<html><head>\n",
1688                  "\t<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n",
1689                  "\t<title>PC DIMM Serial Presence Detect Tester/Decoder Output</title>\n",
1690                  "</head><body>\n";
1691}
1692
1693printc("decode-dimms version $revision");
1694printh('Memory Serial Presence Detect Decoder',
1695'By Philip Edelbrock, Christian Zuckschwerdt, Burkart Lingner,
1696Jean Delvare, Trent Piepho and others');
1697
1698
1699sub get_dimm_list
1700{
1701        my ($dir, $file, @files);
1702
1703        if ($use_sysfs) {
1704                $dir = '/sys/bus/i2c/drivers/eeprom';
1705        } else {
1706                $dir = '/proc/sys/dev/sensors';
1707        }
1708
1709        if (opendir(local *DIR, $dir)) {
1710                while (defined($file = readdir(DIR))) {
1711                        next if $use_sysfs && $file !~ /^\d+-[\da-f]+$/i;
1712                        next if !$use_sysfs && $file !~ /^eeprom-/;
1713                        push @files, { eeprom => "$file",
1714                                       file => "$dir/$file" };
1715                }
1716                close(DIR);
1717                return sort { $a->{file} cmp $b->{file} } @files;
1718        } elsif (! -d '/sys/module/eeprom') {
1719                print "No EEPROM found, are you sure the eeprom module is loaded?\n";
1720                exit;
1721        }
1722}
1723
1724# @dimm is a list of hashes. There's one hash for each EEPROM we found.
1725# Each hash has the following keys:
1726#  * eeprom: Name of the eeprom data file
1727#  * file: Full path to the eeprom data file
1728#  * bytes: The EEPROM data (array)
1729#  * is_rambus: Whether this is a RAMBUS DIMM or not (boolean)
1730#  * chk_label: The label to display for the checksum or CRC
1731#  * chk_valid: Whether the checksum or CRC is valid or not (boolean)
1732#  * chk_spd: The checksum or CRC value found in the EEPROM
1733#  * chk_calc: The checksum or CRC computed from the EEPROM data
1734# Keys are added over time.
1735@dimm = get_dimm_list() unless $use_hexdump;
1736
1737for my $i (0 .. $#dimm) {
1738        my @bytes = readspd(0, 128, $dimm[$i]->{file});
1739        $dimm[$i]->{bytes} = \@bytes;
1740        $dimm[$i]->{is_rambus} = $bytes[0] < 4;         # Simple heuristic
1741        if ($dimm[$i]->{is_rambus} || $bytes[2] < 9) {
1742                ($dimm[$i]->{chk_label}, $dimm[$i]->{chk_valid},
1743                 $dimm[$i]->{chk_spd}, $dimm[$i]->{chk_calc}) =
1744                        checksum(\@bytes);
1745        } else {
1746                ($dimm[$i]->{chk_label}, $dimm[$i]->{chk_valid},
1747                 $dimm[$i]->{chk_spd}, $dimm[$i]->{chk_calc}) =
1748                        check_crc(\@bytes);
1749        }
1750}
1751
1752# Checksum or CRC validation
1753if (!$opt_igncheck) {
1754        for (my $i = 0; $i < @dimm; ) {
1755                if ($dimm[$i]->{chk_valid}) {
1756                        $i++;
1757                } else {
1758                        splice(@dimm, $i, 1);
1759                }
1760        }
1761}
1762
1763# Process the valid entries
1764for $current (0 .. $#dimm) {
1765        my @bytes = @{$dimm[$current]->{bytes}};
1766
1767        if ($opt_side_by_side) {
1768                printl("Decoding EEPROM", $dimm[$current]->{eeprom});
1769        }
1770
1771        if (!$use_hexdump) {
1772                if ($dimm[$current]->{file} =~ /-([\da-f]+)$/i) {
1773                        my $dimm_num = hex($1) - 0x50 + 1;
1774                        if ($dimm_num >= 1 && $dimm_num <= 8) {
1775                                printl("Guessing DIMM is in", "bank $dimm_num");
1776                        }
1777                }
1778        }
1779
1780# Decode first 3 bytes (0-2)
1781        prints("SPD EEPROM Information");
1782
1783        printl($dimm[$current]->{chk_label}, ($dimm[$current]->{chk_valid} ?
1784                sprintf("OK (%s)", $dimm[$current]->{chk_calc}) :
1785                sprintf("Bad\n(found %s, calculated %s)",
1786                        $dimm[$current]->{chk_spd}, $dimm[$current]->{chk_calc})));
1787
1788        my $temp;
1789        if ($dimm[$current]->{is_rambus}) {
1790                if ($bytes[0] == 1) { $temp = "0.7"; }
1791                elsif ($bytes[0] == 2) { $temp = "1.0"; }
1792                elsif ($bytes[0] == 0) { $temp = "Invalid"; }
1793                else { $temp = "Reserved"; }
1794                printl("SPD Revision", $temp);
1795        } else {
1796                my ($spd_size, $spd_used) = spd_sizes(\@bytes);
1797                printl("# of bytes written to SDRAM EEPROM", $spd_used);
1798                printl("Total number of bytes in EEPROM", $spd_size);
1799
1800                # If there's more data than what we've read, let's
1801                # read it now.  DDR3 will need this data.
1802                if ($spd_used > @bytes) {
1803                        push (@bytes,
1804                              readspd(@bytes, $spd_used - @bytes,
1805                                      $dimm[$current]->{file}));
1806                }
1807        }
1808
1809        my $type = sprintf("Unknown (0x%02x)", $bytes[2]);
1810        if ($dimm[$current]->{is_rambus}) {
1811                if ($bytes[2] == 1) { $type = "Direct Rambus"; }
1812                elsif ($bytes[2] == 17) { $type = "Rambus"; }
1813        } else {
1814                my @type_list = (
1815                        "Reserved", "FPM DRAM",         # 0, 1
1816                        "EDO", "Pipelined Nibble",      # 2, 3
1817                        "SDR SDRAM", "Multiplexed ROM", # 4, 5
1818                        "DDR SGRAM", "DDR SDRAM",       # 6, 7
1819                        "DDR2 SDRAM", "FB-DIMM",        # 8, 9
1820                        "FB-DIMM Probe", "DDR3 SDRAM",  # 10, 11
1821                );
1822                if ($bytes[2] < @type_list) {
1823                        $type = $type_list[$bytes[2]];
1824                }
1825        }
1826        printl("Fundamental Memory type", $type);
1827
1828# Decode next 61 bytes (3-63, depend on memory type)
1829        $decode_callback{$type}->(\@bytes)
1830                if exists $decode_callback{$type};
1831
1832        if ($type eq "DDR3 SDRAM") {
1833                # Decode DDR3-specific manufacturing data in bytes
1834                # 117-149
1835                decode_ddr3_mfg_data(\@bytes)
1836        } else {
1837                # Decode next 35 bytes (64-98, common to most
1838                # memory types)
1839                decode_manufacturing_information(\@bytes);
1840        }
1841
1842# Next 27 bytes (99-125) are manufacturer specific, can't decode
1843
1844# Last 2 bytes (126-127) are reserved, Intel used them as an extension
1845        if ($type eq "SDR SDRAM") {
1846                decode_intel_spec_freq(\@bytes);
1847        }
1848}
1849
1850# Side-by-side output format is only possible if all DIMMs have a similar
1851# output structure
1852if ($opt_side_by_side) {
1853        for $current (1 .. $#dimm) {
1854                my @ref_output = @{$dimm[0]->{output}};
1855                my @test_output = @{$dimm[$current]->{output}};
1856                my $line;
1857
1858                if (scalar @ref_output != scalar @test_output) {
1859                        $opt_side_by_side = 0;
1860                        last;
1861                }
1862
1863                for ($line = 0; $line < @ref_output; $line++) {
1864                        my ($ref_func, $ref_label, @ref_dummy) = @{$ref_output[$line]};
1865                        my ($test_func, $test_label, @test_dummy) = @{$test_output[$line]};
1866
1867                        if ($ref_func != $test_func || $ref_label ne $test_label) {
1868                                $opt_side_by_side = 0;
1869                                last;
1870                        }
1871                }
1872        }
1873
1874        if (!$opt_side_by_side) {
1875                printc("Side-by-side output only possible if all DIMMS are similar\n");
1876
1877                # Discard "Decoding EEPROM" entry from all outputs
1878                for $current (0 .. $#dimm) {
1879                        shift(@{$dimm[$current]->{output}});
1880                }
1881        }
1882}
1883
1884# Find out the longest value string to adjust the column width
1885# Note: this could be improved a bit by not taking into account strings
1886# which will end up being merged.
1887$sbs_col_width = 15;
1888if ($opt_side_by_side && !$opt_html) {
1889        for $current (0 .. $#dimm) {
1890                my @output = @{$dimm[$current]->{output}};
1891                my $line;
1892                my @strings;
1893
1894                for ($line = 0; $line < @output; $line++) {
1895                        my ($func, $label, $value) = @{$output[$line]};
1896                        push @strings, split("\n", $value) if defined $value;
1897                }
1898
1899                foreach $line (@strings) {
1900                        my $len = length($line);
1901                        $sbs_col_width = $len if $len > $sbs_col_width;
1902                }
1903        }
1904}
1905
1906# Print the decoded information for all DIMMs
1907for $current (0 .. $#dimm) {
1908        if ($opt_side_by_side) {
1909                print "\n\n";
1910        } else {
1911                print "<b><u>" if $opt_html;
1912                printl2("\n\nDecoding EEPROM", $dimm[$current]->{file});
1913                print "</u></b>" if $opt_html;
1914        }
1915        print "<table border=1>\n" if $opt_html;
1916
1917        my @output = @{$dimm[$current]->{output}};
1918        for (my $line = 0; $line < @output; $line++) {
1919                my ($func, @param) = @{$output[$line]};
1920
1921                if ($opt_side_by_side) {
1922                        foreach ($current+1 .. $#dimm) {
1923                                my @xoutput = @{$dimm[$_]->{output}};
1924                                if (@{$xoutput[$line]} == 3) {
1925                                        # Line with data, stack all values
1926                                        push @param, @{$xoutput[$line]}[2];
1927                                } else {
1928                                        # Separator, make it span
1929                                        push @param, scalar @dimm;
1930                                }
1931                        }
1932                }
1933
1934                $func->(@param);
1935        }
1936
1937        print "</table>\n" if $opt_html;
1938        last if $opt_side_by_side;
1939}
1940printl2("\n\nNumber of SDRAM DIMMs detected and decoded", scalar @dimm);
1941
1942print "</body></html>\n" if ($opt_html && !$opt_bodyonly);
Note: See TracBrowser for help on using the browser.