nvramcui: Reformat nvramcui.c

Change-Id: I89dca25d93a4c94cc51f313397e49ba763948450
Signed-off-by: Martin Roth <martinroth@google.com>
Reviewed-on: https://review.coreboot.org/14484
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Martin Roth 2016-04-23 17:24:57 -06:00
parent 9bc6674b7c
commit f62065f15b
1 changed files with 68 additions and 52 deletions

View File

@ -13,12 +13,12 @@
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
#include <libpayload.h>
#include <coreboot_tables.h> #include <coreboot_tables.h>
#include <libpayload.h>
#include <curses.h> #include <curses.h>
#include <menu.h>
#include <form.h> #include <form.h>
#include <menu.h>
#ifndef HOSTED #ifndef HOSTED
#define HOSTED 0 #define HOSTED 0
@ -43,14 +43,15 @@ void render_form(FORM *form)
int y, x, line; int y, x, line;
WINDOW *w = form_win(form); WINDOW *w = form_win(form);
WINDOW *inner_w = form_sub(form); WINDOW *inner_w = form_sub(form);
int numlines = getmaxy(w)-2; int numlines = getmaxy(w) - 2;
getyx(inner_w, y, x); getyx(inner_w, y, x);
line = y - (y % numlines); line = y - (y % numlines);
WINDOW *der = derwin(w, getmaxy(w)-2, getmaxx(w)-2, 1, 1); WINDOW *der = derwin(w, getmaxy(w) - 2, getmaxx(w) - 2, 1, 1);
wclear(der); wclear(der);
wrefresh(der); wrefresh(der);
delwin(der); delwin(der);
copywin(inner_w, w, line, 0, 1, 1, min(numlines, getmaxy(inner_w)-line), 68, 0); copywin(inner_w, w, line, 0, 1, 1,
min(numlines, getmaxy(inner_w) - line), 68, 0);
wmove(w, y + 1 - line, x + 1); wmove(w, y + 1 - line, x + 1);
wrefresh(w); wrefresh(w);
} }
@ -72,11 +73,12 @@ int main()
/* prep CMOS layout into libcurses data structures */ /* prep CMOS layout into libcurses data structures */
/* determine number of options, and maximum option name length */ /* determine number of options, and maximum option name length */
int numopts=0; int numopts = 0;
int maxlength=0; int maxlength = 0;
struct cb_cmos_entries *option = first_cmos_entry(opttbl); struct cb_cmos_entries *option = first_cmos_entry(opttbl);
while (option) { while (option) {
if ((option->config != 'r') && (strcmp("check_sum", option->name) != 0)) { if ((option->config != 'r') &&
(strcmp("check_sum", option->name) != 0)) {
maxlength = max(maxlength, strlen(option->name)); maxlength = max(maxlength, strlen(option->name));
numopts++; numopts++;
} }
@ -86,36 +88,42 @@ int main()
printf("NO CMOS OPTIONS FOUND. EXITING!!!"); printf("NO CMOS OPTIONS FOUND. EXITING!!!");
return 1; return 1;
} }
FIELD **fields = malloc(sizeof(FIELD*)*(2*numopts+1)); FIELD **fields = malloc(sizeof(FIELD *) * (2 * numopts + 1));
int i; int i;
/* walk over options, fetch details */ /* walk over options, fetch details */
option = first_cmos_entry(opttbl); option = first_cmos_entry(opttbl);
for (i=0;i<numopts;i++) { for (i = 0; i < numopts; i++) {
while ((option->config == 'r') || (strcmp("check_sum", option->name) == 0)) { while ((option->config == 'r') ||
(strcmp("check_sum", option->name) == 0)) {
option = next_cmos_entry(option); option = next_cmos_entry(option);
} }
fields[2*i] = new_field(1, strlen(option->name), i*2, 1, 0, 0); fields[2 * i] =
set_field_buffer(fields[2*i], 0, option->name); new_field(1, strlen(option->name), i * 2, 1, 0, 0);
field_opts_off(fields[2*i], O_ACTIVE); set_field_buffer(fields[2 * i], 0, option->name);
field_opts_off(fields[2 * i], O_ACTIVE);
fields[2*i+1] = new_field(1, 40, i*2, maxlength+2, 0, 0); fields[2 * i + 1] =
new_field(1, 40, i * 2, maxlength + 2, 0, 0);
char *buf = NULL; char *buf = NULL;
int fail = get_option_as_string(use_nvram, opttbl, &buf, option->name); int fail =
get_option_as_string(use_nvram, opttbl, &buf, option->name);
switch (option->config) { switch (option->config) {
case 'h': { case 'h': {
set_field_type(fields[2*i+1], TYPE_INTEGER, 0, 0, (1<<option->length)-1); set_field_type(fields[2 * i + 1], TYPE_INTEGER, 0, 0,
field_opts_on(fields[2*i+1], O_BLANK); (1 << option->length) - 1);
field_opts_on(fields[2 * i + 1], O_BLANK);
break; break;
} }
case 's': { case 's': {
set_max_field(fields[2*i+1], option->length/8); set_max_field(fields[2 * i + 1], option->length / 8);
field_opts_off(fields[2*i+1], O_STATIC); field_opts_off(fields[2 * i + 1], O_STATIC);
break; break;
} }
case 'e': { case 'e': {
int numvals = 0; int numvals = 0;
struct cb_cmos_enums *cmos_enum = first_cmos_enum_of_id(opttbl, option->config_id); struct cb_cmos_enums *cmos_enum =
first_cmos_enum_of_id(opttbl, option->config_id);
/* if invalid data in CMOS, set buf to first enum */ /* if invalid data in CMOS, set buf to first enum */
if (fail && cmos_enum) { if (fail && cmos_enum) {
@ -124,37 +132,43 @@ int main()
while (cmos_enum) { while (cmos_enum) {
numvals++; numvals++;
cmos_enum = next_cmos_enum_of_id(cmos_enum, option->config_id); cmos_enum = next_cmos_enum_of_id(
cmos_enum, option->config_id);
} }
char **values = malloc(sizeof(char*)*(numvals + 1)); char **values = malloc(sizeof(char *) * (numvals + 1));
int cnt = 0; int cnt = 0;
cmos_enum = first_cmos_enum_of_id(opttbl, option->config_id); cmos_enum =
first_cmos_enum_of_id(opttbl, option->config_id);
while (cmos_enum) { while (cmos_enum) {
values[cnt] = cmos_enum->text; values[cnt] = cmos_enum->text;
cnt++; cnt++;
cmos_enum = next_cmos_enum_of_id(cmos_enum, option->config_id); cmos_enum = next_cmos_enum_of_id(
cmos_enum, option->config_id);
} }
values[cnt] = NULL; values[cnt] = NULL;
field_opts_off(fields[2*i+1], O_EDIT); field_opts_off(fields[2 * i + 1], O_EDIT);
set_field_type(fields[2*i+1], TYPE_ENUM, values, 1, 1); set_field_type(fields[2 * i + 1], TYPE_ENUM, values, 1,
1);
free(values); // copied by set_field_type free(values); // copied by set_field_type
break; break;
}
default:
break;
} }
if (buf) set_field_buffer(fields[2*i+1], 0, buf); default:
break;
}
if (buf)
set_field_buffer(fields[2 * i + 1], 0, buf);
#if HOSTED #if HOSTED
// underline is non-trivial on VGA text // underline is non-trivial on VGA text
set_field_back(fields[2*i+1], A_UNDERLINE); set_field_back(fields[2 * i + 1], A_UNDERLINE);
#endif #endif
field_opts_off(fields[2*i+1], O_BLANK | O_AUTOSKIP | O_NULLOK); field_opts_off(fields[2 * i + 1],
O_BLANK | O_AUTOSKIP | O_NULLOK);
option = next_cmos_entry(option); option = next_cmos_entry(option);
} }
fields[2*numopts]=NULL; fields[2 * numopts] = NULL;
/* display initialization */ /* display initialization */
initscr(); initscr();
@ -163,7 +177,7 @@ int main()
noecho(); noecho();
if (start_color()) { if (start_color()) {
assume_default_colors (COLOR_BLUE, COLOR_CYAN); assume_default_colors(COLOR_BLUE, COLOR_CYAN);
} }
leaveok(stdscr, TRUE); leaveok(stdscr, TRUE);
curs_set(1); curs_set(1);
@ -174,9 +188,9 @@ int main()
refresh(); refresh();
FORM *form = new_form(fields); FORM *form = new_form(fields);
int numlines = min(numopts*2, 16); int numlines = min(numopts * 2, 16);
WINDOW *w = newwin(numlines+2, 70, 2, 1); WINDOW *w = newwin(numlines + 2, 70, 2, 1);
WINDOW *inner_w = newpad(numopts*2, 68); WINDOW *inner_w = newpad(numopts * 2, 68);
box(w, 0, 0); box(w, 0, 0);
mvwaddstr(w, 0, 2, "Press F1 when done"); mvwaddstr(w, 0, 2, "Press F1 when done");
set_form_win(form, w); set_form_win(form, w);
@ -184,10 +198,11 @@ int main()
post_form(form); post_form(form);
done = 0; done = 0;
while(!done) { while (!done) {
render_form(form); render_form(form);
ch=getch(); ch = getch();
if (ch == ERR) continue; if (ch == ERR)
continue;
switch (ch) { switch (ch) {
case KEY_DOWN: case KEY_DOWN:
form_driver(form, REQ_NEXT_FIELD); form_driver(form, REQ_NEXT_FIELD);
@ -217,7 +232,7 @@ int main()
form_driver(form, REQ_DEL_CHAR); form_driver(form, REQ_DEL_CHAR);
break; break;
case KEY_F(1): case KEY_F(1):
done=1; done = 1;
break; break;
default: default:
form_driver(form, ch); form_driver(form, ch);
@ -228,11 +243,12 @@ int main()
endwin(); endwin();
for (i = 0; i < numopts; i++) { for (i = 0; i < numopts; i++) {
char *name = field_buffer(fields[2*i], 0); char *name = field_buffer(fields[2 * i], 0);
char *value = field_buffer(fields[2*i+1], 0); char *value = field_buffer(fields[2 * i + 1], 0);
char *ptr; char *ptr;
for (ptr = value + strlen (value) - 1; for (ptr = value + strlen(value) - 1;
ptr >= value && *ptr == ' '; ptr--); ptr >= value && *ptr == ' '; ptr--)
;
ptr[1] = '\0'; ptr[1] = '\0';
set_option_from_string(use_nvram, opttbl, value, name); set_option_from_string(use_nvram, opttbl, value, name);
} }
@ -241,6 +257,6 @@ int main()
free_form(form); free_form(form);
/* reboot */ /* reboot */
outb (0x6, 0xcf9); outb(0x6, 0xcf9);
halt(); halt();
} }