1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef __CBFSTOOL_COMMON_H
4 #define __CBFSTOOL_COMMON_H
12 #include <commonlib/helpers.h>
13 #include <console/console.h>
18 #define IS_TOP_ALIGNED_ADDRESS(x) ((uint32_t)(x) > 0x80000000)
20 #define unused __attribute__((unused))
22 static inline uint32_t align_up(uint32_t value
, uint32_t align
)
25 value
+= align
- (value
% align
);
29 /* Buffer and file I/O */
37 static inline void *buffer_get(const struct buffer
*b
)
42 static inline size_t buffer_size(const struct buffer
*b
)
47 static inline size_t buffer_offset(const struct buffer
*b
)
53 * Shrink a buffer toward the beginning of its previous space.
54 * Afterward, buffer_delete() remains the means of cleaning it up. */
55 static inline void buffer_set_size(struct buffer
*b
, size_t size
)
60 /* Initialize a buffer with the given constraints. */
61 static inline void buffer_init(struct buffer
*b
, char *name
, void *data
,
70 /* Splice a buffer into another buffer. Note that it's up to the caller to
71 * bounds check the offset and size. The resulting buffer is backed by the same
72 * storage as the original, so although it is valid to buffer_delete() either
73 * one of them, doing so releases both simultaneously. */
74 static inline void buffer_splice(struct buffer
*dest
, const struct buffer
*src
,
75 size_t offset
, size_t size
)
77 dest
->name
= src
->name
;
78 dest
->data
= src
->data
+ offset
;
79 dest
->offset
= src
->offset
+ offset
;
84 * Shallow copy a buffer. To clean up the resources, buffer_delete()
85 * either one, but not both. */
86 static inline void buffer_clone(struct buffer
*dest
, const struct buffer
*src
)
88 buffer_splice(dest
, src
, 0, src
->size
);
92 * Shrink a buffer toward the end of its previous space.
93 * Afterward, buffer_delete() remains the means of cleaning it up. */
94 static inline void buffer_seek(struct buffer
*b
, size_t size
)
101 /* Returns whether the buffer begins with the specified magic bytes. */
102 static inline bool buffer_check_magic(const struct buffer
*b
, const char *magic
,
106 return b
&& b
->size
>= magic_len
&&
107 memcmp(b
->data
, magic
, magic_len
) == 0;
110 /* Returns the start of the underlying buffer, with the offset undone */
111 static inline void *buffer_get_original_backing(const struct buffer
*b
)
115 return buffer_get(b
) - buffer_offset(b
);
118 /* Creates an empty memory buffer with given size.
119 * Returns 0 on success, otherwise non-zero. */
120 int buffer_create(struct buffer
*buffer
, size_t size
, const char *name
);
122 /* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
123 int buffer_from_file(struct buffer
*buffer
, const char *filename
);
125 /* Writes memory buffer content into file.
126 * Returns 0 on success, otherwise non-zero. */
127 int buffer_write_file(struct buffer
*buffer
, const char *filename
);
129 /* Destroys a memory buffer. */
130 void buffer_delete(struct buffer
*buffer
);
132 const char *arch_to_string(uint32_t a
);
133 uint32_t string_to_arch(const char *arch_string
);
135 /* Compress in_len bytes from in, storing the result at out, returning the
136 * resulting length in out_len.
137 * Returns 0 on error,
138 * != 0 otherwise, depending on the compressing function.
140 typedef int (*comp_func_ptr
) (char *in
, int in_len
, char *out
, int *out_len
);
142 /* Decompress in_len bytes from in, storing the result at out, up to out_len
144 * Returns 0 on error,
145 * != 0 otherwise, depending on the decompressing function.
147 typedef int (*decomp_func_ptr
) (char *in
, int in_len
, char *out
, int out_len
,
148 size_t *actual_size
);
151 CBFS_COMPRESS_NONE
= 0,
152 CBFS_COMPRESS_LZMA
= 1,
153 CBFS_COMPRESS_LZ4
= 2,
161 static const struct typedesc_t types_cbfs_compression
[] = {
162 {CBFS_COMPRESS_NONE
, "none"},
163 {CBFS_COMPRESS_LZMA
, "LZMA"},
164 {CBFS_COMPRESS_LZ4
, "LZ4"},
168 comp_func_ptr
compression_function(enum comp_algo algo
);
169 decomp_func_ptr
decompression_function(enum comp_algo algo
);
171 uint64_t intfiletype(const char *name
);
173 /* cbfs-mkpayload.c */
174 int parse_elf_to_payload(const struct buffer
*input
, struct buffer
*output
,
175 enum comp_algo algo
);
176 int parse_fv_to_payload(const struct buffer
*input
, struct buffer
*output
,
177 enum comp_algo algo
);
178 int parse_fit_to_payload(const struct buffer
*input
, struct buffer
*output
,
179 enum comp_algo algo
);
180 int parse_bzImage_to_payload(const struct buffer
*input
,
181 struct buffer
*output
, const char *initrd
,
182 char *cmdline
, enum comp_algo algo
);
183 int parse_flat_binary_to_payload(const struct buffer
*input
,
184 struct buffer
*output
,
185 uint32_t loadaddress
,
187 enum comp_algo algo
);
189 int parse_elf_to_stage(const struct buffer
*input
, struct buffer
*output
,
190 enum comp_algo algo
, uint32_t *location
,
191 const char *ignore_section
);
192 /* location is TOP aligned. */
193 int parse_elf_to_xip_stage(const struct buffer
*input
, struct buffer
*output
,
194 uint32_t *location
, const char *ignore_section
);
196 void print_supported_architectures(void);
197 void print_supported_filetypes(void);
200 int do_lzma_compress(char *in
, int in_len
, char *out
, int *out_len
);
201 int do_lzma_uncompress(char *dst
, int dst_len
, char *src
, int src_len
,
202 size_t *actual_size
);
206 uint8_t (*get8
)(struct buffer
*input
);
207 uint16_t (*get16
)(struct buffer
*input
);
208 uint32_t (*get32
)(struct buffer
*input
);
209 uint64_t (*get64
)(struct buffer
*input
);
210 void (*put8
)(struct buffer
*input
, uint8_t val
);
211 void (*put16
)(struct buffer
*input
, uint16_t val
);
212 void (*put32
)(struct buffer
*input
, uint32_t val
);
213 void (*put64
)(struct buffer
*input
, uint64_t val
);
216 extern struct xdr xdr_le
, xdr_be
;
217 size_t bgets(struct buffer
*input
, void *output
, size_t len
);
218 size_t bputs(struct buffer
*b
, const void *data
, size_t len
);
220 /* Returns a 0-terminated string containing a hex representation of
221 * len bytes starting at data.
222 * The string is malloc'd and it's the caller's responsibility to free
224 * On error, bintohex returns NULL.
226 char *bintohex(uint8_t *data
, size_t len
);