diff mbox

[U-Boot,3/6] fdt: Add fdtdec functions to read byte array

Message ID 1322881071-11148-4-git-send-email-sjg@chromium.org
State New, archived
Headers show

Commit Message

Simon Glass Dec. 3, 2011, 2:57 a.m. UTC
From: Anton Staff <robotboy@chromium.org>

Sometimes we don't need a full cell for each value. This provides
a simple function to read a byte array, both with and without
copying it.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
 include/fdtdec.h |   32 ++++++++++++++++++++++++++++++++
 lib/fdtdec.c     |   24 ++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

Comments

Stephen Warren Dec. 5, 2011, 11:54 p.m. UTC | #1
On 12/02/2011 07:57 PM, Simon Glass wrote:
> From: Anton Staff <robotboy@chromium.org>
> 
> Sometimes we don't need a full cell for each value. This provides
> a simple function to read a byte array, both with and without
> copying it.

> +const u8 *fdtdec_locate_byte_array(const void *blob, int node,
> +			     const char *prop_name, int count)
> +{
> +	const u8 *cell;
> +	int err;
> +
> +	cell = get_prop_len(blob, node, prop_name, count, &err);

Isn't that called get_prop_check_min_len() now?

> +	if (err)
> +		return NULL;
> +	return cell;
> +}
Simon Glass Dec. 6, 2011, 12:27 a.m. UTC | #2
Hi Stephen,

On Mon, Dec 5, 2011 at 3:54 PM, Stephen Warren <swarren@nvidia.com> wrote:
> On 12/02/2011 07:57 PM, Simon Glass wrote:
>> From: Anton Staff <robotboy@chromium.org>
>>
>> Sometimes we don't need a full cell for each value. This provides
>> a simple function to read a byte array, both with and without
>> copying it.
>
>> +const u8 *fdtdec_locate_byte_array(const void *blob, int node,
>> +                          const char *prop_name, int count)
>> +{
>> +     const u8 *cell;
>> +     int err;
>> +
>> +     cell = get_prop_len(blob, node, prop_name, count, &err);
>
> Isn't that called get_prop_check_min_len() now?

It depends on your definition of 'now'. I will of course rebase this
series and test once the usb one is ready. At present it is probably
based on the original v1 usb series. But there may be other changes
too...

Regards,
Simon

>
>> +     if (err)
>> +             return NULL;
>> +     return cell;
>> +}
>
> --
> nvpublic
diff mbox

Patch

diff --git a/include/fdtdec.h b/include/fdtdec.h
index 9871b81..f72d219 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -231,3 +231,35 @@  int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  * @return 0 if all ok or gpio was FDT_GPIO_NONE; -1 on error
  */
 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio);
+
+/**
+ * Look up a property in a node and return its contents in a byte
+ * array of given length. The property must have at least enough data for
+ * the array (count bytes). It may have more, but this will be ignored.
+ *
+ * @param blob		FDT blob
+ * @param node		node to examine
+ * @param prop_name	name of property to find
+ * @param array		array to fill with data
+ * @param count		number of array elements
+ * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
+ *		or -FDT_ERR_BADLAYOUT if not enough data
+ */
+int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
+		u8 *array, int count);
+
+/**
+ * Look up a property in a node and return a pointer to its contents as a
+ * byte array of given length. The property must have at least enough data
+ * for the array (count bytes). It may have more, but this will be ignored.
+ * The data is not copied.
+ *
+ * @param blob		FDT blob
+ * @param node		node to examine
+ * @param prop_name	name of property to find
+ * @param count		number of array elements
+ * @return pointer to byte array if found, or NULL if the property is not
+ *		found or there is not enough data
+ */
+const u8 *fdtdec_locate_byte_array(const void *blob, int node,
+			     const char *prop_name, int count);
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index 509b81f..90db53c 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -315,3 +315,27 @@  int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
 		return gpio_direction_input(gpio->gpio);
 	}
 }
+
+int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
+		u8 *array, int count)
+{
+	const u8 *cell;
+	int err;
+
+	cell = get_prop_len(blob, node, prop_name, count, &err);
+	if (!err)
+		memcpy(array, cell, count);
+	return err;
+}
+
+const u8 *fdtdec_locate_byte_array(const void *blob, int node,
+			     const char *prop_name, int count)
+{
+	const u8 *cell;
+	int err;
+
+	cell = get_prop_len(blob, node, prop_name, count, &err);
+	if (err)
+		return NULL;
+	return cell;
+}