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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 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 | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 24 | #include <stddef.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 25 | #include <stdint.h> |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 26 | #include <string.h> |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 27 | #include <assert.h> |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 28 | |
| 29 | /* Endianess */ |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 30 | #include "swab.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 31 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 32 | /* Message output */ |
| 33 | extern int verbose; |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 34 | #define ERROR(...) { fprintf(stderr, "E: " __VA_ARGS__); } |
| 35 | #define WARN(...) { fprintf(stderr, "W: " __VA_ARGS__); } |
| 36 | #define LOG(...) { fprintf(stderr, __VA_ARGS__); } |
| 37 | #define INFO(...) { if (verbose > 0) fprintf(stderr, "INFO: " __VA_ARGS__); } |
| 38 | #define DEBUG(...) { if (verbose > 1) fprintf(stderr, "DEBUG: " __VA_ARGS__); } |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 39 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 40 | /* Helpers */ |
| 41 | #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) |
| 42 | #define ALIGN(val, by) (((val) + (by)-1)&~((by)-1)) |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 43 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
| 44 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 45 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 46 | #define IS_TOP_ALIGNED_ADDRESS(x) ((uint32_t)(x) > 0x80000000) |
| 47 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 48 | #define unused __attribute__((unused)) |
| 49 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 50 | static inline uint32_t align_up(uint32_t value, uint32_t align) |
| 51 | { |
| 52 | if (value % align) |
| 53 | value += align - (value % align); |
| 54 | return value; |
| 55 | } |
| 56 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 57 | /* Buffer and file I/O */ |
| 58 | struct buffer { |
| 59 | char *name; |
| 60 | char *data; |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 61 | size_t offset; |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 62 | size_t size; |
| 63 | }; |
| 64 | |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 65 | static inline void *buffer_get(const struct buffer *b) |
| 66 | { |
| 67 | return b->data; |
| 68 | } |
| 69 | |
| 70 | static inline size_t buffer_size(const struct buffer *b) |
| 71 | { |
| 72 | return b->size; |
| 73 | } |
| 74 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Shrink a buffer toward the beginning of its previous space. |
| 77 | * Afterward, buffer_delete() remains the means of cleaning it up. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 78 | static inline void buffer_set_size(struct buffer *b, size_t size) |
| 79 | { |
| 80 | b->size = size; |
| 81 | } |
| 82 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 83 | /* Initialize a buffer with the given constraints. */ |
| 84 | static inline void buffer_init(struct buffer *b, char *name, void *data, |
| 85 | size_t size) |
| 86 | { |
| 87 | b->name = name; |
| 88 | b->data = data; |
| 89 | b->size = size; |
| 90 | } |
| 91 | |
Sol Boucher | 1a3349f | 2015-05-05 18:25:18 -0700 | [diff] [blame] | 92 | /* 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] | 93 | * bounds check the offset and size. The resulting buffer is backed by the same |
| 94 | * storage as the original, so although it is valid to buffer_delete() either |
| 95 | * one of them, doing so releases both simultaneously. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 96 | static inline void buffer_splice(struct buffer *dest, const struct buffer *src, |
| 97 | size_t offset, size_t size) |
| 98 | { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 99 | dest->name = src->name; |
| 100 | dest->data = src->data + offset; |
| 101 | dest->offset = src->offset + offset; |
| 102 | dest->size = size; |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 103 | } |
| 104 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Shallow copy a buffer. To clean up the resources, buffer_delete() |
| 107 | * either one, but not both. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 108 | static inline void buffer_clone(struct buffer *dest, const struct buffer *src) |
| 109 | { |
Sol Boucher | 1a3349f | 2015-05-05 18:25:18 -0700 | [diff] [blame] | 110 | buffer_splice(dest, src, 0, src->size); |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 111 | } |
| 112 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 113 | /* |
| 114 | * Shrink a buffer toward the end of its previous space. |
| 115 | * Afterward, buffer_delete() remains the means of cleaning it up. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 116 | static inline void buffer_seek(struct buffer *b, size_t size) |
| 117 | { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 118 | b->offset += size; |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 119 | b->size -= size; |
| 120 | b->data += size; |
| 121 | } |
| 122 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 123 | /* Returns whether the buffer begins with the specified magic bytes. */ |
| 124 | static inline bool buffer_check_magic(const struct buffer *b, const char *magic, |
| 125 | size_t magic_len) |
| 126 | { |
| 127 | assert(magic); |
| 128 | return b && b->size >= magic_len && |
| 129 | memcmp(b->data, magic, magic_len) == 0; |
| 130 | } |
| 131 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 132 | /* Creates an empty memory buffer with given size. |
| 133 | * Returns 0 on success, otherwise non-zero. */ |
| 134 | int buffer_create(struct buffer *buffer, size_t size, const char *name); |
| 135 | |
| 136 | /* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ |
| 137 | int buffer_from_file(struct buffer *buffer, const char *filename); |
| 138 | |
| 139 | /* Writes memory buffer content into file. |
| 140 | * Returns 0 on success, otherwise non-zero. */ |
| 141 | int buffer_write_file(struct buffer *buffer, const char *filename); |
| 142 | |
| 143 | /* Destroys a memory buffer. */ |
| 144 | void buffer_delete(struct buffer *buffer); |
| 145 | |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 146 | const char *arch_to_string(uint32_t a); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 147 | uint32_t string_to_arch(const char *arch_string); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 148 | |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 149 | /* Compress in_len bytes from in, storing the result at out, returning the |
| 150 | * resulting length in out_len. |
| 151 | * Returns 0 on error, |
| 152 | * != 0 otherwise, depending on the compressing function. |
| 153 | */ |
| 154 | typedef int (*comp_func_ptr) (char *in, int in_len, char *out, int *out_len); |
| 155 | |
| 156 | /* Decompress in_len bytes from in, storing the result at out, up to out_len |
| 157 | * bytes. |
| 158 | * Returns 0 on error, |
| 159 | * != 0 otherwise, depending on the decompressing function. |
| 160 | */ |
| 161 | typedef int (*decomp_func_ptr) (char *in, int in_len, char *out, int out_len); |
| 162 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 163 | enum comp_algo { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 }; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 164 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 165 | comp_func_ptr compression_function(enum comp_algo algo); |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 166 | decomp_func_ptr decompression_function(enum comp_algo algo); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 167 | |
| 168 | uint64_t intfiletype(const char *name); |
| 169 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 170 | /* cbfs-mkpayload.c */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 171 | int parse_elf_to_payload(const struct buffer *input, struct buffer *output, |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 172 | enum comp_algo algo); |
| 173 | int parse_fv_to_payload(const struct buffer *input, struct buffer *output, |
| 174 | enum comp_algo algo); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 175 | int parse_bzImage_to_payload(const struct buffer *input, |
| 176 | struct buffer *output, const char *initrd, |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 177 | char *cmdline, enum comp_algo algo); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 178 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 179 | struct buffer *output, |
| 180 | uint32_t loadaddress, |
| 181 | uint32_t entrypoint, |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 182 | enum comp_algo algo); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 183 | /* cbfs-mkstage.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 184 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 185 | enum comp_algo algo, uint32_t *location, |
Furquan Shaikh | 405304a | 2014-10-30 11:44:20 -0700 | [diff] [blame] | 186 | const char *ignore_section); |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 187 | /* location is TOP aligned. */ |
| 188 | int parse_elf_to_xip_stage(const struct buffer *input, struct buffer *output, |
| 189 | uint32_t *location, const char *ignore_section); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 190 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 191 | void print_supported_filetypes(void); |
| 192 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 193 | /* lzma/lzma.c */ |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 194 | int do_lzma_compress(char *in, int in_len, char *out, int *out_len); |
| 195 | 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] | 196 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 197 | /* xdr.c */ |
| 198 | struct xdr { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 199 | uint8_t (*get8)(struct buffer *input); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 200 | uint16_t (*get16)(struct buffer *input); |
| 201 | uint32_t (*get32)(struct buffer *input); |
| 202 | uint64_t (*get64)(struct buffer *input); |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 203 | void (*put8)(struct buffer *input, uint8_t val); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 204 | void (*put16)(struct buffer *input, uint16_t val); |
| 205 | void (*put32)(struct buffer *input, uint32_t val); |
| 206 | void (*put64)(struct buffer *input, uint64_t val); |
| 207 | }; |
| 208 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 209 | extern struct xdr xdr_le, xdr_be; |
Aaron Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame] | 210 | size_t bgets(struct buffer *input, void *output, size_t len); |
| 211 | size_t bputs(struct buffer *b, const void *data, size_t len); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 212 | |
| 213 | #endif |