Add initial ARM support, fix bit shifting error.
diff --git a/cper-parse.c b/cper-parse.c
index ffdd45f..4bb6797 100644
--- a/cper-parse.c
+++ b/cper-parse.c
@@ -1,6 +1,6 @@
 /**
- * Describes functions for parsing CPER headers and section descriptions 
- * into an intermediate JSON format.
+ * Describes high level functions for converting an entire CPER log, and functions for parsing 
+ * CPER headers and section descriptions into an intermediate JSON format.
  * 
  * Author: Lawrence.Tang@arm.com
  **/
@@ -12,6 +12,7 @@
 #include "cper-utils.h"
 #include "sections/cper-section-generic.h"
 #include "sections/cper-section-ia32x64.h"
+#include "sections/cper-section-arm.h"
 
 //Private pre-definitions.
 json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header);
@@ -56,6 +57,7 @@
 
     //Create the header JSON object from the read bytes.
     json_object* header_ir = cper_header_to_ir(&header);
+    printf("Finished header.\n");
 
     //Read the appropriate number of section descriptors & sections, and convert them into IR format.
     json_object* section_descriptors_ir = json_object_new_array();
@@ -70,15 +72,18 @@
             return NULL;
         }
         json_object_array_add(section_descriptors_ir, cper_section_descriptor_to_ir(&section_descriptor));
+        printf("Finished descriptor %d.\n", i+1);
 
         //Read the section itself.
         json_object_array_add(sections_ir, cper_section_to_ir(cper_file, &section_descriptor));
+        printf("Finished section %d.\n", i+1);
     }
 
     //Add the header, section descriptors, and sections to a parent object.
     json_object* parent = json_object_new_object();
     json_object_object_add(parent, "header", header_ir);
     json_object_object_add(parent, "sectionDescriptors", section_descriptors_ir);
+    json_object_object_add(parent, "sections", sections_ir);
 
     //...
     return parent;
@@ -102,10 +107,7 @@
     json_object_object_add(header_ir, "severity", error_severity);
 
     //The validation bits for each section.
