From 086336a22508affb7567bf6d383642554eda5a56 Mon Sep 17 00:00:00 2001 From: Heiko Stuebner Date: Wed, 23 Oct 2019 16:46:38 +0200 Subject: fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory() The change adding fdtdec_add_reserved_memory() already protected the added phandle against the phandlep being NULL - making the phandlep var optional. But in the early code checking for an already existing carveout this check was not done and thus the phandle assignment could run into trouble, so add a check there as well, which makes the function still return successfully if a matching region is found, even though no-one wants to work with the phandle. Fixes: c9222a08b3f7 ("fdtdec: Implement fdtdec_add_reserved_memory()") Signed-off-by: Heiko Stuebner Reviewed-by: Simon Glass --- lib/fdtdec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/fdtdec.c') diff --git a/lib/fdtdec.c b/lib/fdtdec.c index 125d9dbf26..38a0cff25e 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -1309,7 +1309,8 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename, } if (addr == carveout->start && (addr + size) == carveout->end) { - *phandlep = fdt_get_phandle(blob, node); + if (phandlep) + *phandlep = fdt_get_phandle(blob, node); return 0; } } -- cgit v1.2.3 From 357d2ceba0354e29462ac25924f5e42623c22b5b Mon Sep 17 00:00:00 2001 From: Heiko Stuebner Date: Wed, 23 Oct 2019 16:46:39 +0200 Subject: fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory() The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added. So just don't create a phandle in that case. Signed-off-by: Heiko Stuebner Reviewed-by: Simon Glass --- include/fdtdec.h | 2 +- lib/fdtdec.c | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/fdtdec.c') diff --git a/include/fdtdec.h b/include/fdtdec.h index e150975b27..696e0fd024 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -1061,7 +1061,7 @@ static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle) * @param basename base name of the node to create * @param carveout information about the carveout region * @param phandlep return location for the phandle of the carveout region - * can be NULL + * can be NULL if no phandle should be added * @return 0 on success or a negative error code on failure */ int fdtdec_add_reserved_memory(void *blob, const char *basename, diff --git a/lib/fdtdec.c b/lib/fdtdec.c index 38a0cff25e..61af3472e6 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -1339,13 +1339,15 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename, if (node < 0) return node; - err = fdt_generate_phandle(blob, &phandle); - if (err < 0) - return err; - - err = fdtdec_set_phandle(blob, node, phandle); - if (err < 0) - return err; + if (phandlep) { + err = fdt_generate_phandle(blob, &phandle); + if (err < 0) + return err; + + err = fdtdec_set_phandle(blob, node, phandle); + if (err < 0) + return err; + } /* store one or two address cells */ if (na > 1) -- cgit v1.2.3