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 | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 24 | #include <assert.h> |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 25 | |
| 26 | /* Endianess */ |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 27 | #include "swab.h" |
Stefan Reinauer | 9da7570 | 2013-01-04 13:51:36 -0800 | [diff] [blame] | 28 | #ifndef __APPLE__ |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 29 | #define ntohl(x) (is_big_endian() ? (x) : swab32(x)) |
| 30 | #define htonl(x) (is_big_endian() ? (x) : swab32(x)) |
Stefan Reinauer | 9da7570 | 2013-01-04 13:51:36 -0800 | [diff] [blame] | 31 | #endif |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 32 | #define ntohll(x) (is_big_endian() ? (x) : swab64(x)) |
| 33 | #define htonll(x) (is_big_endian() ? (x) : swab64(x)) |
| 34 | extern int is_big_endian(void); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 35 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 36 | /* Message output */ |
| 37 | extern 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 Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 44 | /* Buffer and file I/O */ |
| 45 | struct 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. */ |
| 53 | int 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. */ |
| 56 | int 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. */ |
| 60 | int buffer_write_file(struct buffer *buffer, const char *filename); |
| 61 | |
| 62 | /* Destroys a memory buffer. */ |
| 63 | void buffer_delete(struct buffer *buffer); |
| 64 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 65 | extern void *offset; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 66 | extern uint32_t romsize; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 67 | extern int host_bigendian; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 68 | extern uint32_t arch; |
| 69 | |
| 70 | const char *arch_to_string(uint32_t a); |
| 71 | uint32_t string_to_arch(const char *arch_string); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 72 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 73 | static inline void *phys_to_virt(uint32_t addr) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 74 | { |
| 75 | return offset + addr; |
| 76 | } |
| 77 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 78 | static inline uint32_t virt_to_phys(void *addr) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 79 | { |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 80 | return (unsigned long)(addr - offset) & 0xffffffff; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #define ALIGN(val, by) (((val) + (by)-1)&~((by)-1)) |
| 84 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 85 | size_t getfilesize(const char *filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 86 | void *loadfile(const char *filename, uint32_t * romsize_p, void *content, |
| 87 | int place); |
| 88 | void *loadrom(const char *filename); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 89 | int writerom(const char *filename, void *start, uint32_t size); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 90 | |
| 91 | int iself(unsigned char *input); |
| 92 | |
| 93 | typedef void (*comp_func_ptr) (char *, int, char *, int *); |
| 94 | typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo; |
| 95 | |
| 96 | comp_func_ptr compression_function(comp_algo algo); |
| 97 | |
| 98 | uint64_t intfiletype(const char *name); |
| 99 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 100 | /* cbfs-mkpayload.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 101 | int parse_elf_to_payload(const struct buffer *input, |
| 102 | struct buffer *output, comp_algo algo); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 103 | int parse_fv_to_payload(const struct buffer *input, |
| 104 | struct buffer *output, comp_algo algo); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 105 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 106 | struct buffer *output, |
| 107 | uint32_t loadaddress, |
| 108 | uint32_t entrypoint, |
| 109 | comp_algo algo); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 110 | /* cbfs-mkstage.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 111 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
| 112 | comp_algo algo, uint32_t *location); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 113 | |
| 114 | void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, |
| 115 | uint32_t type, uint32_t * location); |
| 116 | |
| 117 | int create_cbfs_image(const char *romfile, uint32_t romsize, |
Stefan Reinauer | a90bd52 | 2012-08-15 16:05:50 -0700 | [diff] [blame] | 118 | const char *bootblock, uint32_t align, uint32_t offs); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 119 | |
| 120 | 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] | 121 | int remove_file_from_cbfs(const char *filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 122 | void print_cbfs_directory(const char *filename); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 123 | 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] | 124 | int remove_file_from_cbfs(const char *filename); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 125 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 126 | uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, |
| 127 | const char *filename, uint32_t align); |
| 128 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 129 | void print_supported_filetypes(void); |
| 130 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 131 | #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) |
| 132 | |
| 133 | #endif |