From patchwork Wed Nov 7 05:39:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 994115 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42qZzB1vpnz9s8T for ; Wed, 7 Nov 2018 16:41:26 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42qZz975fVzF3Fb for ; Wed, 7 Nov 2018 16:41:25 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [IPv6:2401:3900:2:1::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42qZxY6MKwzF3GX for ; Wed, 7 Nov 2018 16:40:01 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPSA id 42qZxY2gzFz9sCX; Wed, 7 Nov 2018 16:40:01 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au From: Alistair Popple To: pdbg@lists.ozlabs.org Date: Wed, 7 Nov 2018 16:39:36 +1100 Message-Id: <20181107053943.4307-10-alistair@popple.id.au> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181107053943.4307-1-alistair@popple.id.au> References: <20181107053943.4307-1-alistair@popple.id.au> Subject: [Pdbg] [PATCH v2 09/16] libpdbg: Move property code into libpdbg/device.c X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: amitay@ozlabs.org MIME-Version: 1.0 Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" Signed-off-by: Alistair Popple Reviewed-by: Amitay Isaacs --- libpdbg/device.c | 72 +++++++++++++++++++++++++++++++++++++++++++++---------- libpdbg/device.h | 30 ----------------------- libpdbg/libpdbg.c | 51 +++++++-------------------------------- 3 files changed, 68 insertions(+), 85 deletions(-) diff --git a/libpdbg/device.c b/libpdbg/device.c index 2f1a97b..af2973b 100644 --- a/libpdbg/device.c +++ b/libpdbg/device.c @@ -38,6 +38,21 @@ static u32 last_phandle = 0; struct pdbg_target *dt_root; +/* + * An in-memory representation of a node in the device tree. + * + * This is trivially flattened into an fdt. + * + * Note that the add_* routines will make a copy of the name if it's not + * a read-only string (ie. usually a string literal). + */ +struct dt_property { + struct list_node list; + const char *name; + size_t len; + char prop[/* len */]; +}; + static const char *take_name(const char *name) { if (!is_rodata(name) && !(name = strdup(name))) { @@ -285,6 +300,17 @@ static struct pdbg_target *dt_find_by_path(struct pdbg_target *root, const char return root; } +static struct dt_property *dt_find_property(const struct pdbg_target *node, + const char *name) +{ + struct dt_property *i; + + list_for_each(&node->properties, i, list) + if (strcmp(i->name, name) == 0) + return i; + return NULL; +} + static struct dt_property *new_property(struct pdbg_target *node, const char *name, size_t size) { @@ -313,7 +339,7 @@ static struct dt_property *new_property(struct pdbg_target *node, return p; } -struct dt_property *dt_add_property(struct pdbg_target *node, +static struct dt_property *dt_add_property(struct pdbg_target *node, const char *name, const void *val, size_t size) { @@ -338,7 +364,7 @@ struct dt_property *dt_add_property(struct pdbg_target *node, return p; } -void dt_resize_property(struct dt_property **prop, size_t len) +static void dt_resize_property(struct dt_property **prop, size_t len) { size_t new_len = sizeof(**prop) + len; @@ -349,6 +375,37 @@ void dt_resize_property(struct dt_property **prop, size_t len) (*prop)->list.prev->next = &(*prop)->list; } +void pdbg_set_target_property(struct pdbg_target *target, const char *name, const void *val, size_t size) +{ + struct dt_property *p; + + if ((p = dt_find_property(target, name))) { + if (size > p->len) { + dt_resize_property(&p, size); + p->len = size; + } + + memcpy(p->prop, val, size); + } else { + dt_add_property(target, name, val, size); + } +} + +void *pdbg_get_target_property(struct pdbg_target *target, const char *name, size_t *size) +{ + struct dt_property *p; + + p = dt_find_property(target, name); + if (p) { + if (size) + *size = p->len; + return p->prop; + } else if (size) + *size = 0; + + return NULL; +} + static u32 dt_property_get_cell(const struct dt_property *prop, u32 index) { assert(prop->len >= (index+1)*sizeof(u32)); @@ -382,17 +439,6 @@ static struct pdbg_target *dt_next(const struct pdbg_target *root, return NULL; } -struct dt_property *dt_find_property(const struct pdbg_target *node, - const char *name) -{ - struct dt_property *i; - - list_for_each(&node->properties, i, list) - if (strcmp(i->name, name) == 0) - return i; - return NULL; -} - static const struct dt_property *dt_require_property(const struct pdbg_target *node, const char *name, int wanted_len) { diff --git a/libpdbg/device.h b/libpdbg/device.h index 098f7f4..f487443 100644 --- a/libpdbg/device.h +++ b/libpdbg/device.h @@ -23,31 +23,8 @@ /* Any property or node with this prefix will not be passed to the kernel. */ #define DT_PRIVATE "skiboot," -/* - * An in-memory representation of a node in the device tree. - * - * This is trivially flattened into an fdt. - * - * Note that the add_* routines will make a copy of the name if it's not - * a read-only string (ie. usually a string literal). - */ -struct dt_property { - struct list_node list; - const char *name; - size_t len; - char prop[/* len */]; -}; - extern struct pdbg_target *dt_root; -/* Add a property node, various forms. */ -struct dt_property *dt_add_property(struct pdbg_target *node, - const char *name, - const void *val, size_t size); - -/* Warning: moves *prop! */ -void dt_resize_property(struct dt_property **prop, size_t len); - /* Check a compatible property */ bool dt_node_is_compatible(const struct pdbg_target *node, const char *compat); @@ -60,10 +37,6 @@ struct pdbg_target *dt_find_compatible_node(struct pdbg_target *root, for (node = NULL; \ (node = dt_find_compatible_node(root, node, compat)) != NULL;) -/* Find a property by name. */ -struct dt_property *dt_find_property(const struct pdbg_target *node,\ - const char *name); - /* Simplified accessors */ u32 dt_prop_get_u32(const struct pdbg_target *node, const char *prop); u32 dt_prop_get_u32_index(const struct pdbg_target *node, const char *prop, u32 index); @@ -71,9 +44,6 @@ const void *dt_prop_get(const struct pdbg_target *node, const char *prop); const void *dt_prop_get_def(const struct pdbg_target *node, const char *prop, void *def); -/* Parsing helpers */ -u64 dt_get_number(const void *pdata, unsigned int cells); - /* Find an chip-id property in this node; if not found, walk up the parent * nodes. Returns -1 if no chip-id property exists. */ u32 dt_get_chip_id(const struct pdbg_target *node); diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c index 8aa22e1..8af216c 100644 --- a/libpdbg/libpdbg.c +++ b/libpdbg/libpdbg.c @@ -135,52 +135,19 @@ const char *pdbg_target_dn_name(struct pdbg_target *target) return target->dn_name; } -void pdbg_set_target_property(struct pdbg_target *target, const char *name, const void *val, size_t size) -{ - struct dt_property *p; - - if ((p = dt_find_property(target, name))) { - if (size > p->len) { - dt_resize_property(&p, size); - p->len = size; - } - - memcpy(p->prop, val, size); - } else { - dt_add_property(target, name, val, size); - } -} - -void *pdbg_get_target_property(struct pdbg_target *target, const char *name, size_t *size) -{ - struct dt_property *p; - - p = dt_find_property(target, name); - if (p) { - if (size) - *size = p->len; - return p->prop; - } else if (size) - *size = 0; - - return NULL; -} - -uint64_t pdbg_get_address(struct pdbg_target *target, uint64_t *size) -{ - return dt_get_address(target, 0, size); -} - int pdbg_get_target_u32_property(struct pdbg_target *target, const char *name, uint32_t *val) { - struct dt_property *p; + uint32_t *p; + size_t size; - p = dt_find_property(target, name); - if (!p) - return -1; + p = pdbg_get_target_property(target, name, &size); + if (!p) + return -1; + + assert(size == 4); + *val = be32toh(*p); - *val = dt_get_number(p->prop, 1); - return 0; + return 0; } void pdbg_progress_tick(uint64_t cur, uint64_t end)