aboutsummaryrefslogtreecommitdiff
path: root/payloads/coreinfo/coreinfo.c
blob: de183886caa3027740aa83950acbd2ad9e08751c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * This file is part of the coreinfo project.
 *
 * Copyright (C) 2008 Advanced Micro Devices, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "coreinfo.h"

#define SCREEN_Y 24
#define SCREEN_X 80

extern struct coreinfo_module cpuinfo_module;
extern struct coreinfo_module pci_module;
extern struct coreinfo_module coreboot_module;

#define MODULE_COUNT 3

struct coreinfo_module *modules[MODULE_COUNT] = {
	&cpuinfo_module,
	&pci_module,
	&coreboot_module
};

static WINDOW *modwin;
static int curwin;

void print_module_title(WINDOW *win, const char *title)
{
	int i;

	wattrset(win, COLOR_PAIR(2));
	mvwprintw(win, 0, 1, title);

	wmove(win, 1, 1);

	for (i = 0; i < 78; i++)
		waddch(win, '\304');
}

void print_menu(void)
{
	int i, j;
	char menu[80];
	char *ptr = menu;

	wmove(stdscr, 23, 0);

	for (j = 0; j < SCREEN_X; j++)
		waddch(stdscr, ' ');

	for (i = 0; i < MODULE_COUNT; i++)
		ptr += sprintf(ptr, "F%d: %s ", i + 1, modules[i]->name);

	mvprintw(23, 0, menu);
}

void center(int row, const char *str)
{
	int len = strlen(str);
	int j;

	wmove(stdscr, row, 0);

	for (j = 0; j < SCREEN_X; j++)
		waddch(stdscr, ' ');

	mvprintw(row, (SCREEN_X - len) / 2, str);
}

void header(int row, const char *str)
{
	char buf[SCREEN_X];
	char *ptr = buf;
	int i;
	int len = strlen(str) + 4;

	for (i = 0; i < (SCREEN_X - len) / 2; i++)
		ptr += sprintf(ptr, "=");

	ptr += sprintf(ptr, "[ %s ]", str);

	for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
		ptr += sprintf(ptr, "=");

	mvprintw(row, 0, buf);
}

static void redraw_module(void)
{
	wclear(modwin);
	modules[curwin]->redraw(modwin);
	refresh();
}

void loop(void)
{
	int key;

	center(0, "coreinfo v0.1");

	print_menu();
	modules[curwin]->redraw(modwin);
	refresh();

	while (1) {
		key = getch();

		if (key == ERR)
			continue;

		if (key >= KEY_F(1) && key <= KEY_F(9)) {
			unsigned char ch = key - KEY_F(1);

			if (ch < MODULE_COUNT) {
				curwin = ch;
				redraw_module();
				continue;
			}
		}

		if (modules[curwin]->handle)
			if (modules[curwin]->handle(key))
				redraw_module();
	}
}

int main(void)
{
	int i, j;

	curses_enable_serial(0);
	curses_enable_vga(1);

	initscr();

	init_pair(1, COLOR_WHITE, COLOR_GREEN);
	init_pair(2, COLOR_BLACK, COLOR_WHITE);
	init_pair(3, COLOR_WHITE, COLOR_WHITE);

	modwin = newwin(23, 80, 1, 0);

	wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
	wattrset(modwin, COLOR_PAIR(2));

	for (i = 0; i < 23; i++) {
		wmove(modwin, i - 1, 0);

		for (j = 0; j < SCREEN_X; j++)
			waddch(modwin, ' ');
	}

	refresh();

	for (i = 0; i < MODULE_COUNT; i++)
		modules[i]->init();

	loop();

	return 0;
}