Remove address from GPLv2 headers
[coreboot.git] / util / cbfstool / common.c
blobfffd096b76305625161f4760d9eff35db019afa7
1 /*
2 * common utility functions for cbfstool
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
6 * Copyright (C) 2012 Google, Inc.
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.
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.
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.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <libgen.h>
30 #include "common.h"
31 #include "cbfs.h"
33 /* Utilities */
34 int verbose = 0;
36 /* Small, OS/libc independent runtime check for endianess */
37 int is_big_endian(void)
39 static const uint32_t inttest = 0x12345678;
40 const uint8_t inttest_lsb = *(const uint8_t *)&inttest;
41 if (inttest_lsb == 0x12) {
42 return 1;
44 return 0;
47 static off_t get_file_size(FILE *f)
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;
55 /* Buffer and file I/O */
57 int buffer_create(struct buffer *buffer, size_t size, const char *name)
59 buffer->name = strdup(name);
60 buffer->offset = 0;
61 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);
67 return (buffer->data == NULL);
70 int buffer_from_file(struct buffer *buffer, const char *filename)
72 FILE *fp = fopen(filename, "rb");
73 if (!fp) {
74 perror(filename);
75 return -1;
77 buffer->offset = 0;
78 buffer->size = get_file_size(fp);
79 if (buffer->size == -1u) {
80 fprintf(stderr, "could not determine size of %s\n", filename);
81 fclose(fp);
82 return -1;
84 buffer->name = strdup(filename);
85 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);
90 buffer_delete(buffer);
91 return -1;
93 fclose(fp);
94 return 0;
97 int buffer_write_file(struct buffer *buffer, const char *filename)
99 FILE *fp = fopen(filename, "wb");
100 if (!fp) {
101 perror(filename);
102 return -1;
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;
110 fclose(fp);
111 return 0;
114 void buffer_delete(struct buffer *buffer)
116 assert(buffer);
117 if (buffer->name) {
118 free(buffer->name);
119 buffer->name = NULL;
121 if (buffer->data) {
122 free(buffer->data - buffer->offset);
123 buffer->data = NULL;
125 buffer->offset = 0;
126 buffer->size = 0;
129 void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
131 bgets(buf, &file->magic, sizeof(file->magic));
132 file->len = xdr_be.get32(buf);
133 file->type = xdr_be.get32(buf);
134 file->checksum = xdr_be.get32(buf);
135 file->offset = xdr_be.get32(buf);
138 static struct {
139 uint32_t arch;
140 const char *name;
141 } arch_names[] = {
142 { CBFS_ARCHITECTURE_AARCH64, "arm64" },
143 { CBFS_ARCHITECTURE_ARM, "arm" },
144 { CBFS_ARCHITECTURE_MIPS, "mips" },
145 { CBFS_ARCHITECTURE_RISCV, "riscv" },
146 { CBFS_ARCHITECTURE_X86, "x86" },
147 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
150 uint32_t string_to_arch(const char *arch_string)
152 int i;
153 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
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;
162 return ret;
165 const char *arch_to_string(uint32_t a)
167 int i;
168 const char *ret = NULL;
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;
177 return ret;
180 static struct filetypes_t {
181 uint32_t type;
182 const char *name;
183 } filetypes[] = {
184 {CBFS_COMPONENT_STAGE, "stage"},
185 {CBFS_COMPONENT_PAYLOAD, "payload"},
186 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
187 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
188 {CBFS_COMPONENT_RAW, "raw"},
189 {CBFS_COMPONENT_VSA, "vsa"},
190 {CBFS_COMPONENT_MBI, "mbi"},
191 {CBFS_COMPONENT_MICROCODE, "microcode"},
192 {CBFS_COMPONENT_FSP, "fsp"},
193 {CBFS_COMPONENT_MRC, "mrc"},
194 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
195 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
196 {CBFS_COMPONENT_SPD, "spd"},
197 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
198 {CBFS_COMPONENT_DELETED, "deleted"},
199 {CBFS_COMPONENT_NULL, "null"}
202 void print_supported_filetypes(void)
204 int i, number = ARRAY_SIZE(filetypes);
206 for (i=0; i<number; i++) {
207 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
208 if ((i%8) == 7)
209 LOG("\n");
213 uint64_t intfiletype(const char *name)
215 size_t i;
216 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
217 if (strcmp(filetypes[i].name, name) == 0)
218 return filetypes[i].type;
219 return -1;