blob: 550e381ed4fccc26c6aeb6838997da65b9195935 [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 Richardsone1486c32014-10-30 17:41:51 -070017/* Version string (autogenerated) */
18extern const char futility_version[];
19
Bill Richardson1eae8732015-02-05 12:36:15 -080020/* Bitfields indicating the struct/format versions supported by a command */
21enum vboot_version {
22 /*
23 * v1.0 is the original structs used since the dawn of time.
24 * v2.0 can verify the firmware in smaller chunks, but there's
25 * no difference in the on-device structs, so it's only
26 * meaningful for the firmware API. Futility doesn't care.
27 */
28 VBOOT_VERSION_1_0 = 0x00000001,
29
30 /*
31 * v2.1 uses new and different structs, and is what v2.0 would have
32 * been if someone hadn't started using it before it was ready.
33 */
34 VBOOT_VERSION_2_1 = 0x00000002,
35
36 /*
37 * Everything we know about to date.
38 */
39 VBOOT_VERSION_ALL = 0x00000003,
40};
41
42/* What's our preferred API & data format? */
43enum vboot_version vboot_version;
44
Bill Richardson7d028c42014-07-12 10:46:56 -070045/* Here's a structure to define the commands that futility implements. */
46struct futil_cmd_t {
Bill Richardson31d95c22014-08-24 22:07:17 -070047 const char *const name;
48 int (*const handler) (int argc, char **argv);
Bill Richardson1eae8732015-02-05 12:36:15 -080049 enum vboot_version version;
Bill Richardson31d95c22014-08-24 22:07:17 -070050 const char *const shorthelp;
Bill Richardson779796f2014-09-23 11:47:40 -070051 void (*longhelp) (const char *cmd);
Bill Richardson7d028c42014-07-12 10:46:56 -070052};
Bill Richardsonfeb25182013-03-07 12:54:29 -080053
Bill Richardson779796f2014-09-23 11:47:40 -070054/* Macro to define a command */
Bill Richardson1eae8732015-02-05 12:36:15 -080055#define DECLARE_FUTIL_COMMAND(NAME, HANDLER, VERSION, SHORTHELP, LONGHELP) \
56 const struct futil_cmd_t __cmd_##NAME = { \
57 .name = #NAME, \
58 .handler = HANDLER, \
59 .version = VERSION, \
60 .shorthelp = SHORTHELP, \
61 .longhelp = LONGHELP, \
Bill Richardson779796f2014-09-23 11:47:40 -070062 }
Bill Richardsonfeb25182013-03-07 12:54:29 -080063
Bill Richardson7d028c42014-07-12 10:46:56 -070064/* This is the list of pointers to all commands. */
Bill Richardson31d95c22014-08-24 22:07:17 -070065extern const struct futil_cmd_t *const futil_cmds[];
Bill Richardsonfeb25182013-03-07 12:54:29 -080066
Bill Richardsone1550442014-07-17 11:32:17 -070067/* Size of an array */
68#ifndef ARRAY_SIZE
69#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
70#endif
71
72/* Test an important condition at compile time, not run time */
73#ifndef BUILD_ASSERT
74#define _BA1_(cond, line) \
Bill Richardson779796f2014-09-23 11:47:40 -070075 extern int __build_assertion_ ## line[1 - 2*!(cond)] \
76 __attribute__ ((unused))
Bill Richardsone1550442014-07-17 11:32:17 -070077#define _BA0_(c, x) _BA1_(c, x)
78#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
79#endif
80
Bill Richardsoncf6e78d2014-08-27 15:50:25 -070081/* Fatal internal stupidness */
82#ifndef DIE
83#define DIE do { \
84 fprintf(stderr, MYNAME ": internal error at %s:%d\n", \
85 __FILE__, __LINE__); \
86 exit(1); \
87 } while (0)
88#endif
89
Bill Richardsone192e7f2014-09-23 12:49:26 -070090/* Debug output (off by default) */
91extern int debugging_enabled;
92void Debug(const char *format, ...);
Bill Richardsoncf6e78d2014-08-27 15:50:25 -070093
94/* Returns true if this looks enough like a GBB header to proceed. */
95int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len);
96
97/*
98 * Returns true if the gbb header is valid (and optionally updates *maxlen).
99 * This doesn't verify the contents, though.
100 */
101int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len,
102 uint32_t *maxlen);
103
Bill Richardson6df3e332014-10-02 18:50:33 -0700104/* For GBB v1.2 and later, update the hwid_digest */
105void update_hwid_digest(GoogleBinaryBlockHeader *gbb);
106
107/* For GBB v1.2 and later, print the stored digest of the HWID (and whether
108 * it's correct). Return true if it is correct. */
109int print_hwid_digest(GoogleBinaryBlockHeader *gbb,
110 const char *banner, const char *footer);
111
Bill Richardson15dc6fc2014-09-02 14:45:44 -0700112/* Copies a file or dies with an error message */
Bill Richardsone192e7f2014-09-23 12:49:26 -0700113void futil_copy_file_or_die(const char *infile, const char *outfile);
Bill Richardson15dc6fc2014-09-02 14:45:44 -0700114
Bill Richardson25593382015-01-30 12:22:28 -0800115/* Possible file operation errors */
116enum futil_file_err {
117 FILE_ERR_NONE,
118 FILE_ERR_STAT,
119 FILE_ERR_SIZE,
120 FILE_ERR_MMAP,
121 FILE_ERR_MSYNC,
122 FILE_ERR_MUNMAP,
123 FILE_ERR_OPEN,
124 FILE_ERR_CLOSE,
125 FILE_ERR_DIR,
126 FILE_ERR_CHR,
127 FILE_ERR_FIFO,
128 FILE_ERR_SOCK,
129};
130
131/* Wrapper for mmap/munmap. Skips stupidly large files. */
Bill Richardsone192e7f2014-09-23 12:49:26 -0700132#define MAP_RO 0
133#define MAP_RW 1
Bill Richardson25593382015-01-30 12:22:28 -0800134enum futil_file_err futil_map_file(int fd, int writeable,
135 uint8_t **buf, uint32_t *len);
136enum futil_file_err futil_unmap_file(int fd, int writeable,
137 uint8_t *buf, uint32_t len);
Bill Richardsone192e7f2014-09-23 12:49:26 -0700138
139/* The CPU architecture is occasionally important */
140enum arch_t {
141 ARCH_UNSPECIFIED,
142 ARCH_X86,
143 ARCH_ARM,
144 ARCH_MIPS
145};
Bill Richardson15dc6fc2014-09-02 14:45:44 -0700146
Bill Richardsonfeb25182013-03-07 12:54:29 -0800147#endif /* VBOOT_REFERENCE_FUTILITY_H_ */