blob: 2fe0f01daf2f699932c348cd3362ae5fe2c81f55 [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>
Uwe Hermann942a40d2010-02-10 19:52:35 +000025#include <libgen.h>
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080026#include "elf.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000027#include "common.h"
28#include "cbfs.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000029
Hung-Te Lin332795c2013-01-28 15:53:34 +080030/* Utilities */
Aaron Durbinfae75172014-03-05 15:02:21 -060031int verbose = 0;
Hung-Te Lin332795c2013-01-28 15:53:34 +080032
33/* Small, OS/libc independent runtime check for endianess */
34int is_big_endian(void)
35{
36 static const uint32_t inttest = 0x12345678;
37 uint8_t inttest_lsb = *(uint8_t *)&inttest;
38 if (inttest_lsb == 0x12) {
39 return 1;
40 }
41 return 0;
42}
43
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080044/* Buffer and file I/O */
45
46int buffer_create(struct buffer *buffer, size_t size, const char *name) {
47 buffer->name = strdup(name);
48 buffer->size = size;
49 buffer->data = (char *)malloc(buffer->size);
50 if (!buffer->data) {
51 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
52 size);
53 }
54 return (buffer->data == NULL);
55}
56
57int buffer_from_file(struct buffer *buffer, const char *filename) {
58 FILE *fp = fopen(filename, "rb");
59 if (!fp) {
60 perror(filename);
61 return -1;
62 }
63 fseek(fp, 0, SEEK_END);
64 buffer->size = ftell(fp);
65 buffer->name = strdup(filename);
66 rewind(fp);
67 buffer->data = (char *)malloc(buffer->size);
68 assert(buffer->data);
69 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
70 fprintf(stderr, "incomplete read: %s\n", filename);
71 fclose(fp);
72 return -1;
73 }
74 fclose(fp);
75 return 0;
76}
77
78int buffer_write_file(struct buffer *buffer, const char *filename) {
79 FILE *fp = fopen(filename, "wb");
80 if (!fp) {
81 perror(filename);
82 return -1;
83 }
84 assert(buffer && buffer->data);
85 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
86 fprintf(stderr, "incomplete write: %s\n", filename);
87 fclose(fp);
88 return -1;
89 }
90 fclose(fp);
91 return 0;
92}
93
94void buffer_delete(struct buffer *buffer) {
95 assert(buffer);
96 if (buffer->name) {
97 free(buffer->name);
98 buffer->name = NULL;
99 }
100 if (buffer->data) {
101 free(buffer->data);
102 buffer->data = NULL;
103 }
104 buffer->size = 0;
105}
106
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800107void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
108{
109 bgets(buf, &file->magic, sizeof(file->magic));
110 file->len = xdr_be.get32(buf);
111 file->type = xdr_be.get32(buf);
112 file->checksum = xdr_be.get32(buf);
113 file->offset = xdr_be.get32(buf);
114}
115
David Hendricks90ca3b62012-11-16 14:48:22 -0800116static struct {
117 uint32_t arch;
118 const char *name;
119} arch_names[] = {
120 { CBFS_ARCHITECTURE_ARMV7, "armv7" },
121 { CBFS_ARCHITECTURE_X86, "x86" },
122 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
123};
124
125uint32_t string_to_arch(const char *arch_string)
126{
127 int i;
128 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
129
130 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
131 if (!strcasecmp(arch_string, arch_names[i].name)) {
132 ret = arch_names[i].arch;
133 break;
134 }
135 }
136
137 return ret;
138}
139
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000140int iself(unsigned char *input)
141{
142 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
143 return !memcmp(ehdr->e_ident, ELFMAG, 4);
144}
145
Mathias Krause941158f2012-04-12 21:36:23 +0200146static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000147 uint32_t type;
148 const char *name;
149} filetypes[] = {
150 {CBFS_COMPONENT_STAGE, "stage"},
151 {CBFS_COMPONENT_PAYLOAD, "payload"},
152 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000153 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
154 {CBFS_COMPONENT_RAW, "raw"},
155 {CBFS_COMPONENT_VSA, "vsa"},
156 {CBFS_COMPONENT_MBI, "mbi"},
157 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000158 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200159 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000160 {CBFS_COMPONENT_DELETED, "deleted"},
161 {CBFS_COMPONENT_NULL, "null"}
162};
163
Stefan Reinauer07040582010-04-24 21:24:06 +0000164void print_supported_filetypes(void)
165{
166 int i, number = ARRAY_SIZE(filetypes);
167
168 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800169 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000170 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800171 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000172 }
173}
174
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000175uint64_t intfiletype(const char *name)
176{
Mathias Krause41c229c2012-07-17 21:17:15 +0200177 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000178 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
179 if (strcmp(filetypes[i].name, name) == 0)
180 return filetypes[i].type;
181 return -1;
182}