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