1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef __CBFSTOOL_COMMON_H
4 #define __CBFSTOOL_COMMON_H
12 #include <commonlib/bsd/cbfs_serialized.h>
13 #include <commonlib/bsd/sysincludes.h>
14 #include <commonlib/helpers.h>
15 #include <console/console.h>
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.
22 #define IS_HOST_SPACE_ADDRESS(addr) ((uint32_t)(addr) > 0x80000000)
24 #define unused __attribute__((unused))
26 static inline uint32_t align_up(uint32_t value
, uint32_t align
)
29 value
+= align
- (value
% align
);
33 /* Buffer and file I/O */
41 static inline void *buffer_get(const struct buffer
*b
)
46 static inline size_t buffer_size(const struct buffer
*b
)
51 static inline size_t buffer_offset(const struct buffer
*b
)
56 static inline void *buffer_end(const struct buffer
*b
)
58 return b
->data
+ b
->size
;
62 * Shrink a buffer toward the beginning of its previous space.
63 * Afterward, buffer_delete() remains the means of cleaning it up. */
64 static inline void buffer_set_size(struct buffer
*b
, size_t size
)
69 /* Initialize a buffer with the given constraints. */
70 static inline void buffer_init(struct buffer
*b
, char *name
, void *data
,
79 /* Splice a buffer into another buffer. Note that it's up to the caller to
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. */
83 static inline void buffer_splice(struct buffer
*dest
, const struct buffer
*src
,
84 size_t offset
, size_t size
)
86 dest
->name
= src
->name
;
87 dest
->data
= src
->data
+ offset
;
88 dest
->offset
= src
->offset
+ offset
;
93 * Shallow copy a buffer. To clean up the resources, buffer_delete()
94 * either one, but not both. */
95 static inline void buffer_clone(struct buffer
*dest
, const struct buffer
*src
)
97 buffer_splice(dest
, src
, 0, src
->size
);
101 * Shrink a buffer toward the end of its previous space.
102 * Afterward, buffer_delete() remains the means of cleaning it up. */
103 static inline void buffer_seek(struct buffer
*b
, size_t size
)
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
,
115 return b
&& b
->size
>= magic_len
&&
116 memcmp(b
->data
, magic
, magic_len
) == 0;
119 /* Returns the start of the underlying buffer, with the offset undone */
120 static inline void *buffer_get_original_backing(const struct buffer
*b
)
124 return buffer_get(b
) - buffer_offset(b
);
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
);
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
);
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
);
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
);
143 /* Destroys a memory buffer. */
144 void buffer_delete(struct buffer
*buffer
);
146 const char *arch_to_string(uint32_t a
);
147 uint32_t string_to_arch(const char *arch_string
);
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.
154 typedef int (*comp_func_ptr
) (char *in
, int in_len
, char *out
, int *out_len
);
156 /* Decompress in_len bytes from in, storing the result at out, up to out_len
158 * Returns 0 on error,
159 * != 0 otherwise, depending on the decompressing function.
161 typedef int (*decomp_func_ptr
) (char *in
, int in_len
, char *out
, int out_len
,
162 size_t *actual_size
);
164 comp_func_ptr
compression_function(enum cbfs_compression algo
);
165 decomp_func_ptr
decompression_function(enum cbfs_compression algo
);
167 uint64_t intfiletype(const char *name
);
169 /* cbfs-mkpayload.c */
170 int parse_elf_to_payload(const struct buffer
*input
, struct buffer
*output
,
171 enum cbfs_compression algo
);
172 int parse_fv_to_payload(const struct buffer
*input
, struct buffer
*output
,
173 enum cbfs_compression algo
);
174 int parse_fit_to_payload(const struct buffer
*input
, struct buffer
*output
,
175 enum cbfs_compression algo
);
176 int parse_bzImage_to_payload(const struct buffer
*input
,
177 struct buffer
*output
, const char *initrd
,
178 char *cmdline
, enum cbfs_compression algo
);
179 int parse_flat_binary_to_payload(const struct buffer
*input
,
180 struct buffer
*output
,
181 uint64_t loadaddress
,
183 enum cbfs_compression algo
);
185 int parse_elf_to_stage(const struct buffer
*input
, struct buffer
*output
,
186 const char *ignore_section
,
187 struct cbfs_file_attr_stageheader
*stageheader
);
188 /* location is TOP aligned. */
189 int parse_elf_to_xip_stage(const struct buffer
*input
, struct buffer
*output
,
190 uint32_t location
, const char *ignore_section
,
191 struct cbfs_file_attr_stageheader
*stageheader
);
193 void print_supported_architectures(void);
194 void print_supported_filetypes(void);
197 int do_lzma_compress(char *in
, int in_len
, char *out
, int *out_len
);
198 int do_lzma_uncompress(char *dst
, int dst_len
, char *src
, int src_len
,
199 size_t *actual_size
);
203 uint8_t (*get8
)(struct buffer
*input
);
204 uint16_t (*get16
)(struct buffer
*input
);
205 uint32_t (*get32
)(struct buffer
*input
);
206 uint64_t (*get64
)(struct buffer
*input
);
207 void (*put8
)(struct buffer
*input
, uint8_t val
);
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
);
213 extern struct xdr xdr_le
, xdr_be
;
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
);
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
221 * On error, bintohex returns NULL.
223 char *bintohex(uint8_t *data
, size_t len
);