blob: 479fd713dd0e57e7e12633218db650bce0213138 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgib7b56dd82009-09-14 13:29:27 +00002
Stefan Reinauer63217582012-10-29 16:52:36 -07003#ifndef __CBFSTOOL_COMMON_H
4#define __CBFSTOOL_COMMON_H
5
Sol Bouchere3260a02015-03-25 13:40:08 -07006#include <stdbool.h>
Sol Boucher69b88bf2015-02-26 11:47:19 -08007#include <stddef.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +00008#include <stdint.h>
Sol Bouchere3260a02015-03-25 13:40:08 -07009#include <string.h>
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080010#include <assert.h>
Hung-Te Lin332795c2013-01-28 15:53:34 +080011
Julius Wernerd4775652020-03-13 16:43:34 -070012#include <commonlib/bsd/cbfs_serialized.h>
Furquan Shaikh161d2332016-05-26 14:41:02 -070013#include <commonlib/helpers.h>
Aaron Durbind38e3de2015-10-01 14:25:19 -050014#include <console/console.h>
15
Stefan Reinauera1e48242011-10-21 14:24:57 -070016#include "swab.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000017
Furquan Shaikh19ba95f2020-11-20 22:50:26 -080018/*
19 * There are two address spaces that this tool deals with - SPI flash address space and host
20 * address space. This macros checks if the address is greater than 2GiB under the assumption
21 * that the low MMIO lives in the top half of the 4G address space of the host.
22 */
23#define IS_HOST_SPACE_ADDRESS(addr) ((uint32_t)(addr) > 0x80000000)
Sol Boucher67d59982015-05-07 02:39:22 -070024
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010025#define unused __attribute__((unused))
26
Julius Wernerefcee762014-11-10 13:14:24 -080027static inline uint32_t align_up(uint32_t value, uint32_t align)
28{
29 if (value % align)
30 value += align - (value % align);
31 return value;
32}
33
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080034/* Buffer and file I/O */
35struct buffer {
36 char *name;
37 char *data;
Sol Boucher64c6cd72015-04-26 02:32:43 -070038 size_t offset;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080039 size_t size;
40};
41
Aaron Durbin6e8c2792014-03-04 22:01:12 -060042static inline void *buffer_get(const struct buffer *b)
43{
44 return b->data;
45}
46
47static inline size_t buffer_size(const struct buffer *b)
48{
49 return b->size;
50}
51
Aaron Durbin2dd5f4c2016-01-26 09:01:14 -060052static inline size_t buffer_offset(const struct buffer *b)
53{
54 return b->offset;
55}
56
Sol Boucher64c6cd72015-04-26 02:32:43 -070057/*
58 * Shrink a buffer toward the beginning of its previous space.
59 * Afterward, buffer_delete() remains the means of cleaning it up. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060060static inline void buffer_set_size(struct buffer *b, size_t size)
61{
62 b->size = size;
63}
64
Aaron Durbin4f3bb802014-03-26 22:57:55 -050065/* Initialize a buffer with the given constraints. */
66static inline void buffer_init(struct buffer *b, char *name, void *data,
67 size_t size)
68{
69 b->name = name;
70 b->data = data;
71 b->size = size;
Aaron Durbincd9ba8a2015-10-23 17:36:57 -050072 b->offset = 0;
Aaron Durbin4f3bb802014-03-26 22:57:55 -050073}
74
Sol Boucher1a3349f2015-05-05 18:25:18 -070075/* Splice a buffer into another buffer. Note that it's up to the caller to
Sol Boucher64c6cd72015-04-26 02:32:43 -070076 * bounds check the offset and size. The resulting buffer is backed by the same
77 * storage as the original, so although it is valid to buffer_delete() either
78 * one of them, doing so releases both simultaneously. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060079static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
80 size_t offset, size_t size)
81{
Sol Boucher64c6cd72015-04-26 02:32:43 -070082 dest->name = src->name;
83 dest->data = src->data + offset;
84 dest->offset = src->offset + offset;
85 dest->size = size;
Aaron Durbin6e8c2792014-03-04 22:01:12 -060086}
87
Sol Boucher64c6cd72015-04-26 02:32:43 -070088/*
89 * Shallow copy a buffer. To clean up the resources, buffer_delete()
90 * either one, but not both. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060091static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
92{
Sol Boucher1a3349f2015-05-05 18:25:18 -070093 buffer_splice(dest, src, 0, src->size);
Aaron Durbin6e8c2792014-03-04 22:01:12 -060094}
95
Sol Boucher64c6cd72015-04-26 02:32:43 -070096/*
97 * Shrink a buffer toward the end of its previous space.
98 * Afterward, buffer_delete() remains the means of cleaning it up. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060099static inline void buffer_seek(struct buffer *b, size_t size)
100{
Sol Boucher64c6cd72015-04-26 02:32:43 -0700101 b->offset += size;
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600102 b->size -= size;
103 b->data += size;
104}
105
Sol Bouchere3260a02015-03-25 13:40:08 -0700106/* Returns whether the buffer begins with the specified magic bytes. */
107static inline bool buffer_check_magic(const struct buffer *b, const char *magic,
108 size_t magic_len)
109{
110 assert(magic);
111 return b && b->size >= magic_len &&
112 memcmp(b->data, magic, magic_len) == 0;
113}
114
Patrick Georgi9b24f7c2015-11-11 18:43:11 +0100115/* Returns the start of the underlying buffer, with the offset undone */
116static inline void *buffer_get_original_backing(const struct buffer *b)
117{
118 if (!b)
119 return NULL;
Aaron Durbin2dd5f4c2016-01-26 09:01:14 -0600120 return buffer_get(b) - buffer_offset(b);
Patrick Georgi9b24f7c2015-11-11 18:43:11 +0100121}
122
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800123/* Creates an empty memory buffer with given size.
124 * Returns 0 on success, otherwise non-zero. */
125int buffer_create(struct buffer *buffer, size_t size, const char *name);
126
127/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
128int buffer_from_file(struct buffer *buffer, const char *filename);
129
130/* Writes memory buffer content into file.
131 * Returns 0 on success, otherwise non-zero. */
132int buffer_write_file(struct buffer *buffer, const char *filename);
133
134/* Destroys a memory buffer. */
135void buffer_delete(struct buffer *buffer);
136
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800137const char *arch_to_string(uint32_t a);
David Hendricks90ca3b62012-11-16 14:48:22 -0800138uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000139
Patrick Georgi61c82292015-08-26 12:53:41 +0200140/* Compress in_len bytes from in, storing the result at out, returning the
141 * resulting length in out_len.
142 * Returns 0 on error,
143 * != 0 otherwise, depending on the compressing function.
144 */
145typedef int (*comp_func_ptr) (char *in, int in_len, char *out, int *out_len);
146
147/* Decompress in_len bytes from in, storing the result at out, up to out_len
148 * bytes.
149 * Returns 0 on error,
150 * != 0 otherwise, depending on the decompressing function.
151 */
Aaron Durbin5213c532015-10-23 17:38:40 -0500152typedef int (*decomp_func_ptr) (char *in, int in_len, char *out, int out_len,
153 size_t *actual_size);
Patrick Georgi61c82292015-08-26 12:53:41 +0200154
Julius Wernerd4775652020-03-13 16:43:34 -0700155comp_func_ptr compression_function(enum cbfs_compression algo);
156decomp_func_ptr decompression_function(enum cbfs_compression algo);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000157
158uint64_t intfiletype(const char *name);
159
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800160/* cbfs-mkpayload.c */
Sol Boucher0e539312015-03-05 15:38:03 -0800161int parse_elf_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700162 enum cbfs_compression algo);
Sol Boucher6310ccc2015-05-07 21:12:28 -0700163int parse_fv_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700164 enum cbfs_compression algo);
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200165int parse_fit_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700166 enum cbfs_compression algo);
Patrick Georgide36d332013-08-27 20:22:21 +0200167int parse_bzImage_to_payload(const struct buffer *input,
168 struct buffer *output, const char *initrd,
Julius Wernerd4775652020-03-13 16:43:34 -0700169 char *cmdline, enum cbfs_compression algo);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800170int parse_flat_binary_to_payload(const struct buffer *input,
171 struct buffer *output,
172 uint32_t loadaddress,
173 uint32_t entrypoint,
Julius Wernerd4775652020-03-13 16:43:34 -0700174 enum cbfs_compression algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800175/* cbfs-mkstage.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800176int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Julius Werner81dc20e2020-10-15 17:37:57 -0700177 const char *ignore_section,
178 struct cbfs_file_attr_stageheader *stageheader);
Aaron Durbin4be16742015-09-15 17:00:23 -0500179/* location is TOP aligned. */
180int parse_elf_to_xip_stage(const struct buffer *input, struct buffer *output,
Julius Werner81dc20e2020-10-15 17:37:57 -0700181 uint32_t *location, const char *ignore_section,
182 struct cbfs_file_attr_stageheader *stageheader);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000183
Jonathan Neuschäferfbc66b92018-04-08 15:05:09 +0200184void print_supported_architectures(void);
Stefan Reinauer07040582010-04-24 21:24:06 +0000185void print_supported_filetypes(void);
186
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800187/* lzma/lzma.c */
Gabe Blackdbd006b2014-02-20 23:38:49 -0800188int do_lzma_compress(char *in, int in_len, char *out, int *out_len);
Aaron Durbin5213c532015-10-23 17:38:40 -0500189int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len,
190 size_t *actual_size);
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100191
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800192/* xdr.c */
193struct xdr {
Aaron Durbin01650042014-03-05 16:38:26 -0600194 uint8_t (*get8)(struct buffer *input);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800195 uint16_t (*get16)(struct buffer *input);
196 uint32_t (*get32)(struct buffer *input);
197 uint64_t (*get64)(struct buffer *input);
Aaron Durbin01650042014-03-05 16:38:26 -0600198 void (*put8)(struct buffer *input, uint8_t val);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800199 void (*put16)(struct buffer *input, uint16_t val);
200 void (*put32)(struct buffer *input, uint32_t val);
201 void (*put64)(struct buffer *input, uint64_t val);
202};
203
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800204extern struct xdr xdr_le, xdr_be;
Aaron Durbin1240d292014-03-10 14:13:27 -0500205size_t bgets(struct buffer *input, void *output, size_t len);
206size_t bputs(struct buffer *b, const void *data, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700207
Patrick Georgica97fa72015-10-01 15:52:56 +0200208/* Returns a 0-terminated string containing a hex representation of
209 * len bytes starting at data.
210 * The string is malloc'd and it's the caller's responsibility to free
211 * the memory.
212 * On error, bintohex returns NULL.
213 */
214char *bintohex(uint8_t *data, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700215#endif