diff mbox series

[v8,08/35] acpi: Support string output

Message ID 20200426152618.125646-6-sjg@chromium.org
State Superseded
Headers show
Series dm: Add programmatic generation of ACPI tables (part B) | expand

Commit Message

Simon Glass April 26, 2020, 3:25 p.m. UTC
Add support for output of strings and streams of bytes.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v8: None
Changes in v3: None
Changes in v2: None

 include/acpi/acpigen.h | 19 +++++++++++++++++++
 lib/acpi/acpigen.c     | 14 ++++++++++++++
 test/dm/acpigen.c      | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+)
diff mbox series

Patch

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 8809cdb4e1..7365cce738 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -46,4 +46,23 @@  void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
  */
 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
 
+/**
+ * acpigen_emit_stream() - Emit a stream of bytes
+ *
+ * @ctx: ACPI context pointer
+ * @data: Data to output
+ * @size: Size of data in bytes
+ */
+void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
+
+/**
+ * acpigen_emit_string() - Emit a string
+ *
+ * Emit a string with a nul terminator
+ *
+ * @ctx: ACPI context pointer
+ * @str: String to output, or NULL for an empty string
+ */
+void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 59bd3af0b7..1223f0d1c4 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -36,3 +36,17 @@  void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
 	acpigen_emit_byte(ctx, (data >> 16) & 0xff);
 	acpigen_emit_byte(ctx, (data >> 24) & 0xff);
 }
+
+void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
+{
+	int i;
+
+	for (i = 0; i < size; i++)
+		acpigen_emit_byte(ctx, data[i]);
+}
+
+void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
+{
+	acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
+	acpigen_emit_byte(ctx, '\0'); /* NUL */
+}
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index ab1261db9f..77d1a3bba6 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -17,6 +17,9 @@ 
 #include <dm/test.h>
 #include <test/ut.h>
 
+#define TEST_STRING	"frogmore"
+#define TEST_STREAM2	"\xfa\xde"
+
 static int alloc_context(struct acpi_ctx **ctxp)
 {
 	struct acpi_ctx *ctx;
@@ -66,6 +69,45 @@  static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_acpi_emit_simple, 0);
 
+/* Test emitting a stream */
+static int dm_test_acpi_emit_stream(struct unit_test_state *uts)
+{
+	struct acpi_ctx *ctx;
+	u8 *ptr;
+
+	ut_assertok(alloc_context(&ctx));
+
+	ptr = acpigen_get_current(ctx);
+	acpigen_emit_stream(ctx, TEST_STREAM2, 2);
+	ut_asserteq(2, acpigen_get_current(ctx) - ptr);
+	ut_asserteq((u8)TEST_STREAM2[0], ptr[0]);
+	ut_asserteq((u8)TEST_STREAM2[1], ptr[1]);
+
+	free_context(&ctx);
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_emit_stream, 0);
+
+/* Test emitting a string */
+static int dm_test_acpi_emit_string(struct unit_test_state *uts)
+{
+	struct acpi_ctx *ctx;
+	u8 *ptr;
+
+	ut_assertok(alloc_context(&ctx));
+
+	ptr = acpigen_get_current(ctx);
+	acpigen_emit_string(ctx, TEST_STRING);
+	ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr);
+	ut_asserteq_str(TEST_STRING, (char *)ptr);
+
+	free_context(&ctx);
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_emit_string, 0);
+
 /* Test emitting an interrupt descriptor */
 static int dm_test_acpi_interrupt(struct unit_test_state *uts)
 {