blob: 52c007ccedb2252bc10866536e7dfa93c98f8729 [file] [log] [blame]
Bill Richardsonfeb25182013-03-07 12:54:29 -08001/*
2 * Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
Bill Richardsonfeb25182013-03-07 12:54:29 -08006#ifndef VBOOT_REFERENCE_FUTILITY_H_
7#define VBOOT_REFERENCE_FUTILITY_H_
Bill Richardsoncf6e78d2014-08-27 15:50:25 -07008#include <stdint.h>
9
10#include "vboot_common.h"
11#include "gbb_header.h"
Bill Richardsone192e7f2014-09-23 12:49:26 -070012#include "host_key.h"
Bill Richardsoncf6e78d2014-08-27 15:50:25 -070013
14/* This program */
15#define MYNAME "futility"
Bill Richardsonfeb25182013-03-07 12:54:29 -080016
Bill Richardson7d028c42014-07-12 10:46:56 -070017/* Here's a structure to define the commands that futility implements. */
18struct futil_cmd_t {
Bill Richardson31d95c22014-08-24 22:07:17 -070019 const char *const name;
20 int (*const handler) (int argc, char **argv);
21 const char *const shorthelp;
Bill Richardson779796f2014-09-23 11:47:40 -070022 void (*longhelp) (const char *cmd);
Bill Richardson7d028c42014-07-12 10:46:56 -070023};
Bill Richardsonfeb25182013-03-07 12:54:29 -080024
Bill Richardson779796f2014-09-23 11:47:40 -070025/* Macro to define a command */
26#define DECLARE_FUTIL_COMMAND(NAME, HANDLER, SHORTHELP, LONGHELP) \
27 const struct futil_cmd_t __cmd_##NAME = { \
28 .name = #NAME, \
29 .handler = HANDLER, \
30 .shorthelp = SHORTHELP, \
31 .longhelp = LONGHELP, \
32 }
Bill Richardsonfeb25182013-03-07 12:54:29 -080033
Bill Richardson7d028c42014-07-12 10:46:56 -070034/* This is the list of pointers to all commands. */
Bill Richardson31d95c22014-08-24 22:07:17 -070035extern const struct futil_cmd_t *const futil_cmds[];
Bill Richardsonfeb25182013-03-07 12:54:29 -080036
Bill Richardsone1550442014-07-17 11:32:17 -070037/* Size of an array */
38#ifndef ARRAY_SIZE
39#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
40#endif
41
42/* Test an important condition at compile time, not run time */
43#ifndef BUILD_ASSERT
44#define _BA1_(cond, line) \
Bill Richardson779796f2014-09-23 11:47:40 -070045 extern int __build_assertion_ ## line[1 - 2*!(cond)] \
46 __attribute__ ((unused))
Bill Richardsone1550442014-07-17 11:32:17 -070047#define _BA0_(c, x) _BA1_(c, x)
48#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
49#endif
50
Bill Richardsoncf6e78d2014-08-27 15:50:25 -070051/* Fatal internal stupidness */
52#ifndef DIE
53#define DIE do { \
54 fprintf(stderr, MYNAME ": internal error at %s:%d\n", \
55 __FILE__, __LINE__); \
56 exit(1); \
57 } while (0)
58#endif
59
Bill Richardsone192e7f2014-09-23 12:49:26 -070060/* Debug output (off by default) */
61extern int debugging_enabled;
62void Debug(const char *format, ...);
Bill Richardsoncf6e78d2014-08-27 15:50:25 -070063
64/* Returns true if this looks enough like a GBB header to proceed. */
65int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len);
66
67/*
68 * Returns true if the gbb header is valid (and optionally updates *maxlen).
69 * This doesn't verify the contents, though.
70 */
71int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len,
72 uint32_t *maxlen);
73
Bill Richardson6df3e332014-10-02 18:50:33 -070074/* For GBB v1.2 and later, update the hwid_digest */
75void update_hwid_digest(GoogleBinaryBlockHeader *gbb);
76
77/* For GBB v1.2 and later, print the stored digest of the HWID (and whether
78 * it's correct). Return true if it is correct. */
79int print_hwid_digest(GoogleBinaryBlockHeader *gbb,
80 const char *banner, const char *footer);
81
Bill Richardson15dc6fc2014-09-02 14:45:44 -070082/* Copies a file or dies with an error message */
Bill Richardsone192e7f2014-09-23 12:49:26 -070083void futil_copy_file_or_die(const char *infile, const char *outfile);
Bill Richardson15dc6fc2014-09-02 14:45:44 -070084
85/* Wrapper for mmap/munmap. Returns 0 on success. Skips stupidly large files. */
Bill Richardsone192e7f2014-09-23 12:49:26 -070086#define MAP_RO 0
87#define MAP_RW 1
88int futil_map_file(int fd, int writeable, uint8_t **buf, uint32_t *len);
89int futil_unmap_file(int fd, int writeable, uint8_t *buf, uint32_t len);
90
91/* The CPU architecture is occasionally important */
92enum arch_t {
93 ARCH_UNSPECIFIED,
94 ARCH_X86,
95 ARCH_ARM,
96 ARCH_MIPS
97};
Bill Richardson15dc6fc2014-09-02 14:45:44 -070098
Bill Richardsonfeb25182013-03-07 12:54:29 -080099#endif /* VBOOT_REFERENCE_FUTILITY_H_ */