blob: e0474b34352fcd64b650f5f865f8043ba82b8663 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010019 * Foundation, Inc.
Patrick Georgib7b56dd82009-09-14 13:29:27 +000020 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080025#include <strings.h>
Patrick Georgi32eeff42014-08-11 09:27:18 +020026#include <sys/types.h>
27#include <sys/stat.h>
28#include <unistd.h>
Uwe Hermann942a40d2010-02-10 19:52:35 +000029#include <libgen.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000030#include "common.h"
31#include "cbfs.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000032
Hung-Te Lin332795c2013-01-28 15:53:34 +080033/* Utilities */
Aaron Durbinfae75172014-03-05 15:02:21 -060034int verbose = 0;
Hung-Te Lin332795c2013-01-28 15:53:34 +080035
36/* Small, OS/libc independent runtime check for endianess */
37int is_big_endian(void)
38{
39 static const uint32_t inttest = 0x12345678;
Sol Boucher0e539312015-03-05 15:38:03 -080040 const uint8_t inttest_lsb = *(const uint8_t *)&inttest;
Hung-Te Lin332795c2013-01-28 15:53:34 +080041 if (inttest_lsb == 0x12) {
42 return 1;
43 }
44 return 0;
45}
46
Patrick Georgi32eeff42014-08-11 09:27:18 +020047static off_t get_file_size(FILE *f)
48{
Stefan Reinauer1bb487c2015-09-14 10:46:44 -070049 off_t fsize;
50 fseek(f, 0, SEEK_END);
51 fsize = ftell(f);
52 fseek(f, 0, SEEK_SET);
53 return fsize;
Patrick Georgi32eeff42014-08-11 09:27:18 +020054}
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080055
Stefan Reinauer1bb487c2015-09-14 10:46:44 -070056/* Buffer and file I/O */
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010057int buffer_create(struct buffer *buffer, size_t size, const char *name)
58{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080059 buffer->name = strdup(name);
Sol Boucher64c6cd72015-04-26 02:32:43 -070060 buffer->offset = 0;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080061 buffer->size = size;
62 buffer->data = (char *)malloc(buffer->size);
63 if (!buffer->data) {
64 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
65 size);
66 }
67 return (buffer->data == NULL);
68}
69
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010070int buffer_from_file(struct buffer *buffer, const char *filename)
71{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080072 FILE *fp = fopen(filename, "rb");
73 if (!fp) {
74 perror(filename);
75 return -1;
76 }
Sol Boucher64c6cd72015-04-26 02:32:43 -070077 buffer->offset = 0;
Patrick Georgi32eeff42014-08-11 09:27:18 +020078 buffer->size = get_file_size(fp);
Sol Boucher0e539312015-03-05 15:38:03 -080079 if (buffer->size == -1u) {
Patrick Georgi32eeff42014-08-11 09:27:18 +020080 fprintf(stderr, "could not determine size of %s\n", filename);
81 fclose(fp);
82 return -1;
83 }
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080084 buffer->name = strdup(filename);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080085 buffer->data = (char *)malloc(buffer->size);
86 assert(buffer->data);
87 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
88 fprintf(stderr, "incomplete read: %s\n", filename);
89 fclose(fp);
Sol Bouchere3260a02015-03-25 13:40:08 -070090 buffer_delete(buffer);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080091 return -1;
92 }
93 fclose(fp);
94 return 0;
95}
96
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010097int buffer_write_file(struct buffer *buffer, const char *filename)
98{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080099 FILE *fp = fopen(filename, "wb");
100 if (!fp) {
101 perror(filename);
102 return -1;
103 }
104 assert(buffer && buffer->data);
105 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
106 fprintf(stderr, "incomplete write: %s\n", filename);
107 fclose(fp);
108 return -1;
109 }
110 fclose(fp);
111 return 0;
112}
113
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100114void buffer_delete(struct buffer *buffer)
115{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800116 assert(buffer);
117 if (buffer->name) {
118 free(buffer->name);
119 buffer->name = NULL;
120 }
121 if (buffer->data) {
Sol Boucher64c6cd72015-04-26 02:32:43 -0700122 free(buffer->data - buffer->offset);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800123 buffer->data = NULL;
124 }
Sol Boucher64c6cd72015-04-26 02:32:43 -0700125 buffer->offset = 0;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800126 buffer->size = 0;
127}
128
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800129void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
130{
131 bgets(buf, &file->magic, sizeof(file->magic));
132 file->len = xdr_be.get32(buf);
133 file->type = xdr_be.get32(buf);
Patrick Georgi0d618af2015-07-15 18:28:23 +0200134 file->attributes_offset = xdr_be.get32(buf);
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800135 file->offset = xdr_be.get32(buf);
136}
137
David Hendricks90ca3b62012-11-16 14:48:22 -0800138static struct {
139 uint32_t arch;
140 const char *name;
141} arch_names[] = {
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700142 { CBFS_ARCHITECTURE_AARCH64, "arm64" },
Gabe Black51edd542013-09-30 23:00:33 -0700143 { CBFS_ARCHITECTURE_ARM, "arm" },
Paul Burton33186922014-06-13 23:56:45 +0100144 { CBFS_ARCHITECTURE_MIPS, "mips" },
Ronald G. Minnich833bf202014-10-16 10:55:39 +0000145 { CBFS_ARCHITECTURE_RISCV, "riscv" },
David Hendricks90ca3b62012-11-16 14:48:22 -0800146 { CBFS_ARCHITECTURE_X86, "x86" },
147 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
148};
149
150uint32_t string_to_arch(const char *arch_string)
151{
152 int i;
153 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
154
155 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
156 if (!strcasecmp(arch_string, arch_names[i].name)) {
157 ret = arch_names[i].arch;
158 break;
159 }
160 }
161
162 return ret;
163}
164
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800165const char *arch_to_string(uint32_t a)
166{
167 int i;
168 const char *ret = NULL;
169
170 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
171 if (a == arch_names[i].arch) {
172 ret = arch_names[i].name;
173 break;
174 }
175 }
176
177 return ret;
178}
179
Stefan Reinauer07040582010-04-24 21:24:06 +0000180void print_supported_filetypes(void)
181{
182 int i, number = ARRAY_SIZE(filetypes);
183
184 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800185 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000186 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800187 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000188 }
189}
190
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000191uint64_t intfiletype(const char *name)
192{
Mathias Krause41c229c2012-07-17 21:17:15 +0200193 size_t i;
Patrick Georgidc37dab2015-09-09 16:46:00 +0200194 for (i = 0; i < (sizeof(filetypes) / sizeof(struct typedesc_t)); i++)
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000195 if (strcmp(filetypes[i].name, name) == 0)
196 return filetypes[i].type;
197 return -1;
198}