1 /**
2  * Describes functions for converting memory error CPER sections from binary and JSON format
3  * into an intermediate format.
4  *
5  * Author: Lawrence.Tang@arm.com
6  **/
7 #include <stdio.h>
8 #include "json.h"
9 #include "../edk/Cper.h"
10 #include "../cper-utils.h"
11 #include "cper-section-memory.h"
12 
13 //Converts a single memory error CPER section into JSON IR.
14 json_object* cper_section_platform_memory_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
15 {
16     EFI_PLATFORM_MEMORY_ERROR_DATA* memory_error = (EFI_PLATFORM_MEMORY_ERROR_DATA*)section;
17     json_object* section_ir = json_object_new_object();
18 
19     //Validation bitfield.
20     json_object* validation = bitfield_to_ir(memory_error->ValidFields, 22, MEMORY_ERROR_VALID_BITFIELD_NAMES);
21     json_object_object_add(section_ir, "validationBits", validation);
22 
23     //Error status.
24     json_object* error_status = cper_generic_error_status_to_ir(&memory_error->ErrorStatus);
25     json_object_object_add(section_ir, "errorStatus", error_status);
26 
27     //Bank.
28     json_object* bank = json_object_new_object();
29     if ((memory_error->ValidFields >> 5) & 0x1)
30     {
31         //Entire bank address mode.
32         json_object_object_add(bank, "value", json_object_new_uint64(memory_error->Bank));
33     }
34     else
35     {
36         //Address/group address mode.
37         json_object_object_add(bank, "address", json_object_new_uint64(memory_error->Bank & 0xFF));
38         json_object_object_add(bank, "group", json_object_new_uint64(memory_error->Bank >> 8));
39     }
40     json_object_object_add(section_ir, "bank", bank);
41 
42     //Memory error type.
43     json_object* memory_error_type = integer_to_readable_pair(memory_error->ErrorType, 16,
44         MEMORY_ERROR_TYPES_KEYS,
45         MEMORY_ERROR_TYPES_VALUES,
46         "Unknown (Reserved)");
47     json_object_object_add(section_ir, "memoryErrorType", memory_error_type);
48 
49     //"Extended" row/column indication field + misc.
50     json_object* extended = json_object_new_object();
51     json_object_object_add(extended, "rowBit16", json_object_new_boolean(memory_error->Extended & 0b1));
52     json_object_object_add(extended, "rowBit17", json_object_new_boolean((memory_error->Extended >> 1) & 0b1));
53     json_object_object_add(extended, "chipIdentification", json_object_new_int(memory_error->Extended >> 5));
54     json_object_object_add(section_ir, "extended", extended);
55 
56     //Miscellaneous numeric fields.
57     json_object_object_add(section_ir, "physicalAddress", json_object_new_uint64(memory_error->PhysicalAddress));
58     json_object_object_add(section_ir, "physicalAddressMask", json_object_new_uint64(memory_error->PhysicalAddressMask));
59     json_object_object_add(section_ir, "node", json_object_new_uint64(memory_error->Node));
60     json_object_object_add(section_ir, "card", json_object_new_uint64(memory_error->Card));
61     json_object_object_add(section_ir, "moduleRank", json_object_new_uint64(memory_error->ModuleRank));
62     json_object_object_add(section_ir, "device", json_object_new_uint64(memory_error->Device));
63     json_object_object_add(section_ir, "row", json_object_new_uint64(memory_error->Row));
64     json_object_object_add(section_ir, "column", json_object_new_uint64(memory_error->Column));
65     json_object_object_add(section_ir, "bitPosition", json_object_new_uint64(memory_error->BitPosition));
66     json_object_object_add(section_ir, "requestorID", json_object_new_uint64(memory_error->RequestorId));
67     json_object_object_add(section_ir, "responderID", json_object_new_uint64(memory_error->ResponderId));
68     json_object_object_add(section_ir, "targetID", json_object_new_uint64(memory_error->TargetId));
69     json_object_object_add(section_ir, "rankNumber", json_object_new_uint64(memory_error->RankNum));
70     json_object_object_add(section_ir, "cardSmbiosHandle", json_object_new_uint64(memory_error->CardHandle));
71     json_object_object_add(section_ir, "moduleSmbiosHandle", json_object_new_uint64(memory_error->ModuleHandle));
72 
73     return section_ir;
74 }
75 
76 //Converts a single memory error 2 CPER section into JSON IR.
77 json_object* cper_section_platform_memory2_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
78 {
79     EFI_PLATFORM_MEMORY2_ERROR_DATA* memory_error = (EFI_PLATFORM_MEMORY2_ERROR_DATA*)section;
80     json_object* section_ir = json_object_new_object();
81 
82     //Validation bits.
83     json_object* validation = bitfield_to_ir(memory_error->ValidFields, 22, MEMORY_ERROR_2_VALID_BITFIELD_NAMES);
84     json_object_object_add(section_ir, "validationBits", validation);
85 
86     //Error status.
87     json_object* error_status = cper_generic_error_status_to_ir(&memory_error->ErrorStatus);
88     json_object_object_add(section_ir, "errorStatus", error_status);
89 
90     //Bank.
91     json_object* bank = json_object_new_object();
92     if ((memory_error->ValidFields >> 5) & 0x1)
93     {
94         //Entire bank address mode.
95         json_object_object_add(bank, "value", json_object_new_uint64(memory_error->Bank));
96     }
97     else
98     {
99         //Address/group address mode.
100         json_object_object_add(bank, "address", json_object_new_uint64(memory_error->Bank & 0xFF));
101         json_object_object_add(bank, "group", json_object_new_uint64(memory_error->Bank >> 8));
102     }
103     json_object_object_add(section_ir, "bank", bank);
104 
105     //Memory error type.
106     json_object* memory_error_type = integer_to_readable_pair(memory_error->MemErrorType, 16,
107         MEMORY_ERROR_TYPES_KEYS,
108         MEMORY_ERROR_TYPES_VALUES,
109         "Unknown (Reserved)");
110     json_object_object_add(section_ir, "memoryErrorType", memory_error_type);
111 
112     //Status.
113     json_object* status = json_object_new_object();
114     json_object_object_add(status, "value", json_object_new_int(memory_error->Status));
115     json_object_object_add(status, "state", json_object_new_string(memory_error->Status & 0b1 == 0 ? "Corrected" : "Uncorrected"));
116     json_object_object_add(section_ir, "status", status);
117 
118     //Miscellaneous numeric fields.
119     json_object_object_add(section_ir, "physicalAddress", json_object_new_uint64(memory_error->PhysicalAddress));
120     json_object_object_add(section_ir, "physicalAddressMask", json_object_new_uint64(memory_error->PhysicalAddressMask));
121     json_object_object_add(section_ir, "node", json_object_new_uint64(memory_error->Node));
122     json_object_object_add(section_ir, "card", json_object_new_uint64(memory_error->Card));
123     json_object_object_add(section_ir, "module", json_object_new_uint64(memory_error->Module));
124     json_object_object_add(section_ir, "device", json_object_new_uint64(memory_error->Device));
125     json_object_object_add(section_ir, "row", json_object_new_uint64(memory_error->Row));
126     json_object_object_add(section_ir, "column", json_object_new_uint64(memory_error->Column));
127     json_object_object_add(section_ir, "rank", json_object_new_uint64(memory_error->Rank));
128     json_object_object_add(section_ir, "bitPosition", json_object_new_uint64(memory_error->BitPosition));
129     json_object_object_add(section_ir, "chipID", json_object_new_uint64(memory_error->ChipId));
130     json_object_object_add(section_ir, "requestorID", json_object_new_uint64(memory_error->RequestorId));
131     json_object_object_add(section_ir, "responderID", json_object_new_uint64(memory_error->ResponderId));
132     json_object_object_add(section_ir, "targetID", json_object_new_uint64(memory_error->TargetId));
133     json_object_object_add(section_ir, "cardSmbiosHandle", json_object_new_uint64(memory_error->CardHandle));
134     json_object_object_add(section_ir, "moduleSmbiosHandle", json_object_new_uint64(memory_error->ModuleHandle));
135 
136     return section_ir;
137 }