util/pgtblgen: Fix typo
[coreboot.git] / payloads / coreinfo / coreinfo.c
blob53985b293a85e4491b5daa3baca89c6617e1a95b
1 /*
2 * This file is part of the coreinfo project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
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.
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.
16 #include "coreinfo.h"
18 #define KEY_ESC 27
20 extern struct coreinfo_module cpuinfo_module;
21 extern struct coreinfo_module pci_module;
22 extern struct coreinfo_module coreboot_module;
23 extern struct coreinfo_module multiboot_module;
24 extern struct coreinfo_module nvram_module;
25 extern struct coreinfo_module bootlog_module;
26 extern struct coreinfo_module ramdump_module;
27 extern struct coreinfo_module cbfs_module;
28 extern struct coreinfo_module timestamps_module;
30 struct coreinfo_module *system_modules[] = {
31 #if CONFIG(MODULE_CPUINFO)
32 &cpuinfo_module,
33 #endif
34 #if CONFIG(MODULE_PCI)
35 &pci_module,
36 #endif
37 #if CONFIG(MODULE_NVRAM)
38 &nvram_module,
39 #endif
40 #if CONFIG(MODULE_RAMDUMP)
41 &ramdump_module,
42 #endif
45 struct coreinfo_module *firmware_modules[] = {
46 #if CONFIG(MODULE_COREBOOT)
47 &coreboot_module,
48 #endif
49 #if CONFIG(MODULE_MULTIBOOT)
50 &multiboot_module,
51 #endif
52 #if CONFIG(MODULE_BOOTLOG)
53 &bootlog_module,
54 #endif
55 #if CONFIG(MODULE_CBFS)
56 &cbfs_module,
57 #endif
58 #if CONFIG(MODULE_TIMESTAMPS)
59 &timestamps_module,
60 #endif
63 struct coreinfo_cat {
64 char name[15];
65 int cur;
66 int count;
67 struct coreinfo_module **modules;
68 } categories[] = {
70 .name = "System",
71 .modules = system_modules,
72 .count = ARRAY_SIZE(system_modules),
75 .name = "Firmware",
76 .modules = firmware_modules,
77 .count = ARRAY_SIZE(firmware_modules),
81 static WINDOW *modwin, *menuwin;
82 static int curwin;
84 void print_module_title(WINDOW *win, const char *title)
86 int i;
88 wattrset(win, COLOR_PAIR(2));
89 mvwprintw(win, 0, 1, title);
91 wmove(win, 1, 1);
92 for (i = 0; i < 78; i++)
93 waddch(win, ACS_HLINE);
96 static void print_submenu(struct coreinfo_cat *cat)
98 int i, j;
99 char menu[80];
100 char *ptr = menu;
102 wmove(menuwin, 0, 0);
104 for (j = 0; j < SCREEN_X; j++)
105 waddch(menuwin, ' ');
107 if (!cat->count)
108 return;
110 for (i = 0; i < cat->count; i++)
111 ptr += sprintf(ptr, "[%c: %s] ", 'A' + i,
112 cat->modules[i]->name);
114 mvwprintw(menuwin, 0, 0, menu);
117 #if CONFIG(SHOW_DATE_TIME)
118 static void print_time_and_date(void)
120 struct tm tm;
122 while (nvram_updating())
123 mdelay(10);
125 rtc_read_clock(&tm);
127 mvwprintw(menuwin, 1, 57, "%02d/%02d/%04d - %02d:%02d:%02d",
128 tm.tm_mon + 1, tm.tm_mday, 1900 + tm.tm_year, tm.tm_hour,
129 tm.tm_min, tm.tm_sec);
131 #endif
133 static void print_menu(void)
135 int j;
136 char menu[80];
137 char *ptr = menu;
139 wmove(menuwin, 1, 0);
140 for (j = 0; j < SCREEN_X; j++)
141 waddch(menuwin, ' ');
143 for (size_t i = 0; i < ARRAY_SIZE(categories); i++) {
144 if (categories[i].count == 0)
145 continue;
147 ptr += sprintf(ptr, "F%zu: %s ", i + 1, categories[i].name);
150 mvwprintw(menuwin, 1, 0, menu);
152 #if CONFIG(SHOW_DATE_TIME)
153 print_time_and_date();
154 #endif
157 static void center(int row, const char *str)
159 int j, len = strlen(str);
161 wmove(stdscr, row, 0);
162 for (j = 0; j < SCREEN_X; j++)
163 waddch(stdscr, ' ');
165 mvprintw(row, (SCREEN_X - len) / 2, str);
168 /* FIXME: Currently unused. */
169 #if 0
170 static void header(int row, const char *str)
172 char buf[SCREEN_X];
173 char *ptr = buf;
174 int i;
175 int len = strlen(str) + 4;
177 for (i = 0; i < (SCREEN_X - len) / 2; i++)
178 ptr += sprintf(ptr, "=");
180 ptr += sprintf(ptr, "[ %s ]", str);
182 for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
183 ptr += sprintf(ptr, "=");
185 mvprintw(row, 0, buf);
187 #endif
189 static void redraw_module(struct coreinfo_cat *cat)
191 if (cat->count == 0)
192 return;
194 wclear(modwin);
195 cat->modules[cat->cur]->redraw(modwin);
196 wrefresh(modwin);
199 static void handle_category_key(struct coreinfo_cat *cat, int key)
201 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z')) {
202 int index;
203 if (key >= 'A' && key <= 'Z') {
204 index = key - 'A';
205 } else {
206 index = key - 'a';
208 if (index < cat->count) {
209 cat->cur = index;
210 redraw_module(cat);
211 return;
215 if (cat->count && cat->modules[cat->cur]->handle) {
216 if (cat->modules[cat->cur]->handle(key))
217 redraw_module(cat);
221 static void print_no_modules_selected(void)
223 int height = getmaxy(stdscr);
225 for (size_t i = 0; i < ARRAY_SIZE(categories); i++)
226 if (categories[i].count > 0)
227 return;
229 color_set(2, NULL); // White on black
230 center(height / 2, "No modules selected");
233 static int first_nonempty_category(void)
235 for (size_t i = 0; i < ARRAY_SIZE(categories); i++)
236 if (categories[i].count > 0)
237 return i;
238 return 0;
241 static void loop(void)
243 int key;
245 center(0, CONFIG_PAYLOAD_INFO_NAME " " CONFIG_PAYLOAD_INFO_VERSION);
246 print_no_modules_selected();
247 refresh();
249 curwin = first_nonempty_category();
250 print_menu();
251 print_submenu(&categories[curwin]);
252 redraw_module(&categories[curwin]);
254 halfdelay(10);
256 while (1) {
257 int ch = -1;
259 #if CONFIG(SHOW_DATE_TIME)
260 print_time_and_date();
261 wrefresh(menuwin);
262 #endif
264 key = getch();
266 if (key == ERR)
267 continue;
269 if (key >= KEY_F(1) && key <= KEY_F(9))
270 ch = key - KEY_F(1);
271 if (key >= '1' && key <= '9')
272 ch = key - '1';
274 if (ch >= 0 && (unsigned int)ch <= ARRAY_SIZE(categories)) {
275 if (ch == ARRAY_SIZE(categories))
276 continue;
277 if (categories[ch].count == 0)
278 continue;
280 curwin = ch;
281 print_submenu(&categories[curwin]);
282 redraw_module(&categories[curwin]);
283 continue;
286 if (key == KEY_ESC)
287 return;
289 handle_category_key(&categories[curwin], key);
293 int main(void)
295 int j;
297 if (CONFIG(LP_USB))
298 usb_initialize();
300 initscr();
302 start_color();
303 init_pair(1, COLOR_WHITE, COLOR_GREEN);
304 init_pair(2, COLOR_WHITE, COLOR_BLACK);
305 init_pair(3, COLOR_BLACK, COLOR_WHITE);
307 modwin = newwin(SCREEN_Y - 3, SCREEN_X, 1, 0);
308 menuwin = newwin(2, SCREEN_X, SCREEN_Y - 2, 0);
310 wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
311 wattrset(modwin, COLOR_PAIR(2));
312 wattrset(menuwin, COLOR_PAIR(1) | A_BOLD);
314 werase(modwin);
316 for (size_t i = 0; i < ARRAY_SIZE(categories); i++) {
317 for (j = 0; j < categories[i].count; j++)
318 categories[i].modules[j]->init();
321 noecho(); /* don't let curses echo keyboard chars */
322 keypad(stdscr, TRUE); /* allow KEY_F(n) keys to be seen */
323 curs_set(0); /* Hide blinking cursor */
325 loop();
327 /* reboot */
328 outb(0x6, 0xcf9);
329 halt();
330 return 0;
333 PAYLOAD_INFO(name, CONFIG_PAYLOAD_INFO_NAME);
334 PAYLOAD_INFO(listname, CONFIG_PAYLOAD_INFO_LISTNAME);
335 PAYLOAD_INFO(desc, CONFIG_PAYLOAD_INFO_DESC);