blob: 8cb76a427601b06c49674b4cf782df05ce094b38 [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.
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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Uwe Hermann941c1fd2009-07-07 15:10:13 +000018 */
19
20#include "coreinfo.h"
Gabe Black0af03d22012-03-19 03:06:46 -070021#include "endian.h"
Uwe Hermann941c1fd2009-07-07 15:10:13 +000022
Stefan Reinauer04fb7a82015-06-29 16:08:39 -070023#if IS_ENABLED(CONFIG_MODULE_CBFS)
Uwe Hermann941c1fd2009-07-07 15:10:13 +000024
Uwe Hermann941c1fd2009-07-07 15:10:13 +000025#define HEADER_MAGIC 0x4F524243
26#define HEADER_ADDR 0xfffffffc
27#define LARCHIVE_MAGIC 0x455649484352414cLL /* "LARCHIVE" */
28
29#define COMPONENT_DELETED 0x00
30#define COMPONENT_STAGE 0x10
31#define COMPONENT_PAYLOAD 0x20
32#define COMPONENT_OPTIONROM 0x30
33#define COMPONENT_NULL 0xffffffff
34
35struct cbheader {
36 u32 magic;
37 u32 version;
38 u32 romsize;
39 u32 bootblocksize;
40 u32 align;
41 u32 offset;
David Hendricks90ca3b62012-11-16 14:48:22 -080042 u32 architecture;
43 u32 pad[1];
Uwe Hermann941c1fd2009-07-07 15:10:13 +000044} __attribute__ ((packed));
45
46struct cbfile {
47 u64 magic;
48 u32 len;
49 u32 type;
50 u32 checksum;
51 u32 offset;
52 char filename[0];
53} __attribute__ ((packed));
54
55static int filecount = 0, selected = 0;
56static char **filenames;
57static struct cbheader *header = NULL;
58
59static struct cbfile *getfile(struct cbfile *f)
60{
61 while (1) {
62 if (f < (struct cbfile *)(0xffffffff - ntohl(header->romsize)))
63 return NULL;
64 if (f->magic == 0)
65 return NULL;
66 if (f->magic == LARCHIVE_MAGIC)
67 return f;
68 f = (void *)f + ntohl(header->align);
69 }
70}
71
72static struct cbfile *firstfile(void)
73{
74 return getfile((void *)(0 - ntohl(header->romsize) +
75 ntohl(header->offset)));
76}
77
78static struct cbfile *nextfile(struct cbfile *f)
79{
80 f = (void *)f + ALIGN(ntohl(f->len) + ntohl(f->offset),
81 ntohl(header->align));
82 return getfile(f);
83}
84
85static struct cbfile *findfile(const char *filename)
86{
87 struct cbfile *f;
88 for (f = firstfile(); f; f = nextfile(f)) {
89 if (strcmp(filename, f->filename) == 0)
90 return f;
91 }
92 return NULL;
93}
94
95static int cbfs_module_init(void)
96{
97 struct cbfile *f;
98 int index = 0;
99
100 header = *(void **)HEADER_ADDR;
101 if (header->magic != ntohl(HEADER_MAGIC)) {
102 header = NULL;
103 return 0;
104 }
105
106 for (f = firstfile(); f; f = nextfile(f))
107 filecount++;
108
109 filenames = malloc(filecount * sizeof(char *));
110 if (filenames == NULL)
111 return 0;
112
113 for (f = firstfile(); f; f = nextfile(f))
114 filenames[index++] = strdup((const char *)f->filename);
115
116 return 0;
117}
118
119static int cbfs_module_redraw(WINDOW * win)
120{
121 struct cbfile *f;
122 int i, row = 2;
123
124 print_module_title(win, "CBFS Listing");
125
126 if (!header) {
127 mvwprintw(win, 11, 61 / 2, "Bad or missing CBFS header");
128 return 0;
129 }
130
131 /* Draw a line down the middle. */
132 for (i = 2; i < 21; i++)
133 mvwaddch(win, i, 30, ACS_VLINE);
134
135 /* Draw the names down the left side. */
136 for (i = 0; i < filecount; i++) {
137 if (i == selected)
138 wattrset(win, COLOR_PAIR(3) | A_BOLD);
139 else
140 wattrset(win, COLOR_PAIR(2));
141 if (i == filecount - 1)
142 mvwprintw(win, 2 + i, 1, "<free space>");
143 else
144 mvwprintw(win, 2 + i, 1, "%.25s", filenames[i]);
145 }
146
147 f = findfile(filenames[selected]);
148 if (!f) {
149 mvwprintw(win, 11, 32, "ERROR: CBFS component not found");
150 return 0;
151 }
152
153 wattrset(win, COLOR_PAIR(2));
154
155 /* mvwprintw(win, row++, 32, "Offset: 0x%x", f->offset); *//* FIXME */
156 mvwprintw(win, row, 32, "Type: ");
157 switch (ntohl(f->type)) {
158 case COMPONENT_STAGE:
159 mvwprintw(win, row++, 38, "stage");
160 break;
161 case COMPONENT_PAYLOAD:
162 mvwprintw(win, row++, 38, "payload");
163 break;
164 case COMPONENT_OPTIONROM:
165 mvwprintw(win, row++, 38, "optionrom");
166 break;
167 case COMPONENT_NULL:
168 mvwprintw(win, row++, 38, "free");
169 break;
170 case COMPONENT_DELETED:
171 mvwprintw(win, row++, 38, "deleted");
172 break;
173 default:
174 mvwprintw(win, row++, 38, "Unknown (0x%x)", ntohl(f->type));
175 break;
176 }
177 mvwprintw(win, row++, 32, "Size: %d", ntohl(f->len));
178 mvwprintw(win, row++, 32, "Checksum: 0x%x", ntohl(f->checksum));
179
180 return 0;
181}
182
183static int cbfs_module_handle(int key)
184{
185 int ret = 0;
186
187 if (filecount == 0)
188 return 0;
189
190 switch (key) {
191 case KEY_DOWN:
192 if (selected + 1 < filecount) {
193 selected++;
194 ret = 1;
195 }
196 break;
197 case KEY_UP:
198 if (selected > 0) {
199 selected--;
200 ret = 1;
201 }
202 break;
203 }
204
205 return ret;
206}
207
208struct coreinfo_module cbfs_module = {
209 .name = "CBFS",
210 .init = cbfs_module_init,
211 .redraw = cbfs_module_redraw,
212 .handle = cbfs_module_handle
213};
214
215#else
216
217struct coreinfo_module cbfs_module = {
218};
219
220#endif