It is convenient to write a length value for preceding a block of data.
Of course the length is not known or is hard to calculate a priori. So add
a way to mark the start on a stack, so the length can be updated when
known.
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>
struct acpi_ctx;
+/* Top 4 bits of the value used to indicate a three-byte length value */
+#define ACPI_PKG_LEN_3_BYTES 0x80
+
/**
* acpigen_get_current() - Get the current ACPI code output pointer
*
*/
void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
+/**
+ * acpigen_write_len_f() - Write a 'forward' length placeholder
+ *
+ * This adds space for a length value in the ACPI stream and pushes the current
+ * position (before the length) on the stack. After calling this you can write
+ * some data and then call acpigen_pop_len() to update the length value.
+ *
+ * Usage:
+ *
+ * acpigen_write_len_f() ------\
+ * acpigen_write...() |
+ * acpigen_write...() |
+ * acpigen_write_len_f() --\ |
+ * acpigen_write...() | |
+ * acpigen_write...() | |
+ * acpigen_pop_len() ------/ |
+ * acpigen_write...() |
+ * acpigen_pop_len() ----------/
+ *
+ * See ACPI 6.3 section 20.2.4 Package Length Encoding
+ *
+ * This implementation always uses a 3-byte packet length for simplicity. It
+ * could be adjusted to support other lengths.
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_write_len_f(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_pop_len() - Update the previously stacked length placeholder
+ *
+ * Call this after the data for the block has been written. It updates the
+ * top length value in the stack and pops it off.
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_pop_len(struct acpi_ctx *ctx);
+
#endif
#define ACPI_OPS_PTR(_ptr)
#endif
-/* Length of an ACPI name string, excluding nul terminator */
+/* Length of an ACPI name string, excluding null terminator */
#define ACPI_NAME_LEN 4
/* Length of an ACPI name string including nul terminator */
#define ACPI_NAME_MAX (ACPI_NAME_LEN + 1)
+/* Number of nested objects supported */
+#define ACPIGEN_LENSTACK_SIZE 10
+
#if !defined(__ACPI__)
/**
* adding a new table. The RSDP holds pointers to the RSDT and XSDT.
* @rsdt: Pointer to the Root System Description Table
* @xsdt: Pointer to the Extended System Description Table
+ * @len_stack: Stack of 'length' words to fix up later
+ * @ltop: Points to current top of stack (0 = empty)
*/
struct acpi_ctx {
void *base;
struct acpi_rsdp *rsdp;
struct acpi_rsdt *rsdt;
struct acpi_xsdt *xsdt;
+ char *len_stack[ACPIGEN_LENSTACK_SIZE];
+ int ltop;
};
/**
#include <common.h>
#include <dm.h>
+#include <log.h>
#include <acpi/acpigen.h>
#include <dm/acpi.h>
acpigen_emit_byte(ctx, (data >> 24) & 0xff);
}
+/*
+ * Maximum length for an ACPI object generated by this code,
+ *
+ * If you need to change this, change acpigen_write_len_f(ctx) and
+ * acpigen_pop_len(ctx)
+ */
+#define ACPIGEN_MAXLEN 0xfffff
+
+void acpigen_write_len_f(struct acpi_ctx *ctx)
+{
+ assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
+ ctx->len_stack[ctx->ltop++] = ctx->current;
+ acpigen_emit_byte(ctx, 0);
+ acpigen_emit_byte(ctx, 0);
+ acpigen_emit_byte(ctx, 0);
+}
+
+void acpigen_pop_len(struct acpi_ctx *ctx)
+{
+ int len;
+ char *p;
+
+ assert(ctx->ltop > 0);
+ p = ctx->len_stack[--ctx->ltop];
+ len = ctx->current - (void *)p;
+ assert(len <= ACPIGEN_MAXLEN);
+ /* generate store length for 0xfffff max */
+ p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
+ p[1] = len >> 4 & 0xff;
+ p[2] = len >> 12 & 0xff;
+}
+
void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
{
int i;
#define TEST_STRING "frogmore"
#define TEST_STREAM2 "\xfa\xde"
-static int alloc_context(struct acpi_ctx **ctxp)
+static int alloc_context_size(struct acpi_ctx **ctxp, int size)
{
struct acpi_ctx *ctx;
ctx = malloc(sizeof(*ctx));
if (!ctx)
return -ENOMEM;
- ctx->base = malloc(ACPI_CONTEXT_SIZE);
+ ctx->base = malloc(size);
if (!ctx->base) {
free(ctx);
return -ENOMEM;
}
+ ctx->ltop = 0;
ctx->current = ctx->base;
*ctxp = ctx;
return 0;
}
+static int alloc_context(struct acpi_ctx **ctxp)
+{
+ return alloc_context_size(ctxp, ACPI_CONTEXT_SIZE);
+}
+
static void free_context(struct acpi_ctx **ctxp)
{
free((*ctxp)->base);
return 0;
}
DM_TEST(dm_test_acpi_spi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/**
+ * get_length() - decode a three-byte length field
+ *
+ * @ptr: Length encoded as per ACPI
+ * @return decoded length, or -EINVAL on error
+ */
+static int get_length(u8 *ptr)
+{
+ if (!(*ptr & 0x80))
+ return -EINVAL;
+
+ return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12;
+}
+
+/* Test emitting a length */
+static int dm_test_acpi_len(struct unit_test_state *uts)
+{
+ const int size = 0xc0000;
+ struct acpi_ctx *ctx;
+ u8 *ptr;
+ int i;
+
+ ut_assertok(alloc_context_size(&ctx, size));
+
+ ptr = acpigen_get_current(ctx);
+
+ /* Write a byte and a 3-byte length */
+ acpigen_write_len_f(ctx);
+ acpigen_emit_byte(ctx, 0x23);
+ acpigen_pop_len(ctx);
+ ut_asserteq(1 + 3, get_length(ptr));
+
+ /* Write 200 bytes so we need two length bytes */
+ ptr = ctx->current;
+ acpigen_write_len_f(ctx);
+ for (i = 0; i < 200; i++)
+ acpigen_emit_byte(ctx, 0x23);
+ acpigen_pop_len(ctx);
+ ut_asserteq(200 + 3, get_length(ptr));
+
+ /* Write 40KB so we need three length bytes */
+ ptr = ctx->current;
+ acpigen_write_len_f(ctx);
+ for (i = 0; i < 40000; i++)
+ acpigen_emit_byte(ctx, 0x23);
+ acpigen_pop_len(ctx);
+ ut_asserteq(40000 + 3, get_length(ptr));
+
+ free_context(&ctx);
+
+ return 0;
+}
+DM_TEST(dm_test_acpi_len, 0);