blob: 98d9517bf43b0288eee6424d1c6ccb40edbf98f8 [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
David Hendricks90ca3b62012-11-16 14:48:22 -0800106static struct {
107 uint32_t arch;
108 const char *name;
109} arch_names[] = {
110 { CBFS_ARCHITECTURE_ARMV7, "armv7" },
111 { CBFS_ARCHITECTURE_X86, "x86" },
112 { CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
113};
114
115uint32_t string_to_arch(const char *arch_string)
116{
117 int i;
118 uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
119
120 for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
121 if (!strcasecmp(arch_string, arch_names[i].name)) {
122 ret = arch_names[i].arch;
123 break;
124 }
125 }
126
127 return ret;
128}
129
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000130int iself(unsigned char *input)
131{
132 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
133 return !memcmp(ehdr->e_ident, ELFMAG, 4);
134}
135
Mathias Krause941158f2012-04-12 21:36:23 +0200136static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000137 uint32_t type;
138 const char *name;
139} filetypes[] = {
140 {CBFS_COMPONENT_STAGE, "stage"},
141 {CBFS_COMPONENT_PAYLOAD, "payload"},
142 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000143 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
144 {CBFS_COMPONENT_RAW, "raw"},
145 {CBFS_COMPONENT_VSA, "vsa"},
146 {CBFS_COMPONENT_MBI, "mbi"},
147 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000148 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200149 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000150 {CBFS_COMPONENT_DELETED, "deleted"},
151 {CBFS_COMPONENT_NULL, "null"}
152};
153
Stefan Reinauer07040582010-04-24 21:24:06 +0000154void print_supported_filetypes(void)
155{
156 int i, number = ARRAY_SIZE(filetypes);
157
158 for (i=0; i<number; i++) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800159 LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
Stefan Reinauer07040582010-04-24 21:24:06 +0000160 if ((i%8) == 7)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800161 LOG("\n");
Stefan Reinauer07040582010-04-24 21:24:06 +0000162 }
163}
164
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000165uint64_t intfiletype(const char *name)
166{
Mathias Krause41c229c2012-07-17 21:17:15 +0200167 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000168 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
169 if (strcmp(filetypes[i].name, name) == 0)
170 return filetypes[i].type;
171 return -1;
172}