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