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 }