Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 1 | /* |
| 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 Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 6 | #ifndef VBOOT_REFERENCE_FUTILITY_H_ |
| 7 | #define VBOOT_REFERENCE_FUTILITY_H_ |
Bill Richardson | cf6e78d | 2014-08-27 15:50:25 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "vboot_common.h" |
| 11 | #include "gbb_header.h" |
Bill Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 12 | #include "host_key.h" |
Bill Richardson | cf6e78d | 2014-08-27 15:50:25 -0700 | [diff] [blame] | 13 | |
| 14 | /* This program */ |
| 15 | #define MYNAME "futility" |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 16 | |
Bill Richardson | e1486c3 | 2014-10-30 17:41:51 -0700 | [diff] [blame] | 17 | /* Version string (autogenerated) */ |
| 18 | extern const char futility_version[]; |
| 19 | |
Bill Richardson | 1eae873 | 2015-02-05 12:36:15 -0800 | [diff] [blame] | 20 | /* Bitfields indicating the struct/format versions supported by a command */ |
| 21 | enum 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? */ |
| 43 | enum vboot_version vboot_version; |
| 44 | |
Bill Richardson | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 45 | /* Here's a structure to define the commands that futility implements. */ |
| 46 | struct futil_cmd_t { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 47 | const char *const name; |
| 48 | int (*const handler) (int argc, char **argv); |
Bill Richardson | 1eae873 | 2015-02-05 12:36:15 -0800 | [diff] [blame] | 49 | enum vboot_version version; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 50 | const char *const shorthelp; |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 51 | void (*longhelp) (const char *cmd); |
Bill Richardson | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 52 | }; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 53 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 54 | /* Macro to define a command */ |
Bill Richardson | 1eae873 | 2015-02-05 12:36:15 -0800 | [diff] [blame] | 55 | #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 Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 62 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 63 | |
Bill Richardson | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 64 | /* This is the list of pointers to all commands. */ |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 65 | extern const struct futil_cmd_t *const futil_cmds[]; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 66 | |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 67 | /* 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 Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 75 | extern int __build_assertion_ ## line[1 - 2*!(cond)] \ |
| 76 | __attribute__ ((unused)) |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 77 | #define _BA0_(c, x) _BA1_(c, x) |
| 78 | #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__) |
| 79 | #endif |
| 80 | |
Bill Richardson | cf6e78d | 2014-08-27 15:50:25 -0700 | [diff] [blame] | 81 | /* 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 Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 90 | /* Debug output (off by default) */ |
| 91 | extern int debugging_enabled; |
| 92 | void Debug(const char *format, ...); |
Bill Richardson | cf6e78d | 2014-08-27 15:50:25 -0700 | [diff] [blame] | 93 | |
| 94 | /* Returns true if this looks enough like a GBB header to proceed. */ |
| 95 | int 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 | */ |
| 101 | int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len, |
| 102 | uint32_t *maxlen); |
| 103 | |
Bill Richardson | 6df3e33 | 2014-10-02 18:50:33 -0700 | [diff] [blame] | 104 | /* For GBB v1.2 and later, update the hwid_digest */ |
| 105 | void 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. */ |
| 109 | int print_hwid_digest(GoogleBinaryBlockHeader *gbb, |
| 110 | const char *banner, const char *footer); |
| 111 | |
Bill Richardson | 15dc6fc | 2014-09-02 14:45:44 -0700 | [diff] [blame] | 112 | /* Copies a file or dies with an error message */ |
Bill Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 113 | void futil_copy_file_or_die(const char *infile, const char *outfile); |
Bill Richardson | 15dc6fc | 2014-09-02 14:45:44 -0700 | [diff] [blame] | 114 | |
Bill Richardson | 2559338 | 2015-01-30 12:22:28 -0800 | [diff] [blame] | 115 | /* Possible file operation errors */ |
| 116 | enum 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 Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 132 | #define MAP_RO 0 |
| 133 | #define MAP_RW 1 |
Bill Richardson | 2559338 | 2015-01-30 12:22:28 -0800 | [diff] [blame] | 134 | enum futil_file_err futil_map_file(int fd, int writeable, |
| 135 | uint8_t **buf, uint32_t *len); |
| 136 | enum futil_file_err futil_unmap_file(int fd, int writeable, |
| 137 | uint8_t *buf, uint32_t len); |
Bill Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 138 | |
| 139 | /* The CPU architecture is occasionally important */ |
| 140 | enum arch_t { |
| 141 | ARCH_UNSPECIFIED, |
| 142 | ARCH_X86, |
| 143 | ARCH_ARM, |
| 144 | ARCH_MIPS |
| 145 | }; |
Bill Richardson | 15dc6fc | 2014-09-02 14:45:44 -0700 | [diff] [blame] | 146 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 147 | #endif /* VBOOT_REFERENCE_FUTILITY_H_ */ |