Nico Huber | 413b0d9 | 2013-06-19 12:41:19 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * ifdfake - Create an Intel Firmware Descriptor with just a section layout |
| 3 | * |
| 4 | * Copyright (C) 2013 secunet Security Networks AG |
| 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 |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <getopt.h> |
| 26 | |
| 27 | #define FDBAR_OFFSET 0x10 |
| 28 | #define FRBA_OFFSET 0x40 |
| 29 | |
| 30 | typedef struct { |
| 31 | uint32_t base, limit, size; |
| 32 | } region_t; |
| 33 | |
| 34 | static void write_image(const region_t regions[], const char *const image) |
| 35 | { |
| 36 | FILE *const f = fopen(image, "w"); |
| 37 | if (!f) { |
| 38 | perror("Could not open file"); |
| 39 | exit(EXIT_FAILURE); |
| 40 | } |
| 41 | |
| 42 | if (fseek(f, 0x1000 - 1, SEEK_SET)) { |
| 43 | perror("Failed to seek to end of descriptor"); |
| 44 | exit(EXIT_FAILURE); |
| 45 | } |
| 46 | char zero = '\0'; |
| 47 | if (fwrite(&zero, 1, 1, f) != 1) { |
| 48 | fprintf(stderr, "Failed to write at end of descriptor.\n"); |
| 49 | exit(EXIT_FAILURE); |
| 50 | } |
| 51 | |
| 52 | if (fseek(f, FDBAR_OFFSET, SEEK_SET)) { |
| 53 | perror("Failed to seek to fdbar"); |
| 54 | exit(EXIT_FAILURE); |
| 55 | } |
| 56 | |
| 57 | struct { |
| 58 | uint32_t flvalsig; |
| 59 | uint32_t flmap0; |
| 60 | } fdbar; |
| 61 | memset(&fdbar, 0x00, sizeof(fdbar)); |
| 62 | fdbar.flvalsig = 0x0ff0a55a; |
| 63 | fdbar.flmap0 = (FRBA_OFFSET >> 4) << 16; |
| 64 | if (fwrite(&fdbar, sizeof(fdbar), 1, f) != 1) { |
| 65 | fprintf(stderr, "Failed to write fdbar.\n"); |
| 66 | exit(EXIT_FAILURE); |
| 67 | } |
| 68 | |
| 69 | int i; |
| 70 | uint32_t frba[5]; |
| 71 | for (i = 0; i < 5; ++i) { |
| 72 | if (regions[i].size) |
| 73 | frba[i] = ((regions[i].limit & 0xfff000) << (16 - 12)) | |
| 74 | ((regions[i].base & 0xfff000) >> 12); |
| 75 | else |
| 76 | frba[i] = 0x00000fff; |
| 77 | } |
| 78 | |
| 79 | if (fseek(f, FRBA_OFFSET, SEEK_SET)) { |
| 80 | perror("Failed to seek to frba"); |
| 81 | exit(EXIT_FAILURE); |
| 82 | } |
| 83 | if (fwrite(frba, sizeof(frba), 1, f) != 1) { |
| 84 | fprintf(stderr, "Failed to write frba.\n"); |
| 85 | exit(EXIT_FAILURE); |
| 86 | } |
| 87 | |
| 88 | fclose(f); |
| 89 | } |
| 90 | |
| 91 | static int parse_region(const char *_arg, region_t *const region) |
| 92 | { |
| 93 | char *const start = strdup(_arg); |
| 94 | if (!start) { |
| 95 | fprintf(stderr, "Out of memory.\n"); |
| 96 | exit(EXIT_FAILURE); |
| 97 | } |
| 98 | |
| 99 | char *const colon = strchr(start, ':'); |
| 100 | if (!colon) { |
| 101 | free(start); |
| 102 | return -1; |
| 103 | } |
| 104 | *colon = '\0'; |
| 105 | |
| 106 | char *const end = colon + 1; |
| 107 | |
| 108 | errno = 0; |
| 109 | region->base = strtoul(start, NULL, 0); |
| 110 | region->limit = strtoul(end, NULL, 0); |
| 111 | region->size = region->limit - region->base + 1; |
| 112 | |
| 113 | free(start); |
| 114 | if (errno) { |
| 115 | perror("Failed to parse region"); |
| 116 | return -1; |
| 117 | } else { |
| 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static void print_usage(const char *name) |
| 123 | { |
| 124 | printf("usage: %s [(-b|-m|-g|-p) <start>:<end>]... <output file>\n", name); |
| 125 | printf("\n" |
| 126 | " -b | --bios <start>:<end> BIOS region\n" |
| 127 | " -m | --me <start>:<end> Intel ME region\n" |
| 128 | " -g | --gbe <start>:<end> Gigabit Ethernet region\n" |
| 129 | " -p | --platform <start>:<end> Platform Data region\n" |
| 130 | " -h | --help print this help\n\n" |
| 131 | "<start> and <end> bounds are given in bytes, the <end> bound is inclusive.\n" |
| 132 | "All regions must be multiples of 4K in size and 4K aligned.\n" |
| 133 | "The descriptor region always resides in the first 4K.\n\n" |
| 134 | "An IFD created with ifdfake won't work as a replacement for a real IFD.\n" |
| 135 | "Never try to flash such an IFD to your board!\n\n"); |
| 136 | } |
| 137 | |
| 138 | int main(int argc, char *argv[]) |
| 139 | { |
| 140 | int opt, option_index = 0, idx; |
| 141 | region_t regions[5]; |
| 142 | |
| 143 | memset(regions, 0x00, sizeof(regions)); |
| 144 | |
| 145 | static struct option long_options[] = { |
| 146 | {"bios", 1, NULL, 'b'}, |
| 147 | {"me", 1, NULL, 'm'}, |
| 148 | {"gbe", 1, NULL, 'g'}, |
| 149 | {"platform", 1, NULL, 'p'}, |
| 150 | {"help", 0, NULL, 'h'}, |
| 151 | {0, 0, 0, 0} |
| 152 | }; |
| 153 | |
| 154 | while ((opt = getopt_long(argc, argv, "b:m:g:p:h?", |
| 155 | long_options, &option_index)) != EOF) { |
| 156 | switch (opt) { |
| 157 | case 'b': case 'm': case 'g': case 'p': |
| 158 | switch (opt) { |
| 159 | case 'b': idx = 1; break; |
| 160 | case 'm': idx = 2; break; |
| 161 | case 'g': idx = 3; break; |
| 162 | case 'p': idx = 4; break; |
| 163 | default: idx = 0; break; /* can't happen */ |
| 164 | } |
| 165 | if (parse_region(optarg, ®ions[idx])) { |
| 166 | print_usage(argv[0]); |
| 167 | exit(EXIT_FAILURE); |
| 168 | } |
| 169 | break; |
| 170 | case 'h': |
| 171 | case '?': |
| 172 | default: |
| 173 | print_usage(argv[0]); |
| 174 | exit(EXIT_SUCCESS); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (optind + 1 != argc) { |
| 180 | fprintf(stderr, "No output file given.\n\n"); |
| 181 | print_usage(argv[0]); |
| 182 | exit(EXIT_FAILURE); |
| 183 | } |
| 184 | |
| 185 | regions[0].base = 0x00000000; |
| 186 | regions[0].limit = 0x00000fff; |
| 187 | regions[0].size = 0x00001000; |
| 188 | for (idx = 1; idx < 5; ++idx) { |
| 189 | if (regions[idx].size) { |
| 190 | if (regions[idx].base & 0xfff) |
| 191 | fprintf(stderr, "Region %d is " |
| 192 | "not 4K aligned.\n", idx); |
| 193 | else if (regions[idx].size & 0xfff) |
| 194 | fprintf(stderr, "Region %d size is " |
| 195 | "no multiple of 4K.\n", idx); |
| 196 | else if (regions[idx].limit <= regions[idx].base) |
| 197 | fprintf(stderr, "Region %d is empty.\n", idx); |
| 198 | else |
| 199 | continue; |
| 200 | print_usage(argv[0]); |
| 201 | exit(EXIT_FAILURE); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | write_image(regions, argv[optind]); |
| 206 | |
| 207 | return 0; |
| 208 | } |