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
21 #include "arch/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
45 } __attribute__ ((packed));
54 } __attribute__ ((packed));
56 static int filecount = 0, selected = 0;
57 static char **filenames;
58 static struct cbheader *header = NULL;
60 static struct cbfile *getfile(struct cbfile *f)
63 if (f < (struct cbfile *)(0xffffffff - ntohl(header->romsize)))
67 if (f->magic == LARCHIVE_MAGIC)
69 f = (void *)f + ntohl(header->align);
73 static struct cbfile *firstfile(void)
75 return getfile((void *)(0 - ntohl(header->romsize) +
76 ntohl(header->offset)));
79 static struct cbfile *nextfile(struct cbfile *f)
81 f = (void *)f + ALIGN(ntohl(f->len) + ntohl(f->offset),
82 ntohl(header->align));
86 static struct cbfile *findfile(const char *filename)
89 for (f = firstfile(); f; f = nextfile(f)) {
90 if (strcmp(filename, f->filename) == 0)
96 static int cbfs_module_init(void)
101 header = *(void **)HEADER_ADDR;
102 if (header->magic != ntohl(HEADER_MAGIC)) {
107 for (f = firstfile(); f; f = nextfile(f))
110 filenames = malloc(filecount * sizeof(char *));
111 if (filenames == NULL)
114 for (f = firstfile(); f; f = nextfile(f))
115 filenames[index++] = strdup((const char *)f->filename);
120 static int cbfs_module_redraw(WINDOW * win)
125 print_module_title(win, "CBFS Listing");
128 mvwprintw(win, 11, 61 / 2, "Bad or missing CBFS header");
132 /* Draw a line down the middle. */
133 for (i = 2; i < 21; i++)
134 mvwaddch(win, i, 30, ACS_VLINE);
136 /* Draw the names down the left side. */
137 for (i = 0; i < filecount; i++) {
139 wattrset(win, COLOR_PAIR(3) | A_BOLD);
141 wattrset(win, COLOR_PAIR(2));
142 if (i == filecount - 1)
143 mvwprintw(win, 2 + i, 1, "<free space>");
145 mvwprintw(win, 2 + i, 1, "%.25s", filenames[i]);
148 f = findfile(filenames[selected]);
150 mvwprintw(win, 11, 32, "ERROR: CBFS component not found");
154 wattrset(win, COLOR_PAIR(2));
156 /* mvwprintw(win, row++, 32, "Offset: 0x%x", f->offset); *//* FIXME */
157 mvwprintw(win, row, 32, "Type: ");
158 switch (ntohl(f->type)) {
159 case COMPONENT_STAGE:
160 mvwprintw(win, row++, 38, "stage");
162 case COMPONENT_PAYLOAD:
163 mvwprintw(win, row++, 38, "payload");
165 case COMPONENT_OPTIONROM:
166 mvwprintw(win, row++, 38, "optionrom");
169 mvwprintw(win, row++, 38, "free");
171 case COMPONENT_DELETED:
172 mvwprintw(win, row++, 38, "deleted");
175 mvwprintw(win, row++, 38, "Unknown (0x%x)", ntohl(f->type));
178 mvwprintw(win, row++, 32, "Size: %d", ntohl(f->len));
179 mvwprintw(win, row++, 32, "Checksum: 0x%x", ntohl(f->checksum));
184 static int cbfs_module_handle(int key)
193 if (selected + 1 < filecount) {
209 struct coreinfo_module cbfs_module = {
211 .init = cbfs_module_init,
212 .redraw = cbfs_module_redraw,
213 .handle = cbfs_module_handle
218 struct coreinfo_module cbfs_module = {