amd/stoneyridge: Add warm reset detection
[coreboot.git] / payloads / coreinfo / cbfs_module.c
blob275c84e98668e6f082720b7e56811885dcbb2ae5
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.
16 #include "coreinfo.h"
17 #include "endian.h"
19 #if IS_ENABLED(CONFIG_MODULE_CBFS)
21 #define FILES_VISIBLE 19
23 #define HEADER_MAGIC 0x4F524243
24 #define HEADER_ADDR 0xfffffffc
25 #define LARCHIVE_MAGIC 0x455649484352414cLL /* "LARCHIVE" */
27 #define COMPONENT_DELETED 0x00
28 #define COMPONENT_BOOTBLOCK 0x01
29 #define COMPONENT_CBFSHEADER 0x02
30 #define COMPONENT_STAGE 0x10
31 #define COMPONENT_SELF 0x20
32 #define COMPONENT_FIT 0x21
33 #define COMPONENT_OPTIONROM 0x30
34 #define COMPONENT_RAW 0x50
35 #define COMPONENT_MICROCODE 0x53
36 #define COMPONENT_CMOS_LAYOUT 0x1aa
37 #define COMPONENT_NULL 0xffffffff
39 struct cbheader {
40 u32 magic;
41 u32 version;
42 u32 romsize;
43 u32 bootblocksize;
44 u32 align;
45 u32 offset;
46 u32 architecture;
47 u32 pad[1];
48 } __packed;
50 struct cbfile {
51 u64 magic;
52 u32 len;
53 u32 type;
54 u32 checksum;
55 u32 offset;
56 char filename[0];
57 } __packed;
59 static int filecount = 0, selected = 0, start_row = 0;
60 static char **filenames;
61 static struct cbheader *header = NULL;
63 static struct cbfile *getfile(struct cbfile *f)
65 while (1) {
66 if (f < (struct cbfile *)(0xffffffff - ntohl(header->romsize)))
67 return NULL;
68 if (f->magic == 0)
69 return NULL;
70 if (f->magic == LARCHIVE_MAGIC)
71 return f;
72 f = (void *)f + ntohl(header->align);
76 static struct cbfile *firstfile(void)
78 return getfile((void *)(0 - ntohl(header->romsize) +
79 ntohl(header->offset)));
82 static struct cbfile *nextfile(struct cbfile *f)
84 f = (void *)f + ALIGN(ntohl(f->len) + ntohl(f->offset),
85 ntohl(header->align));
86 return getfile(f);
89 static struct cbfile *findfile(const char *filename)
91 struct cbfile *f;
92 for (f = firstfile(); f; f = nextfile(f)) {
93 if (strcmp(filename, f->filename) == 0)
94 return f;
96 return NULL;
99 static int cbfs_module_init(void)
101 struct cbfile *f;
102 int index = 0;
104 header = *(void **)HEADER_ADDR;
105 if (header->magic != ntohl(HEADER_MAGIC)) {
106 header = NULL;
107 return 0;
110 for (f = firstfile(); f; f = nextfile(f))
111 filecount++;
113 filenames = malloc(filecount * sizeof(char *));
114 if (filenames == NULL)
115 return 0;
117 for (f = firstfile(); f; f = nextfile(f))
118 filenames[index++] = strdup((const char *)f->filename);
120 return 0;
123 static int cbfs_module_redraw(WINDOW * win)
125 struct cbfile *f;
126 int i, row, frow;
128 print_module_title(win, "CBFS Listing");
130 if (!header) {
131 mvwprintw(win, 11, 61 / 2, "Bad or missing CBFS header");
132 return 0;
135 /* Draw a line down the middle. */
136 for (i = 2; i < 21; i++)
137 mvwaddch(win, i, 30, ACS_VLINE);
139 /* Draw the names down the left side. */
140 for (frow = 0; frow < FILES_VISIBLE; frow++) {
141 row = 2 + frow;
142 i = start_row + frow;
143 if (i >= filecount)
144 break;
145 if (i == selected)
146 wattrset(win, COLOR_PAIR(3) | A_BOLD);
147 else
148 wattrset(win, COLOR_PAIR(2));
150 if (strlen(filenames[i]) == 0) {
151 if (findfile(filenames[i])->type == COMPONENT_NULL)
152 mvwprintw(win, row, 1, "<free space>");
153 else
154 mvwprintw(win, row, 1, "<unnamed>");
155 } else {
156 mvwprintw(win, row, 1, "%.25s", filenames[i]);
158 /* show scroll arrows */
159 if (frow == 0 && start_row > 0) {
160 wattrset(win, COLOR_PAIR(2));
161 mvwaddch(win, row, 28, ACS_UARROW);
163 if (frow == FILES_VISIBLE - 1 && i != filecount - 1) {
164 wattrset(win, COLOR_PAIR(2));
165 mvwaddch(win, row, 28, ACS_DARROW);
169 f = findfile(filenames[selected]);
170 if (!f) {
171 mvwprintw(win, 11, 32, "ERROR: CBFS component not found");
172 return 0;
175 wattrset(win, COLOR_PAIR(2));
177 /* Draw the file information */
178 row = 2;
179 /* mvwprintw(win, row++, 32, "Offset: 0x%x", f->offset); *//* FIXME */
180 mvwprintw(win, row, 32, "Type: ");
181 switch (ntohl(f->type)) {
182 case COMPONENT_BOOTBLOCK:
183 mvwprintw(win, row++, 38, "bootblock");
184 break;
185 case COMPONENT_CBFSHEADER:
186 mvwprintw(win, row++, 38, "CBFS header");
187 break;
188 case COMPONENT_STAGE:
189 mvwprintw(win, row++, 38, "stage");
190 break;
191 case COMPONENT_SELF:
192 mvwprintw(win, row++, 38, "simple ELF");
193 break;
194 case COMPONENT_FIT:
195 mvwprintw(win, row++, 38, "FIT");
196 break;
197 case COMPONENT_OPTIONROM:
198 mvwprintw(win, row++, 38, "optionrom");
199 break;
200 case COMPONENT_RAW:
201 mvwprintw(win, row++, 38, "raw");
202 break;
203 case COMPONENT_MICROCODE:
204 mvwprintw(win, row++, 38, "microcode");
205 break;
206 case COMPONENT_CMOS_LAYOUT:
207 mvwprintw(win, row++, 38, "cmos layout");
208 break;
209 case COMPONENT_NULL:
210 mvwprintw(win, row++, 38, "free");
211 break;
212 case COMPONENT_DELETED:
213 mvwprintw(win, row++, 38, "deleted");
214 break;
215 default:
216 mvwprintw(win, row++, 38, "Unknown (0x%x)", ntohl(f->type));
217 break;
219 mvwprintw(win, row++, 32, "Size: %d", ntohl(f->len));
220 mvwprintw(win, row++, 32, "Checksum: 0x%x", ntohl(f->checksum));
222 return 0;
225 static int cbfs_module_handle(int key)
227 int ret = 0;
229 if (filecount == 0)
230 return 0;
232 switch (key) {
233 case KEY_DOWN:
234 if (selected + 1 < filecount) {
235 selected++;
236 if (selected >= start_row + FILES_VISIBLE - 1)
237 start_row = selected - (FILES_VISIBLE - 1);
238 ret = 1;
240 break;
241 case KEY_UP:
242 if (selected > 0) {
243 selected--;
244 if (selected < start_row)
245 start_row = selected;
246 ret = 1;
248 break;
251 return ret;
254 struct coreinfo_module cbfs_module = {
255 .name = "CBFS",
256 .init = cbfs_module_init,
257 .redraw = cbfs_module_redraw,
258 .handle = cbfs_module_handle
261 #else
263 struct coreinfo_module cbfs_module = {
266 #endif