blob: ed75a7fd028b429e65c030fd85417d4475ce04a4 [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
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080065extern void *cbfstool_offset;
Stefan Reinauer63217582012-10-29 16:52:36 -070066extern uint32_t romsize;
Stefan Reinauera1e48242011-10-21 14:24:57 -070067extern int host_bigendian;
David Hendricks90ca3b62012-11-16 14:48:22 -080068extern uint32_t arch;
69
70const char *arch_to_string(uint32_t a);
71uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000072
Stefan Reinauera1e48242011-10-21 14:24:57 -070073static inline void *phys_to_virt(uint32_t addr)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000074{
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080075 return cbfstool_offset + addr;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000076}
77
Stefan Reinauera1e48242011-10-21 14:24:57 -070078static inline uint32_t virt_to_phys(void *addr)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000079{
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080080 return (unsigned long)(addr - cbfstool_offset) & 0xffffffff;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000081}
82
83#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
84
Stefan Reinauer63217582012-10-29 16:52:36 -070085size_t getfilesize(const char *filename);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000086void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
87 int place);
88void *loadrom(const char *filename);
Stefan Reinauer9bb043852010-06-24 13:37:59 +000089int writerom(const char *filename, void *start, uint32_t size);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000090
91int iself(unsigned char *input);
92
93typedef void (*comp_func_ptr) (char *, int, char *, int *);
94typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
95
96comp_func_ptr compression_function(comp_algo algo);
97
98uint64_t intfiletype(const char *name);
99
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800100/* cbfs-mkpayload.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800101int parse_elf_to_payload(const struct buffer *input,
102 struct buffer *output, comp_algo algo);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800103int parse_fv_to_payload(const struct buffer *input,
104 struct buffer *output, comp_algo algo);
Patrick Georgide36d332013-08-27 20:22:21 +0200105int parse_bzImage_to_payload(const struct buffer *input,
106 struct buffer *output, const char *initrd,
107 char *cmdline, comp_algo algo);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800108int parse_flat_binary_to_payload(const struct buffer *input,
109 struct buffer *output,
110 uint32_t loadaddress,
111 uint32_t entrypoint,
112 comp_algo algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800113/* cbfs-mkstage.c */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800114int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
115 comp_algo algo, uint32_t *location);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000116
117void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
118 uint32_t type, uint32_t * location);
119
120int create_cbfs_image(const char *romfile, uint32_t romsize,
Stefan Reinauera90bd522012-08-15 16:05:50 -0700121 const char *bootblock, uint32_t align, uint32_t offs);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000122
123int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location);
Stefan Reinauera90bd522012-08-15 16:05:50 -0700124int remove_file_from_cbfs(const char *filename);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000125void print_cbfs_directory(const char *filename);
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000126int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000127
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000128uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
129 const char *filename, uint32_t align);
130
Stefan Reinauer07040582010-04-24 21:24:06 +0000131void print_supported_filetypes(void);
132
Stefan Reinauer63217582012-10-29 16:52:36 -0700133#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800134/* lzma/lzma.c */
135void do_lzma_compress(char *in, int in_len, char *out, int *out_len);
136void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len);
137/* xdr.c */
138struct xdr {
139 uint16_t (*get16)(struct buffer *input);
140 uint32_t (*get32)(struct buffer *input);
141 uint64_t (*get64)(struct buffer *input);
142 void (*put16)(struct buffer *input, uint16_t val);
143 void (*put32)(struct buffer *input, uint32_t val);
144 void (*put64)(struct buffer *input, uint64_t val);
145};
146
147/* common.c */
148
149int find_master_header(void *romarea, size_t size);
150void recalculate_rom_geometry(void *romarea);
151const char *strfiletype(uint32_t number);
152
153/* cbfs_image.c */
154uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value);
155const char *get_cbfs_entry_type_name(uint32_t type);
156uint32_t get_cbfs_compression(const char *name, uint32_t unknown);
157
158extern struct xdr xdr_le, xdr_be;
Stefan Reinauer63217582012-10-29 16:52:36 -0700159
160#endif