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