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 | |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 51 | static inline void *buffer_get(const struct buffer *b) |
| 52 | { |
| 53 | return b->data; |
| 54 | } |
| 55 | |
| 56 | static inline size_t buffer_size(const struct buffer *b) |
| 57 | { |
| 58 | return b->size; |
| 59 | } |
| 60 | |
| 61 | static inline void buffer_set_size(struct buffer *b, size_t size) |
| 62 | { |
| 63 | b->size = size; |
| 64 | } |
| 65 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 66 | /* Initialize a buffer with the given constraints. */ |
| 67 | static inline void buffer_init(struct buffer *b, char *name, void *data, |
| 68 | size_t size) |
| 69 | { |
| 70 | b->name = name; |
| 71 | b->data = data; |
| 72 | b->size = size; |
| 73 | } |
| 74 | |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 75 | /* |
| 76 | * Splice a buffer into another buffer. If size is zero the entire buffer |
| 77 | * is spliced while if size is non-zero the buffer is spliced starting at |
| 78 | * offset for size bytes. Note that it's up to caller to bounds check. |
| 79 | */ |
| 80 | static inline void buffer_splice(struct buffer *dest, const struct buffer *src, |
| 81 | size_t offset, size_t size) |
| 82 | { |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 83 | buffer_init(dest, src->name, src->data, src->size); |
Aaron Durbin | 6e8c279 | 2014-03-04 22:01:12 -0600 | [diff] [blame] | 84 | if (size != 0) { |
| 85 | dest->data += offset; |
| 86 | buffer_set_size(dest, size); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static inline void buffer_clone(struct buffer *dest, const struct buffer *src) |
| 91 | { |
| 92 | buffer_splice(dest, src, 0, 0); |
| 93 | } |
| 94 | |
| 95 | static inline void buffer_seek(struct buffer *b, size_t size) |
| 96 | { |
| 97 | b->size -= size; |
| 98 | b->data += size; |
| 99 | } |
| 100 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame] | 101 | /* Creates an empty memory buffer with given size. |
| 102 | * Returns 0 on success, otherwise non-zero. */ |
| 103 | int buffer_create(struct buffer *buffer, size_t size, const char *name); |
| 104 | |
| 105 | /* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ |
| 106 | int buffer_from_file(struct buffer *buffer, const char *filename); |
| 107 | |
| 108 | /* Writes memory buffer content into file. |
| 109 | * Returns 0 on success, otherwise non-zero. */ |
| 110 | int buffer_write_file(struct buffer *buffer, const char *filename); |
| 111 | |
| 112 | /* Destroys a memory buffer. */ |
| 113 | void buffer_delete(struct buffer *buffer); |
| 114 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 115 | uint32_t string_to_arch(const char *arch_string); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 116 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 117 | #define ALIGN(val, by) (((val) + (by)-1)&~((by)-1)) |
| 118 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 119 | typedef void (*comp_func_ptr) (char *, int, char *, int *); |
| 120 | typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo; |
| 121 | |
| 122 | comp_func_ptr compression_function(comp_algo algo); |
| 123 | |
| 124 | uint64_t intfiletype(const char *name); |
| 125 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 126 | /* cbfs-mkpayload.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 127 | int parse_elf_to_payload(const struct buffer *input, |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 128 | struct buffer *output, uint32_t arch, comp_algo algo); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 129 | int parse_fv_to_payload(const struct buffer *input, |
| 130 | struct buffer *output, comp_algo algo); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 131 | int parse_bzImage_to_payload(const struct buffer *input, |
| 132 | struct buffer *output, const char *initrd, |
| 133 | char *cmdline, comp_algo algo); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 134 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 135 | struct buffer *output, |
| 136 | uint32_t loadaddress, |
| 137 | uint32_t entrypoint, |
| 138 | comp_algo algo); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 139 | /* cbfs-mkstage.c */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 140 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 141 | uint32_t arch, comp_algo algo, uint32_t *location); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 142 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 143 | void print_supported_filetypes(void); |
| 144 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 145 | #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 146 | /* lzma/lzma.c */ |
| 147 | void do_lzma_compress(char *in, int in_len, char *out, int *out_len); |
| 148 | void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len); |
| 149 | /* xdr.c */ |
| 150 | struct xdr { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 151 | uint8_t (*get8)(struct buffer *input); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 152 | uint16_t (*get16)(struct buffer *input); |
| 153 | uint32_t (*get32)(struct buffer *input); |
| 154 | uint64_t (*get64)(struct buffer *input); |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 155 | void (*put8)(struct buffer *input, uint8_t val); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 156 | void (*put16)(struct buffer *input, uint16_t val); |
| 157 | void (*put32)(struct buffer *input, uint32_t val); |
| 158 | void (*put64)(struct buffer *input, uint64_t val); |
| 159 | }; |
| 160 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 161 | /* xdr.c */ |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 162 | extern struct xdr xdr_le, xdr_be; |
Aaron Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame] | 163 | size_t bgets(struct buffer *input, void *output, size_t len); |
| 164 | size_t bputs(struct buffer *b, const void *data, size_t len); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 165 | |
| 166 | #endif |