c515d29bab2dd3850832e154187664c6f12d439b
[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 extern struct coreinfo_module bootlog_module;
30
31 struct coreinfo_module *system_modules[] = {
32 #ifdef CONFIG_MODULE_CPUINFO
33         &cpuinfo_module,
34 #endif
35 #ifdef CONFIG_MODULE_PCI
36         &pci_module,
37 #endif
38 #ifdef CONFIG_MODULE_NVRAM
39         &nvram_module,
40 #endif
41 };
42
43 struct coreinfo_module *coreboot_modules[] = {
44 #ifdef CONFIG_MODULE_COREBOOT
45         &coreboot_module,
46 #endif
47 #ifdef CONFIG_MODULE_BOOTLOG
48         &bootlog_module,
49 #endif
50 };
51
52 struct coreinfo_cat {
53         char name[15];
54         int cur;
55         int count;
56         struct coreinfo_module **modules;
57 } categories[] = {
58         {
59                 .name = "System",
60                 .modules = system_modules,
61                 .count = ARRAY_SIZE(system_modules),
62         },
63         {
64                 .name = "Coreboot",
65                 .modules = coreboot_modules,
66                 .count = ARRAY_SIZE(coreboot_modules),
67         }
68 };
69
70
71 static WINDOW *modwin;
72 static int curwin;
73
74 void print_module_title(WINDOW *win, const char *title)
75 {
76         int i;
77
78         wattrset(win, COLOR_PAIR(2));
79         mvwprintw(win, 0, 1, title);
80
81         wmove(win, 1, 1);
82
83         for (i = 0; i < 78; i++)
84                 waddch(win, '\304');
85 }
86
87 static void print_submenu(struct coreinfo_cat *cat)
88 {
89         int i, j;
90         char menu[80];
91         char *ptr = menu;
92
93         wmove(stdscr, 22, 0);
94
95         for (j = 0; j < SCREEN_X; j++)
96                 waddch(stdscr, ' ');
97
98         if (!cat->count)
99                 return;
100
101         for (i = 0; i < cat->count; i++)
102                 ptr += sprintf(ptr, "[%c: %s] ", 'A' + i, cat->modules[i]->name);
103
104         mvprintw(22, 0, menu);
105 }
106
107 static void print_menu(void)
108 {
109         int i, j;
110         char menu[80];
111         char *ptr = menu;
112
113         wmove(stdscr, 23, 0);
114
115         for (j = 0; j < SCREEN_X; j++)
116                 waddch(stdscr, ' ');
117
118         for (i = 0; i < ARRAY_SIZE(categories); i++) {
119                 if (categories[i].count == 0)
120                         continue;
121
122                 ptr += sprintf(ptr, "F%d: %s ", i + 1, categories[i].name);
123         }
124
125         mvprintw(23, 0, menu);
126
127 #ifdef CONFIG_SHOW_DATE_TIME
128         mvprintw(23, 59, "%02d/%02d/20%02d - %02d:%02d:%02d",
129                  bcd2dec(nvram_read(NVRAM_RTC_MONTH)),
130                  bcd2dec(nvram_read(NVRAM_RTC_DAY)),
131                  bcd2dec(nvram_read(NVRAM_RTC_YEAR)),
132                  bcd2dec(nvram_read(NVRAM_RTC_HOURS)),
133                  bcd2dec(nvram_read(NVRAM_RTC_MINUTES)),
134                  bcd2dec(nvram_read(NVRAM_RTC_SECONDS)));
135 #endif
136 }
137
138 static void center(int row, const char *str)
139 {
140         int len = strlen(str);
141         int j;
142
143         wmove(stdscr, row, 0);
144
145         for (j = 0; j < SCREEN_X; j++)
146                 waddch(stdscr, ' ');
147
148         mvprintw(row, (SCREEN_X - len) / 2, str);
149 }
150
151 /* FIXME: Currently unused. */
152 #if 0
153 static void header(int row, const char *str)
154 {
155         char buf[SCREEN_X];
156         char *ptr = buf;
157         int i;
158         int len = strlen(str) + 4;
159
160         for (i = 0; i < (SCREEN_X - len) / 2; i++)
161                 ptr += sprintf(ptr, "=");
162
163         ptr += sprintf(ptr, "[ %s ]", str);
164
165
166
167         for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
168                 ptr += sprintf(ptr, "=");
169
170         mvprintw(row, 0, buf);
171 }
172 #endif
173
174 static void redraw_module(struct coreinfo_cat *cat)
175 {
176         if (cat->count == 0)
177                 return;
178
179         wclear(modwin);
180         cat->modules[cat->cur]->redraw(modwin);
181         refresh();
182 }
183
184 static void handle_category_key(struct coreinfo_cat *cat, int key)
185 {
186         if (key >= 'a' && key <= 'z') {
187                 int index = key - 'a';
188
189                 if (index < cat->count) {
190
191                 cat->cur = index;
192                         redraw_module(cat);
193                         return;
194                 }
195         }
196
197         if (cat->count && cat->modules[cat->cur]->handle) {
198                 if (cat->modules[cat->cur]->handle(key))
199                         redraw_module(cat);
200         }
201 }
202
203 static void loop(void)
204 {
205         int key;
206
207         center(0, "coreinfo v0.1");
208
209         print_menu();
210         print_submenu(&categories[curwin]);
211         redraw_module(&categories[curwin]);
212
213         while (1) {
214                 key = getch();
215
216                 if (key == ERR)
217                         continue;
218
219                 if (key >= KEY_F(1) && key <= KEY_F(9)) {
220                         unsigned char ch = key - KEY_F(1);
221
222                         if (ch <= ARRAY_SIZE(categories)) {
223                                 if (ch == ARRAY_SIZE(categories))
224                                         continue;
225                                 if (categories[ch].count == 0)
226                                         continue;
227
228                                 curwin = ch;
229                                 print_submenu(&categories[curwin]);
230                                 redraw_module(&categories[curwin]);
231                                 continue;
232                         }
233                 }
234
235
236                 handle_category_key(&categories[curwin], key);
237         }
238 }
239
240 int main(void)
241 {
242         int i, j;
243
244         curses_enable_serial(0);
245         curses_enable_vga(1);
246
247         initscr();
248
249         init_pair(1, COLOR_WHITE, COLOR_GREEN);
250         init_pair(2, COLOR_BLACK, COLOR_WHITE);
251         init_pair(3, COLOR_WHITE, COLOR_WHITE);
252
253         modwin = newwin(22, 80, 1, 0);
254
255         wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
256         wattrset(modwin, COLOR_PAIR(2));
257
258         for (i = 0; i < 23; i++) {
259                 wmove(modwin, i - 1, 0);
260
261                 for (j = 0; j < SCREEN_X; j++)
262                         waddch(modwin, ' ');
263         }
264
265         refresh();
266
267         for (i = 0; i < ARRAY_SIZE(categories); i++) {
268                 for(j = 0; j < categories[i].count; j++)
269                         categories[i].modules[j]->init();
270
271         }
272
273         loop();
274
275         return 0;
276 }