blob: c1725dc90384e36bfa61ee147195aeafc6eff963 [file] [log] [blame]
Patrick Georgib7b56dd82009-09-14 13:29:27 +00001/*
2 * common utility functions for cbfstool
3 *
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
David Hendricks90ca3b62012-11-16 14:48:22 -08006 * Copyright (C) 2012 Google, Inc.
Patrick Georgib7b56dd82009-09-14 13:29:27 +00007 *
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 Georgib7b56dd82009-09-14 13:29:27 +000016 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080021#include <strings.h>
Patrick Georgi32eeff42014-08-11 09:27:18 +020022#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
Uwe Hermann942a40d2010-02-10 19:52:35 +000025#include <libgen.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000026#include "common.h"
27#include "cbfs.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000028
Hung-Te Lin332795c2013-01-28 15:53:34 +080029/* Utilities */
Aaron Durbinfae75172014-03-05 15:02:21 -060030int verbose = 0;
Hung-Te Lin332795c2013-01-28 15:53:34 +080031
Elyes HAOUAS3db01982018-08-23 18:08:20 +020032/* Small, OS/libc independent runtime check for endianness */
Hung-Te Lin332795c2013-01-28 15:53:34 +080033int is_big_endian(void)
34{
35 static const uint32_t inttest = 0x12345678;
Sol Boucher0e539312015-03-05 15:38:03 -080036 const uint8_t inttest_lsb = *(const uint8_t *)&inttest;
Hung-Te Lin332795c2013-01-28 15:53:34 +080037 if (inttest_lsb == 0x12) {
38 return 1;
39 }
40 return 0;
41}
42
Patrick Georgi32eeff42014-08-11 09:27:18 +020043static off_t get_file_size(FILE *f)
44{
Stefan Reinauer1bb487c2015-09-14 10:46:44 -070045 off_t fsize;
46 fseek(f, 0, SEEK_END);
47 fsize = ftell(f);
48 fseek(f, 0, SEEK_SET);
49 return fsize;
Patrick Georgi32eeff42014-08-11 09:27:18 +020050}
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080051
Stefan Reinauer1bb487c2015-09-14 10:46:44 -070052/* Buffer and file I/O */
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010053int buffer_create(struct buffer *buffer, size_t size, const char *name)
54{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080055 buffer->name = strdup(name);
Sol Boucher64c6cd72015-04-26 02:32:43 -070056 buffer->offset = 0;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080057 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 Reinauer2dd161f2015-03-04 00:55:03 +010066int buffer_from_file(struct buffer *buffer, const char *filename)
67{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080068 FILE *fp = fopen(filename, "rb");
69 if (!fp) {
70 perror(filename);
71 return -1;
72 }
Sol Boucher64c6cd72015-04-26 02:32:43 -070073 buffer->offset = 0;
Patrick Georgi80998032016-12-14 16:16:47 +010074 off_t file_size = get_file_size(fp);
75 if (file_size < 0) {
Patrick Georgi32eeff42014-08-11 09:27:18 +020076 fprintf(stderr, "could not determine size of %s\n", filename);
77 fclose(fp);
78 return -1;
79 }
Patrick Georgi80998032016-12-14 16:16:47 +010080 buffer->size = file_size;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080081 buffer->name = strdup(filename);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080082 buffer->data = (char *)malloc(buffer->size);
83 assert(buffer->data);
84 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
85 fprintf(stderr, "incomplete read: %s\n", filename);
86 fclose(fp);
Sol Bouchere3260a02015-03-25 13:40:08 -070087 buffer_delete(buffer);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080088 return -1;
89 }
90 fclose(fp);
91 return 0;
92}
93
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010094int buffer_write_file(struct buffer *buffer, const char *filename)
95{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080096 FILE *fp = fopen(filename, "wb");
97 if (!fp) {
98 perror(filename);
99 return -1;
100 }
101 assert(buffer && buffer->data);
102 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
103 fprintf(stderr, "incomplete write: %s\n", filename);
104 fclose(fp);
105 return -1;
106 }
107 fclose(fp);
108 return 0;
109}
110
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100111void buffer_delete(struct buffer *buffer)
112{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800113 assert(buffer);
114 if (buffer->name) {
115 free(buffer->name);
116 buffer->name = NULL;
117 }
118 if (buffer->data) {
Patrick Georgi9b24f7c2015-11-11 18:43:11 +0100119 free(buffer_get_original_backing(buffer));
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800120 buffer->data = NULL;
121 }
Sol Boucher64c6cd72015-04-26 02:32:43 -0700122 buffer->offset = 0;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800123 buffer->size = 0;
124}
125
David Hendricks90ca3b62012-11-16 14:48:22 -0800126static struct {
127 uint32_t arch;
128 const char *name;
129} arch_names[] = {
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700130 { CBFS_ARCHITECTURE_AARCH64, "arm64" },
Gabe Black51edd542013-09-30 23:00:33 -0700131 { CBFS_ARCHITECTURE_ARM, "arm" },
Paul Burton33186922014-06-13 23:56:45 +0100132 { CBFS_ARCHITECTURE_MIPS, "mips" },
Ronald G. Minniched4aa042015-12-11 18:19:52 +0000133 { CBFS_ARCHITECTURE_PPC64, "ppc64" },
134 /* power8 is a reasonable alias */
135 { CBFS_ARCHITECTURE_PPC64, "power8" },
Ronald G. Minnich833bf202014-10-16 10:55:39 +0000136 { CBFS_ARCHITECTURE_RISCV, "riscv" },
David Hendricks90ca3b62012-11-16 14:48:22 -0800137 { CBFS_ARCHITECTURE_X86, "x86" },
138 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
139};
140
141uint32_t string_to_arch(const char *arch_string)
142{
Furquan Shaikh161d2332016-05-26 14:41:02 -0700143 size_t i;
David Hendricks90ca3b62012-11-16 14:48:22 -0800144 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
145
146 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
147 if (!strcasecmp(arch_string, arch_names[i].name)) {
148 ret = arch_names[i].arch;
149 break;
150 }
151 }
152
153 return ret;
154}
155
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800156const char *arch_to_string(uint32_t a)
157{
Furquan Shaikh161d2332016-05-26 14:41:02 -0700158 size_t i;
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800159 const char *ret = NULL;
160
161 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
162 if (a == arch_names[i].arch) {
163 ret = arch_names[i].name;
164 break;
165 }
166 }
167
168 return ret;
169}
170
Jonathan Neuschäferfbc66b92018-04-08 15:05:09 +0200171void print_supported_architectures(void)
172{
173 size_t i;
174
175 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
176 printf(i == 0? " ":", ");
177 printf("%s", arch_names[i].name);
178 }
179
180 printf("\n");
181}
182
Stefan Reinauer07040582010-04-24 21:24:06 +0000183void print_supported_filetypes(void)
184{
185 int i, number = ARRAY_SIZE(filetypes);
186
187 for (i=0; i<number; i++) {
Jonathan Neuschäfere32cea12018-04-08 15:05:09 +0200188 printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000189 if ((i%8) == 7)
Jonathan Neuschäfere32cea12018-04-08 15:05:09 +0200190 printf("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000191 }
192}
193
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000194uint64_t intfiletype(const char *name)
195{
Mathias Krause41c229c2012-07-17 21:17:15 +0200196 size_t i;
Patrick Georgidc37dab2015-09-09 16:46:00 +0200197 for (i = 0; i < (sizeof(filetypes) / sizeof(struct typedesc_t)); i++)
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000198 if (strcmp(filetypes[i].name, name) == 0)
199 return filetypes[i].type;
200 return -1;
201}
Patrick Georgica97fa72015-10-01 15:52:56 +0200202
203char *bintohex(uint8_t *data, size_t len)
204{
205 static const char translate[16] = "0123456789abcdef";
206
207 char *result = malloc(len * 2 + 1);
208 if (result == NULL)
209 return NULL;
210
211 result[len*2] = '\0';
212 unsigned int i;
213 for (i = 0; i < len; i++) {
214 result[i*2] = translate[(data[i] >> 4) & 0xf];
215 result[i*2+1] = translate[data[i] & 0xf];
216 }
217 return result;
218}