blob: f594e8453b7e7f0646dc3618eda8948bcbec2888 [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 */
31
32/* Small, OS/libc independent runtime check for endianess */
33int is_big_endian(void)
34{
35 static const uint32_t inttest = 0x12345678;
36 uint8_t inttest_lsb = *(uint8_t *)&inttest;
37 if (inttest_lsb == 0x12) {
38 return 1;
39 }
40 return 0;
41}
42
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080043/* Buffer and file I/O */
44
45int buffer_create(struct buffer *buffer, size_t size, const char *name) {
46 buffer->name = strdup(name);
47 buffer->size = size;
48 buffer->data = (char *)malloc(buffer->size);
49 if (!buffer->data) {
50 fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
51 size);
52 }
53 return (buffer->data == NULL);
54}
55
56int buffer_from_file(struct buffer *buffer, const char *filename) {
57 FILE *fp = fopen(filename, "rb");
58 if (!fp) {
59 perror(filename);
60 return -1;
61 }
62 fseek(fp, 0, SEEK_END);
63 buffer->size = ftell(fp);
64 buffer->name = strdup(filename);
65 rewind(fp);
66 buffer->data = (char *)malloc(buffer->size);
67 assert(buffer->data);
68 if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
69 fprintf(stderr, "incomplete read: %s\n", filename);
70 fclose(fp);
71 return -1;
72 }
73 fclose(fp);
74 return 0;
75}
76
77int buffer_write_file(struct buffer *buffer, const char *filename) {
78 FILE *fp = fopen(filename, "wb");
79 if (!fp) {
80 perror(filename);
81 return -1;
82 }
83 assert(buffer && buffer->data);
84 if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
85 fprintf(stderr, "incomplete write: %s\n", filename);
86 fclose(fp);
87 return -1;
88 }
89 fclose(fp);
90 return 0;
91}
92
93void buffer_delete(struct buffer *buffer) {
94 assert(buffer);
95 if (buffer->name) {
96 free(buffer->name);
97 buffer->name = NULL;
98 }
99 if (buffer->data) {
100 free(buffer->data);
101 buffer->data = NULL;
102 }
103 buffer->size = 0;
104}
105
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800106void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
107{
108 bgets(buf, &file->magic, sizeof(file->magic));
109 file->len = xdr_be.get32(buf);
110 file->type = xdr_be.get32(buf);
111 file->checksum = xdr_be.get32(buf);
112 file->offset = xdr_be.get32(buf);
113}
114
David Hendricks90ca3b62012-11-16 14:48:22 -0800115static struct {
116 uint32_t arch;
117 const char *name;
118} arch_names[] = {
119 { CBFS_ARCHITECTURE_ARMV7, "armv7" },
120 { CBFS_ARCHITECTURE_X86, "x86" },
121 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
122};
123
124uint32_t string_to_arch(const char *arch_string)
125{
126 int i;
127 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
128
129 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
130 if (!strcasecmp(arch_string, arch_names[i].name)) {
131 ret = arch_names[i].arch;
132 break;
133 }
134 }
135
136 return ret;
137}
138
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000139int iself(unsigned char *input)
140{
141 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
142 return !memcmp(ehdr->e_ident, ELFMAG, 4);
143}
144
Mathias Krause941158f2012-04-12 21:36:23 +0200145static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000146 uint32_t type;
147 const char *name;
148} filetypes[] = {
149 {CBFS_COMPONENT_STAGE, "stage"},
150 {CBFS_COMPONENT_PAYLOAD, "payload"},
151 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000152 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
153 {CBFS_COMPONENT_RAW, "raw"},
154 {CBFS_COMPONENT_VSA, "vsa"},
155 {CBFS_COMPONENT_MBI, "mbi"},
156 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000157 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200158 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000159 {CBFS_COMPONENT_DELETED, "deleted"},
160 {CBFS_COMPONENT_NULL, "null"}
161};
162
Stefan Reinauer07040582010-04-24 21:24:06 +0000163void print_supported_filetypes(void)
164{
165 int i, number = ARRAY_SIZE(filetypes);
166
167 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800168 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000169 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800170 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000171 }
172}
173
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000174uint64_t intfiletype(const char *name)
175{
Mathias Krause41c229c2012-07-17 21:17:15 +0200176 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000177 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
178 if (strcmp(filetypes[i].name, name) == 0)
179 return filetypes[i].type;
180 return -1;
181}