blob: 831d3eb0888c4839bc3fdb93e419dcdd981129a1 [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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
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
29/* Endianess */
Stefan Reinauera1e48242011-10-21 14:24:57 -070030#include "swab.h"
Stefan Reinauer9da75702013-01-04 13:51:36 -080031#ifndef __APPLE__
Sol Boucher0e539312015-03-05 15:38:03 -080032#define ntohl(x) (is_big_endian() ? (uint32_t)(x) : swab32(x))
33#define htonl(x) (is_big_endian() ? (uint32_t)(x) : swab32(x))
Stefan Reinauer9da75702013-01-04 13:51:36 -080034#endif
Sol Boucher0e539312015-03-05 15:38:03 -080035#define ntohll(x) (is_big_endian() ? (uint64_t)(x) : swab64(x))
36#define htonll(x) (is_big_endian() ? (uint64_t)(x) : swab64(x))
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010037int is_big_endian(void);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000038
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080039/* Message output */
40extern int verbose;
Sol Boucher0e539312015-03-05 15:38:03 -080041#define ERROR(...) { fprintf(stderr, "E: " __VA_ARGS__); }
42#define WARN(...) { fprintf(stderr, "W: " __VA_ARGS__); }
43#define LOG(...) { fprintf(stderr, __VA_ARGS__); }
44#define INFO(...) { if (verbose > 0) fprintf(stderr, "INFO: " __VA_ARGS__); }
45#define DEBUG(...) { if (verbose > 1) fprintf(stderr, "DEBUG: " __VA_ARGS__); }
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080046
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010047/* Helpers */
48#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
49#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
Sol Boucher67a0a862015-03-18 12:36:27 -070050#define MAX(x, y) ((x) > (y) ? (x) : (y))
51#define MIN(x, y) ((x) < (y) ? (x) : (y))
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010052
53#define unused __attribute__((unused))
54
Julius Wernerefcee762014-11-10 13:14:24 -080055static inline uint32_t align_up(uint32_t value, uint32_t align)
56{
57 if (value % align)
58 value += align - (value % align);
59 return value;
60}
61
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080062/* Buffer and file I/O */
63struct buffer {
64 char *name;
65 char *data;
Sol Boucher64c6cd72015-04-26 02:32:43 -070066 size_t offset;
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080067 size_t size;
68};
69
Aaron Durbin6e8c2792014-03-04 22:01:12 -060070static inline void *buffer_get(const struct buffer *b)
71{
72 return b->data;
73}
74
75static inline size_t buffer_size(const struct buffer *b)
76{
77 return b->size;
78}
79
Sol Boucher64c6cd72015-04-26 02:32:43 -070080/*
81 * Shrink a buffer toward the beginning of its previous space.
82 * Afterward, buffer_delete() remains the means of cleaning it up. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -060083static inline void buffer_set_size(struct buffer *b, size_t size)
84{
85 b->size = size;
86}
87
Aaron Durbin4f3bb802014-03-26 22:57:55 -050088/* Initialize a buffer with the given constraints. */
89static inline void buffer_init(struct buffer *b, char *name, void *data,
90 size_t size)
91{
92 b->name = name;
93 b->data = data;
94 b->size = size;
95}
96
Sol Boucher1a3349f2015-05-05 18:25:18 -070097/* Splice a buffer into another buffer. Note that it's up to the caller to
Sol Boucher64c6cd72015-04-26 02:32:43 -070098 * bounds check the offset and size. The resulting buffer is backed by the same
99 * storage as the original, so although it is valid to buffer_delete() either
100 * one of them, doing so releases both simultaneously. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600101static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
102 size_t offset, size_t size)
103{
Sol Boucher64c6cd72015-04-26 02:32:43 -0700104 dest->name = src->name;
105 dest->data = src->data + offset;
106 dest->offset = src->offset + offset;
107 dest->size = size;
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600108}
109
Sol Boucher64c6cd72015-04-26 02:32:43 -0700110/*
111 * Shallow copy a buffer. To clean up the resources, buffer_delete()
112 * either one, but not both. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600113static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
114{
Sol Boucher1a3349f2015-05-05 18:25:18 -0700115 buffer_splice(dest, src, 0, src->size);
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600116}
117
Sol Boucher64c6cd72015-04-26 02:32:43 -0700118/*
119 * Shrink a buffer toward the end of its previous space.
120 * Afterward, buffer_delete() remains the means of cleaning it up. */
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600121static inline void buffer_seek(struct buffer *b, size_t size)
122{
Sol Boucher64c6cd72015-04-26 02:32:43 -0700123 b->offset += size;
Aaron Durbin6e8c2792014-03-04 22:01:12 -0600124 b->size -= size;
125 b->data += size;
126}
127
Sol Bouchere3260a02015-03-25 13:40:08 -0700128/* Returns whether the buffer begins with the specified magic bytes. */
129static inline bool buffer_check_magic(const struct buffer *b, const char *magic,
130 size_t magic_len)
131{
132 assert(magic);
133 return b && b->size >= magic_len &&
134 memcmp(b->data, magic, magic_len) == 0;
135}
136
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800137/* Creates an empty memory buffer with given size.
138 * Returns 0 on success, otherwise non-zero. */
139int buffer_create(struct buffer *buffer, size_t size, const char *name);
140
141/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
142int buffer_from_file(struct buffer *buffer, const char *filename);
143
144/* Writes memory buffer content into file.
145 * Returns 0 on success, otherwise non-zero. */
146int buffer_write_file(struct buffer *buffer, const char *filename);
147
148/* Destroys a memory buffer. */
149void buffer_delete(struct buffer *buffer);
150
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800151const char *arch_to_string(uint32_t a);
David Hendricks90ca3b62012-11-16 14:48:22 -0800152uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000153
Gabe Blackdbd006b2014-02-20 23:38:49 -0800154typedef int (*comp_func_ptr) (char *, int, char *, int *);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000155typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
156
157comp_func_ptr compression_function(comp_algo algo);
158
159uint64_t intfiletype(const char *name);
160
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800161/* cbfs-mkpayload.c */
Sol Boucher0e539312015-03-05 15:38:03 -0800162int parse_elf_to_payload(const struct buffer *input, struct buffer *output,
163 comp_algo algo);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800164int parse_fv_to_payload(const struct buffer *input,
165 struct buffer *output, comp_algo algo);
Patrick Georgide36d332013-08-27 20:22:21 +0200166int parse_bzImage_to_payload(const struct buffer *input,
167 struct buffer *output, const char *initrd,
168 char *cmdline, comp_algo algo);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800169int parse_flat_binary_to_payload(const struct buffer *input,
170 struct buffer *output,
171 uint32_t loadaddress,
172 uint32_t entrypoint,
173 comp_algo algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800174/* cbfs-mkstage.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800175int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Sol Boucher0e539312015-03-05 15:38:03 -0800176 comp_algo algo, uint32_t *location,
Furquan Shaikh405304a2014-10-30 11:44:20 -0700177 const char *ignore_section);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000178
Stefan Reinauer07040582010-04-24 21:24:06 +0000179void print_supported_filetypes(void);
180
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800181/* lzma/lzma.c */
Gabe Blackdbd006b2014-02-20 23:38:49 -0800182int do_lzma_compress(char *in, int in_len, char *out, int *out_len);
183int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len);
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100184
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800185/* xdr.c */
186struct xdr {
Aaron Durbin01650042014-03-05 16:38:26 -0600187 uint8_t (*get8)(struct buffer *input);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800188 uint16_t (*get16)(struct buffer *input);
189 uint32_t (*get32)(struct buffer *input);
190 uint64_t (*get64)(struct buffer *input);
Aaron Durbin01650042014-03-05 16:38:26 -0600191 void (*put8)(struct buffer *input, uint8_t val);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800192 void (*put16)(struct buffer *input, uint16_t val);
193 void (*put32)(struct buffer *input, uint32_t val);
194 void (*put64)(struct buffer *input, uint64_t val);
195};
196
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800197extern struct xdr xdr_le, xdr_be;
Aaron Durbin1240d292014-03-10 14:13:27 -0500198size_t bgets(struct buffer *input, void *output, size_t len);
199size_t bputs(struct buffer *b, const void *data, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700200
201#endif