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