google/eve: Configure I2C3 pins as GPIO inputs
[coreboot.git] / util / cbfstool / common.c
blob3093ba1465ce3c3dcd2d7c61524f57c6092594dd
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.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <strings.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <libgen.h>
26 #include "common.h"
27 #include "cbfs.h"
29 /* Utilities */
30 int verbose = 0;
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) {
38 return 1;
40 return 0;
43 static off_t get_file_size(FILE *f)
45 off_t fsize;
46 fseek(f, 0, SEEK_END);
47 fsize = ftell(f);
48 fseek(f, 0, SEEK_SET);
49 return fsize;
52 /* Buffer and file I/O */
53 int buffer_create(struct buffer *buffer, size_t size, const char *name)
55 buffer->name = strdup(name);
56 buffer->offset = 0;
57 buffer->size = size;
58 buffer->data = (char *)malloc(buffer->size);
59 if (!buffer->data) {
60 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
61 size);
63 return (buffer->data == NULL);
66 int buffer_from_file(struct buffer *buffer, const char *filename)
68 FILE *fp = fopen(filename, "rb");
69 if (!fp) {
70 perror(filename);
71 return -1;
73 buffer->offset = 0;
74 buffer->size = get_file_size(fp);
75 if (buffer->size == -1u) {
76 fprintf(stderr, "could not determine size of %s\n", filename);
77 fclose(fp);
78 return -1;
80 buffer->name = strdup(filename);
81 buffer->data = (char *)malloc(buffer->size);
82 assert(buffer->data);
83 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
84 fprintf(stderr, "incomplete read: %s\n", filename);
85 fclose(fp);
86 buffer_delete(buffer);
87 return -1;
89 fclose(fp);
90 return 0;
93 int buffer_write_file(struct buffer *buffer, const char *filename)
95 FILE *fp = fopen(filename, "wb");
96 if (!fp) {
97 perror(filename);
98 return -1;
100 assert(buffer && buffer->data);
101 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
102 fprintf(stderr, "incomplete write: %s\n", filename);
103 fclose(fp);
104 return -1;
106 fclose(fp);
107 return 0;
110 void buffer_delete(struct buffer *buffer)
112 assert(buffer);
113 if (buffer->name) {
114 free(buffer->name);
115 buffer->name = NULL;
117 if (buffer->data) {
118 free(buffer_get_original_backing(buffer));
119 buffer->data = NULL;
121 buffer->offset = 0;
122 buffer->size = 0;
125 static struct {
126 uint32_t arch;
127 const char *name;
128 } arch_names[] = {
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)
142 size_t i;
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;
148 break;
152 return ret;
155 const char *arch_to_string(uint32_t a)
157 size_t i;
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;
163 break;
167 return ret;
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':',');
176 if ((i%8) == 7)
177 LOG("\n");
181 uint64_t intfiletype(const char *name)
183 size_t i;
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;
187 return -1;
190 char *bintohex(uint8_t *data, size_t len)
192 static const char translate[16] = "0123456789abcdef";
194 char *result = malloc(len * 2 + 1);
195 if (result == NULL)
196 return NULL;
198 result[len*2] = '\0';
199 unsigned int i;
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];
204 return result;