Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 coresystems GmbH |
| 3 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 4 | * Copyright (C) 2012 Google, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 5 | * |
| 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 Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 20 | #ifndef __CBFSTOOL_COMMON_H |
| 21 | #define __CBFSTOOL_COMMON_H |
| 22 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 23 | #include <stdint.h> |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame^] | 24 | |
| 25 | /* Endianess */ |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 26 | #include "swab.h" |
Stefan Reinauer | 9da7570 | 2013-01-04 13:51:36 -0800 | [diff] [blame] | 27 | #ifndef __APPLE__ |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame^] | 28 | #define ntohl(x) (is_big_endian() ? (x) : swab32(x)) |
| 29 | #define htonl(x) (is_big_endian() ? (x) : swab32(x)) |
Stefan Reinauer | 9da7570 | 2013-01-04 13:51:36 -0800 | [diff] [blame] | 30 | #endif |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame^] | 31 | #define ntohll(x) (is_big_endian() ? (x) : swab64(x)) |
| 32 | #define htonll(x) (is_big_endian() ? (x) : swab64(x)) |
| 33 | extern int is_big_endian(void); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 34 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 35 | /* Message output */ |
| 36 | extern int verbose; |
| 37 | #define ERROR(x...) { fprintf(stderr, "E: " x); } |
| 38 | #define WARN(x...) { fprintf(stderr, "W: " x); } |
| 39 | #define LOG(x...) { fprintf(stderr, x); } |
| 40 | #define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); } |
| 41 | #define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); } |
| 42 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 43 | extern void *offset; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 44 | extern uint32_t romsize; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 45 | extern int host_bigendian; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 46 | extern uint32_t arch; |
| 47 | |
| 48 | const char *arch_to_string(uint32_t a); |
| 49 | uint32_t string_to_arch(const char *arch_string); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 50 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 51 | static inline void *phys_to_virt(uint32_t addr) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 52 | { |
| 53 | return offset + addr; |
| 54 | } |
| 55 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 56 | static inline uint32_t virt_to_phys(void *addr) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 57 | { |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 58 | return (unsigned long)(addr - offset) & 0xffffffff; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | #define ALIGN(val, by) (((val) + (by)-1)&~((by)-1)) |
| 62 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 63 | size_t getfilesize(const char *filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 64 | void *loadfile(const char *filename, uint32_t * romsize_p, void *content, |
| 65 | int place); |
| 66 | void *loadrom(const char *filename); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 67 | int writerom(const char *filename, void *start, uint32_t size); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 68 | |
| 69 | int iself(unsigned char *input); |
| 70 | |
| 71 | typedef void (*comp_func_ptr) (char *, int, char *, int *); |
| 72 | typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo; |
| 73 | |
| 74 | comp_func_ptr compression_function(comp_algo algo); |
| 75 | |
| 76 | uint64_t intfiletype(const char *name); |
| 77 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 78 | /* cbfs-mkpayload.c */ |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 79 | int parse_elf_to_payload(unsigned char *input, unsigned char **output, |
| 80 | comp_algo algo); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 81 | int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output, |
| 82 | int32_t input_size, uint32_t loadaddress, |
| 83 | uint32_t entrypoint, comp_algo algo); |
| 84 | /* cbfs-mkstage.c */ |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 85 | int parse_elf_to_stage(unsigned char *input, unsigned char **output, |
| 86 | comp_algo algo, uint32_t * location); |
| 87 | |
| 88 | void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, |
| 89 | uint32_t type, uint32_t * location); |
| 90 | |
| 91 | int create_cbfs_image(const char *romfile, uint32_t romsize, |
Stefan Reinauer | a90bd52 | 2012-08-15 16:05:50 -0700 | [diff] [blame] | 92 | const char *bootblock, uint32_t align, uint32_t offs); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 93 | |
| 94 | int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location); |
Stefan Reinauer | a90bd52 | 2012-08-15 16:05:50 -0700 | [diff] [blame] | 95 | int remove_file_from_cbfs(const char *filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 96 | void print_cbfs_directory(const char *filename); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 97 | int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath); |
Mathias Krause | 5c581c4 | 2012-07-17 21:11:59 +0200 | [diff] [blame] | 98 | int remove_file_from_cbfs(const char *filename); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 99 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 100 | uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, |
| 101 | const char *filename, uint32_t align); |
| 102 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 103 | void print_supported_filetypes(void); |
| 104 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 105 | #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) |
| 106 | |
| 107 | #endif |