blob: a28e74179de00bf21103696879de4dbf7020e90b [file] [log] [blame]
Patrick Georgib7b56dd82009-09-14 13:29:27 +00001/*
2 * common utility functions for cbfstool
3 *
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
David Hendricks90ca3b62012-11-16 14:48:22 -08006 * Copyright (C) 2012 Google, Inc.
Patrick Georgib7b56dd82009-09-14 13:29:27 +00007 *
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.
11 *
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.
16 *
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
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Patrick Georgi32eeff42014-08-11 09:27:18 +020025#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
Uwe Hermann942a40d2010-02-10 19:52:35 +000028#include <libgen.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000029#include "common.h"
30#include "cbfs.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000031
Hung-Te Lin332795c2013-01-28 15:53:34 +080032/* Utilities */
Aaron Durbinfae75172014-03-05 15:02:21 -060033int verbose = 0;
Hung-Te Lin332795c2013-01-28 15:53:34 +080034
35/* Small, OS/libc independent runtime check for endianess */
36int is_big_endian(void)
37{
38 static const uint32_t inttest = 0x12345678;
39 uint8_t inttest_lsb = *(uint8_t *)&inttest;
40 if (inttest_lsb == 0x12) {
41 return 1;
42 }
43 return 0;
44}
45
Patrick Georgi32eeff42014-08-11 09:27:18 +020046static off_t get_file_size(FILE *f)
47{
48 struct stat s;
49 int fd = fileno(f);
50 if (fd == -1) return -1;
51 if (fstat(fd, &s) == -1) return -1;
52 return s.st_size;
53}
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080054/* Buffer and file I/O */
55
56int buffer_create(struct buffer *buffer, size_t size, const char *name) {
57 buffer->name = strdup(name);
58 buffer->size = size;
59 buffer->data = (char *)malloc(buffer->size);
60 if (!buffer->data) {
61 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
62 size);
63 }
64 return (buffer->data == NULL);
65}
66
67int buffer_from_file(struct buffer *buffer, const char *filename) {
68 FILE *fp = fopen(filename, "rb");
69 if (!fp) {
70 perror(filename);
71 return -1;
72 }
Patrick Georgi32eeff42014-08-11 09:27:18 +020073 buffer->size = get_file_size(fp);
74 if (buffer->size == -1) {
75 fprintf(stderr, "could not determine size of %s\n", filename);
76 fclose(fp);
77 return -1;
78 }
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080079 buffer->name = strdup(filename);
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080080 buffer->data = (char *)malloc(buffer->size);
81 assert(buffer->data);
82 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
83 fprintf(stderr, "incomplete read: %s\n", filename);
84 fclose(fp);
85 return -1;
86 }
87 fclose(fp);
88 return 0;
89}
90
91int buffer_write_file(struct buffer *buffer, const char *filename) {
92 FILE *fp = fopen(filename, "wb");
93 if (!fp) {
94 perror(filename);
95 return -1;
96 }
97 assert(buffer && buffer->data);
98 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
99 fprintf(stderr, "incomplete write: %s\n", filename);
100 fclose(fp);
101 return -1;
102 }
103 fclose(fp);
104 return 0;
105}
106
107void buffer_delete(struct buffer *buffer) {
108 assert(buffer);
109 if (buffer->name) {
110 free(buffer->name);
111 buffer->name = NULL;
112 }
113 if (buffer->data) {
114 free(buffer->data);
115 buffer->data = NULL;
116 }
117 buffer->size = 0;
118}
119
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800120void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
121{
122 bgets(buf, &file->magic, sizeof(file->magic));
123 file->len = xdr_be.get32(buf);
124 file->type = xdr_be.get32(buf);
125 file->checksum = xdr_be.get32(buf);
126 file->offset = xdr_be.get32(buf);
127}
128
David Hendricks90ca3b62012-11-16 14:48:22 -0800129static struct {
130 uint32_t arch;
131 const char *name;
132} arch_names[] = {
Gabe Black51edd542013-09-30 23:00:33 -0700133 { CBFS_ARCHITECTURE_ARM, "arm" },
David Hendricks90ca3b62012-11-16 14:48:22 -0800134 { CBFS_ARCHITECTURE_X86, "x86" },
135 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
136};
137
138uint32_t string_to_arch(const char *arch_string)
139{
140 int i;
141 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
142
143 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
144 if (!strcasecmp(arch_string, arch_names[i].name)) {
145 ret = arch_names[i].arch;
146 break;
147 }
148 }
149
150 return ret;
151}
152
Mathias Krause941158f2012-04-12 21:36:23 +0200153static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000154 uint32_t type;
155 const char *name;
156} filetypes[] = {
157 {CBFS_COMPONENT_STAGE, "stage"},
158 {CBFS_COMPONENT_PAYLOAD, "payload"},
159 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000160 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
161 {CBFS_COMPONENT_RAW, "raw"},
162 {CBFS_COMPONENT_VSA, "vsa"},
163 {CBFS_COMPONENT_MBI, "mbi"},
164 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000165 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200166 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000167 {CBFS_COMPONENT_DELETED, "deleted"},
168 {CBFS_COMPONENT_NULL, "null"}
169};
170
Stefan Reinauer07040582010-04-24 21:24:06 +0000171void print_supported_filetypes(void)
172{
173 int i, number = ARRAY_SIZE(filetypes);
174
175 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800176 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000177 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800178 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000179 }
180}
181
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000182uint64_t intfiletype(const char *name)
183{
Mathias Krause41c229c2012-07-17 21:17:15 +0200184 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000185 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
186 if (strcmp(filetypes[i].name, name) == 0)
187 return filetypes[i].type;
188 return -1;
189}