/ util / scripts / decode_spd.sh
decode_spd.sh
 1  #!/usr/bin/env bash
 2  #
 3  #
 4  # SPDX-License-Identifier: GPL-2.0-only
 5  
 6  # Parses spd hex files and outputs the contents in various formats
 7  #
 8  #
 9  # Outputs csv, set, and json in same folder as SPD_HEX_FILE
10  #
11  # Example:
12  # 	decode_spd.sh ../../src/mainboard/google/zork/spd/micron-MT40A512M16TB-062E-J.spd.hex
13  #
14  # Outputs ../../src/mainboard/google/zork/spd/micron-MT40A512M16TB-062E-J.spd.{json|csv|set}
15  #
16  # TODO: This script assumes bincfg binary is at ../bincfg/bincfg (which is the
17  #	result of running the bincfg make), and the specs are at
18  #	../bincfg/*.spec. This dependency should be made more resilliant and
19  #	configurable.
20  
21  set -e
22  
23  function read8 () {
24  	echo $(( 16#$(xxd -s "${2}" -l 1 -p "${1}") ))
25  }
26  
27  for file in "$@"
28  do
29  	bintmp=$(mktemp)
30  	outfile="${file%.hex}.set"
31  
32  	echo "Decoding ${file}, outputting to ${outfile}"
33  
34  	grep -v '^#' "${file}" | xxd -r -p - "${bintmp}"
35  	dram_type=$(read8 "${bintmp}" 2)
36  	if [ ! "${dram_type}" -eq 12 ]
37  	then
38  		#TODO: Handle other dram types
39  		printf "Error: Expecting dram4 (12), got %d\n" "${dram_type}"
40  		continue
41  	fi
42  
43  	revision=$(read8 "${bintmp}" 1)
44  	if [ ! "${revision}" -eq $((0x13)) ]
45  	then
46  		printf "Warning: Expecting revision 0x13, got 0x%x.\n" "${revision}"
47  	fi
48  
49  	module_type=$(read8 "${bintmp}" 3)
50  	case "${module_type}" in
51  	1) # RDIMM
52  		spec="../bincfg/ddr4_registered_spd_512.spec"
53  	;;
54  	2 | 3) #UDIMM | SO-DIMM
55  		spec="../bincfg/ddr4_unbuffered_spd_512.spec"
56  	;;
57  	* )
58  		printf "Error: Unhandled module type %d.\n" "${module_type}"
59  	;;
60  	esac
61  
62  	../bincfg/bincfg -d "${spec}" "${bintmp}" "${outfile}"
63  	grep -v '^#' "${outfile}" | sed -e 's/ = \([^,]\+\)/: "\1"/g' \
64  		> "${file%.hex}.json"
65  	grep -v -e '^#' -e '^{' -e '^}' "${outfile}" | sed -e 's/=/,/g' \
66  		> "${file%.hex}.csv"
67  done