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.
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
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
)
66 if (f
< (struct cbfile
*)(0xffffffff - ntohl(header
->romsize
)))
70 if (f
->magic
== LARCHIVE_MAGIC
)
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
));
89 static struct cbfile
*findfile(const char *filename
)
92 for (f
= firstfile(); f
; f
= nextfile(f
)) {
93 if (strcmp(filename
, f
->filename
) == 0)
99 static int cbfs_module_init(void)
104 header
= *(void **)HEADER_ADDR
;
105 if (header
->magic
!= ntohl(HEADER_MAGIC
)) {
110 for (f
= firstfile(); f
; f
= nextfile(f
))
113 filenames
= malloc(filecount
* sizeof(char *));
114 if (filenames
== NULL
)
117 for (f
= firstfile(); f
; f
= nextfile(f
))
118 filenames
[index
++] = strdup((const char *)f
->filename
);
123 static int cbfs_module_redraw(WINDOW
* win
)
128 print_module_title(win
, "CBFS Listing");
131 mvwprintw(win
, 11, 61 / 2, "Bad or missing CBFS header");
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
++) {
142 i
= start_row
+ frow
;
146 wattrset(win
, COLOR_PAIR(3) | A_BOLD
);
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>");
154 mvwprintw(win
, row
, 1, "<unnamed>");
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
]);
171 mvwprintw(win
, 11, 32, "ERROR: CBFS component not found");
175 wattrset(win
, COLOR_PAIR(2));
177 /* Draw the file information */
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");
185 case COMPONENT_CBFSHEADER
:
186 mvwprintw(win
, row
++, 38, "CBFS header");
188 case COMPONENT_STAGE
:
189 mvwprintw(win
, row
++, 38, "stage");
192 mvwprintw(win
, row
++, 38, "simple ELF");
195 mvwprintw(win
, row
++, 38, "FIT");
197 case COMPONENT_OPTIONROM
:
198 mvwprintw(win
, row
++, 38, "optionrom");
201 mvwprintw(win
, row
++, 38, "raw");
203 case COMPONENT_MICROCODE
:
204 mvwprintw(win
, row
++, 38, "microcode");
206 case COMPONENT_CMOS_LAYOUT
:
207 mvwprintw(win
, row
++, 38, "cmos layout");
210 mvwprintw(win
, row
++, 38, "free");
212 case COMPONENT_DELETED
:
213 mvwprintw(win
, row
++, 38, "deleted");
216 mvwprintw(win
, row
++, 38, "Unknown (0x%x)", ntohl(f
->type
));
219 mvwprintw(win
, row
++, 32, "Size: %d", ntohl(f
->len
));
220 mvwprintw(win
, row
++, 32, "Checksum: 0x%x", ntohl(f
->checksum
));
225 static int cbfs_module_handle(int key
)
234 if (selected
+ 1 < filecount
) {
236 if (selected
>= start_row
+ FILES_VISIBLE
- 1)
237 start_row
= selected
- (FILES_VISIBLE
- 1);
244 if (selected
< start_row
)
245 start_row
= selected
;
254 struct coreinfo_module cbfs_module
= {
256 .init
= cbfs_module_init
,
257 .redraw
= cbfs_module_redraw
,
258 .handle
= cbfs_module_handle
263 struct coreinfo_module cbfs_module
= {