-    json_object* validation_bits = json_object_new_object();
-    json_object_object_add(validation_bits, "platformID", json_object_new_boolean(header->ValidationBits & 0b1));
-    json_object_object_add(validation_bits, "timestamp", json_object_new_boolean(header->ValidationBits & 0b10 >> 1));
-    json_object_object_add(validation_bits, "partitionID", json_object_new_boolean(header->ValidationBits & 0b100 >> 2));
+    json_object* validation_bits = bitfield_to_ir(header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
     json_object_object_add(header_ir, "validationBits", validation_bits);
 
     //Total length of the record (including headers) in bytes.
@@ -219,15 +221,7 @@
     json_object_object_add(section_descriptor_ir, "validationBits", validation_bits);
 
     //Flag bits.
-    json_object* flags = json_object_new_object();
-    json_object_object_add(flags, "primary", json_object_new_boolean(section_descriptor->SectionFlags >> 31));
-    json_object_object_add(flags, "containmentWarning", json_object_new_boolean((section_descriptor->SectionFlags >> 30) & 0b1));
-    json_object_object_add(flags, "reset", json_object_new_boolean((section_descriptor->SectionFlags >> 29) & 0b1));
-    json_object_object_add(flags, "errorThresholdExceeded", json_object_new_boolean((section_descriptor->SectionFlags >> 28) & 0b1));
-    json_object_object_add(flags, "resourceNotAccessible", json_object_new_boolean((section_descriptor->SectionFlags >> 27) & 0b1));
-    json_object_object_add(flags, "latentError", json_object_new_boolean((section_descriptor->SectionFlags >> 26) & 0b1));
-    json_object_object_add(flags, "propagated", json_object_new_boolean((section_descriptor->SectionFlags >> 25) & 0b1));
-    json_object_object_add(flags, "overflow", json_object_new_boolean((section_descriptor->SectionFlags >> 24) & 0b1));
+    json_object* flags = bitfield_to_ir(section_descriptor->SectionFlags, 8, CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
     json_object_object_add(section_descriptor_ir, "flags", flags);
 
     //Section type (GUID).
@@ -298,7 +292,7 @@
 {
     //Read section as described by the section descriptor.
     fseek(handle, descriptor->SectionOffset, SEEK_SET);
-    void* section = malloc(descriptor->SectionLength);
+    void* section = calloc(1, descriptor->SectionLength);
     if (fread(section, descriptor->SectionLength, 1, handle) != 1)
     {
         printf("Section read failed: Could not read %d bytes from global offset %d.", 
@@ -308,29 +302,29 @@
 
     json_object* result = NULL;
     if (guid_equal(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
-        result = cper_section_generic_to_ir(handle, descriptor);
-    if (guid_equal(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
-        result = cper_section_ia32x64_to_ir(handle, descriptor);
+        result = cper_section_generic_to_ir(section, descriptor);
+    else if (guid_equal(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
+        result = cper_section_ia32x64_to_ir(section, descriptor);
     // //todo: Why does IPF have an overly long GUID?
     // // if (guid_equal(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
-    // if (guid_equal(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
-    //     result = cper_section_arm_to_ir(handle);
+    else if (guid_equal(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
+        result = cper_section_arm_to_ir(section, descriptor);
     // if (guid_equal(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
-    //     result = cper_section_platform_memory_to_ir(handle);
+    //     result = cper_section_platform_memory_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiPcieErrorSectionGuid))
-    //     result = cper_section_pcie_to_ir(handle);
+    //     result = cper_section_pcie_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
-    //     result = cper_section_firmware_error_to_ir(handle);
+    //     result = cper_section_firmware_error_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
-    //     result = cper_section_pci_bus_to_ir(handle);
+    //     result = cper_section_pci_bus_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
-    //     result = cper_section_pci_dev_to_ir(handle);
+    //     result = cper_section_pci_dev_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
-    //     result = cper_section_dmar_generic_to_ir(handle);
+    //     result = cper_section_dmar_generic_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
-    //     result = cper_section_intel_io_dma_to_ir(handle);
+    //     result = cper_section_intel_io_dma_to_ir(section);
     // if (guid_equal(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
-    //     result = cper_section_iommu_dma_to_ir(handle);
+    //     result = cper_section_iommu_dma_to_ir(section);
 
     //Free section memory, return result.
     free(section);
diff --git a/cper-parse.h b/cper-parse.h
index 8bb883a..a49c455 100644
--- a/cper-parse.h
+++ b/cper-parse.h
@@ -2,6 +2,10 @@
 #define CPER_PARSE_H
 #include "json.h"
 
+#define CPER_HEADER_VALID_BITFIELD_NAMES (const char*[]) {"platformID", "timestamp", "partitionID"}
+#define CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES (const char*[]) \
+    {"primary", "containmentWarning", "reset", "errorThresholdExceeded", "resourceNotAccessible", "latentError", \
+    "propagated", "overflow"}
 #define CPER_HEADER_FLAG_TYPES_KEYS (int []){1, 2, 3}
 #define CPER_HEADER_FLAG_TYPES_VALUES (const char*[]){"HW_ERROR_FLAGS_RECOVERED", "HW_ERROR_FLAGS_PREVERR", "HW_ERROR_FLAGS_SIMULATED"}
 
diff --git a/cper-utils.c b/cper-utils.c
index 61a796a..00581e2 100644
--- a/cper-utils.c
+++ b/cper-utils.c
@@ -36,7 +36,7 @@
     json_object* result = json_object_new_object();
     for (int i=0; i<num_fields; i++)
     {
-        json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> (7 - i)) & 0b1));
+        json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1));
     }
 
     return result;
@@ -48,7 +48,7 @@
     json_object* result = json_object_new_object();
     for (int i=0; i<num_fields; i++)
     {
-        json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> (31 - i)) & 0b1));
+        json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1));
     }
 
     return result;
@@ -60,7 +60,7 @@
     json_object* result = json_object_new_object();
     for (int i=0; i<num_fields; i++)
     {
-        json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> (63 - i)) & 0b1));
+        json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1));
     }
 
     return result;
diff --git a/edk/Cper.h b/edk/Cper.h
index 3723f5f..d8d0f41 100644
--- a/edk/Cper.h
+++ b/edk/Cper.h
@@ -739,6 +739,22 @@
   UINT64    Resv1           : 50;

 } EFI_IA32_X64_VALID_BITS;

 

+

+///

+/// ARM Processor Error Structure

+///

+typedef struct {

+  UINT32    ValidFields;

+  UINT16    ErrInfoNum;

+  UINT16    ContextInfoNum;

+  UINT32    SectionLength;

+  UINT32  ErrorAffinityLevel;

+  UINT64  MPIDR_EL1;

+  UINT64  MIDR_EL1;

+  UINT32 RunningState;

+  UINT32 PsciState;

+} EFI_ARM_PROCESSOR_ERROR_RECORD;

+

 ///

 /// Error Status Fields

 ///

