acpi: Support string output
authorSimon Glass <sjg@chromium.org>
Tue, 7 Jul 2020 19:11:45 +0000 (13:11 -0600)
committerBin Meng <bmeng.cn@gmail.com>
Fri, 17 Jul 2020 06:32:24 +0000 (14:32 +0800)
Add support for output of strings and streams of bytes.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Wolfgang Wallner <wolfgang.wallner@br-automation.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
include/acpi/acpigen.h
lib/acpi/acpigen.c
test/dm/acpigen.c

index 8809cdb..9c1faf7 100644 (file)
@@ -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 null 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
index a42ae26..671f1d0 100644 (file)
@@ -37,3 +37,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');
+}
index a4adfbf..26d1b76 100644 (file)
@@ -20,6 +20,9 @@
 /* Maximum size of the ACPI context needed for most tests */
 #define ACPI_CONTEXT_SIZE      150
 
+#define TEST_STRING    "frogmore"
+#define TEST_STREAM2   "\xfa\xde"
+
 static int alloc_context(struct acpi_ctx **ctxp)
 {
        struct acpi_ctx *ctx;
@@ -73,6 +76,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)
 {