blob: bd5ef65f16824bbe275be3da4fd109deb1cea545 [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>
Stefan Reinauera1e48242011-10-21 14:24:57 -070024#include "swab.h"
Stefan Reinauer9da75702013-01-04 13:51:36 -080025#ifndef __APPLE__
Stefan Reinauera1e48242011-10-21 14:24:57 -070026#define ntohl(x) (host_bigendian?(x):swab32(x))
27#define htonl(x) (host_bigendian?(x):swab32(x))
Stefan Reinauer9da75702013-01-04 13:51:36 -080028#endif
Stefan Reinauera1e48242011-10-21 14:24:57 -070029#define ntohll(x) (host_bigendian?(x):swab64(x))
30#define htonll(x) (host_bigendian?(x):swab64(x))
Patrick Georgib7b56dd82009-09-14 13:29:27 +000031
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080032/* Message output */
33extern int verbose;
34#define ERROR(x...) { fprintf(stderr, "E: " x); }
35#define WARN(x...) { fprintf(stderr, "W: " x); }
36#define LOG(x...) { fprintf(stderr, x); }
37#define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); }
38#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
39
Patrick Georgib7b56dd82009-09-14 13:29:27 +000040extern void *offset;
Stefan Reinauer63217582012-10-29 16:52:36 -070041extern uint32_t romsize;
Stefan Reinauera1e48242011-10-21 14:24:57 -070042extern int host_bigendian;
David Hendricks90ca3b62012-11-16 14:48:22 -080043extern uint32_t arch;
44
45const char *arch_to_string(uint32_t a);
46uint32_t string_to_arch(const char *arch_string);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000047
Stefan Reinauera1e48242011-10-21 14:24:57 -070048static inline void *phys_to_virt(uint32_t addr)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000049{
50 return offset + addr;
51}
52
Stefan Reinauera1e48242011-10-21 14:24:57 -070053static inline uint32_t virt_to_phys(void *addr)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000054{
Stefan Reinauer853270a2009-09-22 15:55:01 +000055 return (unsigned long)(addr - offset) & 0xffffffff;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000056}
57
58#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
59
Stefan Reinauer63217582012-10-29 16:52:36 -070060size_t getfilesize(const char *filename);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000061void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
62 int place);
63void *loadrom(const char *filename);
Stefan Reinauer9bb043852010-06-24 13:37:59 +000064int writerom(const char *filename, void *start, uint32_t size);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000065
66int iself(unsigned char *input);
67
68typedef void (*comp_func_ptr) (char *, int, char *, int *);
69typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
70
71comp_func_ptr compression_function(comp_algo algo);
72
73uint64_t intfiletype(const char *name);
74
Hung-Te Lin05dccae2013-01-28 15:04:30 +080075/* cbfs-mkpayload.c */
Patrick Georgib7b56dd82009-09-14 13:29:27 +000076int parse_elf_to_payload(unsigned char *input, unsigned char **output,
77 comp_algo algo);
Hung-Te Lin05dccae2013-01-28 15:04:30 +080078int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output,
79 int32_t input_size, uint32_t loadaddress,
80 uint32_t entrypoint, comp_algo algo);
81/* cbfs-mkstage.c */
Patrick Georgib7b56dd82009-09-14 13:29:27 +000082int parse_elf_to_stage(unsigned char *input, unsigned char **output,
83 comp_algo algo, uint32_t * location);
84
85void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
86 uint32_t type, uint32_t * location);
87
88int create_cbfs_image(const char *romfile, uint32_t romsize,
Stefan Reinauera90bd522012-08-15 16:05:50 -070089 const char *bootblock, uint32_t align, uint32_t offs);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000090
91int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location);
Stefan Reinauera90bd522012-08-15 16:05:50 -070092int remove_file_from_cbfs(const char *filename);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000093void print_cbfs_directory(const char *filename);
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +000094int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath);
Mathias Krause5c581c42012-07-17 21:11:59 +020095int remove_file_from_cbfs(const char *filename);
Stefan Reinauer853270a2009-09-22 15:55:01 +000096
Patrick Georgi0da38dd2009-11-09 17:18:02 +000097uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
98 const char *filename, uint32_t align);
99
Stefan Reinauer07040582010-04-24 21:24:06 +0000100void print_supported_filetypes(void);
101
Stefan Reinauer63217582012-10-29 16:52:36 -0700102#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
103
104#endif