The previous commit had more in it then I wanted - so I am reverting
[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 *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_COREBOOT
39         &coreboot_module,
40 #endif
41 #ifdef CONFIG_MODULE_NVRAM
42         &nvram_module,
43 #endif
44 #ifdef CONFIG_MODULE_BOOTLOG
45         &bootlog_module,
46 #endif
47 };
48
49 static WINDOW *modwin;
50 static int curwin;
51
52 void print_module_title(WINDOW *win, const char *title)
53 {
54         int i;
55
56         wattrset(win, COLOR_PAIR(2));
57         mvwprintw(win, 0, 1, title);
58
59         wmove(win, 1, 1);
60
61         for (i = 0; i < 78; i++)
62                 waddch(win, '\304');
63 }
64
65 static void print_menu(void)
66 {
67         int i, j;
68         char menu[80];
69         char *ptr = menu;
70
71         wmove(stdscr, 23, 0);
72
73         for (j = 0; j < SCREEN_X; j++)
74                 waddch(stdscr, ' ');
75
76         for (i = 0; i < ARRAY_SIZE(modules); i++)
77                 ptr += sprintf(ptr, "F%d: %s ", i + 1, modules[i]->name);
78
79         if (ARRAY_SIZE(modules) != 0)
80                 mvprintw(23, 0, menu);
81
82 #ifdef CONFIG_SHOW_DATE_TIME
83         mvprintw(23, 59, "%02d/%02d/20%02d - %02d:%02d:%02d",
84                  bcd2dec(nvram_read(NVRAM_RTC_MONTH)),
85                  bcd2dec(nvram_read(NVRAM_RTC_DAY)),
86                  bcd2dec(nvram_read(NVRAM_RTC_YEAR)),
87                  bcd2dec(nvram_read(NVRAM_RTC_HOURS)),
88                  bcd2dec(nvram_read(NVRAM_RTC_MINUTES)),
89                  bcd2dec(nvram_read(NVRAM_RTC_SECONDS)));
90 #endif
91 }
92
93 static void center(int row, const char *str)
94 {
95         int len = strlen(str);
96         int j;
97
98         wmove(stdscr, row, 0);
99
100         for (j = 0; j < SCREEN_X; j++)
101                 waddch(stdscr, ' ');
102
103         mvprintw(row, (SCREEN_X - len) / 2, str);
104 }
105
106 /* FIXME: Currently unused. */
107 #if 0
108 static void header(int row, const char *str)
109 {
110         char buf[SCREEN_X];
111         char *ptr = buf;
112         int i;
113         int len = strlen(str) + 4;
114
115         for (i = 0; i < (SCREEN_X - len) / 2; i++)
116                 ptr += sprintf(ptr, "=");
117
118         ptr += sprintf(ptr, "[ %s ]", str);
119
120         for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
121                 ptr += sprintf(ptr, "=");
122
123         mvprintw(row, 0, buf);
124 }
125 #endif
126
127 static void redraw_module(void)
128 {
129         if (ARRAY_SIZE(modules) == 0)
130                 return;
131
132         wclear(modwin);
133         modules[curwin]->redraw(modwin);
134         refresh();
135 }
136
137 static void loop(void)
138 {
139         int key;
140
141         center(0, "coreinfo v0.1");
142
143         print_menu();
144         if (ARRAY_SIZE(modules) != 0)
145                 modules[curwin]->redraw(modwin);
146         refresh();
147
148         while (1) {
149                 key = getch();
150
151                 if (key == ERR)
152                         continue;
153
154                 if (key >= KEY_F(1) && key <= KEY_F(9)) {
155                         unsigned char ch = key - KEY_F(1);
156
157                         if (ch <= ARRAY_SIZE(modules)) {
158                                 if (ch == ARRAY_SIZE(modules))
159                                         continue;
160                                 curwin = ch;
161                                 redraw_module();
162                                 continue;
163                         }
164                 }
165
166                 if (ARRAY_SIZE(modules) != 0 && modules[curwin]->handle)
167                         if (modules[curwin]->handle(key))
168                                 redraw_module();
169         }
170 }
171
172 int main(void)
173 {
174         int i, j;
175
176         curses_enable_serial(0);
177         curses_enable_vga(1);
178
179         initscr();
180
181         init_pair(1, COLOR_WHITE, COLOR_GREEN);
182         init_pair(2, COLOR_BLACK, COLOR_WHITE);
183         init_pair(3, COLOR_WHITE, COLOR_WHITE);
184
185         modwin = newwin(23, 80, 1, 0);
186
187         wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
188         wattrset(modwin, COLOR_PAIR(2));
189
190         for (i = 0; i < 23; i++) {
191                 wmove(modwin, i - 1, 0);
192
193                 for (j = 0; j < SCREEN_X; j++)
194                         waddch(modwin, ' ');
195         }
196
197         refresh();
198
199         for (i = 0; i < ARRAY_SIZE(modules); i++)
200                 modules[i]->init();
201
202         loop();
203
204         return 0;
205 }