summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2011-11-17 14:30:06 -0800
committerGerrit <chrome-bot@google.com>2011-11-18 16:21:36 -0800
commita4a24c8a87ee3121bde71dde04e58b699670761d (patch)
tree4fa930ff7a747881ae5a0941e77fc193834070c1
parent318c9b21768d937e4a8984af86fec6fcd02242f2 (diff)
Tidy up fdt_decode_alloc_region() to make alloc separate
Move the malloc() out of fdt_decode_alloc_region() and rename it accordingly. This makes the code somewhat cleaner and allows us to print a sensible error message. BUG=chromium-os:17062 TEST=build and boot on Kaen Change-Id: I8edc8809baa42578e74c5e42cf47494b31b774e7 Reviewed-on: https://gerrit.chromium.org/gerrit/11878 Commit-Ready: Simon Glass <sjg@chromium.org> Reviewed-by: Simon Glass <sjg@chromium.org> Tested-by: Simon Glass <sjg@chromium.org>
-rw-r--r--common/fdt_decode.c14
-rw-r--r--include/fdt_decode.h10
-rw-r--r--lib/chromeos/fdt_decode.c20
3 files changed, 28 insertions, 16 deletions
diff --git a/common/fdt_decode.c b/common/fdt_decode.c
index b6beebda69..dab673a02c 100644
--- a/common/fdt_decode.c
+++ b/common/fdt_decode.c
@@ -23,7 +23,6 @@
#include <fdt_decode.h>
#include <libfdt.h>
-#include <malloc.h>
#include <serial.h>
/* we need a generic GPIO interface here */
@@ -703,22 +702,19 @@ int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config)
return fdt_decode_gpio(blob, node, "wp-gpio", &config->wp_gpio);
}
-void *fdt_decode_alloc_region(const void *blob, int node,
- const char *prop_name, size_t *size)
+int fdt_decode_region(const void *blob, int node,
+ const char *prop_name, void **ptrp, size_t *size)
{
const addr_t *cell;
- void *ptr;
int len;
debug("%s: %s\n", __func__, prop_name);
cell = fdt_getprop(blob, node, prop_name, &len);
if (!cell || (len != sizeof(addr_t) * 2))
- return NULL;
+ return -1;
- ptr = (void *)addr_to_cpu(*cell);
+ *ptrp = (void *)addr_to_cpu(*cell);
*size = size_to_cpu(cell[1]);
debug("%s: size=%zx\n", __func__, *size);
- if (!ptr)
- ptr = malloc(*size);
- return ptr;
+ return 0;
}
diff --git a/include/fdt_decode.h b/include/fdt_decode.h
index e787b902bf..6087c21861 100644
--- a/include/fdt_decode.h
+++ b/include/fdt_decode.h
@@ -600,8 +600,7 @@ int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config);
/**
* Look up a property in a node which contains a memory region address and
- * size. Then return a pointer to this address. if the address is zero, it is
- * allocated with malloc() instead.
+ * size. Then return a pointer to this address.
*
* The property must hold one address with a length. This is only tested on
* 32-bit machines.
@@ -609,8 +608,9 @@ int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config);
* @param blob FDT blob
* @param node node to examine
* @param prop_name name of property to find
+ * @param ptrp returns pointer to region, or NULL if no address
* @param size returns size of region
- * @return pointer to region, or NULL if property not found/malloc failed
+ * @return 0 if ok, -1 on error (propery not found)
*/
-void *fdt_decode_alloc_region(const void *blob, int node,
- const char *prop_name, size_t *size);
+int fdt_decode_region(const void *blob, int node,
+ const char *prop_name, void **ptrp, size_t *size);
diff --git a/lib/chromeos/fdt_decode.c b/lib/chromeos/fdt_decode.c
index fbc44a320e..3be76643d2 100644
--- a/lib/chromeos/fdt_decode.c
+++ b/lib/chromeos/fdt_decode.c
@@ -15,6 +15,7 @@
#include <chromeos/fmap.h>
#include <fdt_decode.h>
#include <linux/string.h>
+#include <malloc.h>
#define PREFIX "chromeos/fdt_decode: "
@@ -161,9 +162,24 @@ void *fdt_decode_chromeos_alloc_region(const void *blob,
const char *prop_name, size_t *size)
{
int node = fdt_path_offset(blob, "/chromeos-config");
+ void *ptr;
- if (node < 0)
+ if (node < 0) {
+ VBDEBUG(PREFIX "failed to find /chromeos-config in fdt'\n");
return NULL;
+ }
- return fdt_decode_alloc_region(blob, node, prop_name, size);
+ if (fdt_decode_region(blob, node, prop_name, &ptr, size)) {
+ VBDEBUG(PREFIX "failed to find %s in /chromeos-config'\n",
+ prop_name);
+ return NULL;
+ }
+
+ if (!ptr)
+ ptr = malloc(*size);
+ if (!ptr) {
+ VBDEBUG(PREFIX "failed to alloc %d bytes for %s'\n",
+ *size, prop_name);
+ }
+ return ptr;
}