lib/cbfs: deserialize cbfs_stage objects correctly
[coreboot.git] / util / cbfstool / common.h
blob7dbc1f575d4f17ca0cc4136b4c73cb865be4b014
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef __CBFSTOOL_COMMON_H
4 #define __CBFSTOOL_COMMON_H
6 #include <stdbool.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <assert.h>
12 #include <commonlib/helpers.h>
13 #include <console/console.h>
15 /* Endianness */
16 #include "swab.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)
24 if (value % align)
25 value += align - (value % align);
26 return value;
29 /* Buffer and file I/O */
30 struct buffer {
31 char *name;
32 char *data;
33 size_t offset;
34 size_t size;
37 static inline void *buffer_get(const struct buffer *b)
39 return b->data;
42 static inline size_t buffer_size(const struct buffer *b)
44 return b->size;
47 static inline size_t buffer_offset(const struct buffer *b)
49 return b->offset;
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)
57 b->size = size;
60 /* Initialize a buffer with the given constraints. */
61 static inline void buffer_init(struct buffer *b, char *name, void *data,
62 size_t size)
64 b->name = name;
65 b->data = data;
66 b->size = size;
67 b->offset = 0;
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;
80 dest->size = size;
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)
96 b->offset += size;
97 b->size -= size;
98 b->data += 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,
103 size_t magic_len)
105 assert(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)
113 if (!b)
114 return NULL;
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
143 * bytes.
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);
150 enum comp_algo {
151 CBFS_COMPRESS_NONE = 0,
152 CBFS_COMPRESS_LZMA = 1,
153 CBFS_COMPRESS_LZ4 = 2,
156 struct typedesc_t {
157 uint32_t type;
158 const char *name;
161 static const struct typedesc_t types_cbfs_compression[] = {
162 {CBFS_COMPRESS_NONE, "none"},
163 {CBFS_COMPRESS_LZMA, "LZMA"},
164 {CBFS_COMPRESS_LZ4, "LZ4"},
165 {0, NULL},
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,
186 uint32_t entrypoint,
187 enum comp_algo algo);
188 /* cbfs-mkstage.c */
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);
199 /* lzma/lzma.c */
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);
204 /* xdr.c */
205 struct xdr {
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
223 * the memory.
224 * On error, bintohex returns NULL.
226 char *bintohex(uint8_t *data, size_t len);
227 #endif