Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * common utility functions for cbfstool |
| 3 | * |
| 4 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 6 | * Copyright (C) 2012 Google, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 7 | * |
| 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 Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 25 | #include <libgen.h> |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 26 | #include "elf.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 27 | #include "common.h" |
| 28 | #include "cbfs.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 29 | |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 30 | /* Utilities */ |
| 31 | |
| 32 | /* Small, OS/libc independent runtime check for endianess */ |
| 33 | int 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 Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 43 | /* Buffer and file I/O */ |
| 44 | |
| 45 | int 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 | |
| 56 | int 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 | |
| 77 | int 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 | |
| 93 | void 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. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 106 | void 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 Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 115 | static 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 | |
| 124 | uint32_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 Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 139 | int iself(unsigned char *input) |
| 140 | { |
| 141 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; |
| 142 | return !memcmp(ehdr->e_ident, ELFMAG, 4); |
| 143 | } |
| 144 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 145 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 146 | 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 Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 152 | {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 Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 157 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 158 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 159 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 160 | {CBFS_COMPONENT_NULL, "null"} |
| 161 | }; |
| 162 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 163 | void print_supported_filetypes(void) |
| 164 | { |
| 165 | int i, number = ARRAY_SIZE(filetypes); |
| 166 | |
| 167 | for (i=0; i<number; i++) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 168 | LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 169 | if ((i%8) == 7) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 170 | LOG("\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 174 | uint64_t intfiletype(const char *name) |
| 175 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 176 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 177 | 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 | } |