2009-09-14 15:29:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 coresystems GmbH
|
|
|
|
* written by Patrick Georgi <patrick.georgi@coresystems.de>
|
2012-11-16 23:48:22 +01:00
|
|
|
* Copyright (C) 2012 Google, Inc.
|
2009-09-14 15:29:27 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
#ifndef __CBFSTOOL_COMMON_H
|
|
|
|
#define __CBFSTOOL_COMMON_H
|
|
|
|
|
2009-09-14 15:29:27 +02:00
|
|
|
#include <stdint.h>
|
2013-01-29 17:43:46 +01:00
|
|
|
#include <assert.h>
|
2013-01-28 08:53:34 +01:00
|
|
|
|
|
|
|
/* Endianess */
|
2011-10-21 23:24:57 +02:00
|
|
|
#include "swab.h"
|
2013-01-04 22:51:36 +01:00
|
|
|
#ifndef __APPLE__
|
2013-01-28 08:53:34 +01:00
|
|
|
#define ntohl(x) (is_big_endian() ? (x) : swab32(x))
|
|
|
|
#define htonl(x) (is_big_endian() ? (x) : swab32(x))
|
2013-01-04 22:51:36 +01:00
|
|
|
#endif
|
2013-01-28 08:53:34 +01:00
|
|
|
#define ntohll(x) (is_big_endian() ? (x) : swab64(x))
|
|
|
|
#define htonll(x) (is_big_endian() ? (x) : swab64(x))
|
|
|
|
extern int is_big_endian(void);
|
2009-09-14 15:29:27 +02:00
|
|
|
|
2013-01-28 07:39:43 +01:00
|
|
|
/* Message output */
|
|
|
|
extern int verbose;
|
|
|
|
#define ERROR(x...) { fprintf(stderr, "E: " x); }
|
|
|
|
#define WARN(x...) { fprintf(stderr, "W: " x); }
|
|
|
|
#define LOG(x...) { fprintf(stderr, x); }
|
|
|
|
#define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); }
|
|
|
|
#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
|
|
|
|
|
2013-01-29 17:43:46 +01:00
|
|
|
/* Buffer and file I/O */
|
|
|
|
struct buffer {
|
|
|
|
char *name;
|
|
|
|
char *data;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2014-03-05 05:01:12 +01:00
|
|
|
static inline void *buffer_get(const struct buffer *b)
|
|
|
|
{
|
|
|
|
return b->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t buffer_size(const struct buffer *b)
|
|
|
|
{
|
|
|
|
return b->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void buffer_set_size(struct buffer *b, size_t size)
|
|
|
|
{
|
|
|
|
b->size = size;
|
|
|
|
}
|
|
|
|
|
2014-03-27 04:57:55 +01:00
|
|
|
/* Initialize a buffer with the given constraints. */
|
|
|
|
static inline void buffer_init(struct buffer *b, char *name, void *data,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
b->name = name;
|
|
|
|
b->data = data;
|
|
|
|
b->size = size;
|
|
|
|
}
|
|
|
|
|
2014-03-05 05:01:12 +01:00
|
|
|
/*
|
|
|
|
* Splice a buffer into another buffer. If size is zero the entire buffer
|
|
|
|
* is spliced while if size is non-zero the buffer is spliced starting at
|
|
|
|
* offset for size bytes. Note that it's up to caller to bounds check.
|
|
|
|
*/
|
|
|
|
static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
|
|
|
|
size_t offset, size_t size)
|
|
|
|
{
|
2014-03-27 04:57:55 +01:00
|
|
|
buffer_init(dest, src->name, src->data, src->size);
|
2014-03-05 05:01:12 +01:00
|
|
|
if (size != 0) {
|
|
|
|
dest->data += offset;
|
|
|
|
buffer_set_size(dest, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
|
|
|
|
{
|
|
|
|
buffer_splice(dest, src, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void buffer_seek(struct buffer *b, size_t size)
|
|
|
|
{
|
|
|
|
b->size -= size;
|
|
|
|
b->data += size;
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:43:46 +01:00
|
|
|
/* Creates an empty memory buffer with given size.
|
|
|
|
* Returns 0 on success, otherwise non-zero. */
|
|
|
|
int buffer_create(struct buffer *buffer, size_t size, const char *name);
|
|
|
|
|
|
|
|
/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
|
|
|
|
int buffer_from_file(struct buffer *buffer, const char *filename);
|
|
|
|
|
|
|
|
/* Writes memory buffer content into file.
|
|
|
|
* Returns 0 on success, otherwise non-zero. */
|
|
|
|
int buffer_write_file(struct buffer *buffer, const char *filename);
|
|
|
|
|
|
|
|
/* Destroys a memory buffer. */
|
|
|
|
void buffer_delete(struct buffer *buffer);
|
|
|
|
|
2013-11-13 23:34:57 +01:00
|
|
|
const char *arch_to_string(uint32_t a);
|
2012-11-16 23:48:22 +01:00
|
|
|
uint32_t string_to_arch(const char *arch_string);
|
2009-09-14 15:29:27 +02:00
|
|
|
|
|
|
|
#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
|
|
|
|
|
|
|
|
typedef void (*comp_func_ptr) (char *, int, char *, int *);
|
|
|
|
typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
|
|
|
|
|
|
|
|
comp_func_ptr compression_function(comp_algo algo);
|
|
|
|
|
|
|
|
uint64_t intfiletype(const char *name);
|
|
|
|
|
2013-01-28 08:04:30 +01:00
|
|
|
/* cbfs-mkpayload.c */
|
2013-01-29 08:22:11 +01:00
|
|
|
int parse_elf_to_payload(const struct buffer *input,
|
2014-02-03 05:37:28 +01:00
|
|
|
struct buffer *output, uint32_t arch, comp_algo algo);
|
2013-02-05 00:39:13 +01:00
|
|
|
int parse_fv_to_payload(const struct buffer *input,
|
|
|
|
struct buffer *output, comp_algo algo);
|
2013-08-27 20:22:21 +02:00
|
|
|
int parse_bzImage_to_payload(const struct buffer *input,
|
|
|
|
struct buffer *output, const char *initrd,
|
|
|
|
char *cmdline, comp_algo algo);
|
2013-01-29 08:22:11 +01:00
|
|
|
int parse_flat_binary_to_payload(const struct buffer *input,
|
|
|
|
struct buffer *output,
|
|
|
|
uint32_t loadaddress,
|
|
|
|
uint32_t entrypoint,
|
|
|
|
comp_algo algo);
|
2013-01-28 08:04:30 +01:00
|
|
|
/* cbfs-mkstage.c */
|
2013-01-29 08:22:11 +01:00
|
|
|
int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
|
2014-02-03 05:37:28 +01:00
|
|
|
uint32_t arch, comp_algo algo, uint32_t *location);
|
2009-09-14 15:29:27 +02:00
|
|
|
|
2010-04-24 23:24:06 +02:00
|
|
|
void print_supported_filetypes(void);
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
|
2013-12-03 20:13:35 +01:00
|
|
|
/* lzma/lzma.c */
|
|
|
|
void do_lzma_compress(char *in, int in_len, char *out, int *out_len);
|
|
|
|
void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len);
|
|
|
|
/* xdr.c */
|
|
|
|
struct xdr {
|
2014-03-05 23:38:26 +01:00
|
|
|
uint8_t (*get8)(struct buffer *input);
|
2013-12-03 20:13:35 +01:00
|
|
|
uint16_t (*get16)(struct buffer *input);
|
|
|
|
uint32_t (*get32)(struct buffer *input);
|
|
|
|
uint64_t (*get64)(struct buffer *input);
|
2014-03-05 23:38:26 +01:00
|
|
|
void (*put8)(struct buffer *input, uint8_t val);
|
2013-12-03 20:13:35 +01:00
|
|
|
void (*put16)(struct buffer *input, uint16_t val);
|
|
|
|
void (*put32)(struct buffer *input, uint32_t val);
|
|
|
|
void (*put64)(struct buffer *input, uint64_t val);
|
|
|
|
};
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
/* xdr.c */
|
2013-12-03 20:13:35 +01:00
|
|
|
extern struct xdr xdr_le, xdr_be;
|
2014-03-10 20:13:27 +01:00
|
|
|
size_t bgets(struct buffer *input, void *output, size_t len);
|
|
|
|
size_t bputs(struct buffer *b, const void *data, size_t len);
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
#endif
|