blob: 4534d8526a681893eb057e38fe0f948f4db74ec2 [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
32/* Small, OS/libc independent runtime check for endianess */
33int 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 Georgi32eeff42014-08-11 09:27:18 +020074 buffer->size = get_file_size(fp);
Sol Boucher0e539312015-03-05 15:38:03 -080075 if (buffer->size == -1u) {
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 }
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080080 buffer->name = strdup(filename);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080081 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 Bouchere3260a02015-03-25 13:40:08 -070086 buffer_delete(buffer);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080087 return -1;
88 }
89 fclose(fp);
90 return 0;
91}
92
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010093int buffer_write_file(struct buffer *buffer, const char *filename)
94{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080095 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 Reinauer2dd161f2015-03-04 00:55:03 +0100110void buffer_delete(struct buffer *buffer)
111{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800112 assert(buffer);
113 if (buffer->name) {
114 free(buffer->name);
115 buffer->name = NULL;
116 }
117 if (buffer->data) {
Patrick Georgi9b24f7c2015-11-11 18:43:11 +0100118 free(buffer_get_original_backing(buffer));
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800119 buffer->data = NULL;
120 }
Sol Boucher64c6cd72015-04-26 02:32:43 -0700121 buffer->offset = 0;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800122 buffer->size = 0;
123}
124
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800125void 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 Georgi0d618af2015-07-15 18:28:23 +0200130 file->attributes_offset = xdr_be.get32(buf);
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800131 file->offset = xdr_be.get32(buf);
132}
133
David Hendricks90ca3b62012-11-16 14:48:22 -0800134static struct {
135 uint32_t arch;
136 const char *name;
137} arch_names[] = {
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700138 { CBFS_ARCHITECTURE_AARCH64, "arm64" },
Gabe Black51edd542013-09-30 23:00:33 -0700139 { CBFS_ARCHITECTURE_ARM, "arm" },
Paul Burton33186922014-06-13 23:56:45 +0100140 { CBFS_ARCHITECTURE_MIPS, "mips" },
Ronald G. Minniched4aa042015-12-11 18:19:52 +0000141 { CBFS_ARCHITECTURE_PPC64, "ppc64" },
142 /* power8 is a reasonable alias */
143 { CBFS_ARCHITECTURE_PPC64, "power8" },
Ronald G. Minnich833bf202014-10-16 10:55:39 +0000144 { CBFS_ARCHITECTURE_RISCV, "riscv" },
David Hendricks90ca3b62012-11-16 14:48:22 -0800145 { CBFS_ARCHITECTURE_X86, "x86" },
146 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
147};
148
149uint32_t string_to_arch(const char *arch_string)
150{
151 int i;
152 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
153
154 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
155 if (!strcasecmp(arch_string, arch_names[i].name)) {
156 ret = arch_names[i].arch;
157 break;
158 }
159 }
160
161 return ret;
162}
163
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800164const char *arch_to_string(uint32_t a)
165{
166 int i;
167 const char *ret = NULL;
168
169 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
170 if (a == arch_names[i].arch) {
171 ret = arch_names[i].name;
172 break;
173 }
174 }
175
176 return ret;
177}
178
Stefan Reinauer07040582010-04-24 21:24:06 +0000179void print_supported_filetypes(void)
180{
181 int i, number = ARRAY_SIZE(filetypes);
182
183 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800184 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000185 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800186 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000187 }
188}
189
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000190uint64_t intfiletype(const char *name)
191{
Mathias Krause41c229c2012-07-17 21:17:15 +0200192 size_t i;
Patrick Georgidc37dab2015-09-09 16:46:00 +0200193 for (i = 0; i < (sizeof(filetypes) / sizeof(struct typedesc_t)); i++)
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000194 if (strcmp(filetypes[i].name, name) == 0)
195 return filetypes[i].type;
196 return -1;
197}
Patrick Georgica97fa72015-10-01 15:52:56 +0200198
199char *bintohex(uint8_t *data, size_t len)
200{
201 static const char translate[16] = "0123456789abcdef";
202
203 char *result = malloc(len * 2 + 1);
204 if (result == NULL)
205 return NULL;
206
207 result[len*2] = '\0';
208 unsigned int i;
209 for (i = 0; i < len; i++) {
210 result[i*2] = translate[(data[i] >> 4) & 0xf];
211 result[i*2+1] = translate[data[i] & 0xf];
212 }
213 return result;
214}