Add support for AMPERE CPERs

Support Ampere CPER entries

Change-Id: I607a89209138fa53914c55c07aba8b7d6f382e5e
Signed-off-by: Dung Cao <dung@os.amperecomputing.com>
diff --git a/sections/cper-section-ampere.c b/sections/cper-section-ampere.c
new file mode 100644
index 0000000..264c2f6
--- /dev/null
+++ b/sections/cper-section-ampere.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <json.h>
+#include "../edk/Cper.h"
+#include "../cper-utils.h"
+#include "cper-section-ampere.h"
+
+//Converts the given processor-generic CPER section into JSON IR.
+json_object *cper_section_ampere_to_ir(void *section)
+{
+	EFI_AMPERE_ERROR_DATA *record = (EFI_AMPERE_ERROR_DATA *)section;
+	json_object *section_ir = json_object_new_object();
+
+	json_object_object_add(section_ir, "typeId",
+			       json_object_new_int(record->TypeId));
+	json_object_object_add(section_ir, "subTypeId",
+			       json_object_new_int(record->SubtypeId));
+	json_object_object_add(section_ir, "instanceId",
+			       json_object_new_int(record->InstanceId));
+
+	return section_ir;
+}
+
+//Converts a single CPER-JSON ARM error section into CPER binary, outputting to the given stream.
+void ir_section_ampere_to_cper(json_object *section, FILE *out)
+{
+	EFI_AMPERE_ERROR_DATA *section_cper = (EFI_AMPERE_ERROR_DATA *)calloc(
+		1, sizeof(EFI_AMPERE_ERROR_DATA));
+
+	//Count of error/context info structures.
+	section_cper->TypeId =
+		json_object_get_int(json_object_object_get(section, "typeId"));
+	section_cper->SubtypeId = json_object_get_int(
+		json_object_object_get(section, "subTypeId"));
+	section_cper->InstanceId = json_object_get_int(
+		json_object_object_get(section, "instanceId"));
+
+	//Flush header to stream.
+	fwrite(section_cper, sizeof(EFI_AMPERE_ERROR_DATA), 1, out);
+	fflush(out);
+	free(section_cper);
+}
diff --git a/sections/cper-section-ampere.h b/sections/cper-section-ampere.h
new file mode 100644
index 0000000..9264098
--- /dev/null
+++ b/sections/cper-section-ampere.h
@@ -0,0 +1,10 @@
+#ifndef CPER_SECTION_AMPERE_H
+#define CPER_SECTION_AMPERE_H
+
+#include <json.h>
+#include "../edk/Cper.h"
+
+json_object *cper_section_ampere_to_ir(void *section);
+void ir_section_ampere_to_cper(json_object *section, FILE *out);
+
+#endif
diff --git a/sections/cper-section.c b/sections/cper-section.c
index 1845118..defecf6 100644
--- a/sections/cper-section.c
+++ b/sections/cper-section.c
@@ -21,6 +21,7 @@
 #include "cper-section-cxl-protocol.h"
 #include "cper-section-cxl-component.h"
 #include "cper-section-nvidia.h"
+#include "cper-section-ampere.h"
 
 //Definitions of all sections available to the CPER parser.
 CPER_SECTION_DEFINITION section_definitions[] = {
@@ -72,6 +73,8 @@
 	  cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper },
 	{ &gEfiNvidiaErrorSectionGuid, "NVIDIA", cper_section_nvidia_to_ir,
 	  ir_section_nvidia_to_cper },
+	{ &gEfiAmpereErrorSectionGuid, "Ampere", cper_section_ampere_to_ir,
+	  ir_section_ampere_to_cper },
 };
 const size_t section_definitions_len =
 	sizeof(section_definitions) / sizeof(CPER_SECTION_DEFINITION);