blob: e8c2ccc45663a8b5428b094ccceeb3e5deaa14d4 [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
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
20 */
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{
49 struct stat s;
50 int fd = fileno(f);
51 if (fd == -1) return -1;
52 if (fstat(fd, &s) == -1) return -1;
53 return s.st_size;
54}
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080055/* Buffer and file I/O */
56
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);
60 buffer->size = size;
61 buffer->data = (char *)malloc(buffer->size);
62 if (!buffer->data) {
63 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
64 size);
65 }
66 return (buffer->data == NULL);
67}
68
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010069int buffer_from_file(struct buffer *buffer, const char *filename)
70{
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080071 FILE *fp = fopen(filename, "rb");
72 if (!fp) {
73 perror(filename);
74 return -1;
75 }
Patrick Georgi32eeff42014-08-11 09:27:18 +020076 buffer->size = get_file_size(fp);
Sol Boucher0e539312015-03-05 15:38:03 -080077 if (buffer->size == -1u) {
Patrick Georgi32eeff42014-08-11 09:27:18 +020078 fprintf(stderr, "could not determine size of %s\n", filename);
79 fclose(fp);
80 return -1;
81 }
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080082 buffer->name = strdup(filename);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080083 buffer->data = (char *)malloc(buffer->size);
84 assert(buffer->data);
85 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
86 fprintf(stderr, "incomplete read: %s\n", filename);
87 fclose(fp);
88 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) {
119 free(buffer->data);
120 buffer->data = NULL;
121 }
122 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);
130 file->checksum = xdr_be.get32(buf);
131 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. Minnich833bf202014-10-16 10:55:39 +0000141 { CBFS_ARCHITECTURE_RISCV, "riscv" },
David Hendricks90ca3b62012-11-16 14:48:22 -0800142 { CBFS_ARCHITECTURE_X86, "x86" },
143 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
144};
145
146uint32_t string_to_arch(const char *arch_string)
147{
148 int i;
149 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
150
151 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
152 if (!strcasecmp(arch_string, arch_names[i].name)) {
153 ret = arch_names[i].arch;
154 break;
155 }
156 }
157
158 return ret;
159}
160
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800161const char *arch_to_string(uint32_t a)
162{
163 int i;
164 const char *ret = NULL;
165
166 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
167 if (a == arch_names[i].arch) {
168 ret = arch_names[i].name;
169 break;
170 }
171 }
172
173 return ret;
174}
175
Mathias Krause941158f2012-04-12 21:36:23 +0200176static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000177 uint32_t type;
178 const char *name;
179} filetypes[] = {
180 {CBFS_COMPONENT_STAGE, "stage"},
181 {CBFS_COMPONENT_PAYLOAD, "payload"},
182 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000183 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
184 {CBFS_COMPONENT_RAW, "raw"},
185 {CBFS_COMPONENT_VSA, "vsa"},
186 {CBFS_COMPONENT_MBI, "mbi"},
187 {CBFS_COMPONENT_MICROCODE, "microcode"},
Martin Rothdde307c2015-03-24 15:54:20 -0600188 {CBFS_COMPONENT_FSP, "fsp"},
189 {CBFS_COMPONENT_MRC, "mrc"},
Patrick Georgia865b172011-01-14 07:40:24 +0000190 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200191 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Martin Rothdde307c2015-03-24 15:54:20 -0600192 {CBFS_COMPONENT_SPD, "spd"},
193 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000194 {CBFS_COMPONENT_DELETED, "deleted"},
195 {CBFS_COMPONENT_NULL, "null"}
196};
197
Stefan Reinauer07040582010-04-24 21:24:06 +0000198void print_supported_filetypes(void)
199{
200 int i, number = ARRAY_SIZE(filetypes);
201
202 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800203 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000204 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800205 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000206 }
207}
208
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000209uint64_t intfiletype(const char *name)
210{
Mathias Krause41c229c2012-07-17 21:17:15 +0200211 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000212 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
213 if (strcmp(filetypes[i].name, name) == 0)
214 return filetypes[i].type;
215 return -1;
216}