Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 1 | /* |
| 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 Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 16 | #include <coreboot_tables.h> |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 17 | #include <libpayload.h> |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 18 | |
| 19 | #include <curses.h> |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 20 | #include <form.h> |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 21 | #include <menu.h> |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 22 | |
| 23 | #ifndef HOSTED |
| 24 | #define HOSTED 0 |
| 25 | #endif |
| 26 | |
| 27 | static int min(int x, int y) |
| 28 | { |
| 29 | if (x < y) |
| 30 | return x; |
| 31 | return y; |
| 32 | } |
| 33 | |
| 34 | static int max(int x, int y) |
| 35 | { |
| 36 | if (x > y) |
| 37 | return x; |
| 38 | return y; |
| 39 | } |
| 40 | |
| 41 | void render_form(FORM *form) |
| 42 | { |
| 43 | int y, x, line; |
| 44 | WINDOW *w = form_win(form); |
| 45 | WINDOW *inner_w = form_sub(form); |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 46 | int numlines = getmaxy(w) - 2; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 47 | getyx(inner_w, y, x); |
| 48 | line = y - (y % numlines); |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 49 | WINDOW *der = derwin(w, getmaxy(w) - 2, getmaxx(w) - 2, 1, 1); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 50 | wclear(der); |
| 51 | wrefresh(der); |
| 52 | delwin(der); |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 53 | copywin(inner_w, w, line, 0, 1, 1, |
| 54 | min(numlines, getmaxy(inner_w) - line), 68, 0); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 55 | wmove(w, y + 1 - line, x + 1); |
| 56 | wrefresh(w); |
| 57 | } |
| 58 | |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 59 | /* determine number of options, and maximum option name length */ |
| 60 | static int count_cmos_options(struct cb_cmos_entries *option, int *numopts, |
| 61 | int *maxlength) |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 62 | { |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 63 | int n_opts = 0; |
| 64 | int max_l = 0; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 65 | |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 66 | while (option) { |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 67 | if ((option->config != 'r') && |
Martin Roth | 0cf7acc | 2016-04-26 12:33:44 -0600 | [diff] [blame] | 68 | (strcmp("check_sum", (char *)option->name) != 0)) { |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 69 | max_l = max(max_l, strlen((char *)option->name)); |
| 70 | n_opts++; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 71 | } |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 72 | |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 73 | option = next_cmos_entry(option); |
| 74 | } |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 75 | |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 76 | if (n_opts == 0) { |
| 77 | printf("NO CMOS OPTIONS FOUND. EXITING!!!"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | *numopts = n_opts; |
| 82 | *maxlength = max_l; |
| 83 | |
| 84 | return 0; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | /* walk over options, fetch details */ |
| 89 | static void cmos_walk_options(struct cb_cmos_option_table *opttbl, |
| 90 | FIELD **fields, int numopts, int maxlength) |
| 91 | { |
| 92 | struct cb_cmos_entries *option = first_cmos_entry(opttbl); |
Paul Menzel | e7385d1 | 2017-01-20 23:29:21 +0100 | [diff] [blame] | 93 | int i; |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 94 | |
Paul Menzel | e7385d1 | 2017-01-20 23:29:21 +0100 | [diff] [blame] | 95 | for (i = 0; i < numopts; i++) { |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 96 | while ((option->config == 'r') || |
Martin Roth | 0cf7acc | 2016-04-26 12:33:44 -0600 | [diff] [blame] | 97 | (strcmp("check_sum", (char *)option->name) == 0)) { |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 98 | option = next_cmos_entry(option); |
| 99 | } |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 100 | fields[2 * i] = |
Martin Roth | 0cf7acc | 2016-04-26 12:33:44 -0600 | [diff] [blame] | 101 | new_field(1, strlen((char *)option->name), i * 2, 1, 0, 0); |
| 102 | set_field_buffer(fields[2 * i], 0, (char *)option->name); |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 103 | field_opts_off(fields[2 * i], O_ACTIVE); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 104 | |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 105 | fields[2 * i + 1] = |
| 106 | new_field(1, 40, i * 2, maxlength + 2, 0, 0); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 107 | char *buf = NULL; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 108 | int fail = |
Martin Roth | 0cf7acc | 2016-04-26 12:33:44 -0600 | [diff] [blame] | 109 | get_option_as_string(use_nvram, opttbl, &buf, (char *)option->name); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 110 | switch (option->config) { |
| 111 | case 'h': { |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 112 | set_field_type(fields[2 * i + 1], TYPE_INTEGER, 0, 0, |
| 113 | (1 << option->length) - 1); |
| 114 | field_opts_on(fields[2 * i + 1], O_BLANK); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 115 | break; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 116 | } |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 117 | case 's': { |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 118 | set_max_field(fields[2 * i + 1], option->length / 8); |
| 119 | field_opts_off(fields[2 * i + 1], O_STATIC); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 120 | break; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 121 | } |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 122 | case 'e': { |
| 123 | int numvals = 0; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 124 | struct cb_cmos_enums *cmos_enum = |
| 125 | first_cmos_enum_of_id(opttbl, option->config_id); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 126 | |
| 127 | /* if invalid data in CMOS, set buf to first enum */ |
| 128 | if (fail && cmos_enum) { |
Martin Roth | 0cf7acc | 2016-04-26 12:33:44 -0600 | [diff] [blame] | 129 | buf = (char *)cmos_enum->text; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | while (cmos_enum) { |
| 133 | numvals++; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 134 | cmos_enum = next_cmos_enum_of_id( |
| 135 | cmos_enum, option->config_id); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 138 | char **values = malloc(sizeof(char *) * (numvals + 1)); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 139 | int cnt = 0; |
| 140 | |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 141 | cmos_enum = |
| 142 | first_cmos_enum_of_id(opttbl, option->config_id); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 143 | while (cmos_enum) { |
Martin Roth | 0cf7acc | 2016-04-26 12:33:44 -0600 | [diff] [blame] | 144 | values[cnt] = (char *)cmos_enum->text; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 145 | cnt++; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 146 | cmos_enum = next_cmos_enum_of_id( |
| 147 | cmos_enum, option->config_id); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 148 | } |
| 149 | values[cnt] = NULL; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 150 | field_opts_off(fields[2 * i + 1], O_EDIT); |
| 151 | set_field_type(fields[2 * i + 1], TYPE_ENUM, values, 1, |
| 152 | 1); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 153 | free(values); // copied by set_field_type |
| 154 | break; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 155 | } |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | if (buf) |
| 160 | set_field_buffer(fields[2 * i + 1], 0, buf); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 161 | #if HOSTED |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 162 | // underline is non-trivial on VGA text |
| 163 | set_field_back(fields[2 * i + 1], A_UNDERLINE); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 164 | #endif |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 165 | field_opts_off(fields[2 * i + 1], |
| 166 | O_BLANK | O_AUTOSKIP | O_NULLOK); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 167 | |
| 168 | option = next_cmos_entry(option); |
| 169 | } |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 170 | |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 171 | fields[2 * numopts] = NULL; |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int main(void) |
| 175 | { |
| 176 | int ch, done; |
Paul Menzel | e7385d1 | 2017-01-20 23:29:21 +0100 | [diff] [blame] | 177 | int i; |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 178 | |
Julius Werner | eab2a29 | 2019-03-05 16:55:15 -0800 | [diff] [blame] | 179 | if (CONFIG(LP_USB)) |
Paul Menzel | afbc2c9 | 2017-03-13 17:56:17 +0100 | [diff] [blame] | 180 | usb_initialize(); |
Nicola Corna | fece39b | 2017-03-10 17:34:03 +0100 | [diff] [blame] | 181 | |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 182 | /* coreboot data structures */ |
| 183 | lib_get_sysinfo(); |
| 184 | |
| 185 | struct cb_cmos_option_table *opttbl = get_system_option_table(); |
| 186 | |
Antonello Dettori | 079feec | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 187 | if (opttbl == NULL) { |
| 188 | printf("Could not find coreboot option table.\n"); |
Antonello Dettori | 904dd30 | 2016-08-18 10:32:27 +0200 | [diff] [blame] | 189 | halt(); |
| 190 | } |
| 191 | |
| 192 | /* prep CMOS layout into libcurses data structures */ |
| 193 | |
| 194 | struct cb_cmos_entries *option = first_cmos_entry(opttbl); |
| 195 | int numopts = 0; |
| 196 | int maxlength = 0; |
| 197 | |
| 198 | count_cmos_options(option, &numopts, &maxlength); |
| 199 | |
| 200 | FIELD **fields = malloc(sizeof(FIELD *) * (2 * numopts + 1)); |
| 201 | |
| 202 | cmos_walk_options(opttbl, fields, numopts, maxlength); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 203 | |
Lubomir Rintel | 1ecc8af | 2015-02-24 20:33:35 +0100 | [diff] [blame] | 204 | /* display initialization */ |
| 205 | initscr(); |
| 206 | keypad(stdscr, TRUE); |
| 207 | cbreak(); |
| 208 | noecho(); |
| 209 | |
| 210 | if (start_color()) { |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 211 | assume_default_colors(COLOR_BLUE, COLOR_CYAN); |
Lubomir Rintel | 1ecc8af | 2015-02-24 20:33:35 +0100 | [diff] [blame] | 212 | } |
| 213 | leaveok(stdscr, TRUE); |
| 214 | curs_set(1); |
| 215 | |
| 216 | erase(); |
| 217 | box(stdscr, 0, 0); |
| 218 | mvaddstr(0, 2, "coreboot configuration utility"); |
| 219 | refresh(); |
| 220 | |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 221 | FORM *form = new_form(fields); |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 222 | int numlines = min(numopts * 2, 16); |
| 223 | WINDOW *w = newwin(numlines + 2, 70, 2, 1); |
| 224 | WINDOW *inner_w = newpad(numopts * 2, 68); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 225 | box(w, 0, 0); |
| 226 | mvwaddstr(w, 0, 2, "Press F1 when done"); |
| 227 | set_form_win(form, w); |
| 228 | set_form_sub(form, inner_w); |
| 229 | post_form(form); |
| 230 | |
| 231 | done = 0; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 232 | while (!done) { |
Lubomir Rintel | 68009e9 | 2015-02-01 15:24:43 +0100 | [diff] [blame] | 233 | render_form(form); |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 234 | ch = getch(); |
| 235 | if (ch == ERR) |
| 236 | continue; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 237 | switch (ch) { |
| 238 | case KEY_DOWN: |
| 239 | form_driver(form, REQ_NEXT_FIELD); |
| 240 | break; |
| 241 | case KEY_UP: |
| 242 | form_driver(form, REQ_PREV_FIELD); |
| 243 | break; |
| 244 | case KEY_LEFT: |
| 245 | if (field_type(current_field(form)) == TYPE_ENUM) { |
| 246 | form_driver(form, REQ_PREV_CHOICE); |
| 247 | } else { |
| 248 | form_driver(form, REQ_LEFT_CHAR); |
| 249 | } |
| 250 | break; |
| 251 | case KEY_RIGHT: |
| 252 | if (field_type(current_field(form)) == TYPE_ENUM) { |
| 253 | form_driver(form, REQ_NEXT_CHOICE); |
| 254 | } else { |
| 255 | form_driver(form, REQ_RIGHT_CHAR); |
| 256 | } |
| 257 | break; |
| 258 | case KEY_BACKSPACE: |
| 259 | case '\b': |
| 260 | form_driver(form, REQ_DEL_PREV); |
| 261 | break; |
| 262 | case KEY_DC: |
| 263 | form_driver(form, REQ_DEL_CHAR); |
| 264 | break; |
| 265 | case KEY_F(1): |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 266 | done = 1; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 267 | break; |
| 268 | default: |
| 269 | form_driver(form, ch); |
| 270 | break; |
| 271 | } |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Lubomir Rintel | 1ecc8af | 2015-02-24 20:33:35 +0100 | [diff] [blame] | 274 | endwin(); |
| 275 | |
Paul Menzel | e7385d1 | 2017-01-20 23:29:21 +0100 | [diff] [blame] | 276 | for (i = 0; i < numopts; i++) { |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 277 | char *name = field_buffer(fields[2 * i], 0); |
| 278 | char *value = field_buffer(fields[2 * i + 1], 0); |
Vladimir Serbinenko | c9babb2 | 2014-01-15 22:07:52 +0100 | [diff] [blame] | 279 | char *ptr; |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 280 | for (ptr = value + strlen(value) - 1; |
| 281 | ptr >= value && *ptr == ' '; ptr--) |
| 282 | ; |
Vladimir Serbinenko | c9babb2 | 2014-01-15 22:07:52 +0100 | [diff] [blame] | 283 | ptr[1] = '\0'; |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 284 | set_option_from_string(use_nvram, opttbl, value, name); |
| 285 | } |
| 286 | |
| 287 | unpost_form(form); |
| 288 | free_form(form); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 289 | |
Vladimir Serbinenko | c728288 | 2014-01-15 22:09:25 +0100 | [diff] [blame] | 290 | /* reboot */ |
Martin Roth | f62065f | 2016-04-23 17:24:57 -0600 | [diff] [blame] | 291 | outb(0x6, 0xcf9); |
Patrick Georgi | 4b62ebe | 2012-11-16 15:05:11 +0100 | [diff] [blame] | 292 | halt(); |
| 293 | } |