blob: bd9cdc0aa5c4925170100d4832610e95d7f4b121 [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>
Patrick Georgi32eeff42014-08-11 09:27:18 +020025#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
Uwe Hermann942a40d2010-02-10 19:52:35 +000028#include <libgen.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000029#include "common.h"
30#include "cbfs.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000031
Hung-Te Lin332795c2013-01-28 15:53:34 +080032/* Utilities */
Aaron Durbinfae75172014-03-05 15:02:21 -060033int verbose = 0;
Hung-Te Lin332795c2013-01-28 15:53:34 +080034
35/* Small, OS/libc independent runtime check for endianess */
36int is_big_endian(void)
37{
38 static const uint32_t inttest = 0x12345678;
39 uint8_t inttest_lsb = *(uint8_t *)&inttest;
40 if (inttest_lsb == 0x12) {
41 return 1;
42 }
43 return 0;
44}
45
Patrick Georgi32eeff42014-08-11 09:27:18 +020046static off_t get_file_size(FILE *f)
47{
48 struct stat s;
49 int fd = fileno(f);
50 if (fd == -1) return -1;
51 if (fstat(fd, &s) == -1) return -1;
52 return s.st_size;
53}
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080054/* Buffer and file I/O */
55
56int buffer_create(struct buffer *buffer, size_t size, const char *name) {
57 buffer->name = strdup(name);
58 buffer->size = size;
59 buffer->data = (char *)malloc(buffer->size);
60 if (!buffer->data) {
61 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
62 size);
63 }
64 return (buffer->data == NULL);
65}
66
67int buffer_from_file(struct buffer *buffer, const char *filename) {
68 FILE *fp = fopen(filename, "rb");
69 if (!fp) {
70 perror(filename);
71 return -1;
72 }
Patrick Georgi32eeff42014-08-11 09:27:18 +020073 buffer->size = get_file_size(fp);
74 if (buffer->size == -1) {
75 fprintf(stderr, "could not determine size of %s\n", filename);
76 fclose(fp);
77 return -1;
78 }
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080079 buffer->name = strdup(filename);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080080 buffer->data = (char *)malloc(buffer->size);
81 assert(buffer->data);
82 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
83 fprintf(stderr, "incomplete read: %s\n", filename);
84 fclose(fp);
85 return -1;
86 }
87 fclose(fp);
88 return 0;
89}
90
91int buffer_write_file(struct buffer *buffer, const char *filename) {
92 FILE *fp = fopen(filename, "wb");
93 if (!fp) {
94 perror(filename);
95 return -1;
96 }
97 assert(buffer && buffer->data);
98 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
99 fprintf(stderr, "incomplete write: %s\n", filename);
100 fclose(fp);
101 return -1;
102 }
103 fclose(fp);
104 return 0;
105}
106
107void buffer_delete(struct buffer *buffer) {
108 assert(buffer);
109 if (buffer->name) {
110 free(buffer->name);
111 buffer->name = NULL;
112 }
113 if (buffer->data) {
114 free(buffer->data);
115 buffer->data = NULL;
116 }
117 buffer->size = 0;
118}
119
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800120void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
121{
122 bgets(buf, &file->magic, sizeof(file->magic));
123 file->len = xdr_be.get32(buf);
124 file->type = xdr_be.get32(buf);
125 file->checksum = xdr_be.get32(buf);
126 file->offset = xdr_be.get32(buf);
127}
128
David Hendricks90ca3b62012-11-16 14:48:22 -0800129static struct {
130 uint32_t arch;
131 const char *name;
132} arch_names[] = {
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700133 { CBFS_ARCHITECTURE_AARCH64, "arm64" },
Gabe Black51edd542013-09-30 23:00:33 -0700134 { CBFS_ARCHITECTURE_ARM, "arm" },
Paul Burton33186922014-06-13 23:56:45 +0100135 { CBFS_ARCHITECTURE_MIPS, "mips" },
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{
143 int i;
144 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{
158 int i;
159 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
Mathias Krause941158f2012-04-12 21:36:23 +0200171static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000172 uint32_t type;
173 const char *name;
174} filetypes[] = {
175 {CBFS_COMPONENT_STAGE, "stage"},
176 {CBFS_COMPONENT_PAYLOAD, "payload"},
177 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000178 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
179 {CBFS_COMPONENT_RAW, "raw"},
180 {CBFS_COMPONENT_VSA, "vsa"},
181 {CBFS_COMPONENT_MBI, "mbi"},
182 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000183 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200184 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000185 {CBFS_COMPONENT_DELETED, "deleted"},
186 {CBFS_COMPONENT_NULL, "null"}
187};
188
Stefan Reinauer07040582010-04-24 21:24:06 +0000189void print_supported_filetypes(void)
190{
191 int i, number = ARRAY_SIZE(filetypes);
192
193 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800194 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000195 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800196 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000197 }
198}
199
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000200uint64_t intfiletype(const char *name)
201{
Mathias Krause41c229c2012-07-17 21:17:15 +0200202 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000203 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
204 if (strcmp(filetypes[i].name, name) == 0)
205 return filetypes[i].type;
206 return -1;
207}