blob: b6386533cfefe8b3c766707ddb1831ff5603e07c [file] [log] [blame]
Patrick Georgib7b56dd82009-09-14 13:29:27 +00001/*
2 * Copyright (C) 2009 coresystems GmbH
3 * written by Patrick Georgi <patrick.georgi@coresystems.de>
David Hendricks90ca3b62012-11-16 14:48:22 -08004 * Copyright (C) 2012 Google, Inc.
Patrick Georgib7b56dd82009-09-14 13:29:27 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Patrick Georgib7b56dd82009-09-14 13:29:27 +000018 */
19
Stefan Reinauer63217582012-10-29 16:52:36 -070020#ifndef __CBFSTOOL_COMMON_H
21#define __CBFSTOOL_COMMON_H
22
Sol Bouchere3260a02015-03-25 13:40:08 -070023#include <stdbool.h>
Sol Boucher69b88bf2015-02-26 11:47:19 -080024#include <stddef.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000025#include <stdint.h>
Sol Bouchere3260a02015-03-25 13:40:08 -070026#include <string.h>
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080027#include <assert.h>
Hung-Te Lin332795c2013-01-28 15:53:34 +080028
Aaron Durbind38e3de2015-10-01 14:25:19 -050029#include <console/console.h>
30
Hung-Te Lin332795c2013-01-28 15:53:34 +080031/* Endianess */
Stefan Reinauera1e48242011-10-21 14:24:57 -070032#include "swab.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000033
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010034/* Helpers */
35#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
36#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
Sol Boucher67a0a862015-03-18 12:36:27 -070037#define MAX(x, y) ((x) > (y) ? (x) : (y))
38#define MIN(x, y) ((x) < (y) ? (x) : (y))
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010039
Sol Boucher67d59982015-05-07 02:39:22 -070040#define IS_TOP_ALIGNED_ADDRESS(x) ((uint32_t)(x) > 0x80000000)
41
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010042#define unused __attribute__((unused))
43
Julius Wernerefcee762014-11-10 13:14:24 -080044static inline uint32_t align_up(uint32_t value, uint32_t align)
45{
46 if (value % align)
47 value += align - (value % align);
48 return value;
49}
50
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080051/* Buffer and file I/O */
52struct buffer {
53 char *name;
54 char *data;
Sol Boucher64c6cd72015-04-26 02:32:43 -070055 size_t offset;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080056 size_t size;
57};
58
Aaron Durbin6e8c2792014-03-04 22:01:12 -060059static inline void *buffer_get(const struct buffer *b)
60{
61 return b->data;
62}
63
64static inline size_t buffer_size(const struct buffer *b)
65{
66 return b->size;
67}
68
Sol Boucher64c6cd72015-04-26 02:32:43 -070069/*
70 * Shrink a buffer toward the beginning of its previous space.
71 * Afterward, buffer_delete() remains the means of cleaning it up. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060072static inline void buffer_set_size(struct buffer *b, size_t size)
73{
74 b->size = size;
75}
76
Aaron Durbin4f3bb802014-03-26 22:57:55 -050077/* Initialize a buffer with the given constraints. */
78static inline void buffer_init(struct buffer *b, char *name, void *data,
79 size_t size)
80{
81 b->name = name;
82 b->data = data;
83 b->size = size;
84}
85
Sol Boucher1a3349f2015-05-05 18:25:18 -070086/* Splice a buffer into another buffer. Note that it's up to the caller to
Sol Boucher64c6cd72015-04-26 02:32:43 -070087 * bounds check the offset and size. The resulting buffer is backed by the same
88 * storage as the original, so although it is valid to buffer_delete() either
89 * one of them, doing so releases both simultaneously. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060090static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
91 size_t offset, size_t size)
92{
Sol Boucher64c6cd72015-04-26 02:32:43 -070093 dest->name = src->name;
94 dest->data = src->data + offset;
95 dest->offset = src->offset + offset;
96 dest->size = size;
Aaron Durbin6e8c2792014-03-04 22:01:12 -060097}
98
Sol Boucher64c6cd72015-04-26 02:32:43 -070099/*
100 * Shallow copy a buffer. To clean up the resources, buffer_delete()
101 * either one, but not both. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600102static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
103{
Sol Boucher1a3349f2015-05-05 18:25:18 -0700104 buffer_splice(dest, src, 0, src->size);
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600105}
106
Sol Boucher64c6cd72015-04-26 02:32:43 -0700107/*
108 * Shrink a buffer toward the end of its previous space.
109 * Afterward, buffer_delete() remains the means of cleaning it up. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600110static inline void buffer_seek(struct buffer *b, size_t size)
111{
Sol Boucher64c6cd72015-04-26 02:32:43 -0700112 b->offset += size;
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600113 b->size -= size;
114 b->data += size;
115}
116
Sol Bouchere3260a02015-03-25 13:40:08 -0700117/* Returns whether the buffer begins with the specified magic bytes. */
118static inline bool buffer_check_magic(const struct buffer *b, const char *magic,
119 size_t magic_len)
120{
121 assert(magic);
122 return b && b->size >= magic_len &&
123 memcmp(b->data, magic, magic_len) == 0;
124}
125
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800126/* Creates an empty memory buffer with given size.
127 * Returns 0 on success, otherwise non-zero. */
128int buffer_create(struct buffer *buffer, size_t size, const char *name);
129
130/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
131int buffer_from_file(struct buffer *buffer, const char *filename);
132
133/* Writes memory buffer content into file.
134 * Returns 0 on success, otherwise non-zero. */
135int buffer_write_file(struct buffer *buffer, const char *filename);
136
137/* Destroys a memory buffer. */
138void buffer_delete(struct buffer *buffer);
139
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800140const char *arch_to_string(uint32_t a);
David Hendricks90ca3b62012-11-16 14:48:22 -0800141uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000142
Patrick Georgi61c82292015-08-26 12:53:41 +0200143/* Compress in_len bytes from in, storing the result at out, returning the
144 * resulting length in out_len.
145 * Returns 0 on error,
146 * != 0 otherwise, depending on the compressing function.
147 */
148typedef int (*comp_func_ptr) (char *in, int in_len, char *out, int *out_len);
149
150/* Decompress in_len bytes from in, storing the result at out, up to out_len
151 * bytes.
152 * Returns 0 on error,
153 * != 0 otherwise, depending on the decompressing function.
154 */
155typedef int (*decomp_func_ptr) (char *in, int in_len, char *out, int out_len);
156
Sol Boucher6310ccc2015-05-07 21:12:28 -0700157enum comp_algo { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 };
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000158
Sol Boucher6310ccc2015-05-07 21:12:28 -0700159comp_func_ptr compression_function(enum comp_algo algo);
Patrick Georgi61c82292015-08-26 12:53:41 +0200160decomp_func_ptr decompression_function(enum comp_algo algo);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000161
162uint64_t intfiletype(const char *name);
163
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800164/* cbfs-mkpayload.c */
Sol Boucher0e539312015-03-05 15:38:03 -0800165int parse_elf_to_payload(const struct buffer *input, struct buffer *output,
Sol Boucher6310ccc2015-05-07 21:12:28 -0700166 enum comp_algo algo);
167int parse_fv_to_payload(const struct buffer *input, struct buffer *output,
168 enum comp_algo algo);
Patrick Georgide36d332013-08-27 20:22:21 +0200169int parse_bzImage_to_payload(const struct buffer *input,
170 struct buffer *output, const char *initrd,
Sol Boucher6310ccc2015-05-07 21:12:28 -0700171 char *cmdline, enum comp_algo algo);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800172int parse_flat_binary_to_payload(const struct buffer *input,
173 struct buffer *output,
174 uint32_t loadaddress,
175 uint32_t entrypoint,
Sol Boucher6310ccc2015-05-07 21:12:28 -0700176 enum comp_algo algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800177/* cbfs-mkstage.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800178int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Sol Boucher6310ccc2015-05-07 21:12:28 -0700179 enum comp_algo algo, uint32_t *location,
Furquan Shaikh405304a2014-10-30 11:44:20 -0700180 const char *ignore_section);
Aaron Durbin4be16742015-09-15 17:00:23 -0500181/* location is TOP aligned. */
182int parse_elf_to_xip_stage(const struct buffer *input, struct buffer *output,
183 uint32_t *location, const char *ignore_section);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000184
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);
189int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len);
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100190
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800191/* xdr.c */
192struct xdr {
Aaron Durbin01650042014-03-05 16:38:26 -0600193 uint8_t (*get8)(struct buffer *input);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800194 uint16_t (*get16)(struct buffer *input);
195 uint32_t (*get32)(struct buffer *input);
196 uint64_t (*get64)(struct buffer *input);
Aaron Durbin01650042014-03-05 16:38:26 -0600197 void (*put8)(struct buffer *input, uint8_t val);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800198 void (*put16)(struct buffer *input, uint16_t val);
199 void (*put32)(struct buffer *input, uint32_t val);
200 void (*put64)(struct buffer *input, uint64_t val);
201};
202
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800203extern struct xdr xdr_le, xdr_be;
Aaron Durbin1240d292014-03-10 14:13:27 -0500204size_t bgets(struct buffer *input, void *output, size_t len);
205size_t bputs(struct buffer *b, const void *data, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700206
Patrick Georgica97fa72015-10-01 15:52:56 +0200207/* Returns a 0-terminated string containing a hex representation of
208 * len bytes starting at data.
209 * The string is malloc'd and it's the caller's responsibility to free
210 * the memory.
211 * On error, bintohex returns NULL.
212 */
213char *bintohex(uint8_t *data, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700214#endif