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> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "cbfs.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 28 | |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 29 | /* Utilities */ |
Aaron Durbin | fae7517 | 2014-03-05 15:02:21 -0600 | [diff] [blame] | 30 | int verbose = 0; |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 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 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 139 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 140 | uint32_t type; |
| 141 | const char *name; |
| 142 | } filetypes[] = { |
| 143 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 144 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 145 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 146 | {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"}, |
| 147 | {CBFS_COMPONENT_RAW, "raw"}, |
| 148 | {CBFS_COMPONENT_VSA, "vsa"}, |
| 149 | {CBFS_COMPONENT_MBI, "mbi"}, |
| 150 | {CBFS_COMPONENT_MICROCODE, "microcode"}, |
Patrick Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 151 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 152 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 153 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 154 | {CBFS_COMPONENT_NULL, "null"} |
| 155 | }; |
| 156 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 157 | void print_supported_filetypes(void) |
| 158 | { |
| 159 | int i, number = ARRAY_SIZE(filetypes); |
| 160 | |
| 161 | for (i=0; i<number; i++) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 162 | LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 163 | if ((i%8) == 7) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 164 | LOG("\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 168 | uint64_t intfiletype(const char *name) |
| 169 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 170 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 171 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 172 | if (strcmp(filetypes[i].name, name) == 0) |
| 173 | return filetypes[i].type; |
| 174 | return -1; |
| 175 | } |