Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 1 | /* |
| 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 Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "coreinfo.h" |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 17 | #include "endian.h" |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 18 | |
Stefan Reinauer | 04fb7a8 | 2015-06-29 16:08:39 -0700 | [diff] [blame] | 19 | #if IS_ENABLED(CONFIG_MODULE_CBFS) |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 20 | |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 21 | #define FILES_VISIBLE 19 |
| 22 | |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 23 | #define HEADER_MAGIC 0x4F524243 |
| 24 | #define HEADER_ADDR 0xfffffffc |
| 25 | #define LARCHIVE_MAGIC 0x455649484352414cLL /* "LARCHIVE" */ |
| 26 | |
| 27 | #define COMPONENT_DELETED 0x00 |
Jonathan Neuschäfer | 5368504 | 2016-03-10 05:37:27 +0100 | [diff] [blame] | 28 | #define COMPONENT_BOOTBLOCK 0x01 |
| 29 | #define COMPONENT_CBFSHEADER 0x02 |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 30 | #define COMPONENT_STAGE 0x10 |
Patrick Rudolph | 4f5bed5 | 2018-05-02 09:44:08 +0200 | [diff] [blame] | 31 | #define COMPONENT_SELF 0x20 |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 32 | #define COMPONENT_FIT 0x21 |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 33 | #define COMPONENT_OPTIONROM 0x30 |
Jonathan Neuschäfer | 5368504 | 2016-03-10 05:37:27 +0100 | [diff] [blame] | 34 | #define COMPONENT_RAW 0x50 |
| 35 | #define COMPONENT_MICROCODE 0x53 |
| 36 | #define COMPONENT_CMOS_LAYOUT 0x1aa |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 37 | #define COMPONENT_NULL 0xffffffff |
| 38 | |
| 39 | struct cbheader { |
| 40 | u32 magic; |
| 41 | u32 version; |
| 42 | u32 romsize; |
| 43 | u32 bootblocksize; |
| 44 | u32 align; |
| 45 | u32 offset; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 46 | u32 architecture; |
| 47 | u32 pad[1]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 48 | } __packed; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 49 | |
| 50 | struct cbfile { |
| 51 | u64 magic; |
| 52 | u32 len; |
| 53 | u32 type; |
| 54 | u32 checksum; |
| 55 | u32 offset; |
| 56 | char filename[0]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 57 | } __packed; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 58 | |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 59 | static int filecount = 0, selected = 0, start_row = 0; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 60 | static char **filenames; |
| 61 | static struct cbheader *header = NULL; |
| 62 | |
| 63 | static struct cbfile *getfile(struct cbfile *f) |
| 64 | { |
| 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; |
Richard Spiegel | b93796d | 2018-10-29 08:01:53 -0700 | [diff] [blame] | 72 | f = (struct cbfile *)((u8 *)f + ntohl(header->align)); |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | static struct cbfile *firstfile(void) |
| 77 | { |
| 78 | return getfile((void *)(0 - ntohl(header->romsize) + |
| 79 | ntohl(header->offset))); |
| 80 | } |
| 81 | |
| 82 | static struct cbfile *nextfile(struct cbfile *f) |
| 83 | { |
Richard Spiegel | b93796d | 2018-10-29 08:01:53 -0700 | [diff] [blame] | 84 | f = (struct cbfile *)((u8 *)f + ALIGN(ntohl(f->len) + ntohl(f->offset), |
| 85 | ntohl(header->align))); |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 86 | return getfile(f); |
| 87 | } |
| 88 | |
| 89 | static struct cbfile *findfile(const char *filename) |
| 90 | { |
| 91 | struct cbfile *f; |
| 92 | for (f = firstfile(); f; f = nextfile(f)) { |
| 93 | if (strcmp(filename, f->filename) == 0) |
| 94 | return f; |
| 95 | } |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | static int cbfs_module_init(void) |
| 100 | { |
| 101 | struct cbfile *f; |
| 102 | int index = 0; |
| 103 | |
| 104 | header = *(void **)HEADER_ADDR; |
| 105 | if (header->magic != ntohl(HEADER_MAGIC)) { |
| 106 | header = NULL; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | for (f = firstfile(); f; f = nextfile(f)) |
| 111 | filecount++; |
| 112 | |
| 113 | filenames = malloc(filecount * sizeof(char *)); |
| 114 | if (filenames == NULL) |
| 115 | return 0; |
| 116 | |
| 117 | for (f = firstfile(); f; f = nextfile(f)) |
| 118 | filenames[index++] = strdup((const char *)f->filename); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int cbfs_module_redraw(WINDOW * win) |
| 124 | { |
| 125 | struct cbfile *f; |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 126 | int i, row, frow; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 127 | |
| 128 | print_module_title(win, "CBFS Listing"); |
| 129 | |
| 130 | if (!header) { |
| 131 | mvwprintw(win, 11, 61 / 2, "Bad or missing CBFS header"); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* Draw a line down the middle. */ |
| 136 | for (i = 2; i < 21; i++) |
| 137 | mvwaddch(win, i, 30, ACS_VLINE); |
| 138 | |
| 139 | /* Draw the names down the left side. */ |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 140 | for (frow = 0; frow < FILES_VISIBLE; frow++) { |
| 141 | row = 2 + frow; |
| 142 | i = start_row + frow; |
| 143 | if (i >= filecount) |
| 144 | break; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 145 | if (i == selected) |
| 146 | wattrset(win, COLOR_PAIR(3) | A_BOLD); |
| 147 | else |
| 148 | wattrset(win, COLOR_PAIR(2)); |
Jonathan Neuschäfer | 1aca8b8 | 2016-03-10 04:32:46 +0100 | [diff] [blame] | 149 | |
| 150 | if (strlen(filenames[i]) == 0) { |
| 151 | if (findfile(filenames[i])->type == COMPONENT_NULL) |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 152 | mvwprintw(win, row, 1, "<free space>"); |
Jonathan Neuschäfer | 1aca8b8 | 2016-03-10 04:32:46 +0100 | [diff] [blame] | 153 | else |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 154 | mvwprintw(win, row, 1, "<unnamed>"); |
Jonathan Neuschäfer | 1aca8b8 | 2016-03-10 04:32:46 +0100 | [diff] [blame] | 155 | } else { |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 156 | mvwprintw(win, row, 1, "%.25s", filenames[i]); |
| 157 | } |
| 158 | /* show scroll arrows */ |
| 159 | if (frow == 0 && start_row > 0) { |
| 160 | wattrset(win, COLOR_PAIR(2)); |
| 161 | mvwaddch(win, row, 28, ACS_UARROW); |
| 162 | } |
| 163 | if (frow == FILES_VISIBLE - 1 && i != filecount - 1) { |
| 164 | wattrset(win, COLOR_PAIR(2)); |
| 165 | mvwaddch(win, row, 28, ACS_DARROW); |
Jonathan Neuschäfer | 1aca8b8 | 2016-03-10 04:32:46 +0100 | [diff] [blame] | 166 | } |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | f = findfile(filenames[selected]); |
| 170 | if (!f) { |
| 171 | mvwprintw(win, 11, 32, "ERROR: CBFS component not found"); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | wattrset(win, COLOR_PAIR(2)); |
| 176 | |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 177 | /* Draw the file information */ |
| 178 | row = 2; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 179 | /* mvwprintw(win, row++, 32, "Offset: 0x%x", f->offset); *//* FIXME */ |
| 180 | mvwprintw(win, row, 32, "Type: "); |
| 181 | switch (ntohl(f->type)) { |
Jonathan Neuschäfer | 5368504 | 2016-03-10 05:37:27 +0100 | [diff] [blame] | 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; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 188 | case COMPONENT_STAGE: |
| 189 | mvwprintw(win, row++, 38, "stage"); |
| 190 | break; |
Patrick Rudolph | 4f5bed5 | 2018-05-02 09:44:08 +0200 | [diff] [blame] | 191 | case COMPONENT_SELF: |
Ronald G. Minnich | c6d1349 | 2018-05-15 18:05:07 -0700 | [diff] [blame] | 192 | mvwprintw(win, row++, 38, "simple ELF"); |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 193 | break; |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 194 | case COMPONENT_FIT: |
| 195 | mvwprintw(win, row++, 38, "FIT"); |
| 196 | break; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 197 | case COMPONENT_OPTIONROM: |
| 198 | mvwprintw(win, row++, 38, "optionrom"); |
| 199 | break; |
Jonathan Neuschäfer | 5368504 | 2016-03-10 05:37:27 +0100 | [diff] [blame] | 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; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 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; |
| 218 | } |
| 219 | mvwprintw(win, row++, 32, "Size: %d", ntohl(f->len)); |
| 220 | mvwprintw(win, row++, 32, "Checksum: 0x%x", ntohl(f->checksum)); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int cbfs_module_handle(int key) |
| 226 | { |
| 227 | int ret = 0; |
| 228 | |
| 229 | if (filecount == 0) |
| 230 | return 0; |
| 231 | |
| 232 | switch (key) { |
| 233 | case KEY_DOWN: |
| 234 | if (selected + 1 < filecount) { |
| 235 | selected++; |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 236 | if (selected >= start_row + FILES_VISIBLE - 1) |
| 237 | start_row = selected - (FILES_VISIBLE - 1); |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 238 | ret = 1; |
| 239 | } |
| 240 | break; |
| 241 | case KEY_UP: |
| 242 | if (selected > 0) { |
| 243 | selected--; |
Ben Gardner | d770bad | 2016-04-29 12:45:43 -0500 | [diff] [blame] | 244 | if (selected < start_row) |
| 245 | start_row = selected; |
Uwe Hermann | 941c1fd | 2009-07-07 15:10:13 +0000 | [diff] [blame] | 246 | ret = 1; |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | struct coreinfo_module cbfs_module = { |
| 255 | .name = "CBFS", |
| 256 | .init = cbfs_module_init, |
| 257 | .redraw = cbfs_module_redraw, |
| 258 | .handle = cbfs_module_handle |
| 259 | }; |
| 260 | |
| 261 | #else |
| 262 | |
| 263 | struct coreinfo_module cbfs_module = { |
| 264 | }; |
| 265 | |
| 266 | #endif |