Replace hlt() loops with halt()
[coreboot.git] / payloads / coreinfo / cbfs_module.c
blobe3678dd3c241c710e44163c744619b793cbd3ad5
1 /*
2 * This file is part of the coreinfo project.
4 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
20 #include "coreinfo.h"
21 #include "endian.h"
23 #ifdef CONFIG_MODULE_CBFS
25 #define ALIGN(_v, _a) (((_v) + ((_a) - 1)) & ~((_a) - 1))
27 #define HEADER_MAGIC 0x4F524243
28 #define HEADER_ADDR 0xfffffffc
29 #define LARCHIVE_MAGIC 0x455649484352414cLL /* "LARCHIVE" */
31 #define COMPONENT_DELETED 0x00
32 #define COMPONENT_STAGE 0x10
33 #define COMPONENT_PAYLOAD 0x20
34 #define COMPONENT_OPTIONROM 0x30
35 #define COMPONENT_NULL 0xffffffff
37 struct cbheader {
38 u32 magic;
39 u32 version;
40 u32 romsize;
41 u32 bootblocksize;
42 u32 align;
43 u32 offset;
44 u32 architecture;
45 u32 pad[1];
46 } __attribute__ ((packed));
48 struct cbfile {
49 u64 magic;
50 u32 len;
51 u32 type;
52 u32 checksum;
53 u32 offset;
54 char filename[0];
55 } __attribute__ ((packed));
57 static int filecount = 0, selected = 0;
58 static char **filenames;
59 static struct cbheader *header = NULL;
61 static struct cbfile *getfile(struct cbfile *f)
63 while (1) {
64 if (f < (struct cbfile *)(0xffffffff - ntohl(header->romsize)))
65 return NULL;
66 if (f->magic == 0)
67 return NULL;
68 if (f->magic == LARCHIVE_MAGIC)
69 return f;
70 f = (void *)f + ntohl(header->align);
74 static struct cbfile *firstfile(void)
76 return getfile((void *)(0 - ntohl(header->romsize) +
77 ntohl(header->offset)));
80 static struct cbfile *nextfile(struct cbfile *f)
82 f = (void *)f + ALIGN(ntohl(f->len) + ntohl(f->offset),
83 ntohl(header->align));
84 return getfile(f);
87 static struct cbfile *findfile(const char *filename)
89 struct cbfile *f;
90 for (f = firstfile(); f; f = nextfile(f)) {
91 if (strcmp(filename, f->filename) == 0)
92 return f;
94 return NULL;
97 static int cbfs_module_init(void)
99 struct cbfile *f;
100 int index = 0;
102 header = *(void **)HEADER_ADDR;
103 if (header->magic != ntohl(HEADER_MAGIC)) {
104 header = NULL;
105 return 0;
108 for (f = firstfile(); f; f = nextfile(f))
109 filecount++;
111 filenames = malloc(filecount * sizeof(char *));
112 if (filenames == NULL)
113 return 0;
115 for (f = firstfile(); f; f = nextfile(f))
116 filenames[index++] = strdup((const char *)f->filename);
118 return 0;
121 static int cbfs_module_redraw(WINDOW * win)
123 struct cbfile *f;
124 int i, row = 2;
126 print_module_title(win, "CBFS Listing");
128 if (!header) {
129 mvwprintw(win, 11, 61 / 2, "Bad or missing CBFS header");
130 return 0;
133 /* Draw a line down the middle. */
134 for (i = 2; i < 21; i++)
135 mvwaddch(win, i, 30, ACS_VLINE);
137 /* Draw the names down the left side. */
138 for (i = 0; i < filecount; i++) {
139 if (i == selected)
140 wattrset(win, COLOR_PAIR(3) | A_BOLD);
141 else
142 wattrset(win, COLOR_PAIR(2));
143 if (i == filecount - 1)
144 mvwprintw(win, 2 + i, 1, "<free space>");
145 else
146 mvwprintw(win, 2 + i, 1, "%.25s", filenames[i]);
149 f = findfile(filenames[selected]);
150 if (!f) {
151 mvwprintw(win, 11, 32, "ERROR: CBFS component not found");
152 return 0;
155 wattrset(win, COLOR_PAIR(2));
157 /* mvwprintw(win, row++, 32, "Offset: 0x%x", f->offset); *//* FIXME */
158 mvwprintw(win, row, 32, "Type: ");
159 switch (ntohl(f->type)) {
160 case COMPONENT_STAGE:
161 mvwprintw(win, row++, 38, "stage");
162 break;
163 case COMPONENT_PAYLOAD:
164 mvwprintw(win, row++, 38, "payload");
165 break;
166 case COMPONENT_OPTIONROM:
167 mvwprintw(win, row++, 38, "optionrom");
168 break;
169 case COMPONENT_NULL:
170 mvwprintw(win, row++, 38, "free");
171 break;
172 case COMPONENT_DELETED:
173 mvwprintw(win, row++, 38, "deleted");
174 break;
175 default:
176 mvwprintw(win, row++, 38, "Unknown (0x%x)", ntohl(f->type));
177 break;
179 mvwprintw(win, row++, 32, "Size: %d", ntohl(f->len));
180 mvwprintw(win, row++, 32, "Checksum: 0x%x", ntohl(f->checksum));
182 return 0;
185 static int cbfs_module_handle(int key)
187 int ret = 0;
189 if (filecount == 0)
190 return 0;
192 switch (key) {
193 case KEY_DOWN:
194 if (selected + 1 < filecount) {
195 selected++;
196 ret = 1;
198 break;
199 case KEY_UP:
200 if (selected > 0) {
201 selected--;
202 ret = 1;
204 break;
207 return ret;
210 struct coreinfo_module cbfs_module = {
211 .name = "CBFS",
212 .init = cbfs_module_init,
213 .redraw = cbfs_module_redraw,
214 .handle = cbfs_module_handle
217 #else
219 struct coreinfo_module cbfs_module = {
222 #endif