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.
22 #include <sys/types.h>
32 /* Small, OS/libc independent runtime check for endianess */
33 int is_big_endian(void)
35 static const uint32_t inttest
= 0x12345678;
36 const uint8_t inttest_lsb
= *(const uint8_t *)&inttest
;
37 if (inttest_lsb
== 0x12) {
43 static off_t
get_file_size(FILE *f
)
46 fseek(f
, 0, SEEK_END
);
48 fseek(f
, 0, SEEK_SET
);
52 /* Buffer and file I/O */
53 int buffer_create(struct buffer
*buffer
, size_t size
, const char *name
)
55 buffer
->name
= strdup(name
);
58 buffer
->data
= (char *)malloc(buffer
->size
);
60 fprintf(stderr
, "buffer_create: Insufficient memory (0x%zx).\n",
63 return (buffer
->data
== NULL
);
66 int buffer_from_file(struct buffer
*buffer
, const char *filename
)
68 FILE *fp
= fopen(filename
, "rb");
74 buffer
->size
= get_file_size(fp
);
75 if (buffer
->size
== -1u) {
76 fprintf(stderr
, "could not determine size of %s\n", filename
);
80 buffer
->name
= strdup(filename
);
81 buffer
->data
= (char *)malloc(buffer
->size
);
83 if (fread(buffer
->data
, 1, buffer
->size
, fp
) != buffer
->size
) {
84 fprintf(stderr
, "incomplete read: %s\n", filename
);
86 buffer_delete(buffer
);
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
)
118 free(buffer_get_original_backing(buffer
));
129 { CBFS_ARCHITECTURE_AARCH64
, "arm64" },
130 { CBFS_ARCHITECTURE_ARM
, "arm" },
131 { CBFS_ARCHITECTURE_MIPS
, "mips" },
132 { CBFS_ARCHITECTURE_PPC64
, "ppc64" },
133 /* power8 is a reasonable alias */
134 { CBFS_ARCHITECTURE_PPC64
, "power8" },
135 { CBFS_ARCHITECTURE_RISCV
, "riscv" },
136 { CBFS_ARCHITECTURE_X86
, "x86" },
137 { CBFS_ARCHITECTURE_UNKNOWN
, "unknown" }
140 uint32_t string_to_arch(const char *arch_string
)
143 uint32_t ret
= CBFS_ARCHITECTURE_UNKNOWN
;
145 for (i
= 0; i
< ARRAY_SIZE(arch_names
); i
++) {
146 if (!strcasecmp(arch_string
, arch_names
[i
].name
)) {
147 ret
= arch_names
[i
].arch
;
155 const char *arch_to_string(uint32_t a
)
158 const char *ret
= NULL
;
160 for (i
= 0; i
< ARRAY_SIZE(arch_names
); i
++) {
161 if (a
== arch_names
[i
].arch
) {
162 ret
= arch_names
[i
].name
;
170 void print_supported_filetypes(void)
172 int i
, number
= ARRAY_SIZE(filetypes
);
174 for (i
=0; i
<number
; i
++) {
175 LOG(" %s%c", filetypes
[i
].name
, (i
==(number
-1))?'\n':',');
181 uint64_t intfiletype(const char *name
)
184 for (i
= 0; i
< (sizeof(filetypes
) / sizeof(struct typedesc_t
)); i
++)
185 if (strcmp(filetypes
[i
].name
, name
) == 0)
186 return filetypes
[i
].type
;
190 char *bintohex(uint8_t *data
, size_t len
)
192 static const char translate
[16] = "0123456789abcdef";
194 char *result
= malloc(len
* 2 + 1);
198 result
[len
*2] = '\0';
200 for (i
= 0; i
< len
; i
++) {
201 result
[i
*2] = translate
[(data
[i
] >> 4) & 0xf];
202 result
[i
*2+1] = translate
[data
[i
] & 0xf];