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