corinfo: Inital release of the coreinfo code
[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 void print_menu(void) {
54         int i, len;
55         char menu[80];
56         char *ptr = menu;
57         int j;
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 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 void header(int row, const char *str)
84 {
85         char buf[SCREEN_X];
86         char *ptr = buf;
87         int i;
88         int len = strlen(str) + 4;
89
90         for(i = 0; i < (SCREEN_X - len) / 2; i++)
91                 ptr += sprintf(ptr, "=");
92
93         ptr += sprintf(ptr, "[ %s ]", str);
94
95
96         for(i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X ; i++)
97                 ptr += sprintf(ptr, "=");
98
99         mvprintw(row, 0, buf);
100 }
101
102 static void redraw_module(void)
103 {
104         wclear(modwin);
105         modules[curwin]->redraw(modwin);
106         refresh();
107 }
108
109 int loop(void)
110 {
111         int key;
112
113         center(0, "coreinfo v0.1");
114
115         print_menu();
116         modules[curwin]->redraw(modwin);
117         refresh();
118
119         while(1) {
120                 key = getch();
121
122                 if (key == ERR)
123                   continue;
124
125                 if (key >= KEY_F(1) && key <= KEY_F(9)) {
126                         unsigned char ch = key - KEY_F(1);
127
128                         if (ch < MODULE_COUNT) {
129                                 curwin = ch;
130                                 redraw_module();
131                                 continue;
132                         }
133                 }
134
135                 if (modules[curwin]->handle)
136                         if (modules[curwin]->handle(key))
137                                 redraw_module();
138         }
139 }
140
141 int main(void)
142 {
143         int i, j;
144
145         curses_enable_serial(0);
146         curses_enable_vga(1);
147
148         initscr();
149
150         init_pair(1, COLOR_WHITE, COLOR_GREEN);
151         init_pair(2, COLOR_BLACK, COLOR_WHITE);
152         init_pair(3, COLOR_WHITE, COLOR_WHITE);
153
154         modwin = newwin(23, 80, 1, 0);
155
156         wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
157         wattrset(modwin, COLOR_PAIR(2));
158
159         for(i = 0; i < 23; i++) {
160                 wmove(modwin, i - 1, 0);
161
162                 for(j = 0; j < SCREEN_X; j++)
163                         waddch(modwin, ' ');
164         }
165
166         refresh();
167
168         for(i = 0; i < MODULE_COUNT; i++)
169                 modules[i]->init();
170         
171         loop();
172 }