summaryrefslogtreecommitdiff
path: root/futility/futility.h
blob: 03b4cb573c3bf3faf0c6f34df358eb12130543da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef VBOOT_REFERENCE_FUTILITY_H_
#define VBOOT_REFERENCE_FUTILITY_H_
#include <stdint.h>

#include "vboot_common.h"
#include "gbb_header.h"

/* This program */
#define MYNAME "futility"

/* Here's a structure to define the commands that futility implements. */
struct futil_cmd_t {
	const char *const name;
	int (*const handler) (int argc, char **argv);
	const char *const shorthelp;
};

/*
 * Macro to define a command.
 *
 * This defines the struct, then puts a pointer to it in a separate section.
 * We'll have a linker script to gather the pointers up later, so we can refer
 * to them without explictly declaring every function in a header somewhere.
 */
#define DECLARE_FUTIL_COMMAND(NAME, HANDLER, SHORTHELP)           \
        const struct futil_cmd_t __cmd_##NAME = {                 \
                .name = #NAME,                                    \
                .handler = HANDLER,                               \
                .shorthelp = SHORTHELP                            \
        };

/* This is the list of pointers to all commands. */
extern const struct futil_cmd_t *const futil_cmds[];

/* Size of an array */
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
#endif

/* Test an important condition at compile time, not run time */
#ifndef BUILD_ASSERT
#define _BA1_(cond, line) \
        extern int __build_assertion_ ## line[1 - 2*!(cond)] \
        __attribute__ ((unused))
#define _BA0_(c, x) _BA1_(c, x)
#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
#endif

/* Fatal internal stupidness */
#ifndef DIE
#define DIE do {							\
		fprintf(stderr, MYNAME ": internal error at %s:%d\n",	\
			__FILE__, __LINE__);				\
		exit(1);						\
	} while (0)
#endif


/* Returns true if this looks enough like a GBB header to proceed. */
int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len);

/*
 * Returns true if the gbb header is valid (and optionally updates *maxlen).
 * This doesn't verify the contents, though.
 */
int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len,
			   uint32_t *maxlen);

/* Copies a file or dies with an error message */
void copy_file_or_die(const char *infile, const char *outfile);

/* Wrapper for mmap/munmap. Returns 0 on success. Skips stupidly large files. */
int map_it(int fd, int writeable, void **buf, uint32_t *len);
int unmap_it(int fd, int writeable, void *buf, uint32_t len);

#endif /* VBOOT_REFERENCE_FUTILITY_H_ */