blob: cb3553eec2005d074ee900e0053ed1c4b16bd530 [file] [log] [blame]
Patrick Georgi4b62ebe2012-11-16 15:05:11 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010014 */
15
16#include <stdlib.h>
17#include <string.h>
18
19#include <libpayload.h>
20#include <coreboot_tables.h>
21
22#include <curses.h>
23#include <menu.h>
24#include <form.h>
25
26#ifndef HOSTED
27#define HOSTED 0
28#endif
29
30static int min(int x, int y)
31{
32 if (x < y)
33 return x;
34 return y;
35}
36
37static int max(int x, int y)
38{
39 if (x > y)
40 return x;
41 return y;
42}
43
44void render_form(FORM *form)
45{
46 int y, x, line;
47 WINDOW *w = form_win(form);
48 WINDOW *inner_w = form_sub(form);
49 int numlines = getmaxy(w)-2;
50 getyx(inner_w, y, x);
51 line = y - (y % numlines);
52 WINDOW *der = derwin(w, getmaxy(w)-2, getmaxx(w)-2, 1, 1);
53 wclear(der);
54 wrefresh(der);
55 delwin(der);
56 copywin(inner_w, w, line, 0, 1, 1, min(numlines, getmaxy(inner_w)-line), 68, 0);
57 wmove(w, y + 1 - line, x + 1);
58 wrefresh(w);
59}
60
61int main()
62{
63 int ch, done;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010064
65 /* coreboot data structures */
66 lib_get_sysinfo();
67
68 struct cb_cmos_option_table *opttbl = get_system_option_table();
69
70 if (opttbl == NULL) {
71 printf("Could not find coreboot option table\n");
72 halt();
73 }
74
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010075 /* prep CMOS layout into libcurses data structures */
76
77 /* determine number of options, and maximum option name length */
78 int numopts=0;
79 int maxlength=0;
80 struct cb_cmos_entries *option = first_cmos_entry(opttbl);
81 while (option) {
82 if ((option->config != 'r') && (strcmp("check_sum", option->name) != 0)) {
83 maxlength = max(maxlength, strlen(option->name));
84 numopts++;
85 }
86 option = next_cmos_entry(option);
87 }
88 if (numopts == 0) {
89 printf("NO CMOS OPTIONS FOUND. EXITING!!!");
90 return 1;
91 }
92 FIELD **fields = malloc(sizeof(FIELD*)*(2*numopts+1));
93 int i;
94
95 /* walk over options, fetch details */
96 option = first_cmos_entry(opttbl);
97 for (i=0;i<numopts;i++) {
98 while ((option->config == 'r') || (strcmp("check_sum", option->name) == 0)) {
99 option = next_cmos_entry(option);
100 }
101 fields[2*i] = new_field(1, strlen(option->name), i*2, 1, 0, 0);
102 set_field_buffer(fields[2*i], 0, option->name);
103 field_opts_off(fields[2*i], O_ACTIVE);
104
105 fields[2*i+1] = new_field(1, 40, i*2, maxlength+2, 0, 0);
106 char *buf = NULL;
107 int fail = get_option_as_string(use_nvram, opttbl, &buf, option->name);
108 switch (option->config) {
109 case 'h': {
110 set_field_type(fields[2*i+1], TYPE_INTEGER, 0, 0, (1<<option->length)-1);
111 field_opts_on(fields[2*i+1], O_BLANK);
112 break;
113 }
114 case 's': {
115 set_max_field(fields[2*i+1], option->length/8);
116 field_opts_off(fields[2*i+1], O_STATIC);
117 break;
118 }
119 case 'e': {
120 int numvals = 0;
121 struct cb_cmos_enums *cmos_enum = first_cmos_enum_of_id(opttbl, option->config_id);
122
123 /* if invalid data in CMOS, set buf to first enum */
124 if (fail && cmos_enum) {
125 buf = cmos_enum->text;
126 }
127
128 while (cmos_enum) {
129 numvals++;
130 cmos_enum = next_cmos_enum_of_id(cmos_enum, option->config_id);
131 }
132
Lubomir Rintel18860d72015-02-01 16:56:58 +0100133 char **values = malloc(sizeof(char*)*(numvals + 1));
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100134 int cnt = 0;
135
136 cmos_enum = first_cmos_enum_of_id(opttbl, option->config_id);
137 while (cmos_enum) {
138 values[cnt] = cmos_enum->text;
139 cnt++;
140 cmos_enum = next_cmos_enum_of_id(cmos_enum, option->config_id);
141 }
142 values[cnt] = NULL;
143 field_opts_off(fields[2*i+1], O_EDIT);
144 set_field_type(fields[2*i+1], TYPE_ENUM, values, 1, 1);
145 free(values); // copied by set_field_type
146 break;
147 }
148 default:
149 break;
150 }
151 if (buf) set_field_buffer(fields[2*i+1], 0, buf);
152#if HOSTED
153// underline is non-trivial on VGA text
154 set_field_back(fields[2*i+1], A_UNDERLINE);
155#endif
156 field_opts_off(fields[2*i+1], O_BLANK | O_AUTOSKIP | O_NULLOK);
157
158 option = next_cmos_entry(option);
159 }
160 fields[2*numopts]=NULL;
161
Lubomir Rintel1ecc8af2015-02-24 20:33:35 +0100162 /* display initialization */
163 initscr();
164 keypad(stdscr, TRUE);
165 cbreak();
166 noecho();
167
168 if (start_color()) {
169 assume_default_colors (COLOR_BLUE, COLOR_CYAN);
170 }
171 leaveok(stdscr, TRUE);
172 curs_set(1);
173
174 erase();
175 box(stdscr, 0, 0);
176 mvaddstr(0, 2, "coreboot configuration utility");
177 refresh();
178
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100179 FORM *form = new_form(fields);
180 int numlines = min(numopts*2, 16);
181 WINDOW *w = newwin(numlines+2, 70, 2, 1);
182 WINDOW *inner_w = newpad(numopts*2, 68);
183 box(w, 0, 0);
184 mvwaddstr(w, 0, 2, "Press F1 when done");
185 set_form_win(form, w);
186 set_form_sub(form, inner_w);
187 post_form(form);
188
189 done = 0;
190 while(!done) {
Lubomir Rintel68009e92015-02-01 15:24:43 +0100191 render_form(form);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100192 ch=getch();
193 if (ch == ERR) continue;
194 switch (ch) {
195 case KEY_DOWN:
196 form_driver(form, REQ_NEXT_FIELD);
197 break;
198 case KEY_UP:
199 form_driver(form, REQ_PREV_FIELD);
200 break;
201 case KEY_LEFT:
202 if (field_type(current_field(form)) == TYPE_ENUM) {
203 form_driver(form, REQ_PREV_CHOICE);
204 } else {
205 form_driver(form, REQ_LEFT_CHAR);
206 }
207 break;
208 case KEY_RIGHT:
209 if (field_type(current_field(form)) == TYPE_ENUM) {
210 form_driver(form, REQ_NEXT_CHOICE);
211 } else {
212 form_driver(form, REQ_RIGHT_CHAR);
213 }
214 break;
215 case KEY_BACKSPACE:
216 case '\b':
217 form_driver(form, REQ_DEL_PREV);
218 break;
219 case KEY_DC:
220 form_driver(form, REQ_DEL_CHAR);
221 break;
222 case KEY_F(1):
223 done=1;
224 break;
225 default:
226 form_driver(form, ch);
227 break;
228 }
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100229 }
230
Lubomir Rintel1ecc8af2015-02-24 20:33:35 +0100231 endwin();
232
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100233 for (i = 0; i < numopts; i++) {
234 char *name = field_buffer(fields[2*i], 0);
235 char *value = field_buffer(fields[2*i+1], 0);
Vladimir Serbinenkoc9babb22014-01-15 22:07:52 +0100236 char *ptr;
237 for (ptr = value + strlen (value) - 1;
238 ptr >= value && *ptr == ' '; ptr--);
239 ptr[1] = '\0';
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100240 set_option_from_string(use_nvram, opttbl, value, name);
241 }
242
243 unpost_form(form);
244 free_form(form);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100245
Vladimir Serbinenkoc7282882014-01-15 22:09:25 +0100246 /* reboot */
247 outb (0x6, 0xcf9);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100248 halt();
249}