blob: daf153b9d7243986cb9facbe96b153a6e3a50053 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Patrick Georgi4b62ebe2012-11-16 15:05:11 +010018 */
19
20#include <stdlib.h>
21#include <string.h>
22
23#include <libpayload.h>
24#include <coreboot_tables.h>
25
26#include <curses.h>
27#include <menu.h>
28#include <form.h>
29
30#ifndef HOSTED
31#define HOSTED 0
32#endif
33
34static int min(int x, int y)
35{
36 if (x < y)
37 return x;
38 return y;
39}
40
41static int max(int x, int y)
42{
43 if (x > y)
44 return x;
45 return y;
46}
47
48void render_form(FORM *form)
49{
50 int y, x, line;
51 WINDOW *w = form_win(form);
52 WINDOW *inner_w = form_sub(form);
53 int numlines = getmaxy(w)-2;
54 getyx(inner_w, y, x);
55 line = y - (y % numlines);
56 WINDOW *der = derwin(w, getmaxy(w)-2, getmaxx(w)-2, 1, 1);
57 wclear(der);
58 wrefresh(der);
59 delwin(der);
60 copywin(inner_w, w, line, 0, 1, 1, min(numlines, getmaxy(inner_w)-line), 68, 0);
61 wmove(w, y + 1 - line, x + 1);
62 wrefresh(w);
63}
64
65int main()
66{
67 int ch, done;
68 ITEM *cur;
69
70 /* coreboot data structures */
71 lib_get_sysinfo();
72
73 struct cb_cmos_option_table *opttbl = get_system_option_table();
74
75 if (opttbl == NULL) {
76 printf("Could not find coreboot option table\n");
77 halt();
78 }
79
80 /* display initialization */
81 initscr();
82 keypad(stdscr, TRUE);
83 cbreak();
84 noecho();
85 start_color();
86 leaveok(stdscr, TRUE);
87 curs_set(1);
88
89 erase();
90 box(stdscr, 0, 0);
91 mvaddstr(0, 2, "coreboot configuration utility");
92
93 /* prep CMOS layout into libcurses data structures */
94
95 /* determine number of options, and maximum option name length */
96 int numopts=0;
97 int maxlength=0;
98 struct cb_cmos_entries *option = first_cmos_entry(opttbl);
99 while (option) {
100 if ((option->config != 'r') && (strcmp("check_sum", option->name) != 0)) {
101 maxlength = max(maxlength, strlen(option->name));
102 numopts++;
103 }
104 option = next_cmos_entry(option);
105 }
106 if (numopts == 0) {
107 printf("NO CMOS OPTIONS FOUND. EXITING!!!");
108 return 1;
109 }
110 FIELD **fields = malloc(sizeof(FIELD*)*(2*numopts+1));
111 int i;
112
113 /* walk over options, fetch details */
114 option = first_cmos_entry(opttbl);
115 for (i=0;i<numopts;i++) {
116 while ((option->config == 'r') || (strcmp("check_sum", option->name) == 0)) {
117 option = next_cmos_entry(option);
118 }
119 fields[2*i] = new_field(1, strlen(option->name), i*2, 1, 0, 0);
120 set_field_buffer(fields[2*i], 0, option->name);
121 field_opts_off(fields[2*i], O_ACTIVE);
122
123 fields[2*i+1] = new_field(1, 40, i*2, maxlength+2, 0, 0);
124 char *buf = NULL;
125 int fail = get_option_as_string(use_nvram, opttbl, &buf, option->name);
126 switch (option->config) {
127 case 'h': {
128 set_field_type(fields[2*i+1], TYPE_INTEGER, 0, 0, (1<<option->length)-1);
129 field_opts_on(fields[2*i+1], O_BLANK);
130 break;
131 }
132 case 's': {
133 set_max_field(fields[2*i+1], option->length/8);
134 field_opts_off(fields[2*i+1], O_STATIC);
135 break;
136 }
137 case 'e': {
138 int numvals = 0;
139 struct cb_cmos_enums *cmos_enum = first_cmos_enum_of_id(opttbl, option->config_id);
140
141 /* if invalid data in CMOS, set buf to first enum */
142 if (fail && cmos_enum) {
143 buf = cmos_enum->text;
144 }
145
146 while (cmos_enum) {
147 numvals++;
148 cmos_enum = next_cmos_enum_of_id(cmos_enum, option->config_id);
149 }
150
151 char **values = malloc(sizeof(char*)*numvals + 1);
152 int cnt = 0;
153
154 cmos_enum = first_cmos_enum_of_id(opttbl, option->config_id);
155 while (cmos_enum) {
156 values[cnt] = cmos_enum->text;
157 cnt++;
158 cmos_enum = next_cmos_enum_of_id(cmos_enum, option->config_id);
159 }
160 values[cnt] = NULL;
161 field_opts_off(fields[2*i+1], O_EDIT);
162 set_field_type(fields[2*i+1], TYPE_ENUM, values, 1, 1);
163 free(values); // copied by set_field_type
164 break;
165 }
166 default:
167 break;
168 }
169 if (buf) set_field_buffer(fields[2*i+1], 0, buf);
170#if HOSTED
171// underline is non-trivial on VGA text
172 set_field_back(fields[2*i+1], A_UNDERLINE);
173#endif
174 field_opts_off(fields[2*i+1], O_BLANK | O_AUTOSKIP | O_NULLOK);
175
176 option = next_cmos_entry(option);
177 }
178 fields[2*numopts]=NULL;
179
180 FORM *form = new_form(fields);
181 int numlines = min(numopts*2, 16);
182 WINDOW *w = newwin(numlines+2, 70, 2, 1);
183 WINDOW *inner_w = newpad(numopts*2, 68);
184 box(w, 0, 0);
185 mvwaddstr(w, 0, 2, "Press F1 when done");
186 set_form_win(form, w);
187 set_form_sub(form, inner_w);
188 post_form(form);
189
190 done = 0;
191 while(!done) {
192 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 }
229 render_form(form);
230 }
231
232 for (i = 0; i < numopts; i++) {
233 char *name = field_buffer(fields[2*i], 0);
234 char *value = field_buffer(fields[2*i+1], 0);
Vladimir Serbinenkoc9babb22014-01-15 22:07:52 +0100235 char *ptr;
236 for (ptr = value + strlen (value) - 1;
237 ptr >= value && *ptr == ' '; ptr--);
238 ptr[1] = '\0';
Patrick Georgi4b62ebe2012-11-16 15:05:11 +0100239 set_option_from_string(use_nvram, opttbl, value, name);
240 }
241
242 unpost_form(form);
243 free_form(form);
244 touchwin(stdscr);
245 refresh();
246
247 endwin();
248 /* TODO: reboot */
249 halt();
250}
251