xref: /openbmc/libcper/docs/OEMExtensions.md (revision a3b7f8a2)
1# Extending `cper-parse` With OEM Sections
2
3Section definitions within `cper-parse` are entirely modular, and can be easily
4modified at compile time to include custom OEM section extensions. This document
5will detail how these extensions can be added in from a stock version of the
6project.
7
8## Creating a Section Parser
9
10First, we need to create a parser to actually handle the conversion of the
11section from CPER -> CPER-JSON and CPER-JSON -> CPER. For the purposes of
12example here, we will create a fake CPER section, "myVendorSection", in
13`cper-section-myvendor.h` and `cper-section-myvendor.c`.
14
15_sections/cper-section-myvendor.h_:
16
17```c
18#ifndef CPER_SECTION_MYVENDOR
19#define CPER_SECTION_MYVENDOR
20
21#include <json.h>
22#include "Cper.h"
23
24json_object* cper_section_myvendor_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
25void ir_section_myvendor_to_cper(json_object* section, FILE* out);
26
27#endif
28```
29
30_sections/cper-section-myvendor.c_:
31
32```c
33/**
34 * Describes functions for converting MyVendor sections from binary and JSON format
35 * into an intermediate format.
36 *
37 * Author: author@example.com
38 **/
39#include <stdio.h>
40#include <json.h>
41#include "Cper.h"
42#include "cper-section-ccix-per.h"
43
44json_object* cper_section_myvendor_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
45{
46    //Define a method here that converts the bytes starting from "section" into JSON IR.
47    //The length of the bytes is described in descriptor->Length.
48    //...
49}
50
51void ir_section_myvendor_to_cper(json_object* section, FILE* out)
52{
53    //Define a method here that converts the given JSON IR object into CPER binary,
54    //writing the output to the provided stream.
55    //...
56}
57```
58
59Once this is done, we can add our section to the parser.
60
61## Adding a Section GUID
62
63To identify our section for parsing, we must define a section GUID within
64`Cper.h` and `Cper.c` respectively. They are defined here for shared use in both
65`cper-parse` and also `cper-generator` if you wish to write a generation method
66for your section.
67
68_Cper.h_:
69
70```c
71...
72extern EFI_GUID   gEfiCxlVirtualSwitchErrorSectionGuid;
73extern EFI_GUID   gEfiCxlMldPortErrorSectionGuid;
74extern EFI_GUID   gMyVendorSectionGuid;
75```
76
77Cper.c\_:
78
79```c
80...
81EFI_GUID gEfiCxlVirtualSwitchErrorSectionGuid = { 0x40d26425, 0x3396, 0x4c4d, { 0xa5, 0xda, 0x3d, 0x47, 0x26, 0x3a, 0xf4, 0x25 }};
82EFI_GUID gEfiCxlMldPortErrorSectionGuid = { 0x8dc44363, 0x0c96, 0x4710, { 0xb7, 0xbf, 0x04, 0xbb, 0x99, 0x53, 0x4c, 0x3f }};
83EFI_GUID gMyVendorSectionGuid = { 0x40d26425, 0x3396, 0x4c4d, { 0xa5, 0xda, 0x3d, 0x47, 0x26, 0x3a, 0xf4, 0x25 }};
84```
85
86## Adding a Section Definition
87
88Finally, we need to add a section definition for our section, matching the GUID
89to the name and conversion methods. Open `sections/cper-section.c` and add your
90definition to the `section_definitions` array, like so:
91
92_sections/cper-section.c_:
93
94```c
95/**
96 * Describes available sections to the CPER parser.
97 *
98 * Author: Lawrence.Tang@arm.com
99 **/
100#include "Cper.h"
101#include "cper-section.h"
102...
103#include "cper-section-myvendor.h"
104
105//Definitions of all sections available to the CPER parser.
106CPER_SECTION_DEFINITION section_definitions[] = {
107    ...
108    {&gMyVendorSectionGuid, "MyVendor Error", cper_section_myvendor_to_ir, ir_section_myvendor_to_cper},
109};
110```
111
112Now you're done! After a further `cmake .` and `make`, `cper-convert` and all
113other conversion libraries included should successfully convert your OEM CPER
114section between CPER-JSON and CPER binary.
115