Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * common utility functions for cbfstool |
| 3 | * |
| 4 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 6 | * Copyright (C) 2012 Google, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
Uwe Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 28 | #include <libgen.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 29 | #include "common.h" |
| 30 | #include "cbfs.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 31 | |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 32 | /* Utilities */ |
Aaron Durbin | fae7517 | 2014-03-05 15:02:21 -0600 | [diff] [blame] | 33 | int verbose = 0; |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 34 | |
| 35 | /* Small, OS/libc independent runtime check for endianess */ |
| 36 | int is_big_endian(void) |
| 37 | { |
| 38 | static const uint32_t inttest = 0x12345678; |
| 39 | uint8_t inttest_lsb = *(uint8_t *)&inttest; |
| 40 | if (inttest_lsb == 0x12) { |
| 41 | return 1; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 46 | static off_t get_file_size(FILE *f) |
| 47 | { |
| 48 | struct stat s; |
| 49 | int fd = fileno(f); |
| 50 | if (fd == -1) return -1; |
| 51 | if (fstat(fd, &s) == -1) return -1; |
| 52 | return s.st_size; |
| 53 | } |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 54 | /* Buffer and file I/O */ |
| 55 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 56 | int buffer_create(struct buffer *buffer, size_t size, const char *name) |
| 57 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 58 | buffer->name = strdup(name); |
| 59 | buffer->size = size; |
| 60 | buffer->data = (char *)malloc(buffer->size); |
| 61 | if (!buffer->data) { |
| 62 | fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n", |
| 63 | size); |
| 64 | } |
| 65 | return (buffer->data == NULL); |
| 66 | } |
| 67 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 68 | int buffer_from_file(struct buffer *buffer, const char *filename) |
| 69 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 70 | FILE *fp = fopen(filename, "rb"); |
| 71 | if (!fp) { |
| 72 | perror(filename); |
| 73 | return -1; |
| 74 | } |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 75 | buffer->size = get_file_size(fp); |
| 76 | if (buffer->size == -1) { |
| 77 | fprintf(stderr, "could not determine size of %s\n", filename); |
| 78 | fclose(fp); |
| 79 | return -1; |
| 80 | } |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 81 | buffer->name = strdup(filename); |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 82 | buffer->data = (char *)malloc(buffer->size); |
| 83 | assert(buffer->data); |
| 84 | if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 85 | fprintf(stderr, "incomplete read: %s\n", filename); |
| 86 | fclose(fp); |
| 87 | return -1; |
| 88 | } |
| 89 | fclose(fp); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 93 | int buffer_write_file(struct buffer *buffer, const char *filename) |
| 94 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 95 | FILE *fp = fopen(filename, "wb"); |
| 96 | if (!fp) { |
| 97 | perror(filename); |
| 98 | return -1; |
| 99 | } |
| 100 | assert(buffer && buffer->data); |
| 101 | if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 102 | fprintf(stderr, "incomplete write: %s\n", filename); |
| 103 | fclose(fp); |
| 104 | return -1; |
| 105 | } |
| 106 | fclose(fp); |
| 107 | return 0; |
| 108 | } |
| 109 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 110 | void buffer_delete(struct buffer *buffer) |
| 111 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 112 | assert(buffer); |
| 113 | if (buffer->name) { |
| 114 | free(buffer->name); |
| 115 | buffer->name = NULL; |
| 116 | } |
| 117 | if (buffer->data) { |
| 118 | free(buffer->data); |
| 119 | buffer->data = NULL; |
| 120 | } |
| 121 | buffer->size = 0; |
| 122 | } |
| 123 | |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 124 | void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file) |
| 125 | { |
| 126 | bgets(buf, &file->magic, sizeof(file->magic)); |
| 127 | file->len = xdr_be.get32(buf); |
| 128 | file->type = xdr_be.get32(buf); |
| 129 | file->checksum = xdr_be.get32(buf); |
| 130 | file->offset = xdr_be.get32(buf); |
| 131 | } |
| 132 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 133 | static struct { |
| 134 | uint32_t arch; |
| 135 | const char *name; |
| 136 | } arch_names[] = { |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 137 | { CBFS_ARCHITECTURE_AARCH64, "arm64" }, |
Gabe Black | 51edd54 | 2013-09-30 23:00:33 -0700 | [diff] [blame] | 138 | { CBFS_ARCHITECTURE_ARM, "arm" }, |
Paul Burton | 3318692 | 2014-06-13 23:56:45 +0100 | [diff] [blame] | 139 | { CBFS_ARCHITECTURE_MIPS, "mips" }, |
Ronald G. Minnich | 833bf20 | 2014-10-16 10:55:39 +0000 | [diff] [blame] | 140 | { CBFS_ARCHITECTURE_RISCV, "riscv" }, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 141 | { CBFS_ARCHITECTURE_X86, "x86" }, |
| 142 | { CBFS_ARCHITECTURE_UNKNOWN, "unknown" } |
| 143 | }; |
| 144 | |
| 145 | uint32_t string_to_arch(const char *arch_string) |
| 146 | { |
| 147 | int i; |
| 148 | uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN; |
| 149 | |
| 150 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 151 | if (!strcasecmp(arch_string, arch_names[i].name)) { |
| 152 | ret = arch_names[i].arch; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 160 | const char *arch_to_string(uint32_t a) |
| 161 | { |
| 162 | int i; |
| 163 | const char *ret = NULL; |
| 164 | |
| 165 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 166 | if (a == arch_names[i].arch) { |
| 167 | ret = arch_names[i].name; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 175 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 176 | uint32_t type; |
| 177 | const char *name; |
| 178 | } filetypes[] = { |
| 179 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 180 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 181 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 182 | {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"}, |
| 183 | {CBFS_COMPONENT_RAW, "raw"}, |
| 184 | {CBFS_COMPONENT_VSA, "vsa"}, |
| 185 | {CBFS_COMPONENT_MBI, "mbi"}, |
| 186 | {CBFS_COMPONENT_MICROCODE, "microcode"}, |
Martin Roth | dde307c | 2015-03-24 15:54:20 -0600 | [diff] [blame] | 187 | {CBFS_COMPONENT_FSP, "fsp"}, |
| 188 | {CBFS_COMPONENT_MRC, "mrc"}, |
Patrick Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 189 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 190 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Martin Roth | dde307c | 2015-03-24 15:54:20 -0600 | [diff] [blame] | 191 | {CBFS_COMPONENT_SPD, "spd"}, |
| 192 | {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 193 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 194 | {CBFS_COMPONENT_NULL, "null"} |
| 195 | }; |
| 196 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 197 | void print_supported_filetypes(void) |
| 198 | { |
| 199 | int i, number = ARRAY_SIZE(filetypes); |
| 200 | |
| 201 | for (i=0; i<number; i++) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 202 | LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 203 | if ((i%8) == 7) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 204 | LOG("\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 208 | uint64_t intfiletype(const char *name) |
| 209 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 210 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 211 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 212 | if (strcmp(filetypes[i].name, name) == 0) |
| 213 | return filetypes[i].type; |
| 214 | return -1; |
| 215 | } |