blob: 6a3d71ef618bc715154a29ae18ff9e3ab892eacc [file] [log] [blame]
Uwe Hermann941c1fd2009-07-07 15:10:13 +00001/*
2 * This file is part of the coreinfo project.
3 *
4 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
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.
Uwe Hermann941c1fd2009-07-07 15:10:13 +000014 */
15
16#include "coreinfo.h"
Gabe Black0af03d22012-03-19 03:06:46 -070017#include "endian.h"
Uwe Hermann941c1fd2009-07-07 15:10:13 +000018
Stefan Reinauer04fb7a82015-06-29 16:08:39 -070019#if IS_ENABLED(CONFIG_MODULE_CBFS)
Uwe Hermann941c1fd2009-07-07 15:10:13 +000020
Uwe Hermann941c1fd2009-07-07 15:10:13 +000021#define HEADER_MAGIC 0x4F524243
22#define HEADER_ADDR 0xfffffffc
23#define LARCHIVE_MAGIC 0x455649484352414cLL /* "LARCHIVE" */
24
25#define COMPONENT_DELETED 0x00
Jonathan Neuschäfer53685042016-03-10 05:37:27 +010026#define COMPONENT_BOOTBLOCK 0x01
27#define COMPONENT_CBFSHEADER 0x02
Uwe Hermann941c1fd2009-07-07 15:10:13 +000028#define COMPONENT_STAGE 0x10
29#define COMPONENT_PAYLOAD 0x20
30#define COMPONENT_OPTIONROM 0x30
Jonathan Neuschäfer53685042016-03-10 05:37:27 +010031#define COMPONENT_RAW 0x50
32#define COMPONENT_MICROCODE 0x53
33#define COMPONENT_CMOS_LAYOUT 0x1aa
Uwe Hermann941c1fd2009-07-07 15:10:13 +000034#define COMPONENT_NULL 0xffffffff
35
36struct cbheader {
37 u32 magic;
38 u32 version;
39 u32 romsize;
40 u32 bootblocksize;
41 u32 align;
42 u32 offset;
David Hendricks90ca3b62012-11-16 14:48:22 -080043 u32 architecture;
44 u32 pad[1];
Uwe Hermann941c1fd2009-07-07 15:10:13 +000045} __attribute__ ((packed));
46
47struct cbfile {
48 u64 magic;
49 u32 len;
50 u32 type;
51 u32 checksum;
52 u32 offset;
53 char filename[0];
54} __attribute__ ((packed));
55
56static int filecount = 0, selected = 0;
57static char **filenames;
58static struct cbheader *header = NULL;
59
60static struct cbfile *getfile(struct cbfile *f)
61{
62 while (1) {
63 if (f < (struct cbfile *)(0xffffffff - ntohl(header->romsize)))
64 return NULL;
65 if (f->magic == 0)
66 return NULL;
67 if (f->magic == LARCHIVE_MAGIC)
68 return f;
69 f = (void *)f + ntohl(header->align);
70 }
71}
72
73static struct cbfile *firstfile(void)
74{
75 return getfile((void *)(0 - ntohl(header->romsize) +
76 ntohl(header->offset)));
77}
78
79static struct cbfile *nextfile(struct cbfile *f)
80{
81 f = (void *)f + ALIGN(ntohl(f->len) + ntohl(f->offset),
82 ntohl(header->align));
83 return getfile(f);
84}
85
86static struct cbfile *findfile(const char *filename)
87{
88 struct cbfile *f;
89 for (f = firstfile(); f; f = nextfile(f)) {
90 if (strcmp(filename, f->filename) == 0)
91 return f;
92 }
93 return NULL;
94}
95
96static int cbfs_module_init(void)
97{
98 struct cbfile *f;
99 int index = 0;
100
101 header = *(void **)HEADER_ADDR;
102 if (header->magic != ntohl(HEADER_MAGIC)) {
103 header = NULL;
104 return 0;
105 }
106
107 for (f = firstfile(); f; f = nextfile(f))
108 filecount++;
109
110 filenames = malloc(filecount * sizeof(char *));
111 if (filenames == NULL)
112 return 0;
113
114 for (f = firstfile(); f; f = nextfile(f))
115 filenames[index++] = strdup((const char *)f->filename);
116
117 return 0;
118}
119
120static int cbfs_module_redraw(WINDOW * win)
121{
122 struct cbfile *f;
123 int i, row = 2;
124
125 print_module_title(win, "CBFS Listing");
126
127 if (!header) {
128 mvwprintw(win, 11, 61 / 2, "Bad or missing CBFS header");
129 return 0;
130 }
131
132 /* Draw a line down the middle. */
133 for (i = 2; i < 21; i++)
134 mvwaddch(win, i, 30, ACS_VLINE);
135
136 /* Draw the names down the left side. */
137 for (i = 0; i < filecount; i++) {
138 if (i == selected)
139 wattrset(win, COLOR_PAIR(3) | A_BOLD);
140 else
141 wattrset(win, COLOR_PAIR(2));
Jonathan Neuschäfer1aca8b82016-03-10 04:32:46 +0100142
143 if (strlen(filenames[i]) == 0) {
144 if (findfile(filenames[i])->type == COMPONENT_NULL)
145 mvwprintw(win, 2 + i, 1, "<free space>");
146 else
147 mvwprintw(win, 2 + i, 1, "<unnamed>");
148 } else {
Uwe Hermann941c1fd2009-07-07 15:10:13 +0000149 mvwprintw(win, 2 + i, 1, "%.25s", filenames[i]);
Jonathan Neuschäfer1aca8b82016-03-10 04:32:46 +0100150 }
Uwe Hermann941c1fd2009-07-07 15:10:13 +0000151 }
152
153 f = findfile(filenames[selected]);
154 if (!f) {
155 mvwprintw(win, 11, 32, "ERROR: CBFS component not found");
156 return 0;
157 }
158
159 wattrset(win, COLOR_PAIR(2));
160
161 /* mvwprintw(win, row++, 32, "Offset: 0x%x", f->offset); *//* FIXME */
162 mvwprintw(win, row, 32, "Type: ");
163 switch (ntohl(f->type)) {
Jonathan Neuschäfer53685042016-03-10 05:37:27 +0100164 case COMPONENT_BOOTBLOCK:
165 mvwprintw(win, row++, 38, "bootblock");
166 break;
167 case COMPONENT_CBFSHEADER:
168 mvwprintw(win, row++, 38, "CBFS header");
169 break;
Uwe Hermann941c1fd2009-07-07 15:10:13 +0000170 case COMPONENT_STAGE:
171 mvwprintw(win, row++, 38, "stage");
172 break;
173 case COMPONENT_PAYLOAD:
174 mvwprintw(win, row++, 38, "payload");
175 break;
176 case COMPONENT_OPTIONROM:
177 mvwprintw(win, row++, 38, "optionrom");
178 break;
Jonathan Neuschäfer53685042016-03-10 05:37:27 +0100179 case COMPONENT_RAW:
180 mvwprintw(win, row++, 38, "raw");
181 break;
182 case COMPONENT_MICROCODE:
183 mvwprintw(win, row++, 38, "microcode");
184 break;
185 case COMPONENT_CMOS_LAYOUT:
186 mvwprintw(win, row++, 38, "cmos layout");
187 break;
Uwe Hermann941c1fd2009-07-07 15:10:13 +0000188 case COMPONENT_NULL:
189 mvwprintw(win, row++, 38, "free");
190 break;
191 case COMPONENT_DELETED:
192 mvwprintw(win, row++, 38, "deleted");
193 break;
194 default:
195 mvwprintw(win, row++, 38, "Unknown (0x%x)", ntohl(f->type));
196 break;
197 }
198 mvwprintw(win, row++, 32, "Size: %d", ntohl(f->len));
199 mvwprintw(win, row++, 32, "Checksum: 0x%x", ntohl(f->checksum));
200
201 return 0;
202}
203
204static int cbfs_module_handle(int key)
205{
206 int ret = 0;
207
208 if (filecount == 0)
209 return 0;
210
211 switch (key) {
212 case KEY_DOWN:
213 if (selected + 1 < filecount) {
214 selected++;
215 ret = 1;
216 }
217 break;
218 case KEY_UP:
219 if (selected > 0) {
220 selected--;
221 ret = 1;
222 }
223 break;
224 }
225
226 return ret;
227}
228
229struct coreinfo_module cbfs_module = {
230 .name = "CBFS",
231 .init = cbfs_module_init,
232 .redraw = cbfs_module_redraw,
233 .handle = cbfs_module_handle
234};
235
236#else
237
238struct coreinfo_module cbfs_module = {
239};
240
241#endif