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., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
25 #include <sys/types.h>
35 /* Small, OS/libc independent runtime check for endianess */
36 int is_big_endian(void)
38 static const uint32_t inttest
= 0x12345678;
39 uint8_t inttest_lsb
= *(uint8_t *)&inttest
;
40 if (inttest_lsb
== 0x12) {
46 static off_t
get_file_size(FILE *f
)
50 if (fd
== -1) return -1;
51 if (fstat(fd
, &s
) == -1) return -1;
54 /* Buffer and file I/O */
56 int buffer_create(struct buffer
*buffer
, size_t size
, const char *name
)
58 buffer
->name
= strdup(name
);
60 buffer
->data
= (char *)malloc(buffer
->size
);
62 fprintf(stderr
, "buffer_create: Insufficient memory (0x%zx).\n",
65 return (buffer
->data
== NULL
);
68 int buffer_from_file(struct buffer
*buffer
, const char *filename
)
70 FILE *fp
= fopen(filename
, "rb");
75 buffer
->size
= get_file_size(fp
);
76 if (buffer
->size
== -1) {
77 fprintf(stderr
, "could not determine size of %s\n", filename
);
81 buffer
->name
= strdup(filename
);
82 buffer
->data
= (char *)malloc(buffer
->size
);
84 if (fread(buffer
->data
, 1, buffer
->size
, fp
) != buffer
->size
) {
85 fprintf(stderr
, "incomplete read: %s\n", filename
);
93 int buffer_write_file(struct buffer
*buffer
, const char *filename
)
95 FILE *fp
= fopen(filename
, "wb");
100 assert(buffer
&& buffer
->data
);
101 if (fwrite(buffer
->data
, 1, buffer
->size
, fp
) != buffer
->size
) {
102 fprintf(stderr
, "incomplete write: %s\n", filename
);
110 void buffer_delete(struct buffer
*buffer
)
124 void cbfs_file_get_header(struct buffer
*buf
, struct cbfs_file
*file
)
126 bgets(buf
, &file
->magic
, sizeof(file
->magic
));
127 file
->len
= xdr_be
.get32(buf
);
128 file
->type
= xdr_be
.get32(buf
);
129 file
->checksum
= xdr_be
.get32(buf
);
130 file
->offset
= xdr_be
.get32(buf
);
137 { CBFS_ARCHITECTURE_AARCH64
, "arm64" },
138 { CBFS_ARCHITECTURE_ARM
, "arm" },
139 { CBFS_ARCHITECTURE_MIPS
, "mips" },
140 { CBFS_ARCHITECTURE_RISCV
, "riscv" },
141 { CBFS_ARCHITECTURE_X86
, "x86" },
142 { CBFS_ARCHITECTURE_UNKNOWN
, "unknown" }
145 uint32_t string_to_arch(const char *arch_string
)
148 uint32_t ret
= CBFS_ARCHITECTURE_UNKNOWN
;
150 for (i
= 0; i
< ARRAY_SIZE(arch_names
); i
++) {
151 if (!strcasecmp(arch_string
, arch_names
[i
].name
)) {
152 ret
= arch_names
[i
].arch
;
160 const char *arch_to_string(uint32_t a
)
163 const char *ret
= NULL
;
165 for (i
= 0; i
< ARRAY_SIZE(arch_names
); i
++) {
166 if (a
== arch_names
[i
].arch
) {
167 ret
= arch_names
[i
].name
;
175 static struct filetypes_t
{
179 {CBFS_COMPONENT_STAGE
, "stage"},
180 {CBFS_COMPONENT_PAYLOAD
, "payload"},
181 {CBFS_COMPONENT_OPTIONROM
, "optionrom"},
182 {CBFS_COMPONENT_BOOTSPLASH
, "bootsplash"},
183 {CBFS_COMPONENT_RAW
, "raw"},
184 {CBFS_COMPONENT_VSA
, "vsa"},
185 {CBFS_COMPONENT_MBI
, "mbi"},
186 {CBFS_COMPONENT_MICROCODE
, "microcode"},
187 {CBFS_COMPONENT_FSP
, "fsp"},
188 {CBFS_COMPONENT_MRC
, "mrc"},
189 {CBFS_COMPONENT_CMOS_DEFAULT
, "cmos default"},
190 {CBFS_COMPONENT_CMOS_LAYOUT
, "cmos layout"},
191 {CBFS_COMPONENT_SPD
, "spd"},
192 {CBFS_COMPONENT_MRC_CACHE
, "mrc_cache"},
193 {CBFS_COMPONENT_DELETED
, "deleted"},
194 {CBFS_COMPONENT_NULL
, "null"}
197 void print_supported_filetypes(void)
199 int i
, number
= ARRAY_SIZE(filetypes
);
201 for (i
=0; i
<number
; i
++) {
202 LOG(" %s%c", filetypes
[i
].name
, (i
==(number
-1))?'\n':',');
208 uint64_t intfiletype(const char *name
)
211 for (i
= 0; i
< (sizeof(filetypes
) / sizeof(struct filetypes_t
)); i
++)
212 if (strcmp(filetypes
[i
].name
, name
) == 0)
213 return filetypes
[i
].type
;