libfdt: add acpi table verification support

To generate "acpio" and "acpi" images, it needs verify the
original input acpi tables.

Test: can generate acpio/acpi images
Fixes: 111871613

Change-Id: Iadf53df1efc9f8eba3c66d0e1b29af6ecc340d45
diff --git a/libfdt/Android.bp b/libfdt/Android.bp
index e92497b..320d4fc 100644
--- a/libfdt/Android.bp
+++ b/libfdt/Android.bp
@@ -17,6 +17,7 @@
         "fdt_empty_tree.c",
         "fdt_addresses.c",
         "fdt_overlay.c",
+        "acpi.c",
     ],
     export_include_dirs: ["."],
 }
diff --git a/libfdt/Makefile.libfdt b/libfdt/Makefile.libfdt
index 098b3f3..efe1537 100644
--- a/libfdt/Makefile.libfdt
+++ b/libfdt/Makefile.libfdt
@@ -7,5 +7,5 @@
 LIBFDT_INCLUDES = fdt.h libfdt.h libfdt_env.h
 LIBFDT_VERSION = version.lds
 LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c fdt_empty_tree.c \
-	fdt_addresses.c fdt_overlay.c
+	fdt_addresses.c fdt_overlay.c acpi.c
 LIBFDT_OBJS = $(LIBFDT_SRCS:%.c=%.o)
diff --git a/libfdt/acpi.c b/libfdt/acpi.c
new file mode 100644
index 0000000..ba51734
--- /dev/null
+++ b/libfdt/acpi.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2018, Intel Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *    * Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *    * Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer
+ *      in the documentation and/or other materials provided with the
+ *      distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "libacpi.h"
+
+uint8_t acpi_csum(const void *base, int n) {
+	uint8_t *p;
+	uint8_t sum;
+	int bytesDone;
+
+	p = (uint8_t*)base;
+
+	sum = 0;
+	for (bytesDone = 0; bytesDone < n; bytesDone++) {
+		sum += *p;
+		p++;
+	}
+
+	return sum;
+}
+
diff --git a/libfdt/libacpi.h b/libfdt/libacpi.h
new file mode 100644
index 0000000..06cae6c
--- /dev/null
+++ b/libfdt/libacpi.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2018, Intel Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *    * Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *    * Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer
+ *      in the documentation and/or other materials provided with the
+ *      distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef _LIBACPI_H
+#define _LIBACPI_H
+
+#include "libfdt.h"
+
+#pragma pack(1)
+typedef struct {
+  uint32_t  Signature;			/* ASCII Table identifier */
+  uint32_t  Length;			/* Length of the table, including the header */
+  uint8_t   Revision;			/* Revision of the structure */
+  uint8_t   Checksum;			/* Sum of all fields must be 0 */
+  uint8_t   OemId[6];			/* ASCII OEM identifier */
+  uint64_t  OemTableId;			/* ASCII OEM table identifier */
+  uint32_t  OemRevision;		/* OEM supplied revision number */
+  uint32_t  CreatorId;			/* Vendor ID of utility creator of the table */
+  uint32_t  CreatorRevision;		/* Revision of utility creator of the table */
+} EFI_ACPI_DESCRIPTION_HEADER;
+#pragma pack()
+
+/**********************************************************************/
+/* General functions                                                  */
+/**********************************************************************/
+#define acpi_get_header(acpi, field) \
+  ((const EFI_ACPI_DESCRIPTION_HEADER *)(acpi))->field
+#define acpi_signature(acpi)		(acpi_get_header(acpi, Signature))
+#define acpi_length(acpi)		(acpi_get_header(acpi, Length))
+
+/* convert 2 bytes ASCII to uint16 */
+#define SIGNATURE_16(A, B)		((A) | (B << 8))
+/* convert 4 bytes ASCII to uint32 */
+#define SIGNATURE_32(A, B, C, D)	((SIGNATURE_16 (A, B)) | (SIGNATURE_16 (C, D) << 16))
+/* convert 8 bytes ASCII to uint64 */
+#define SIGNATURE_64(A, B, C, D, E, F, G, H) \
+    (SIGNATURE_32 (A, B, C, D) | ((UINT64) (SIGNATURE_32 (E, F, G, H)) << 32))
+
+#define SSDT_MAGIC		(const unsigned)SIGNATURE_32('S', 'S', 'D', 'T')
+#define DSDT_MAGIC		(const unsigned)SIGNATURE_32('D', 'S', 'D', 'T')
+
+#define ACPI_TABLE_MAGIC	0x41435049
+
+/* checksum byte by byte for acpi table */
+uint8_t acpi_csum(const void *base, int n);
+
+#endif /* ifndef _LIBACPI_H */