All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lothar Waßmann" <LW@KARO-electronics.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 05/71] dm: Add a function to create a 'live' device tree
Date: Thu, 11 May 2017 16:59:04 +0200	[thread overview]
Message-ID: <20170511165904.7eb355d2@ipc1.ka-ro> (raw)
In-Reply-To: <20170510142150.30515-6-sjg@chromium.org>

Hi,

On Wed, 10 May 2017 08:20:44 -0600 Simon Glass wrote:
> This function converts the flat device tree into a hierarchical one with
> C structures and pointers. This is easier to access.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
> Changes in v2: None
> 
>  include/of_live.h |  24 ++++
>  lib/Makefile      |   1 +
>  lib/of_live.c     | 333 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 358 insertions(+)
>  create mode 100644 include/of_live.h
>  create mode 100644 lib/of_live.c
> 
> diff --git a/include/of_live.h b/include/of_live.h
> new file mode 100644
> index 0000000000..f5303bb018
> --- /dev/null
> +++ b/include/of_live.h
> @@ -0,0 +1,24 @@
> +/*
> + * Copyright (c) 2017 Google, Inc
> + * Written by Simon Glass <sjg@chromium.org>
> + *
> + * SPDX-License-Identifier:	GPL-2.0+
> + *
> + * Support for a 'live' (as opposed to flat) device tree
> + */
> +
> +#ifndef _OF_LIVE_H
> +#define _OF_LIVE_H
> +
> +struct device_node;
> +
> +/**
> + * of_live_build() - build a live (hierarchical) tree from a flat DT
> + *
> + * @fdt_blob: Input tree to convert
> + * @rootp: Returns live tree that was created
> + * @return 0 if OK, -ve on error
> + */
> +int of_live_build(const void *fdt_blob, struct device_node **rootp);
> +
> +#endif
> diff --git a/lib/Makefile b/lib/Makefile
> index 23e9f1ef11..bc2fb0a361 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -15,6 +15,7 @@ obj-$(CONFIG_ZLIB) += zlib/
>  obj-$(CONFIG_BZIP2) += bzip2/
>  obj-$(CONFIG_TIZEN) += tizen/
>  obj-$(CONFIG_FIT) += libfdt/
> +obj-$(CONFIG_OF_LIVE) += of_live.o
>  obj-$(CONFIG_CMD_DHRYSTONE) += dhry/
>  
>  obj-$(CONFIG_AES) += aes.o
> diff --git a/lib/of_live.c b/lib/of_live.c
> new file mode 100644
> index 0000000000..51927f9e91
> --- /dev/null
> +++ b/lib/of_live.c
> @@ -0,0 +1,333 @@
> +/*
> + * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
> + * benh at kernel.crashing.org
> + *
> + * Based on parts of drivers/of/fdt.c from Linux v4.9
> + * Modifications for U-Boot
> + * Copyright (c) 2017 Google, Inc
> + *
> + * SPDX-License-Identifier:	GPL-2.0+
> + */
> +
> +#include <common.h>
> +#include <libfdt.h>
> +#include <of_live.h>
> +#include <malloc.h>
> +#include <dm/of_access.h>
> +#include <linux/err.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +static void *unflatten_dt_alloc(void **mem, unsigned long size,
> +				unsigned long align)
> +{
> +	void *res;
> +
> +	*mem = PTR_ALIGN(*mem, align);
> +	res = *mem;
> +	*mem += size;
> +
> +	return res;
> +}
> +
> +/**
> + * unflatten_dt_node() - Alloc and populate a device_node from the flat tree
> + * @blob: The parent device tree blob
> + * @mem: Memory chunk to use for allocating device nodes and properties
> + * @poffset: pointer to node in flat tree
> + * @dad: Parent struct device_node
> + * @nodepp: The device_node tree created by the call
> + * @fpsize: Size of the node path up at t05he current depth.
> + * @dryrun: If true, do not allocate device nodes but still calculate needed
> + * memory size
> + */
> +static void *unflatten_dt_node(const void *blob, void *mem, int *poffset,
> +			       struct device_node *dad,
> +			       struct device_node **nodepp,
> +			       unsigned long fpsize, bool dryrun)
> +{
> +	const __be32 *p;
> +	struct device_node *np;
> +	struct property *pp, **prev_pp = NULL;
> +	const char *pathp;
> +	int l;
> +	unsigned int allocl;
> +	static int depth;
> +	int old_depth;
> +	int offset;
> +	int has_name = 0;
> +	int new_format = 0;
> +
> +	pathp = fdt_get_name(blob, *poffset, &l);
> +	if (!pathp)
> +		return mem;
> +
> +	allocl = ++l;
> +
> +	/*
> +	 * version 0x10 has a more compact unit name here instead of the full
> +	 * path. we accumulate the full path size using "fpsize", we'll rebuild
> +	 * it later. We detect this because the first character of the name is
> +	 * not '/'.
> +	 */
> +	if ((*pathp) != '/') {
> +		new_format = 1;
> +		if (fpsize == 0) {
> +			/*
> +			 * root node: special case. fpsize accounts for path
> +			 * plus terminating zero. root node only has '/', so
> +			 * fpsize should be 2, but we want to avoid the first
> +			 * level nodes to have two '/' so we use fpsize 1 here
> +			 */
> +			fpsize = 1;
> +			allocl = 2;
> +			l = 1;
> +			pathp = "";
> +		} else {
> +			/*
> +			 * account for '/' and path size minus terminal 0
> +			 * already in 'l'
> +			 */
> +			fpsize += l;
> +			allocl = fpsize;
> +		}
> +	}
> +
> +	np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
> +				__alignof__(struct device_node));
> +	if (!dryrun) {
> +		char *fn;
> +
> +		fn = (char *)np + sizeof(*np);
> +		np->full_name = fn;
> +		if (new_format) {
> +			/* rebuild full path for new format */
> +			if (dad && dad->parent) {
> +				strcpy(fn, dad->full_name);
>
strcpy() is EVIL! How do you ascertain, that the string
at dad->full_name cannot overflow the buffer at fn?
 

Lothar Waßmann

  reply	other threads:[~2017-05-11 14:59 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-10 14:20 [U-Boot] [PATCH v2 00/71] dm: Add support for a 'live' device tree Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 01/71] dm: core: Set return value first in lists_bind_fdt() Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 02/71] Update WARN_ON() to return a value Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 03/71] dm: core: Add livetree definitions Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 04/71] dm: core: Add livetree access functions Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 05/71] dm: Add a function to create a 'live' device tree Simon Glass
