Commit 26961d76 authored by Kees Cook's avatar Kees Cook

pstore/ram: Refactor DT size parsing

Refactor device tree size parsing routines to be able to pass a non-zero
default value for providing a configurable default for the coming
"max_reason" field. Also rename the helpers, since we're not always
parsing a size -- we're parsing a u32 and making sure it's not greater
than INT_MAX.

Link: https://lore.kernel.org/lkml/20200506211523.15077-4-keescook@chromium.org/
Link: https://lore.kernel.org/lkml/20200521205223.175957-1-tyhicks@linux.microsoft.comSigned-off-by: default avatarKees Cook <keescook@chromium.org>
parent f858b57f
...@@ -644,19 +644,25 @@ static int ramoops_init_prz(const char *name, ...@@ -644,19 +644,25 @@ static int ramoops_init_prz(const char *name,
return 0; return 0;
} }
static int ramoops_parse_dt_size(struct platform_device *pdev, /* Read a u32 from a dt property and make sure it's safe for an int. */
const char *propname, u32 *value) static int ramoops_parse_dt_u32(struct platform_device *pdev,
const char *propname,
u32 default_value, u32 *value)
{ {
u32 val32 = 0; u32 val32 = 0;
int ret; int ret;
ret = of_property_read_u32(pdev->dev.of_node, propname, &val32); ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
if (ret < 0 && ret != -EINVAL) { if (ret == -EINVAL) {
/* field is missing, use default value. */
val32 = default_value;
} else if (ret < 0) {
dev_err(&pdev->dev, "failed to parse property %s: %d\n", dev_err(&pdev->dev, "failed to parse property %s: %d\n",
propname, ret); propname, ret);
return ret; return ret;
} }
/* Sanity check our results. */
if (val32 > INT_MAX) { if (val32 > INT_MAX) {
dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32); dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
return -EOVERFLOW; return -EOVERFLOW;
...@@ -689,21 +695,22 @@ static int ramoops_parse_dt(struct platform_device *pdev, ...@@ -689,21 +695,22 @@ static int ramoops_parse_dt(struct platform_device *pdev,
pdata->mem_type = of_property_read_bool(of_node, "unbuffered"); pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops"); pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops");
#define parse_size(name, field) { \ #define parse_u32(name, field, default_value) { \
ret = ramoops_parse_dt_size(pdev, name, &value); \ ret = ramoops_parse_dt_u32(pdev, name, default_value, \
&value); \
if (ret < 0) \ if (ret < 0) \
return ret; \ return ret; \
field = value; \ field = value; \
} }
parse_size("record-size", pdata->record_size); parse_u32("record-size", pdata->record_size, 0);
parse_size("console-size", pdata->console_size); parse_u32("console-size", pdata->console_size, 0);
parse_size("ftrace-size", pdata->ftrace_size); parse_u32("ftrace-size", pdata->ftrace_size, 0);
parse_size("pmsg-size", pdata->pmsg_size); parse_u32("pmsg-size", pdata->pmsg_size, 0);
parse_size("ecc-size", pdata->ecc_info.ecc_size); parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0);
parse_size("flags", pdata->flags); parse_u32("flags", pdata->flags, 0);
#undef parse_size #undef parse_u32
/* /*
* Some old Chromebooks relied on the kernel setting the * Some old Chromebooks relied on the kernel setting the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment