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 | |
| 56 | int buffer_create(struct buffer *buffer, size_t size, const char *name) { |
| 57 | buffer->name = strdup(name); |
| 58 | buffer->size = size; |
| 59 | buffer->data = (char *)malloc(buffer->size); |
| 60 | if (!buffer->data) { |
| 61 | fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n", |
| 62 | size); |
| 63 | } |
| 64 | return (buffer->data == NULL); |
| 65 | } |
| 66 | |
| 67 | int buffer_from_file(struct buffer *buffer, const char *filename) { |
| 68 | FILE *fp = fopen(filename, "rb"); |
| 69 | if (!fp) { |
| 70 | perror(filename); |
| 71 | return -1; |
| 72 | } |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 73 | buffer->size = get_file_size(fp); |
| 74 | if (buffer->size == -1) { |
| 75 | fprintf(stderr, "could not determine size of %s\n", filename); |
| 76 | fclose(fp); |
| 77 | return -1; |
| 78 | } |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 79 | buffer->name = strdup(filename); |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 80 | buffer->data = (char *)malloc(buffer->size); |
| 81 | assert(buffer->data); |
| 82 | if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 83 | fprintf(stderr, "incomplete read: %s\n", filename); |
| 84 | fclose(fp); |
| 85 | return -1; |
| 86 | } |
| 87 | fclose(fp); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int buffer_write_file(struct buffer *buffer, const char *filename) { |
| 92 | FILE *fp = fopen(filename, "wb"); |
| 93 | if (!fp) { |
| 94 | perror(filename); |
| 95 | return -1; |
| 96 | } |
| 97 | assert(buffer && buffer->data); |
| 98 | if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 99 | fprintf(stderr, "incomplete write: %s\n", filename); |
| 100 | fclose(fp); |
| 101 | return -1; |
| 102 | } |
| 103 | fclose(fp); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void buffer_delete(struct buffer *buffer) { |
| 108 | assert(buffer); |
| 109 | if (buffer->name) { |
| 110 | free(buffer->name); |
| 111 | buffer->name = NULL; |
| 112 | } |
| 113 | if (buffer->data) { |
| 114 | free(buffer->data); |
| 115 | buffer->data = NULL; |
| 116 | } |
| 117 | buffer->size = 0; |
| 118 | } |
| 119 | |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 120 | void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file) |
| 121 | { |
| 122 | bgets(buf, &file->magic, sizeof(file->magic)); |
| 123 | file->len = xdr_be.get32(buf); |
| 124 | file->type = xdr_be.get32(buf); |
| 125 | file->checksum = xdr_be.get32(buf); |
| 126 | file->offset = xdr_be.get32(buf); |
| 127 | } |
| 128 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 129 | static struct { |
| 130 | uint32_t arch; |
| 131 | const char *name; |
| 132 | } arch_names[] = { |
Ronald G. Minnich | 5f43184 | 2013-11-12 09:03:33 -0800 | [diff] [blame] | 133 | { CBFS_ARCHITECTURE_AARCH64, "aarch64" }, |
Gabe Black | 51edd54 | 2013-09-30 23:00:33 -0700 | [diff] [blame] | 134 | { CBFS_ARCHITECTURE_ARM, "arm" }, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 135 | { CBFS_ARCHITECTURE_X86, "x86" }, |
| 136 | { CBFS_ARCHITECTURE_UNKNOWN, "unknown" } |
| 137 | }; |
| 138 | |
| 139 | uint32_t string_to_arch(const char *arch_string) |
| 140 | { |
| 141 | int i; |
| 142 | uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN; |
| 143 | |
| 144 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 145 | if (!strcasecmp(arch_string, arch_names[i].name)) { |
| 146 | ret = arch_names[i].arch; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 154 | const char *arch_to_string(uint32_t a) |
| 155 | { |
| 156 | int i; |
| 157 | const char *ret = NULL; |
| 158 | |
| 159 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 160 | if (a == arch_names[i].arch) { |
| 161 | ret = arch_names[i].name; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 169 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 170 | uint32_t type; |
| 171 | const char *name; |
| 172 | } filetypes[] = { |
| 173 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 174 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 175 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 176 | {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"}, |
| 177 | {CBFS_COMPONENT_RAW, "raw"}, |
| 178 | {CBFS_COMPONENT_VSA, "vsa"}, |
| 179 | {CBFS_COMPONENT_MBI, "mbi"}, |
| 180 | {CBFS_COMPONENT_MICROCODE, "microcode"}, |
Patrick Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 181 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 182 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 183 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 184 | {CBFS_COMPONENT_NULL, "null"} |
| 185 | }; |
| 186 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 187 | void print_supported_filetypes(void) |
| 188 | { |
| 189 | int i, number = ARRAY_SIZE(filetypes); |
| 190 | |
| 191 | for (i=0; i<number; i++) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 192 | LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 193 | if ((i%8) == 7) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 194 | LOG("\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 198 | uint64_t intfiletype(const char *name) |
| 199 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 200 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 201 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 202 | if (strcmp(filetypes[i].name, name) == 0) |
| 203 | return filetypes[i].type; |
| 204 | return -1; |
| 205 | } |