[coreboot-gerrit] New patch to review for coreboot: nvramcui: refactor code

Antonello Dettori (dev@dettori.io) gerrit at coreboot.org
Thu Aug 18 20:55:24 CEST 2016


Antonello Dettori (dev at dettori.io) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/16248

-gerrit

commit a7213990967b977aff22d6c7b9a4b8a6a2dde91f
Author: Antonello Dettori <dev at dettori.io>
Date:   Thu Aug 18 10:32:27 2016 +0200

    nvramcui: refactor code
    
    Split the main() into a couple of smaller functions in order to more
    easily extend the payload.
    
    Change-Id: I4c2b144e2a28c6f15e360d55c49974675e6a80d2
    Signed-off-by: Antonello Dettori <dev at dettori.io>
---
 payloads/nvramcui/nvramcui.c | 82 +++++++++++++++++++++++++++++---------------
 1 file changed, 54 insertions(+), 28 deletions(-)

diff --git a/payloads/nvramcui/nvramcui.c b/payloads/nvramcui/nvramcui.c
index 08fef01..991303e 100644
--- a/payloads/nvramcui/nvramcui.c
+++ b/payloads/nvramcui/nvramcui.c
@@ -56,44 +56,42 @@ void render_form(FORM *form)
 	wrefresh(w);
 }
 
-int main()
+/* determine number of options, and maximum option name length */
+static int count_cmos_options(struct cb_cmos_entries *option, int *numopts,
+		int *maxlength)
 {
-	int ch, done;
-
-	/* coreboot data structures */
-	lib_get_sysinfo();
-
-	struct cb_cmos_option_table *opttbl = get_system_option_table();
+	int n_opts = 0;
+	int max_l = 0;
 
-	if (opttbl == NULL) {
-		printf("Could not find coreboot option table\n");
-		halt();
-	}
-
-	/* prep CMOS layout into libcurses data structures */
-
-	/* determine number of options, and maximum option name length */
-	int numopts = 0;
-	int maxlength = 0;
-	struct cb_cmos_entries *option = first_cmos_entry(opttbl);
 	while (option) {
 		if ((option->config != 'r') &&
 		    (strcmp("check_sum", (char *)option->name) != 0)) {
-			maxlength = max(maxlength, strlen((char *)option->name));
-			numopts++;
+			max_l = max(max_l, strlen((char *)option->name));
+			n_opts++;
 		}
+
 		option = next_cmos_entry(option);
 	}
-	if (numopts == 0) {
+
+	if (n_opts == 0) {
 		printf("NO CMOS OPTIONS FOUND. EXITING!!!");
-		return 1;
+		return -1;
 	}
-	FIELD **fields = malloc(sizeof(FIELD *) * (2 * numopts + 1));
-	int i;
 
-	/* walk over options, fetch details */
-	option = first_cmos_entry(opttbl);
-	for (i = 0; i < numopts; i++) {
+	*numopts = n_opts;
+	*maxlength = max_l;
+
+	return 0;
+
+}
+
+/* walk over options, fetch details */
+static void cmos_walk_options(struct cb_cmos_option_table *opttbl,
+		FIELD **fields, int numopts, int maxlength)
+{
+	struct cb_cmos_entries *option = first_cmos_entry(opttbl);
+
+	for (int i = 0; i < numopts; i++) {
 		while ((option->config == 'r') ||
 		       (strcmp("check_sum", (char *)option->name) == 0)) {
 			option = next_cmos_entry(option);
@@ -168,7 +166,35 @@ int main()
 
 		option = next_cmos_entry(option);
 	}
+
 	fields[2 * numopts] = NULL;
+}
+
+int main(void)
+{
+	int ch, done;
+
+	/* coreboot data structures */
+	lib_get_sysinfo();
+
+	struct cb_cmos_option_table *opttbl = get_system_option_table();
+
+	if (opttbl == NULL && cmos_default == NULL) {
+		printf("Could not find coreboot option table/cmos.default.\n");
+		halt();
+	}
+
+	/* prep CMOS layout into libcurses data structures */
+
+	struct cb_cmos_entries *option = first_cmos_entry(opttbl);
+	int numopts = 0;
+	int maxlength = 0;
+
+	count_cmos_options(option, &numopts, &maxlength);
+
+	FIELD **fields = malloc(sizeof(FIELD *) * (2 * numopts + 1));
+
+	cmos_walk_options(opttbl, fields, numopts, maxlength);
 
 	/* display initialization */
 	initscr();
@@ -242,7 +268,7 @@ int main()
 
 	endwin();
 
-	for (i = 0; i < numopts; i++) {
+	for (int i = 0; i < numopts; i++) {
 		char *name = field_buffer(fields[2 * i], 0);
 		char *value = field_buffer(fields[2 * i + 1], 0);
 		char *ptr;



More information about the coreboot-gerrit mailing list