From: Chen Baozi Subject: Re: [PATCH v6 3/6] xen: Introduce a helper to read a u32 property in device tree. Date: Tue, 13 Aug 2013 09:32:52 +0800 Message-ID: <0C40CCA6-692D-4686-8BC7-EDCE96F27C47@gmail.com> References: <1376014823-28657-1-git-send-email-baozich@gmail.com> <1376014823-28657-4-git-send-email-baozich@gmail.com> <5208FCEF.8040908@linaro.org> Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Julien Grall Cc: Ian Campbell , Xen Developer List List-Id: xen-devel@lists.xenproject.org On Aug 13, 2013, at 6:19 AM, Julien Grall wrote: > On 12 August 2013 16:19, Julien Grall wrote: >> On 08/09/2013 03:20 AM, Chen Baozi wrote: >>> Signed-off-by: Chen Baozi >> Acked-by: Julien Grall >>> --- >>> xen/common/device_tree.c | 15 +++++++++++++++ >>> xen/include/xen/device_tree.h | 11 +++++++++++ >>> 2 files changed, 26 insertions(+) >>> >>> diff --git a/xen/common/device_tree.c b/xen/common/device_tree.c >>> index 84d704d..a4701eb 100644 >>> --- a/xen/common/device_tree.c >>> +++ b/xen/common/device_tree.c >>> @@ -574,6 +574,21 @@ const void *dt_get_property(const struct dt_device_node *np, >>> return pp ? pp->value : NULL; >>> } >>> >>> +bool_t dt_property_read_u32(const struct dt_device_node *np, >>> + const char *name, u32 *out_value) >>> +{ >>> + u32 len; >>> + const __be32 *val; >>> + >>> + val = dt_get_property(np, name, &len); >>> + if ( !val || len > sizeof(*out_value) ) >>> + return 0; > > Hum .. I have just noticed that the check is wrong. > Len must be greater or equal to sizeof (*out_value). Otherwise you can > read random bytes. Oops... I think I mixed up the "len" between linux codes and this. Thanks for catching it out! Baozi > > >>> + >>> + *out_value = be32_to_cpup(val); >>> + >>> + return 1; >>> +} >>> + >>> bool_t dt_device_is_compatible(const struct dt_device_node *device, >>> const char *compat) >>> { >>> diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h >>> index 5a2a5c6..faf727f 100644 >>> --- a/xen/include/xen/device_tree.h >>> +++ b/xen/include/xen/device_tree.h >>> @@ -300,6 +300,17 @@ const void *dt_get_property(const struct dt_device_node *np, >>> const char *name, u32 *lenp); >>> >>> /** >>> + * dt_property_read_u32 - Helper to read a u32 property. >>> + * @np: node to get the value >>> + * @name: name of the property >>> + * @out_value: pointer to return value >>> + * >>> + * Return true if get the desired value. >>> + */ >>> +bool_t dt_property_read_u32(const struct dt_device_node *np, >>> + const char *name, u32 *out_value); >>> + >>> +/** >>> * Checks if the given "compat" string matches one of the strings in >>> * the device's "compatible" property >>> */ >>> >> >> >> -- >> Julien Grall > > > > -- > Julien Grall