1 /**
2  * Describes functions for converting PCI/PCI-X bus 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 <string.h>
9 #include "json.h"
10 #include "../edk/Cper.h"
11 #include "../cper-utils.h"
12 #include "cper-section-pci-bus.h"
13 
14 //Converts a single PCI/PCI-X bus CPER section into JSON IR.
15 json_object* cper_section_pci_bus_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
16 {
17     EFI_PCI_PCIX_BUS_ERROR_DATA* bus_error = (EFI_PCI_PCIX_BUS_ERROR_DATA*)section;
18     json_object* section_ir = json_object_new_object();
19 
20     //Validation bits.
21     json_object* validation = bitfield_to_ir(bus_error->ValidFields, 9, PCI_BUS_ERROR_VALID_BITFIELD_NAMES);
22     json_object_object_add(section_ir, "validationBits", validation);
23 
24     //Error status.
25     json_object* error_status = cper_generic_error_status_to_ir(&bus_error->ErrorStatus);
26     json_object_object_add(section_ir, "errorStatus", error_status);
27 
28     //PCI bus error type.
29     json_object* error_type = integer_to_readable_pair(bus_error->Type, 8,
30         PCI_BUS_ERROR_TYPES_KEYS,
31         PCI_BUS_ERROR_TYPES_VALUES,
32         "Unknown (Reserved)");
33     json_object_object_add(section_ir, "errorType", error_type);
34 
35     //Bus ID.
36     json_object* bus_id = json_object_new_object();
37     json_object_object_add(bus_id, "busNumber", json_object_new_int(bus_error->BusId & 0xFF));
38     json_object_object_add(bus_id, "segmentNumber", json_object_new_int(bus_error->BusId >> 8));
39     json_object_object_add(section_ir, "busID", bus_id);
40 
41     //Miscellaneous numeric fields.
42     UINT8 command_type = (bus_error->BusCommand >> 56) & 0b1; //Byte 7, bit 0.
43     json_object_object_add(section_ir, "busAddress", json_object_new_uint64(bus_error->BusAddress));
44     json_object_object_add(section_ir, "busData", json_object_new_uint64(bus_error->BusData));
45     json_object_object_add(section_ir, "busCommandType", json_object_new_string(command_type == 0 ? "PCI" : "PCI-X"));
46     json_object_object_add(section_ir, "busRequestorID", json_object_new_uint64(bus_error->RequestorId));
47     json_object_object_add(section_ir, "busCompleterID", json_object_new_uint64(bus_error->ResponderId));
48     json_object_object_add(section_ir, "targetID", json_object_new_uint64(bus_error->TargetId));
49 
50     return section_ir;
51 }
52 
53 //Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the
54 //provided stream.
55 void ir_section_pci_bus_to_cper(json_object* section, FILE* out)
56 {
57     EFI_PCI_PCIX_BUS_ERROR_DATA* section_cper =
58         (EFI_PCI_PCIX_BUS_ERROR_DATA*)calloc(1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA));
59 
60     //Validation bits.
61     section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"),
62         9, PCI_BUS_ERROR_VALID_BITFIELD_NAMES);
63 
64     //Error status.
65     ir_generic_error_status_to_cper(json_object_object_get(section, "errorStatus"), &section_cper->ErrorStatus);
66 
67     //Bus ID.
68     json_object* bus_id = json_object_object_get(section, "busID");
69     UINT16 bus_number = (UINT8)json_object_get_int(json_object_object_get(bus_id, "busNumber"));
70     UINT16 segment_number = (UINT8)json_object_get_int(json_object_object_get(bus_id, "segmentNumber"));
71     section_cper->BusId = bus_number + (segment_number << 8);
72 
73     //Remaining fields.
74     UINT64 pcix_command = (UINT64)0x1 << 56;
75     const char* bus_command = json_object_get_string(json_object_object_get(section, "busCommandType"));
76     section_cper->Type = (UINT16)readable_pair_to_integer(json_object_object_get(section, "errorType"));
77     section_cper->BusAddress = json_object_get_uint64(json_object_object_get(section, "busAddress"));
78     section_cper->BusData = json_object_get_uint64(json_object_object_get(section, "busData"));
79     section_cper->BusCommand = strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command;
80     section_cper->RequestorId = json_object_get_uint64(json_object_object_get(section, "busRequestorID"));
81     section_cper->ResponderId = json_object_get_uint64(json_object_object_get(section, "busCompleterID"));
82     section_cper->TargetId = json_object_get_uint64(json_object_object_get(section, "targetID"));
83 
84     //Write to stream, free resources.
85     fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out);
86     fflush(out);
87     free(section_cper);
88 }