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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 19 | * Foundation, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 25 | #include <strings.h> |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <unistd.h> |
Uwe Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 29 | #include <libgen.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 30 | #include "common.h" |
| 31 | #include "cbfs.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 32 | |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 33 | /* Utilities */ |
Aaron Durbin | fae7517 | 2014-03-05 15:02:21 -0600 | [diff] [blame] | 34 | int verbose = 0; |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 35 | |
| 36 | /* Small, OS/libc independent runtime check for endianess */ |
| 37 | int is_big_endian(void) |
| 38 | { |
| 39 | static const uint32_t inttest = 0x12345678; |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 40 | const uint8_t inttest_lsb = *(const uint8_t *)&inttest; |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 41 | if (inttest_lsb == 0x12) { |
| 42 | return 1; |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 47 | static off_t get_file_size(FILE *f) |
| 48 | { |
| 49 | struct stat s; |
| 50 | int fd = fileno(f); |
| 51 | if (fd == -1) return -1; |
| 52 | if (fstat(fd, &s) == -1) return -1; |
| 53 | return s.st_size; |
| 54 | } |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 55 | /* Buffer and file I/O */ |
| 56 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 57 | int buffer_create(struct buffer *buffer, size_t size, const char *name) |
| 58 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 59 | buffer->name = strdup(name); |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 60 | buffer->offset = 0; |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 61 | buffer->size = size; |
| 62 | buffer->data = (char *)malloc(buffer->size); |
| 63 | if (!buffer->data) { |
| 64 | fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n", |
| 65 | size); |
| 66 | } |
| 67 | return (buffer->data == NULL); |
| 68 | } |
| 69 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 70 | int buffer_from_file(struct buffer *buffer, const char *filename) |
| 71 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 72 | FILE *fp = fopen(filename, "rb"); |
| 73 | if (!fp) { |
| 74 | perror(filename); |
| 75 | return -1; |
| 76 | } |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 77 | buffer->offset = 0; |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 78 | buffer->size = get_file_size(fp); |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 79 | if (buffer->size == -1u) { |
Patrick Georgi | 32eeff4 | 2014-08-11 09:27:18 +0200 | [diff] [blame] | 80 | fprintf(stderr, "could not determine size of %s\n", filename); |
| 81 | fclose(fp); |
| 82 | return -1; |
| 83 | } |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 84 | buffer->name = strdup(filename); |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 85 | buffer->data = (char *)malloc(buffer->size); |
| 86 | assert(buffer->data); |
| 87 | if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 88 | fprintf(stderr, "incomplete read: %s\n", filename); |
| 89 | fclose(fp); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 90 | buffer_delete(buffer); |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 91 | return -1; |
| 92 | } |
| 93 | fclose(fp); |
| 94 | return 0; |
| 95 | } |
| 96 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 97 | int buffer_write_file(struct buffer *buffer, const char *filename) |
| 98 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 99 | FILE *fp = fopen(filename, "wb"); |
| 100 | if (!fp) { |
| 101 | perror(filename); |
| 102 | return -1; |
| 103 | } |
| 104 | assert(buffer && buffer->data); |
| 105 | if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 106 | fprintf(stderr, "incomplete write: %s\n", filename); |
| 107 | fclose(fp); |
| 108 | return -1; |
| 109 | } |
| 110 | fclose(fp); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 114 | void buffer_delete(struct buffer *buffer) |
| 115 | { |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 116 | assert(buffer); |
| 117 | if (buffer->name) { |
| 118 | free(buffer->name); |
| 119 | buffer->name = NULL; |
| 120 | } |
| 121 | if (buffer->data) { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 122 | free(buffer->data - buffer->offset); |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 123 | buffer->data = NULL; |
| 124 | } |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 125 | buffer->offset = 0; |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 126 | buffer->size = 0; |
| 127 | } |
| 128 | |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 129 | void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file) |
| 130 | { |
| 131 | bgets(buf, &file->magic, sizeof(file->magic)); |
| 132 | file->len = xdr_be.get32(buf); |
| 133 | file->type = xdr_be.get32(buf); |
Patrick Georgi | 0d618af | 2015-07-15 18:28:23 +0200 | [diff] [blame] | 134 | file->attributes_offset = xdr_be.get32(buf); |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 135 | file->offset = xdr_be.get32(buf); |
| 136 | } |
| 137 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 138 | static struct { |
| 139 | uint32_t arch; |
| 140 | const char *name; |
| 141 | } arch_names[] = { |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 142 | { CBFS_ARCHITECTURE_AARCH64, "arm64" }, |
Gabe Black | 51edd54 | 2013-09-30 23:00:33 -0700 | [diff] [blame] | 143 | { CBFS_ARCHITECTURE_ARM, "arm" }, |
Paul Burton | 3318692 | 2014-06-13 23:56:45 +0100 | [diff] [blame] | 144 | { CBFS_ARCHITECTURE_MIPS, "mips" }, |
Ronald G. Minnich | 833bf20 | 2014-10-16 10:55:39 +0000 | [diff] [blame] | 145 | { CBFS_ARCHITECTURE_RISCV, "riscv" }, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 146 | { CBFS_ARCHITECTURE_X86, "x86" }, |
| 147 | { CBFS_ARCHITECTURE_UNKNOWN, "unknown" } |
| 148 | }; |
| 149 | |
| 150 | uint32_t string_to_arch(const char *arch_string) |
| 151 | { |
| 152 | int i; |
| 153 | uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN; |
| 154 | |
| 155 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 156 | if (!strcasecmp(arch_string, arch_names[i].name)) { |
| 157 | ret = arch_names[i].arch; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 165 | const char *arch_to_string(uint32_t a) |
| 166 | { |
| 167 | int i; |
| 168 | const char *ret = NULL; |
| 169 | |
| 170 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 171 | if (a == arch_names[i].arch) { |
| 172 | ret = arch_names[i].name; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 180 | void print_supported_filetypes(void) |
| 181 | { |
| 182 | int i, number = ARRAY_SIZE(filetypes); |
| 183 | |
| 184 | for (i=0; i<number; i++) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 185 | LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 186 | if ((i%8) == 7) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 187 | LOG("\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 191 | uint64_t intfiletype(const char *name) |
| 192 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 193 | size_t i; |
Patrick Georgi | dc37dab | 2015-09-09 16:46:00 +0200 | [diff] [blame] | 194 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct typedesc_t)); i++) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 195 | if (strcmp(filetypes[i].name, name) == 0) |
| 196 | return filetypes[i].type; |
| 197 | return -1; |
| 198 | } |