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
26 #include <sys/types.h>
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) {
47 static off_t
get_file_size(FILE *f
)
51 if (fd
== -1) return -1;
52 if (fstat(fd
, &s
) == -1) return -1;
55 /* Buffer and file I/O */
57 int buffer_create(struct buffer
*buffer
, size_t size
, const char *name
)
59 buffer
->name
= strdup(name
);
62 buffer
->data
= (char *)malloc(buffer
->size
);
64 fprintf(stderr
, "buffer_create: Insufficient memory (0x%zx).\n",
67 return (buffer
->data
== NULL
);
70 int buffer_from_file(struct buffer
*buffer
, const char *filename
)
72 FILE *fp
= fopen(filename
, "rb");
78 buffer
->size
= get_file_size(fp
);
79 if (buffer
->size
== -1u) {
80 fprintf(stderr
, "could not determine size of %s\n", filename
);
84 buffer
->name
= strdup(filename
);
85 buffer
->data
= (char *)malloc(buffer
->size
);
87 if (fread(buffer
->data
, 1, buffer
->size
, fp
) != buffer
->size
) {
88 fprintf(stderr
, "incomplete read: %s\n", filename
);
90 buffer_delete(buffer
);
97 int buffer_write_file(struct buffer
*buffer
, const char *filename
)
99 FILE *fp
= fopen(filename
, "wb");
104 assert(buffer
&& buffer
->data
);
105 if (fwrite(buffer
->data
, 1, buffer
->size
, fp
) != buffer
->size
) {
106 fprintf(stderr
, "incomplete write: %s\n", filename
);
114 void buffer_delete(struct buffer
*buffer
)
122 free(buffer
->data
- buffer
->offset
);
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
);
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
)
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
;
165 const char *arch_to_string(uint32_t a
)
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
;
180 static struct filetypes_t
{
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':',');
213 uint64_t intfiletype(const char *name
)
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
;