xref: /aosp_15_r20/external/coreboot/util/cbfstool/common.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef __CBFSTOOL_COMMON_H
4 #define __CBFSTOOL_COMMON_H
5 
6 #include <stdbool.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <assert.h>
11 
12 #include <commonlib/bsd/cbfs_serialized.h>
13 #include <commonlib/bsd/sysincludes.h>
14 #include <commonlib/helpers.h>
15 #include <console/console.h>
16 
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)
23 
24 #define unused __attribute__((unused))
25 
align_up(uint32_t value,uint32_t align)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 
33 /* Buffer and file I/O */
34 struct buffer {
35 	char *name;
36 	char *data;
37 	size_t offset;
38 	size_t size;
39 };
40 
buffer_get(const struct buffer * b)41 static inline void *buffer_get(const struct buffer *b)
42 {
43 	return b->data;
44 }
45 
buffer_size(const struct buffer * b)46 static inline size_t buffer_size(const struct buffer *b)
47 {
48 	return b->size;
49 }
50 
buffer_offset(const struct buffer * b)51 static inline size_t buffer_offset(const struct buffer *b)
52 {
53 	return b->offset;
54 }
55 
buffer_end(const struct buffer * b)56 static inline void *buffer_end(const struct buffer *b)
57 {
58 	return b->data + b->size;
59 }
60 
61 /*
62  * Shrink a buffer toward the beginning of its previous space.
63  * Afterward, buffer_delete() remains the means of cleaning it up. */
buffer_set_size(struct buffer * b,size_t size)64 static inline void buffer_set_size(struct buffer *b, size_t size)
65 {
66 	b->size = size;
67 }
68 
69 /* Initialize a buffer with the given constraints. */
buffer_init(struct buffer * b,char * name,void * data,size_t size)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;
76 	b->offset = 0;
77 }
78 
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. */
buffer_splice(struct buffer * dest,const struct buffer * src,size_t offset,size_t size)83 static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
84                                  size_t offset, size_t size)
85 {
86 	dest->name = src->name;
87 	dest->data = src->data + offset;
88 	dest->offset = src->offset + offset;
89 	dest->size = size;
90 }
91 
92 /*
93  * Shallow copy a buffer. To clean up the resources, buffer_delete()
94  * either one, but not both. */
buffer_clone(struct buffer * dest,const struct buffer * src)95 static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
96 {
97 	buffer_splice(dest, src, 0, src->size);
98 }
99 
100 /*
101  * Shrink a buffer toward the end of its previous space.
102  * Afterward, buffer_delete() remains the means of cleaning it up. */
buffer_seek(struct buffer * b,size_t size)103 static inline void buffer_seek(struct buffer *b, size_t size)
104 {
105 	b->offset += size;
106 	b->size -= size;
107 	b->data += size;
108 }
109 
110 /* Returns whether the buffer begins with the specified magic bytes. */
buffer_check_magic(const struct buffer * b,const char * magic,size_t magic_len)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 
119 /* Returns the start of the underlying buffer, with the offset undone */
buffer_get_original_backing(const struct buffer * b)120 static inline void *buffer_get_original_backing(const struct buffer *b)
121 {
122 	if (!b)
123 		return NULL;
124 	return buffer_get(b) - buffer_offset(b);
125 }
126 
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 
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 
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 
146 const char *arch_to_string(uint32_t a);
147 uint32_t string_to_arch(const char *arch_string);
148 
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  */
161 typedef int (*decomp_func_ptr) (char *in, int in_len, char *out, int out_len,
162 				size_t *actual_size);
163 
164 comp_func_ptr compression_function(enum cbfs_compression algo);
165 decomp_func_ptr decompression_function(enum cbfs_compression algo);
166 
167 uint64_t intfiletype(const char *name);
168 
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,
182 				 uint64_t entrypoint,
183 				 enum cbfs_compression algo);
184 /* cbfs-mkstage.c */
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);
192 
193 void print_supported_architectures(void);
194 void print_supported_filetypes(void);
195 
196 /* lzma/lzma.c */
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);
200 
201 /* xdr.c */
202 struct xdr {
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);
211 };
212 
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);
216 
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);
224 #endif
225