Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 coresystems GmbH |
| 3 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 4 | * Copyright (C) 2012 Google, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 20 | #ifndef __CBFSTOOL_COMMON_H |
| 21 | #define __CBFSTOOL_COMMON_H |
| 22 | |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 23 | #include <stddef.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 24 | #include <stdint.h> |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 25 | #include <assert.h> |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 26 | |
| 27 | /* Endianess */ |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 28 | #include "swab.h" |
Stefan Reinauer | 9da7570 | 2013-01-04 13:51:36 -0800 | [diff] [blame] | 29 | #ifndef __APPLE__ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 30 | #define ntohl(x) (is_big_endian() ? (uint32_t)(x) : swab32(x)) |
| 31 | #define htonl(x) (is_big_endian() ? (uint32_t)(x) : swab32(x)) |
Stefan Reinauer | 9da7570 | 2013-01-04 13:51:36 -0800 | [diff] [blame] | 32 | #endif |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 33 | #define ntohll(x) (is_big_endian() ? (uint64_t)(x) : swab64(x)) |
| 34 | #define htonll(x) (is_big_endian() ? (uint64_t)(x) : swab64(x)) |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 35 | int is_big_endian(void); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 36 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 37 | /* Message output */ |
| 38 | extern int verbose; |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 39 | #define ERROR(...) { fprintf(stderr, "E: " __VA_ARGS__); } |
| 40 | #define WARN(...) { fprintf(stderr, "W: " __VA_ARGS__); } |
| 41 | #define LOG(...) { fprintf(stderr, __VA_ARGS__); } |
| 42 | #define INFO(...) { if (verbose > 0) fprintf(stderr, "INFO: " __VA_ARGS__); } |
| 43 | #define DEBUG(...) { if (verbose > 1) fprintf(stderr, "DEBUG: " __VA_ARGS__); } |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 44 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 45 | /* Helpers */ |
| 46 | #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) |
| 47 | #define ALIGN(val, by) (((val) + (by)-1)&~((by)-1)) |
| 48 | |
| 49 | #define unused __attribute__((unused)) |
| 50 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 51 | static inline uint32_t align_up(uint32_t value, uint32_t align) |
| 52 | { |
| 53 | if (value % align) |
| 54 | value += align - (value % align); |
| 55 | return value; |
| 56 | } |
| 57 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 58 | /* Buffer and file I/O */ |
| 59 | struct buffer { |
| 60 | char *name; |
| 61 | char *data; |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 62 | size_t offset; |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 63 | size_t size; |
| 64 | }; |
| 65 | |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 66 | static inline void *buffer_get(const struct buffer *b) |
| 67 | { |
| 68 | return b->data; |
| 69 | } |
| 70 | |
| 71 | static inline size_t buffer_size(const struct buffer *b) |
| 72 | { |
| 73 | return b->size; |
| 74 | } |
| 75 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 76 | /* |
| 77 | * Shrink a buffer toward the beginning of its previous space. |
| 78 | * Afterward, buffer_delete() remains the means of cleaning it up. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 79 | static inline void buffer_set_size(struct buffer *b, size_t size) |
| 80 | { |
| 81 | b->size = size; |
| 82 | } |
| 83 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 84 | /* Initialize a buffer with the given constraints. */ |
| 85 | static inline void buffer_init(struct buffer *b, char *name, void *data, |
| 86 | size_t size) |
| 87 | { |
| 88 | b->name = name; |
| 89 | b->data = data; |
| 90 | b->size = size; |
| 91 | } |
| 92 | |
Sol Boucher | 1a3349f | 2015-05-05 18:25:18 -0700 | [diff] [blame] | 93 | /* Splice a buffer into another buffer. Note that it's up to the caller to |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 94 | * bounds check the offset and size. The resulting buffer is backed by the same |
| 95 | * storage as the original, so although it is valid to buffer_delete() either |
| 96 | * one of them, doing so releases both simultaneously. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 97 | static inline void buffer_splice(struct buffer *dest, const struct buffer *src, |
| 98 | size_t offset, size_t size) |
| 99 | { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 100 | dest->name = src->name; |
| 101 | dest->data = src->data + offset; |
| 102 | dest->offset = src->offset + offset; |
| 103 | dest->size = size; |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 104 | } |
| 105 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 106 | /* |
| 107 | * Shallow copy a buffer. To clean up the resources, buffer_delete() |
| 108 | * either one, but not both. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 109 | static inline void buffer_clone(struct buffer *dest, const struct buffer *src) |
| 110 | { |
Sol Boucher | 1a3349f | 2015-05-05 18:25:18 -0700 | [diff] [blame] | 111 | buffer_splice(dest, src, 0, src->size); |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 112 | } |
| 113 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 114 | /* |
| 115 | * Shrink a buffer toward the end of its previous space. |
| 116 | * Afterward, buffer_delete() remains the means of cleaning it up. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 117 | static inline void buffer_seek(struct buffer *b, size_t size) |
| 118 | { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame^] | 119 | b->offset += size; |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 120 | b->size -= size; |
| 121 | b->data += size; |
| 122 | } |
| 123 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 124 | /* Creates an empty memory buffer with given size. |
| 125 | * Returns 0 on success, otherwise non-zero. */ |
| 126 | int buffer_create(struct buffer *buffer, size_t size, const char *name); |
| 127 | |
| 128 | /* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ |
| 129 | int buffer_from_file(struct buffer *buffer, const char *filename); |
| 130 | |
| 131 | /* Writes memory buffer content into file. |
| 132 | * Returns 0 on success, otherwise non-zero. */ |
| 133 | int buffer_write_file(struct buffer *buffer, const char *filename); |
| 134 | |
| 135 | /* Destroys a memory buffer. */ |
| 136 | void buffer_delete(struct buffer *buffer); |
| 137 | |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 138 | const char *arch_to_string(uint32_t a); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 139 | uint32_t string_to_arch(const char *arch_string); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 140 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 141 | typedef int (*comp_func_ptr) (char *, int, char *, int *); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 142 | typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo; |
| 143 | |
| 144 | comp_func_ptr compression_function(comp_algo algo); |
| 145 | |
| 146 | uint64_t intfiletype(const char *name); |
| 147 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 148 | /* cbfs-mkpayload.c */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 149 | int parse_elf_to_payload(const struct buffer *input, struct buffer *output, |
| 150 | comp_algo algo); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 151 | int parse_fv_to_payload(const struct buffer *input, |
| 152 | struct buffer *output, comp_algo algo); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 153 | int parse_bzImage_to_payload(const struct buffer *input, |
| 154 | struct buffer *output, const char *initrd, |
| 155 | char *cmdline, comp_algo algo); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 156 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 157 | struct buffer *output, |
| 158 | uint32_t loadaddress, |
| 159 | uint32_t entrypoint, |
| 160 | comp_algo algo); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 161 | /* cbfs-mkstage.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 162 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 163 | comp_algo algo, uint32_t *location, |
Furquan Shaikh | 405304a | 2014-10-30 11:44:20 -0700 | [diff] [blame] | 164 | const char *ignore_section); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 165 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 166 | void print_supported_filetypes(void); |
| 167 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 168 | /* lzma/lzma.c */ |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 169 | int do_lzma_compress(char *in, int in_len, char *out, int *out_len); |
| 170 | int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len); |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 171 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 172 | /* xdr.c */ |
| 173 | struct xdr { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 174 | uint8_t (*get8)(struct buffer *input); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 175 | uint16_t (*get16)(struct buffer *input); |
| 176 | uint32_t (*get32)(struct buffer *input); |
| 177 | uint64_t (*get64)(struct buffer *input); |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 178 | void (*put8)(struct buffer *input, uint8_t val); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 179 | void (*put16)(struct buffer *input, uint16_t val); |
| 180 | void (*put32)(struct buffer *input, uint32_t val); |
| 181 | void (*put64)(struct buffer *input, uint64_t val); |
| 182 | }; |
| 183 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 184 | extern struct xdr xdr_le, xdr_be; |
Aaron Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame] | 185 | size_t bgets(struct buffer *input, void *output, size_t len); |
| 186 | size_t bputs(struct buffer *b, const void *data, size_t len); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 187 | |
| 188 | #endif |