Add support for an "NVRAM Dump" screen in coreinfo (optional), as well as for
[coreboot.git] / payloads / coreinfo / coreinfo.c
1 /*
2  * This file is part of the coreinfo project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
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
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include "coreinfo.h"
21
22 #define SCREEN_Y 24
23 #define SCREEN_X 80
24
25 extern struct coreinfo_module cpuinfo_module;
26 extern struct coreinfo_module pci_module;
27 extern struct coreinfo_module coreboot_module;
28 extern struct coreinfo_module nvram_module;
29
30 struct coreinfo_module *modules[] = {
31 #ifdef CONFIG_MODULE_CPUINFO
32         &cpuinfo_module,
33 #endif
34 #ifdef CONFIG_MODULE_PCI
35         &pci_module,
36 #endif
37 #ifdef CONFIG_MODULE_COREBOOT
38         &coreboot_module,
39 #endif
40 #ifdef CONFIG_MODULE_NVRAM
41         &nvram_module,
42 #endif
43 };
44
45 static WINDOW *modwin;
46 static int curwin;
47
48 void print_module_title(WINDOW *win, const char *title)
49 {
50         int i;
51
52         wattrset(win, COLOR_PAIR(2));
53         mvwprintw(win, 0, 1, title);
54
55         wmove(win, 1, 1);
56
57         for (i = 0; i < 78; i++)
58                 waddch(win, '\304');
59 }
60
61 static void print_menu(void)
62 {
63         int i, j;
64         char menu[80];
65         char *ptr = menu;
66
67         wmove(stdscr, 23, 0);
68
69         for (j = 0; j < SCREEN_X; j++)
70                 waddch(stdscr, ' ');
71
72         for (i = 0; i < ARRAY_SIZE(modules); i++)
73                 ptr += sprintf(ptr, "F%d: %s ", i + 1, modules[i]->name);
74
75         mvprintw(23, 0, menu);
76
77 #ifdef CONFIG_SHOW_DATE_TIME
78         mvprintw(23, 59, "%02d/%02d/20%02d - %02d:%02d:%02d",
79                  bcd2dec(nvram_read(NVRAM_RTC_MONTH)),
80                  bcd2dec(nvram_read(NVRAM_RTC_DAY)),
81                  bcd2dec(nvram_read(NVRAM_RTC_YEAR)),
82                  bcd2dec(nvram_read(NVRAM_RTC_HOURS)),
83                  bcd2dec(nvram_read(NVRAM_RTC_MINUTES)),
84                  bcd2dec(nvram_read(NVRAM_RTC_SECONDS)));
85 #endif
86 }
87
88 static void center(int row, const char *str)
89 {
90         int len = strlen(str);
91         int j;
92
93         wmove(stdscr, row, 0);
94
95         for (j = 0; j < SCREEN_X; j++)
96                 waddch(stdscr, ' ');
97
98         mvprintw(row, (SCREEN_X - len) / 2, str);
99 }
100
101 /* FIXME: Currently unused. */
102 #if 0
103 static void header(int row, const char *str)
104 {
105         char buf[SCREEN_X];
106         char *ptr = buf;
107         int i;
108         int len = strlen(str) + 4;
109
110         for (i = 0; i < (SCREEN_X - len) / 2; i++)
111                 ptr += sprintf(ptr, "=");
112
113         ptr += sprintf(ptr, "[ %s ]", str);
114
115         for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
116                 ptr += sprintf(ptr, "=");
117
118         mvprintw(row, 0, buf);
119 }
120 #endif
121
122 static void redraw_module(void)
123 {
124         wclear(modwin);
125         modules[curwin]->redraw(modwin);
126         refresh();
127 }
128
129 static void loop(void)
130 {
131         int key;
132
133         center(0, "coreinfo v0.1");
134
135         print_menu();
136         modules[curwin]->redraw(modwin);
137         refresh();
138
139         while (1) {
140                 key = getch();
141
142                 if (key == ERR)
143                         continue;
144
145                 if (key >= KEY_F(1) && key <= KEY_F(9)) {
146                         unsigned char ch = key - KEY_F(1);
147
148                         if (ch < ARRAY_SIZE(modules)) {
149                                 curwin = ch;
150                                 redraw_module();
151                                 continue;
152                         }
153                 }
154
155                 if (modules[curwin]->handle)
156                         if (modules[curwin]->handle(key))
157                                 redraw_module();
158         }
159 }
160
161 int main(void)
162 {
163         int i, j;
164
165         curses_enable_serial(0);
166         curses_enable_vga(1);
167
168         initscr();
169
170         init_pair(1, COLOR_WHITE, COLOR_GREEN);
171         init_pair(2, COLOR_BLACK, COLOR_WHITE);
172         init_pair(3, COLOR_WHITE, COLOR_WHITE);
173
174         modwin = newwin(23, 80, 1, 0);
175
176         wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
177         wattrset(modwin, COLOR_PAIR(2));
178
179         for (i = 0; i < 23; i++) {
180                 wmove(modwin, i - 1, 0);
181
182                 for (j = 0; j < SCREEN_X; j++)
183                         waddch(modwin, ' ');
184         }
185
186         refresh();
187
188         for (i = 0; i < ARRAY_SIZE(modules); i++)
189                 modules[i]->init();
190
191         loop();
192
193         return 0;
194 }