blob: 16e75f9cf9a2e8ac3b34321572e7118193357e42 [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))
34extern int 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
Hung-Te Lin3cfacbf2013-01-30 00:43:46 +080044/* Buffer and file I/O */
45struct buffer {
46 char *name;
47 char *data;
48 size_t size;
49};
50
51/* Creates an empty memory buffer with given size.
52 * Returns 0 on success, otherwise non-zero. */
53int buffer_create(struct buffer *buffer, size_t size, const char *name);
54
55/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
56int buffer_from_file(struct buffer *buffer, const char *filename);
57
58/* Writes memory buffer content into file.
59 * Returns 0 on success, otherwise non-zero. */
60int buffer_write_file(struct buffer *buffer, const char *filename);
61
62/* Destroys a memory buffer. */
63void buffer_delete(struct buffer *buffer);
64
David Hendricks90ca3b62012-11-16 14:48:22 -080065uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000066
Patrick Georgib7b56dd82009-09-14 13:29:27 +000067#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
68
Patrick Georgib7b56dd82009-09-14 13:29:27 +000069int iself(unsigned char *input);
70
71typedef void (*comp_func_ptr) (char *, int, char *, int *);
72typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
73
74comp_func_ptr compression_function(comp_algo algo);
75
76uint64_t intfiletype(const char *name);
77
Hung-Te Lin05dccae2013-01-28 15:04:30 +080078/* cbfs-mkpayload.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080079int parse_elf_to_payload(const struct buffer *input,
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060080 struct buffer *output, uint32_t arch, comp_algo algo);
Stefan Reinauer543a6822013-02-04 15:39:13 -080081int parse_fv_to_payload(const struct buffer *input,
82 struct buffer *output, comp_algo algo);
Patrick Georgide36d332013-08-27 20:22:21 +020083int parse_bzImage_to_payload(const struct buffer *input,
84 struct buffer *output, const char *initrd,
85 char *cmdline, comp_algo algo);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080086int parse_flat_binary_to_payload(const struct buffer *input,
87 struct buffer *output,
88 uint32_t loadaddress,
89 uint32_t entrypoint,
90 comp_algo algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +080091/* cbfs-mkstage.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080092int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060093 uint32_t arch, comp_algo algo, uint32_t *location);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000094
Stefan Reinauer07040582010-04-24 21:24:06 +000095void print_supported_filetypes(void);
96
Stefan Reinauer63217582012-10-29 16:52:36 -070097#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080098/* lzma/lzma.c */
99void do_lzma_compress(char *in, int in_len, char *out, int *out_len);
100void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len);
101/* xdr.c */
102struct xdr {
Aaron Durbin01650042014-03-05 16:38:26 -0600103 uint8_t (*get8)(struct buffer *input);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800104 uint16_t (*get16)(struct buffer *input);
105 uint32_t (*get32)(struct buffer *input);
106 uint64_t (*get64)(struct buffer *input);
Aaron Durbin01650042014-03-05 16:38:26 -0600107 void (*put8)(struct buffer *input, uint8_t val);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800108 void (*put16)(struct buffer *input, uint16_t val);
109 void (*put32)(struct buffer *input, uint32_t val);
110 void (*put64)(struct buffer *input, uint64_t val);
111};
112
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800113/* xdr.c */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800114extern struct xdr xdr_le, xdr_be;
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800115int bgets(struct buffer *input, void *output, size_t len);
Stefan Reinauer63217582012-10-29 16:52:36 -0700116
117#endif