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