blob: db3e522d605f9d296c92f3db8716d319e57080dd [file] [log] [blame]
Patrick Georgi4b62ebe2012-11-16 15:05:11 +01001/*
Patrick Georgi4b62ebe2012-11-16 15:05:11 +01002 *
3 * Copyright (C) 2012 secunet Security Networks AG
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010013 */
14
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010015#include <coreboot_tables.h>
Martin Rothf62065f2016-04-23 17:24:57 -060016#include <libpayload.h>
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010017
18#include <curses.h>
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010019#include <form.h>
Martin Rothf62065f2016-04-23 17:24:57 -060020#include <menu.h>
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010021
22#ifndef HOSTED
23#define HOSTED 0
24#endif
25
26static int min(int x, int y)
27{
28 if (x < y)
29 return x;
30 return y;
31}
32
33static int max(int x, int y)
34{
35 if (x > y)
36 return x;
37 return y;
38}
39
Nico Huber92675d62020-11-15 19:29:22 +010040static void render_form(FORM *form)
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010041{
42 int y, x, line;
43 WINDOW *w = form_win(form);
44 WINDOW *inner_w = form_sub(form);
Martin Rothf62065f2016-04-23 17:24:57 -060045 int numlines = getmaxy(w) - 2;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010046 getyx(inner_w, y, x);
47 line = y - (y % numlines);
Martin Rothf62065f2016-04-23 17:24:57 -060048 WINDOW *der = derwin(w, getmaxy(w) - 2, getmaxx(w) - 2, 1, 1);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010049 wclear(der);
50 wrefresh(der);
51 delwin(der);
Martin Rothf62065f2016-04-23 17:24:57 -060052 copywin(inner_w, w, line, 0, 1, 1,
53 min(numlines, getmaxy(inner_w) - line), 68, 0);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010054 wmove(w, y + 1 - line, x + 1);
55 wrefresh(w);
56}
57
Antonello Dettori904dd302016-08-18 10:32:27 +020058/* determine number of options, and maximum option name length */
59static int count_cmos_options(struct cb_cmos_entries *option, int *numopts,
60 int *maxlength)
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010061{
Antonello Dettori904dd302016-08-18 10:32:27 +020062 int n_opts = 0;
63 int max_l = 0;
Martin Rothf62065f2016-04-23 17:24:57 -060064
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010065 while (option) {
Martin Rothf62065f2016-04-23 17:24:57 -060066 if ((option->config != 'r') &&
Martin Roth0cf7acc2016-04-26 12:33:44 -060067 (strcmp("check_sum", (char *)option->name) != 0)) {
Antonello Dettori904dd302016-08-18 10:32:27 +020068 max_l = max(max_l, strlen((char *)option->name));
69 n_opts++;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010070 }
Antonello Dettori904dd302016-08-18 10:32:27 +020071
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010072 option = next_cmos_entry(option);
73 }
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010074
Antonello Dettori904dd302016-08-18 10:32:27 +020075 if (n_opts == 0) {
76 printf("NO CMOS OPTIONS FOUND. EXITING!!!");
77 return -1;
78 }
79
80 *numopts = n_opts;
81 *maxlength = max_l;
82
83 return 0;
84
85}
86
87/* walk over options, fetch details */
88static void cmos_walk_options(struct cb_cmos_option_table *opttbl,
89 FIELD **fields, int numopts, int maxlength)
90{
91 struct cb_cmos_entries *option = first_cmos_entry(opttbl);
Paul Menzele7385d12017-01-20 23:29:21 +010092 int i;
Antonello Dettori904dd302016-08-18 10:32:27 +020093
Paul Menzele7385d12017-01-20 23:29:21 +010094 for (i = 0; i < numopts; i++) {
Martin Rothf62065f2016-04-23 17:24:57 -060095 while ((option->config == 'r') ||
Martin Roth0cf7acc2016-04-26 12:33:44 -060096 (strcmp("check_sum", (char *)option->name) == 0)) {
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010097 option = next_cmos_entry(option);
98 }
Martin Rothf62065f2016-04-23 17:24:57 -060099 fields[2 * i] =
Martin Roth0cf7acc2016-04-26 12:33:44 -0600100 new_field(1, strlen((char *)option->name), i * 2, 1, 0, 0);
101 set_field_buffer(fields[2 * i], 0, (char *)option->name);
Martin Rothf62065f2016-04-23 17:24:57 -0600102 field_opts_off(fields[2 * i], O_ACTIVE);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100103
Martin Rothf62065f2016-04-23 17:24:57 -0600104 fields[2 * i + 1] =
105 new_field(1, 40, i * 2, maxlength + 2, 0, 0);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100106 char *buf = NULL;
Martin Rothf62065f2016-04-23 17:24:57 -0600107 int fail =
Martin Roth0cf7acc2016-04-26 12:33:44 -0600108 get_option_as_string(use_nvram, opttbl, &buf, (char *)option->name);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100109 switch (option->config) {
110 case 'h': {
Martin Rothf62065f2016-04-23 17:24:57 -0600111 set_field_type(fields[2 * i + 1], TYPE_INTEGER, 0, 0,
112 (1 << option->length) - 1);
113 field_opts_on(fields[2 * i + 1], O_BLANK);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100114 break;
Martin Rothf62065f2016-04-23 17:24:57 -0600115 }
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100116 case 's': {
Martin Rothf62065f2016-04-23 17:24:57 -0600117 set_max_field(fields[2 * i + 1], option->length / 8);
118 field_opts_off(fields[2 * i + 1], O_STATIC);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100119 break;
Martin Rothf62065f2016-04-23 17:24:57 -0600120 }
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100121 case 'e': {
122 int numvals = 0;
Martin Rothf62065f2016-04-23 17:24:57 -0600123 struct cb_cmos_enums *cmos_enum =
124 first_cmos_enum_of_id(opttbl, option->config_id);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100125
126 /* if invalid data in CMOS, set buf to first enum */
127 if (fail && cmos_enum) {
Martin Roth0cf7acc2016-04-26 12:33:44 -0600128 buf = (char *)cmos_enum->text;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100129 }
130
131 while (cmos_enum) {
132 numvals++;
Martin Rothf62065f2016-04-23 17:24:57 -0600133 cmos_enum = next_cmos_enum_of_id(
134 cmos_enum, option->config_id);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100135 }
136
Martin Rothf62065f2016-04-23 17:24:57 -0600137 char **values = malloc(sizeof(char *) * (numvals + 1));
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100138 int cnt = 0;
139
Martin Rothf62065f2016-04-23 17:24:57 -0600140 cmos_enum =
141 first_cmos_enum_of_id(opttbl, option->config_id);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100142 while (cmos_enum) {
Martin Roth0cf7acc2016-04-26 12:33:44 -0600143 values[cnt] = (char *)cmos_enum->text;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100144 cnt++;
Martin Rothf62065f2016-04-23 17:24:57 -0600145 cmos_enum = next_cmos_enum_of_id(
146 cmos_enum, option->config_id);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100147 }
148 values[cnt] = NULL;
Martin Rothf62065f2016-04-23 17:24:57 -0600149 field_opts_off(fields[2 * i + 1], O_EDIT);
150 set_field_type(fields[2 * i + 1], TYPE_ENUM, values, 1,
151 1);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100152 free(values); // copied by set_field_type
153 break;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100154 }
Martin Rothf62065f2016-04-23 17:24:57 -0600155 default:
156 break;
157 }
158 if (buf)
159 set_field_buffer(fields[2 * i + 1], 0, buf);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100160#if HOSTED
Martin Rothf62065f2016-04-23 17:24:57 -0600161 // underline is non-trivial on VGA text
162 set_field_back(fields[2 * i + 1], A_UNDERLINE);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100163#endif
Martin Rothf62065f2016-04-23 17:24:57 -0600164 field_opts_off(fields[2 * i + 1],
165 O_BLANK | O_AUTOSKIP | O_NULLOK);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100166
167 option = next_cmos_entry(option);
168 }
Antonello Dettori904dd302016-08-18 10:32:27 +0200169
Martin Rothf62065f2016-04-23 17:24:57 -0600170 fields[2 * numopts] = NULL;
Antonello Dettori904dd302016-08-18 10:32:27 +0200171}
172
173int main(void)
174{
175 int ch, done;
Paul Menzele7385d12017-01-20 23:29:21 +0100176 int i;
Antonello Dettori904dd302016-08-18 10:32:27 +0200177
Julius Wernereab2a292019-03-05 16:55:15 -0800178 if (CONFIG(LP_USB))
Paul Menzelafbc2c92017-03-13 17:56:17 +0100179 usb_initialize();
Nicola Cornafece39b2017-03-10 17:34:03 +0100180
Antonello Dettori904dd302016-08-18 10:32:27 +0200181 /* coreboot data structures */
182 lib_get_sysinfo();
183
184 struct cb_cmos_option_table *opttbl = get_system_option_table();
185
Antonello Dettori079feec2016-08-18 10:32:27 +0200186 if (opttbl == NULL) {
187 printf("Could not find coreboot option table.\n");
Antonello Dettori904dd302016-08-18 10:32:27 +0200188 halt();
189 }
190
191 /* prep CMOS layout into libcurses data structures */
192
193 struct cb_cmos_entries *option = first_cmos_entry(opttbl);
194 int numopts = 0;
195 int maxlength = 0;
196
197 count_cmos_options(option, &numopts, &maxlength);
198
199 FIELD **fields = malloc(sizeof(FIELD *) * (2 * numopts + 1));
200
201 cmos_walk_options(opttbl, fields, numopts, maxlength);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100202
Lubomir Rintel1ecc8af2015-02-24 20:33:35 +0100203 /* display initialization */
204 initscr();
205 keypad(stdscr, TRUE);
206 cbreak();
207 noecho();
208
209 if (start_color()) {
Martin Rothf62065f2016-04-23 17:24:57 -0600210 assume_default_colors(COLOR_BLUE, COLOR_CYAN);
Lubomir Rintel1ecc8af2015-02-24 20:33:35 +0100211 }
212 leaveok(stdscr, TRUE);
213 curs_set(1);
214
215 erase();
216 box(stdscr, 0, 0);
217 mvaddstr(0, 2, "coreboot configuration utility");
218 refresh();
219
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100220 FORM *form = new_form(fields);
Martin Rothf62065f2016-04-23 17:24:57 -0600221 int numlines = min(numopts * 2, 16);
222 WINDOW *w = newwin(numlines + 2, 70, 2, 1);
223 WINDOW *inner_w = newpad(numopts * 2, 68);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100224 box(w, 0, 0);
225 mvwaddstr(w, 0, 2, "Press F1 when done");
226 set_form_win(form, w);
227 set_form_sub(form, inner_w);
228 post_form(form);
229
230 done = 0;
Martin Rothf62065f2016-04-23 17:24:57 -0600231 while (!done) {
Lubomir Rintel68009e92015-02-01 15:24:43 +0100232 render_form(form);
Martin Rothf62065f2016-04-23 17:24:57 -0600233 ch = getch();
234 if (ch == ERR)
235 continue;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100236 switch (ch) {
237 case KEY_DOWN:
238 form_driver(form, REQ_NEXT_FIELD);
239 break;
240 case KEY_UP:
241 form_driver(form, REQ_PREV_FIELD);
242 break;
243 case KEY_LEFT:
244 if (field_type(current_field(form)) == TYPE_ENUM) {
245 form_driver(form, REQ_PREV_CHOICE);
246 } else {
247 form_driver(form, REQ_LEFT_CHAR);
248 }
249 break;
250 case KEY_RIGHT:
251 if (field_type(current_field(form)) == TYPE_ENUM) {
252 form_driver(form, REQ_NEXT_CHOICE);
253 } else {
254 form_driver(form, REQ_RIGHT_CHAR);
255 }
256 break;
257 case KEY_BACKSPACE:
258 case '\b':
259 form_driver(form, REQ_DEL_PREV);
260 break;
261 case KEY_DC:
262 form_driver(form, REQ_DEL_CHAR);
263 break;
264 case KEY_F(1):
Martin Rothf62065f2016-04-23 17:24:57 -0600265 done = 1;
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100266 break;
267 default:
268 form_driver(form, ch);
269 break;
270 }
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100271 }
272
Lubomir Rintel1ecc8af2015-02-24 20:33:35 +0100273 endwin();
274
Paul Menzele7385d12017-01-20 23:29:21 +0100275 for (i = 0; i < numopts; i++) {
Martin Rothf62065f2016-04-23 17:24:57 -0600276 char *name = field_buffer(fields[2 * i], 0);
277 char *value = field_buffer(fields[2 * i + 1], 0);
Vladimir Serbinenkoc9babb22014-01-15 22:07:52 +0100278 char *ptr;
Martin Rothf62065f2016-04-23 17:24:57 -0600279 for (ptr = value + strlen(value) - 1;
280 ptr >= value && *ptr == ' '; ptr--)
281 ;
Vladimir Serbinenkoc9babb22014-01-15 22:07:52 +0100282 ptr[1] = '\0';
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100283 set_option_from_string(use_nvram, opttbl, value, name);
284 }
285
286 unpost_form(form);
287 free_form(form);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100288
Vladimir Serbinenkoc7282882014-01-15 22:09:25 +0100289 /* reboot */
Martin Rothf62065f2016-04-23 17:24:57 -0600290 outb(0x6, 0xcf9);
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100291 halt();
292}