Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 2 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 3 | #ifndef __CBFSTOOL_COMMON_H |
| 4 | #define __CBFSTOOL_COMMON_H |
| 5 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 6 | #include <stdbool.h> |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 7 | #include <stddef.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 8 | #include <stdint.h> |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 9 | #include <string.h> |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 10 | #include <assert.h> |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 11 | |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 12 | #include <commonlib/bsd/cbfs_serialized.h> |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 13 | #include <commonlib/bsd/sysincludes.h> |
Furquan Shaikh | 161d233 | 2016-05-26 14:41:02 -0700 | [diff] [blame] | 14 | #include <commonlib/helpers.h> |
Aaron Durbin | d38e3de | 2015-10-01 14:25:19 -0500 | [diff] [blame] | 15 | #include <console/console.h> |
| 16 | |
Furquan Shaikh | 19ba95f | 2020-11-20 22:50:26 -0800 | [diff] [blame] | 17 | /* |
| 18 | * There are two address spaces that this tool deals with - SPI flash address space and host |
| 19 | * address space. This macros checks if the address is greater than 2GiB under the assumption |
| 20 | * that the low MMIO lives in the top half of the 4G address space of the host. |
| 21 | */ |
| 22 | #define IS_HOST_SPACE_ADDRESS(addr) ((uint32_t)(addr) > 0x80000000) |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 23 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 24 | #define unused __attribute__((unused)) |
| 25 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 26 | static inline uint32_t align_up(uint32_t value, uint32_t align) |
| 27 | { |
| 28 | if (value % align) |
| 29 | value += align - (value % align); |
| 30 | return value; |
| 31 | } |
| 32 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 33 | /* Buffer and file I/O */ |
| 34 | struct buffer { |
| 35 | char *name; |
| 36 | char *data; |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 37 | size_t offset; |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 38 | size_t size; |
| 39 | }; |
| 40 | |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 41 | static inline void *buffer_get(const struct buffer *b) |
| 42 | { |
| 43 | return b->data; |
| 44 | } |
| 45 | |
| 46 | static inline size_t buffer_size(const struct buffer *b) |
| 47 | { |
| 48 | return b->size; |
| 49 | } |
| 50 | |
Aaron Durbin | 2dd5f4c | 2016-01-26 09:01:14 -0600 | [diff] [blame] | 51 | static inline size_t buffer_offset(const struct buffer *b) |
| 52 | { |
| 53 | return b->offset; |
| 54 | } |
| 55 | |
Ricardo Quesada | ff236ef | 2021-08-16 14:36:05 -0700 | [diff] [blame] | 56 | static inline void *buffer_end(const struct buffer *b) |
| 57 | { |
| 58 | return b->data + b->size; |
| 59 | } |
| 60 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Shrink a buffer toward the beginning of its previous space. |
| 63 | * Afterward, buffer_delete() remains the means of cleaning it up. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 64 | static inline void buffer_set_size(struct buffer *b, size_t size) |
| 65 | { |
| 66 | b->size = size; |
| 67 | } |
| 68 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 69 | /* Initialize a buffer with the given constraints. */ |
| 70 | static inline void buffer_init(struct buffer *b, char *name, void *data, |
| 71 | size_t size) |
| 72 | { |
| 73 | b->name = name; |
| 74 | b->data = data; |
| 75 | b->size = size; |
Aaron Durbin | cd9ba8a | 2015-10-23 17:36:57 -0500 | [diff] [blame] | 76 | b->offset = 0; |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Sol Boucher | 1a3349f | 2015-05-05 18:25:18 -0700 | [diff] [blame] | 79 | /* 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] | 80 | * bounds check the offset and size. The resulting buffer is backed by the same |
| 81 | * storage as the original, so although it is valid to buffer_delete() either |
| 82 | * one of them, doing so releases both simultaneously. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 83 | static inline void buffer_splice(struct buffer *dest, const struct buffer *src, |
| 84 | size_t offset, size_t size) |
| 85 | { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 86 | dest->name = src->name; |
| 87 | dest->data = src->data + offset; |
| 88 | dest->offset = src->offset + offset; |
| 89 | dest->size = size; |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 90 | } |
| 91 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 92 | /* |
| 93 | * Shallow copy a buffer. To clean up the resources, buffer_delete() |
| 94 | * either one, but not both. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 95 | static inline void buffer_clone(struct buffer *dest, const struct buffer *src) |
| 96 | { |
Sol Boucher | 1a3349f | 2015-05-05 18:25:18 -0700 | [diff] [blame] | 97 | buffer_splice(dest, src, 0, src->size); |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Shrink a buffer toward the end of its previous space. |
| 102 | * Afterward, buffer_delete() remains the means of cleaning it up. */ |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 103 | static inline void buffer_seek(struct buffer *b, size_t size) |
| 104 | { |
Sol Boucher | 64c6cd7 | 2015-04-26 02:32:43 -0700 | [diff] [blame] | 105 | b->offset += size; |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 106 | b->size -= size; |
| 107 | b->data += size; |
| 108 | } |
| 109 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 110 | /* Returns whether the buffer begins with the specified magic bytes. */ |
| 111 | static inline bool buffer_check_magic(const struct buffer *b, const char *magic, |
| 112 | size_t magic_len) |
| 113 | { |
| 114 | assert(magic); |
| 115 | return b && b->size >= magic_len && |
| 116 | memcmp(b->data, magic, magic_len) == 0; |
| 117 | } |
| 118 | |
Patrick Georgi | 9b24f7c | 2015-11-11 18:43:11 +0100 | [diff] [blame] | 119 | /* Returns the start of the underlying buffer, with the offset undone */ |
| 120 | static inline void *buffer_get_original_backing(const struct buffer *b) |
| 121 | { |
| 122 | if (!b) |
| 123 | return NULL; |
Aaron Durbin | 2dd5f4c | 2016-01-26 09:01:14 -0600 | [diff] [blame] | 124 | return buffer_get(b) - buffer_offset(b); |
Patrick Georgi | 9b24f7c | 2015-11-11 18:43:11 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 127 | /* Creates an empty memory buffer with given size. |
| 128 | * Returns 0 on success, otherwise non-zero. */ |
| 129 | int buffer_create(struct buffer *buffer, size_t size, const char *name); |
| 130 | |
| 131 | /* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ |
| 132 | int buffer_from_file(struct buffer *buffer, const char *filename); |
| 133 | |
Furquan Shaikh | 2bfef8d | 2021-06-25 23:55:51 -0700 | [diff] [blame] | 134 | /* Loads a file into memory buffer (with buffer size rounded up to a multiple of |
| 135 | size_granularity). Returns 0 on success, otherwise non-zero. */ |
| 136 | int buffer_from_file_aligned_size(struct buffer *buffer, const char *filename, |
| 137 | size_t size_granularity); |
| 138 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 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 | */ |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 161 | typedef int (*decomp_func_ptr) (char *in, int in_len, char *out, int out_len, |
| 162 | size_t *actual_size); |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 163 | |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 164 | comp_func_ptr compression_function(enum cbfs_compression algo); |
| 165 | decomp_func_ptr decompression_function(enum cbfs_compression algo); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 166 | |
| 167 | uint64_t intfiletype(const char *name); |
| 168 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 169 | /* cbfs-mkpayload.c */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 170 | int parse_elf_to_payload(const struct buffer *input, struct buffer *output, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 171 | enum cbfs_compression algo); |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 172 | int parse_fv_to_payload(const struct buffer *input, struct buffer *output, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 173 | enum cbfs_compression algo); |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 174 | int parse_fit_to_payload(const struct buffer *input, struct buffer *output, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 175 | enum cbfs_compression algo); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 176 | int parse_bzImage_to_payload(const struct buffer *input, |
| 177 | struct buffer *output, const char *initrd, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 178 | char *cmdline, enum cbfs_compression algo); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 179 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 180 | struct buffer *output, |
| 181 | uint32_t loadaddress, |
| 182 | uint32_t entrypoint, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 183 | enum cbfs_compression algo); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 184 | /* cbfs-mkstage.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 185 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 186 | const char *ignore_section, |
| 187 | struct cbfs_file_attr_stageheader *stageheader); |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 188 | /* location is TOP aligned. */ |
| 189 | int parse_elf_to_xip_stage(const struct buffer *input, struct buffer *output, |
Arthur Heymans | 5bb7dc4 | 2021-06-22 15:21:46 +0200 | [diff] [blame] | 190 | uint32_t location, const char *ignore_section, |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 191 | struct cbfs_file_attr_stageheader *stageheader); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 192 | |
Jonathan Neuschäfer | fbc66b9 | 2018-04-08 15:05:09 +0200 | [diff] [blame] | 193 | void print_supported_architectures(void); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 194 | void print_supported_filetypes(void); |
| 195 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 196 | /* lzma/lzma.c */ |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 197 | 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] | 198 | int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len, |
| 199 | size_t *actual_size); |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 200 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 201 | /* xdr.c */ |
| 202 | struct xdr { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 203 | uint8_t (*get8)(struct buffer *input); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 204 | uint16_t (*get16)(struct buffer *input); |
| 205 | uint32_t (*get32)(struct buffer *input); |
| 206 | uint64_t (*get64)(struct buffer *input); |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 207 | void (*put8)(struct buffer *input, uint8_t val); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 208 | void (*put16)(struct buffer *input, uint16_t val); |
| 209 | void (*put32)(struct buffer *input, uint32_t val); |
| 210 | void (*put64)(struct buffer *input, uint64_t val); |
| 211 | }; |
| 212 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 213 | extern struct xdr xdr_le, xdr_be; |
Aaron Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame] | 214 | size_t bgets(struct buffer *input, void *output, size_t len); |
| 215 | size_t bputs(struct buffer *b, const void *data, size_t len); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 216 | |
Patrick Georgi | ca97fa7 | 2015-10-01 15:52:56 +0200 | [diff] [blame] | 217 | /* Returns a 0-terminated string containing a hex representation of |
| 218 | * len bytes starting at data. |
| 219 | * The string is malloc'd and it's the caller's responsibility to free |
| 220 | * the memory. |
| 221 | * On error, bintohex returns NULL. |
| 222 | */ |
| 223 | char *bintohex(uint8_t *data, size_t len); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 224 | #endif |