From 109a9ec339c6c3adc1a3714be46178db13cb8f18 Mon Sep 17 00:00:00 2001 From: Angel Pons Date: Fri, 23 Apr 2021 12:58:42 +0200 Subject: drivers/pc80/rtc: Factor out CMOS entry lookup The procedure is identical for reads and writes. Factor it out. Change-Id: I22b1d334270881734b34312f1fee01aa110a6db4 Signed-off-by: Angel Pons Reviewed-on: https://review.coreboot.org/c/coreboot/+/52636 Reviewed-by: Patrick Rudolph Tested-by: build bot (Jenkins) --- src/drivers/pc80/rtc/option.c | 50 +++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/drivers/pc80/rtc/option.c b/src/drivers/pc80/rtc/option.c index 409f0efb3b..5cc5ea558e 100644 --- a/src/drivers/pc80/rtc/option.c +++ b/src/drivers/pc80/rtc/option.c @@ -68,30 +68,33 @@ static struct cmos_option_table *get_cmos_layout(void) return ct; } +static struct cmos_entries *find_cmos_entry(struct cmos_option_table *ct, const char *name) +{ + /* Figure out how long name is */ + const size_t namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); + struct cmos_entries *ce; + + /* Find the requested entry record */ + ce = (struct cmos_entries *)((unsigned char *)ct + ct->header_length); + for (; ce->tag == LB_TAG_OPTION; + ce = (struct cmos_entries *)((unsigned char *)ce + ce->size)) { + if (memcmp(ce->name, name, namelen) == 0) + return ce; + } + return NULL; +} + enum cb_err cmos_get_option(void *dest, const char *name) { struct cmos_option_table *ct; struct cmos_entries *ce; - size_t namelen; - int found = 0; - - /* Figure out how long name is */ - namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); ct = get_cmos_layout(); if (!ct) return CB_CMOS_LAYOUT_NOT_FOUND; - /* find the requested entry record */ - ce = (struct cmos_entries *)((unsigned char *)ct + ct->header_length); - for (; ce->tag == LB_TAG_OPTION; - ce = (struct cmos_entries *)((unsigned char *)ce + ce->size)) { - if (memcmp(ce->name, name, namelen) == 0) { - found = 1; - break; - } - } - if (!found) { + ce = find_cmos_entry(ct, name); + if (!ce) { printk(BIOS_DEBUG, "No CMOS option '%s'.\n", name); return CB_CMOS_OPTION_NOT_FOUND; } @@ -151,26 +154,13 @@ enum cb_err cmos_set_option(const char *name, void *value) struct cmos_option_table *ct; struct cmos_entries *ce; unsigned long length; - size_t namelen; - int found = 0; - - /* Figure out how long name is */ - namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); ct = get_cmos_layout(); if (!ct) return CB_CMOS_LAYOUT_NOT_FOUND; - /* find the requested entry record */ - ce = (struct cmos_entries *)((unsigned char *)ct + ct->header_length); - for (; ce->tag == LB_TAG_OPTION; - ce = (struct cmos_entries *)((unsigned char *)ce + ce->size)) { - if (memcmp(ce->name, name, namelen) == 0) { - found = 1; - break; - } - } - if (!found) { + ce = find_cmos_entry(ct, name); + if (!ce) { printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); return CB_CMOS_OPTION_NOT_FOUND; } -- cgit v1.2.3