2017-05-11 14:59   ` Lothar Waßmann [this message]
2017-05-15  3:03     ` Simon Glass
2017-05-15  7:34       ` Lothar Waßmann
2017-05-16  0:18         ` Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 06/71] dm: Build a live tree after relocation Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 07/71] dm: core: Rename of_device_is_compatible() Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 08/71] dm: core: Add operations on device tree references Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 09/71] dm: core: Add livetree address functions Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 10/71] fdt: Update fdt_get_base_address() to use const Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 11/71] dm: core: Add address operations on device tree references Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 12/71] dm: core: Add a place to put extra device-tree reading functions Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 13/71] dm: core: Add device-based 'read' functions to access DT Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 14/71] dm: core: Implement live tree 'read' functions Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 15/71] dm: core: Allow binding a device from a live tree Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 16/71] dm: core: Update lists_bind_fdt() to use ofnode Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 17/71] dm: core: Update device_bind_driver_to_node() " Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 18/71] dm: core: Scan the live tree when setting up driver model Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 19/71] dm: core: Add a way to find a device by ofnode Simon Glass
2017-05-10 14:20 ` [U-Boot] [PATCH v2 20/71] dm: regmap: Add support for livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 21/71] dm: simple-bus: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 22/71] dm: core: Update uclass_find_device_by_phandle() " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 23/71] sandbox: Add a way to reset sandbox state for tests Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 24/71] dm: test: Move test running code into a separate function Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 25/71] dm: test: Show the test filename when running Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 26/71] dm: test: Add support for running tests with livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 27/71] dm: core: Run tests with both livetree and flat tree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 28/71] dm: gpio: Refactor to prepare for live tree support Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 29/71] dm: gpio: Drop blank line in gpio_xlate_offs_flags() comment Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 30/71] dm: gpio: sandbox: Use dev_read...() functions to access DT Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 31/71] dm: gpio: Add live tree support Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 32/71] cros_ec: Fix debug() statement in ec_command_inptr() Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 33/71] cros_ec: Convert to support live tree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 34/71] sandbox: Add a new sandbox_flattree board Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 35/71] test: Update 'make test' to run more tests Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 36/71] fdt: Rename a few functions in fdt_support Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 37/71] dm: Add more livetree helpers and definitions Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 38/71] string: Add strchrnul() Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 39/71] string: Add strcspn() Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 40/71] dm: i2c: Convert uclass to livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 41/71] dm: pmic: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 42/71] sandbox: pmic: Convert pmic emulator to support livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 43/71] dm: regulator: Convert regulator uclass " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 44/71] dm: regulator: Update fixed regulator " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 45/71] dm: mmc: Convert uclass to livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 46/71] dm: adc: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 47/71] dm: usb: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 48/71] sandbox: usb: Convert emulators " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 49/71] clk: Modify xlate() method for livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 50/71] dm: clk: Update uclass to support livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 51/71] dm: clk: fixed: Update " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 52/71] dm: test: Separate out the bus DT offset test Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 53/71] dm: test: Disable the fdt_offset test with livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 54/71] dm: phy: Update tests to use ut_asserteq() Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 55/71] dm: mailbox: Update uclass to support livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 56/71] dm: phy: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 57/71] sandbox: phy: Update driver for livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 58/71] dm: power-domain: Update uclass to support livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 59/71] dm: reset: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 60/71] dm: pci: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 61/71] dm: Update the I2C eeprom driver for livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 62/71] cros_ec: Update the cros_ec keyboard driver to livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 63/71] dm: spi: Convert uclass " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 64/71] dm: sandbox: i2c: Drop fdtdec.h header Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 65/71] dm: sandbox: i2c_rtc: " Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 66/71] dm: spi-flash: Convert uclass to livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 67/71] dm: sandbox: spi: Convert driver to support livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 68/71] dm: sandbox: sysreset: Convert driver to livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 69/71] dm: test: Fix nit with position of backslash Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 70/71] dm: gpio: power: Convert pm8916 drivers to livetree Simon Glass
2017-05-10 14:21 ` [U-Boot] [PATCH v2 71/71] sandbox: Move to use live tree Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170511165904.7eb355d2@ipc1.ka-ro \
    --to=lw@karo-electronics.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.