2009-09-14 15:29:27 +02:00
|
|
|
/*
|
|
|
|
* common utility functions for cbfstool
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 coresystems GmbH
|
|
|
|
* written by Patrick Georgi <patrick.georgi@coresystems.de>
|
2012-11-16 23:48:22 +01:00
|
|
|
* Copyright (C) 2012 Google, Inc.
|
2009-09-14 15:29:27 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-02-10 20:52:35 +01:00
|
|
|
#include <libgen.h>
|
2013-12-30 22:16:18 +01:00
|
|
|
#include "elf.h"
|
2009-09-14 15:29:27 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "cbfs.h"
|
|
|
|
|
2013-01-28 08:53:34 +01:00
|
|
|
/* Utilities */
|
|
|
|
|
|
|
|
/* Small, OS/libc independent runtime check for endianess */
|
|
|
|
int is_big_endian(void)
|
|
|
|
{
|
|
|
|
static const uint32_t inttest = 0x12345678;
|
|
|
|
uint8_t inttest_lsb = *(uint8_t *)&inttest;
|
|
|
|
if (inttest_lsb == 0x12) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:43:46 +01:00
|
|
|
/* Buffer and file I/O */
|
|
|
|
|
|
|
|
int buffer_create(struct buffer *buffer, size_t size, const char *name) {
|
|
|
|
buffer->name = strdup(name);
|
|
|
|
buffer->size = size;
|
|
|
|
buffer->data = (char *)malloc(buffer->size);
|
|
|
|
if (!buffer->data) {
|
|
|
|
fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n",
|
|
|
|
size);
|
|
|
|
}
|
|
|
|
return (buffer->data == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_from_file(struct buffer *buffer, const char *filename) {
|
|
|
|
FILE *fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
|
|
|
perror(filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
buffer->size = ftell(fp);
|
|
|
|
buffer->name = strdup(filename);
|
|
|
|
rewind(fp);
|
|
|
|
buffer->data = (char *)malloc(buffer->size);
|
|
|
|
assert(buffer->data);
|
|
|
|
if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
|
|
|
|
fprintf(stderr, "incomplete read: %s\n", filename);
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_write_file(struct buffer *buffer, const char *filename) {
|
|
|
|
FILE *fp = fopen(filename, "wb");
|
|
|
|
if (!fp) {
|
|
|
|
perror(filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
assert(buffer && buffer->data);
|
|
|
|
if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
|
|
|
|
fprintf(stderr, "incomplete write: %s\n", filename);
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_delete(struct buffer *buffer) {
|
|
|
|
assert(buffer);
|
|
|
|
if (buffer->name) {
|
|
|
|
free(buffer->name);
|
|
|
|
buffer->name = NULL;
|
|
|
|
}
|
|
|
|
if (buffer->data) {
|
|
|
|
free(buffer->data);
|
|
|
|
buffer->data = NULL;
|
|
|
|
}
|
|
|
|
buffer->size = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-16 23:48:22 +01:00
|
|
|
static struct {
|
|
|
|
uint32_t arch;
|
|
|
|
const char *name;
|
|
|
|
} arch_names[] = {
|
|
|
|
{ CBFS_ARCHITECTURE_ARMV7, "armv7" },
|
|
|
|
{ CBFS_ARCHITECTURE_X86, "x86" },
|
|
|
|
{ CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t string_to_arch(const char *arch_string)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
|
|
|
|
if (!strcasecmp(arch_string, arch_names[i].name)) {
|
|
|
|
ret = arch_names[i].arch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-14 15:29:27 +02:00
|
|
|
int iself(unsigned char *input)
|
|
|
|
{
|
|
|
|
Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
|
|
|
|
return !memcmp(ehdr->e_ident, ELFMAG, 4);
|
|
|
|
}
|
|
|
|
|
2012-04-12 21:36:23 +02:00
|
|
|
static struct filetypes_t {
|
2009-09-14 15:29:27 +02:00
|
|
|
uint32_t type;
|
|
|
|
const char *name;
|
|
|
|
} filetypes[] = {
|
|
|
|
{CBFS_COMPONENT_STAGE, "stage"},
|
|
|
|
{CBFS_COMPONENT_PAYLOAD, "payload"},
|
|
|
|
{CBFS_COMPONENT_OPTIONROM, "optionrom"},
|
2010-03-01 09:34:19 +01:00
|
|
|
{CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
|
|
|
|
{CBFS_COMPONENT_RAW, "raw"},
|
|
|
|
{CBFS_COMPONENT_VSA, "vsa"},
|
|
|
|
{CBFS_COMPONENT_MBI, "mbi"},
|
|
|
|
{CBFS_COMPONENT_MICROCODE, "microcode"},
|
2011-01-14 08:40:24 +01:00
|
|
|
{CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
|
2012-04-12 21:36:23 +02:00
|
|
|
{CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
|
2009-09-14 15:29:27 +02:00
|
|
|
{CBFS_COMPONENT_DELETED, "deleted"},
|
|
|
|
{CBFS_COMPONENT_NULL, "null"}
|
|
|
|
};
|
|
|
|
|
2010-04-24 23:24:06 +02:00
|
|
|
void print_supported_filetypes(void)
|
|
|
|
{
|
|
|
|
int i, number = ARRAY_SIZE(filetypes);
|
|
|
|
|
|
|
|
for (i=0; i<number; i++) {
|
2013-01-28 07:39:43 +01:00
|
|
|
LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
|
2010-04-24 23:24:06 +02:00
|
|
|
if ((i%8) == 7)
|
2013-01-28 07:39:43 +01:00
|
|
|
LOG("\n");
|
2010-04-24 23:24:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-14 15:29:27 +02:00
|
|
|
uint64_t intfiletype(const char *name)
|
|
|
|
{
|
2012-07-17 21:17:15 +02:00
|
|
|
size_t i;
|
2009-09-14 15:29:27 +02:00
|
|
|
for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
|
|
|
|
if (strcmp(filetypes[i].name, name) == 0)
|
|
|
|
return filetypes[i].type;
|
|
|
|
return -1;
|
|
|
|
}
|