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 <th3fanbus@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/52636
Reviewed-by: Patrick Rudolph <siro@das-labor.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/drivers/pc80/rtc/option.c b/src/drivers/pc80/rtc/option.c
index 409f0ef..5cc5ea5 100644
--- a/src/drivers/pc80/rtc/option.c
+++ b/src/drivers/pc80/rtc/option.c
@@ -68,30 +68,33 @@
 	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 @@
 	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;
 	}