blob: 0284742cb4ce6642072564c9afd7571c09b8a9de [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
Patrick Georgib7b56dd82009-09-14 13:29:27 +000023#include <stdint.h>
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080024#include <assert.h>
Hung-Te Lin332795c2013-01-28 15:53:34 +080025
26/* Endianess */
Stefan Reinauera1e48242011-10-21 14:24:57 -070027#include "swab.h"
Stefan Reinauer9da75702013-01-04 13:51:36 -080028#ifndef __APPLE__
Hung-Te Lin332795c2013-01-28 15:53:34 +080029#define ntohl(x) (is_big_endian() ? (x) : swab32(x))
30#define htonl(x) (is_big_endian() ? (x) : swab32(x))
Stefan Reinauer9da75702013-01-04 13:51:36 -080031#endif
Hung-Te Lin332795c2013-01-28 15:53:34 +080032#define ntohll(x) (is_big_endian() ? (x) : swab64(x))
33#define htonll(x) (is_big_endian() ? (x) : swab64(x))
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010034int is_big_endian(void);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000035
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080036/* Message output */
37extern int verbose;
38#define ERROR(x...) { fprintf(stderr, "E: " x); }
39#define WARN(x...) { fprintf(stderr, "W: " x); }
40#define LOG(x...) { fprintf(stderr, x); }
41#define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); }
42#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
43
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010044/* Helpers */
45#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
46#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
47
48#define unused __attribute__((unused))
49
Julius Wernerefcee762014-11-10 13:14:24 -080050static inline uint32_t align_up(uint32_t value, uint32_t align)
51{
52 if (value % align)
53 value += align - (value % align);
54 return value;
55}
56
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080057/* Buffer and file I/O */
58struct buffer {
59 char *name;
60 char *data;
61 size_t size;
62};
63
Aaron Durbin6e8c2792014-03-04 22:01:12 -060064static inline void *buffer_get(const struct buffer *b)
65{
66 return b->data;
67}
68
69static inline size_t buffer_size(const struct buffer *b)
70{
71 return b->size;
72}
73
74static inline void buffer_set_size(struct buffer *b, size_t size)
75{
76 b->size = size;
77}
78
Aaron Durbin4f3bb802014-03-26 22:57:55 -050079/* Initialize a buffer with the given constraints. */
80static inline void buffer_init(struct buffer *b, char *name, void *data,
81 size_t size)
82{
83 b->name = name;
84 b->data = data;
85 b->size = size;
86}
87
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010088/* Splice a buffer into another buffer. If size is zero the entire buffer
Aaron Durbin6e8c2792014-03-04 22:01:12 -060089 * is spliced while if size is non-zero the buffer is spliced starting at
90 * offset for size bytes. Note that it's up to caller to bounds check.
91 */
92static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
93 size_t offset, size_t size)
94{
Aaron Durbin4f3bb802014-03-26 22:57:55 -050095 buffer_init(dest, src->name, src->data, src->size);
Aaron Durbin6e8c2792014-03-04 22:01:12 -060096 if (size != 0) {
97 dest->data += offset;
98 buffer_set_size(dest, size);
99 }
100}
101
102static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
103{
104 buffer_splice(dest, src, 0, 0);
105}
106
107static inline void buffer_seek(struct buffer *b, size_t size)
108{
109 b->size -= size;
110 b->data += size;
111}
112
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +0800113/* Creates an empty memory buffer with given size.
114 * Returns 0 on success, otherwise non-zero. */
115int buffer_create(struct buffer *buffer, size_t size, const char *name);
116
117/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
118int buffer_from_file(struct buffer *buffer, const char *filename);
119
120/* Writes memory buffer content into file.
121 * Returns 0 on success, otherwise non-zero. */
122int buffer_write_file(struct buffer *buffer, const char *filename);
123
124/* Destroys a memory buffer. */
125void buffer_delete(struct buffer *buffer);
126
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800127const char *arch_to_string(uint32_t a);
David Hendricks90ca3b62012-11-16 14:48:22 -0800128uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000129
Gabe Blackdbd006b2014-02-20 23:38:49 -0800130typedef int (*comp_func_ptr) (char *, int, char *, int *);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000131typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
132
133comp_func_ptr compression_function(comp_algo algo);
134
135uint64_t intfiletype(const char *name);
136
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800137/* cbfs-mkpayload.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800138int parse_elf_to_payload(const struct buffer *input,
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600139 struct buffer *output, uint32_t arch, comp_algo algo);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800140int parse_fv_to_payload(const struct buffer *input,
141 struct buffer *output, comp_algo algo);
Patrick Georgide36d332013-08-27 20:22:21 +0200142int parse_bzImage_to_payload(const struct buffer *input,
143 struct buffer *output, const char *initrd,
144 char *cmdline, comp_algo algo);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800145int parse_flat_binary_to_payload(const struct buffer *input,
146 struct buffer *output,
147 uint32_t loadaddress,
148 uint32_t entrypoint,
149 comp_algo algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800150/* cbfs-mkstage.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800151int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Furquan Shaikh405304a2014-10-30 11:44:20 -0700152 uint32_t arch, comp_algo algo, uint32_t *location,
153 const char *ignore_section);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000154
Stefan Reinauer07040582010-04-24 21:24:06 +0000155void print_supported_filetypes(void);
156
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800157/* lzma/lzma.c */
Gabe Blackdbd006b2014-02-20 23:38:49 -0800158int do_lzma_compress(char *in, int in_len, char *out, int *out_len);
159int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len);
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100160
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800161/* xdr.c */
162struct xdr {
Aaron Durbin01650042014-03-05 16:38:26 -0600163 uint8_t (*get8)(struct buffer *input);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800164 uint16_t (*get16)(struct buffer *input);
165 uint32_t (*get32)(struct buffer *input);
166 uint64_t (*get64)(struct buffer *input);
Aaron Durbin01650042014-03-05 16:38:26 -0600167 void (*put8)(struct buffer *input, uint8_t val);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800168 void (*put16)(struct buffer *input, uint16_t val);
169 void (*put32)(struct buffer *input, uint32_t val);
170 void (*put64)(struct buffer *input, uint64_t val);
171};
172
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800173extern struct xdr xdr_le, xdr_be;
Aaron Durbin1240d292014-03-10 14:13:27 -0500174size_t bgets(struct buffer *input, void *output, size_t len);
175size_t bputs(struct buffer *b, const void *data, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700176
177#endif