diff --git a/sections/cper-section-arm.c b/sections/cper-section-arm.c
new file mode 100644
index 0000000..4dcb6c3
--- /dev/null
+++ b/sections/cper-section-arm.c
@@ -0,0 +1,48 @@
+/**
+ * Describes functions for converting ARM CPER sections from binary and JSON format
+ * into an intermediate format.
+ * 
+ * Author: Lawrence.Tang@arm.com
+ **/
+
+#include <stdio.h>
+#include "json.h"
+#include "../edk/Cper.h"
+#include "../cper-utils.h"
+#include "cper-section-arm.h"
+
+//Converts the given processor-generic CPER section into JSON IR.
+json_object* cper_section_arm_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
+{
+    EFI_ARM_PROCESSOR_ERROR_RECORD* record = (EFI_ARM_PROCESSOR_ERROR_RECORD*)section;
+    json_object* section_ir = json_object_new_object();
+    
+    //Validation bits.
+    json_object* validation = bitfield_to_ir(record->ValidFields, 4, ARM_PROCESSOR_ERROR_VALID_BITFIELD_NAMES);
+    json_object_object_add(section_ir, "validationBits", validation);
+
+    //Number of error info and context info structures, and length.
+    json_object_object_add(section_ir, "errorInfoNum", json_object_new_int(record->ErrInfoNum));
+    json_object_object_add(section_ir, "contextInfoNum", json_object_new_int(record->ContextInfoNum));
+    json_object_object_add(section_ir, "sectionLength", json_object_new_int(record->SectionLength));
+
+    //Error affinity.
+    json_object* error_affinity = json_object_new_object();
+    json_object_object_add(error_affinity, "value", json_object_new_int(record->ErrorAffinityLevel));
+    json_object_object_add(error_affinity, "type", 
+        json_object_new_string(record->ErrorAffinityLevel < 4 ? "Vendor Defined" : "Reserved"));
+    json_object_object_add(section_ir, "errorAffinity", error_affinity);
+
+    //Processor ID (MPIDR_EL1) and chip ID (MIDR_EL1).
+    json_object_object_add(section_ir, "mpidrEl1", json_object_new_uint64(record->MPIDR_EL1));
+    json_object_object_add(section_ir, "midrEl1", json_object_new_uint64(record->MIDR_EL1));
+
+    //Whether the processor is running, and the state of it if so.
+    json_object_object_add(section_ir, "running", json_object_new_boolean(record->RunningState));
+    if (record->RunningState)
+    {
+        //...       
+    }
+
+    return section_ir;
+}
\ No newline at end of file
diff --git a/sections/cper-section-arm.h b/sections/cper-section-arm.h
new file mode 100644
index 0000000..2112442
--- /dev/null
+++ b/sections/cper-section-arm.h
@@ -0,0 +1,12 @@
+#ifndef CPER_SECTION_ARM_H
+#define CPER_SECTION_ARM_H
+
+#include "json.h"
+#include "../edk/Cper.h"
+
+#define ARM_PROCESSOR_ERROR_VALID_BITFIELD_NAMES (const char*[]) \
+    {"mpidrValid", "errorAffinityLevelValid", "runningStateValid", "vendorSpecificInfoValid"}
+    
+json_object* cper_section_arm_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
+
+#endif
\ No newline at end of file
diff --git a/sections/cper-section-ia32x64.c b/sections/cper-section-ia32x64.c
index 187446a..cb56ed7 100644
--- a/sections/cper-section-ia32x64.c
+++ b/sections/cper-section-ia32x64.c
@@ -28,11 +28,11 @@
 
     //Flags.
     json_object* flags = json_object_new_object();
-    json_object_object_add(flags, "localAPICIDValid", json_object_new_boolean(record->ValidFields >> 31));
-    json_object_object_add(flags, "cpuIDInfoValid", json_object_new_boolean((record->ValidFields >> 30) & 0b1));
-    int processor_error_info_num = (record->ValidFields >> 29) & 0b111111;
+    json_object_object_add(flags, "localAPICIDValid", json_object_new_boolean(record->ValidFields & 0b1));
+    json_object_object_add(flags, "cpuIDInfoValid", json_object_new_boolean((record->ValidFields >> 1) & 0b1));
+    int processor_error_info_num = (record->ValidFields >> 2) & 0b111111;
     json_object_object_add(flags, "processorErrorInfoNum", json_object_new_int(processor_error_info_num));
-    int processor_context_info_num = (record->ValidFields >> 23) & 0b111111;
+    int processor_context_info_num = (record->ValidFields >> 8) & 0b111111;
     json_object_object_add(flags, "processorContextInfoNum", json_object_new_int(processor_context_info_num));
     json_object_object_add(record_ir, "flags", flags);