c81e9b389531f86cca4c925b5ab8fa5a397c2d54
[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
29 #define MODULE_COUNT 3
30
31 struct coreinfo_module *modules[MODULE_COUNT] = {
32         &cpuinfo_module,
33         &pci_module,
34         &coreboot_module
35 };
36
37 static WINDOW *modwin;
38 static int curwin;
39
40 void print_module_title(WINDOW *win, const char *title)
41 {
42         int i;
43
44         wattrset(win, COLOR_PAIR(2));
45         mvwprintw(win, 0, 1, title);
46
47         wmove(win, 1, 1);
48
49         for (i = 0; i < 78; i++)
50                 waddch(win, '\304');
51 }
52
53 static void print_menu(void)
54 {
55         int i, j;
56         char menu[80];
57         char *ptr = menu;
58
59         wmove(stdscr, 23, 0);
60
61         for (j = 0; j < SCREEN_X; j++)
62                 waddch(stdscr, ' ');
63
64         for (i = 0; i < MODULE_COUNT; i++)
65                 ptr += sprintf(ptr, "F%d: %s ", i + 1, modules[i]->name);
66
67         mvprintw(23, 0, menu);
68 }
69
70 static void center(int row, const char *str)
71 {
72         int len = strlen(str);
73         int j;
74
75         wmove(stdscr, row, 0);
76
77         for (j = 0; j < SCREEN_X; j++)
78                 waddch(stdscr, ' ');
79
80         mvprintw(row, (SCREEN_X - len) / 2, str);
81 }
82
83 /* FIXME: Currently unused. */
84 #if 0
85 static void header(int row, const char *str)
86 {
87         char buf[SCREEN_X];
88         char *ptr = buf;
89         int i;
90         int len = strlen(str) + 4;
91
92         for (i = 0; i < (SCREEN_X - len) / 2; i++)
93                 ptr += sprintf(ptr, "=");
94
95         ptr += sprintf(ptr, "[ %s ]", str);
96
97         for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
98                 ptr += sprintf(ptr, "=");
99
100         mvprintw(row, 0, buf);
101 }
102 #endif
103
104 static void redraw_module(void)
105 {
106         wclear(modwin);
107         modules[curwin]->redraw(modwin);
108         refresh();
109 }
110
111 static void loop(void)
112 {
113         int key;
114
115         center(0, "coreinfo v0.1");
116
117         print_menu();
118         modules[curwin]->redraw(modwin);
119         refresh();
120
121         while (1) {
122                 key = getch();
123
124                 if (key == ERR)
125                         continue;
126
127                 if (key >= KEY_F(1) && key <= KEY_F(9)) {
128                         unsigned char ch = key - KEY_F(1);
129
130                         if (ch < MODULE_COUNT) {
131                                 curwin = ch;
132                                 redraw_module();
133                                 continue;
134                         }
135                 }
136
137                 if (modules[curwin]->handle)
138                         if (modules[curwin]->handle(key))
139                                 redraw_module();
140         }
141 }
142
143 int main(void)
144 {
145         int i, j;
146
147         curses_enable_serial(0);
148         curses_enable_vga(1);
149
150         initscr();
151
152         init_pair(1, COLOR_WHITE, COLOR_GREEN);
153         init_pair(2, COLOR_BLACK, COLOR_WHITE);
154         init_pair(3, COLOR_WHITE, COLOR_WHITE);
155
156         modwin = newwin(23, 80, 1, 0);
157
158         wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
159         wattrset(modwin, COLOR_PAIR(2));
160
161         for (i = 0; i < 23; i++) {
162                 wmove(modwin, i - 1, 0);
163
164                 for (j = 0; j < SCREEN_X; j++)
165                         waddch(modwin, ' ');
166         }
167
168         refresh();
169
170         for (i = 0; i < MODULE_COUNT; i++)
171                 modules[i]->init();
172
173         loop();
174
175         return 0;
176 }