1 /**
2  * Describes functions for converting PCI/PCI-X device 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-pci-dev.h"
12 
13 //Converts a single PCI/PCI-X device CPER section into JSON IR.
14 json_object* cper_section_pci_dev_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
15 {
16     EFI_PCI_PCIX_DEVICE_ERROR_DATA* dev_error = (EFI_PCI_PCIX_DEVICE_ERROR_DATA*)section;
17     json_object* section_ir = json_object_new_object();
18 
19     //Validation bits.
20     json_object* validation = bitfield_to_ir(dev_error->ValidFields, 5, PCI_DEV_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(&dev_error->ErrorStatus);
25     json_object_object_add(section_ir, "errorStatus", error_status);
26 
27     //ID information.
28     json_object* id_info = json_object_new_object();
29     json_object_object_add(id_info, "vendorID", json_object_new_uint64(dev_error->IdInfo.VendorId));
30     json_object_object_add(id_info, "deviceID", json_object_new_uint64(dev_error->IdInfo.DeviceId));
31     json_object_object_add(id_info, "classCode", json_object_new_uint64(dev_error->IdInfo.ClassCode));
32     json_object_object_add(id_info, "functionNumber", json_object_new_uint64(dev_error->IdInfo.FunctionNumber));
33     json_object_object_add(id_info, "deviceNumber", json_object_new_uint64(dev_error->IdInfo.DeviceNumber));
34     json_object_object_add(id_info, "busNumber", json_object_new_uint64(dev_error->IdInfo.BusNumber));
35     json_object_object_add(id_info, "segmentNumber", json_object_new_uint64(dev_error->IdInfo.SegmentNumber));
36     json_object_object_add(section_ir, "idInfo", id_info);
37 
38     //Number of following register data pairs.
39     json_object_object_add(section_ir, "memoryNumber", json_object_new_uint64(dev_error->MemoryNumber));
40     json_object_object_add(section_ir, "ioNumber", json_object_new_uint64(dev_error->IoNumber));
41     int num_data_pairs = dev_error->MemoryNumber + dev_error->IoNumber;
42 
43     //Register pairs, described by the numeric fields.
44     //The actual "pairs" of address and data aren't necessarily 8 bytes long, so can't assume the contents.
45     //Hence the naming "firstHalf" and "secondHalf" rather than "address" and "data".
46     json_object* register_data_pair_array = json_object_new_array();
47     UINT64* cur_pos = (UINT64*)(dev_error + 1);
48     for (int i=0; i<num_data_pairs; i++)
49     {
50         //Save current pair to array.
51         json_object* register_data_pair = json_object_new_object();
52         json_object_object_add(register_data_pair, "firstHalf", json_object_new_uint64(*cur_pos));
53         json_object_object_add(register_data_pair, "secondHalf", json_object_new_uint64(*(cur_pos + 1)));
54         json_object_array_add(register_data_pair_array, register_data_pair);
55 
56         //Move to next pair.
57         cur_pos += 2;
58     }
59     json_object_object_add(section_ir, "registerDataPairs", register_data_pair_array);
60 
61     return section_ir;
62 }
63 
64 void ir_section_pci_dev_to_cper(json_object* section, FILE* out)
65 {
66     EFI_PCI_PCIX_DEVICE_ERROR_DATA* section_cper =
67         (EFI_PCI_PCIX_DEVICE_ERROR_DATA*)calloc(1, sizeof(EFI_PCI_PCIX_DEVICE_ERROR_DATA));
68 
69     //Validation bits.
70     section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"),
71         5, PCI_DEV_ERROR_VALID_BITFIELD_NAMES);
72 
73     //Error status.
74     ir_generic_error_status_to_cper(json_object_object_get(section, "errorStatus"), &section_cper->ErrorStatus);
75 
76     //Device ID information.
77     json_object* id_info = json_object_object_get(section, "idInfo");
78     section_cper->IdInfo.VendorId = json_object_get_uint64(json_object_object_get(id_info, "vendorID"));
79     section_cper->IdInfo.DeviceId = json_object_get_uint64(json_object_object_get(id_info, "deviceID"));
80     section_cper->IdInfo.ClassCode = json_object_get_uint64(json_object_object_get(id_info, "classCode"));
81     section_cper->IdInfo.FunctionNumber = json_object_get_uint64(json_object_object_get(id_info, "functionNumber"));
82     section_cper->IdInfo.DeviceNumber = json_object_get_uint64(json_object_object_get(id_info, "deviceNumber"));
83     section_cper->IdInfo.BusNumber = json_object_get_uint64(json_object_object_get(id_info, "busNumber"));
84     section_cper->IdInfo.SegmentNumber = json_object_get_uint64(json_object_object_get(id_info, "segmentNumber"));
85 
86     //Amount of following data pairs.
87     section_cper->MemoryNumber = (UINT32)json_object_get_uint64(json_object_object_get(section, "memoryNumber"));
88     section_cper->IoNumber = (UINT32)json_object_get_uint64(json_object_object_get(section, "ioNumber"));
89 
90     //Write header out to stream, free it.
91     fwrite(&section_cper, sizeof(section_cper), 1, out);
92     fflush(out);
93     free(section_cper);
94 
95     //Begin writing register pairs.
96     json_object* register_pairs = json_object_object_get(section, "registerPairs");
97     int num_pairs = json_object_array_length(register_pairs);
98     for (int i=0; i<num_pairs; i++)
99     {
100         //Get the pair array item out.
101         json_object* register_pair = json_object_array_get_idx(register_pairs, i);
102 
103         //Create the pair array.
104         UINT64 pair[2];
105         pair[0] = json_object_get_uint64(json_object_object_get(register_pair, "firstHalf"));
106         pair[1] = json_object_get_uint64(json_object_object_get(register_pair, "secondHalf"));
107 
108         //Push to stream.
109         fwrite(pair, sizeof(UINT64), 2, out);
110         fflush(out);
111     }
112 }