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 | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 17 | /* Here's a structure to define the commands that futility implements. */ |
| 18 | struct futil_cmd_t { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 19 | const char *const name; |
| 20 | int (*const handler) (int argc, char **argv); |
| 21 | const char *const shorthelp; |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 22 | void (*longhelp) (const char *cmd); |
Bill Richardson | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 23 | }; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 24 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 25 | /* 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 Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 33 | |
Bill Richardson | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 34 | /* This is the list of pointers to all commands. */ |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 35 | extern const struct futil_cmd_t *const futil_cmds[]; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 36 | |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 37 | /* 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 Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 45 | extern int __build_assertion_ ## line[1 - 2*!(cond)] \ |
| 46 | __attribute__ ((unused)) |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 47 | #define _BA0_(c, x) _BA1_(c, x) |
| 48 | #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__) |
| 49 | #endif |
| 50 | |
Bill Richardson | cf6e78d | 2014-08-27 15:50:25 -0700 | [diff] [blame] | 51 | /* 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 Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 60 | /* Debug output (off by default) */ |
| 61 | extern int debugging_enabled; |
| 62 | void Debug(const char *format, ...); |
Bill Richardson | cf6e78d | 2014-08-27 15:50:25 -0700 | [diff] [blame] | 63 | |
| 64 | /* Returns true if this looks enough like a GBB header to proceed. */ |
| 65 | int 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 | */ |
| 71 | int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len, |
| 72 | uint32_t *maxlen); |
| 73 | |
Bill Richardson | 6df3e33 | 2014-10-02 18:50:33 -0700 | [diff] [blame^] | 74 | /* For GBB v1.2 and later, update the hwid_digest */ |
| 75 | void 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. */ |
| 79 | int print_hwid_digest(GoogleBinaryBlockHeader *gbb, |
| 80 | const char *banner, const char *footer); |
| 81 | |
Bill Richardson | 15dc6fc | 2014-09-02 14:45:44 -0700 | [diff] [blame] | 82 | /* Copies a file or dies with an error message */ |
Bill Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 83 | void futil_copy_file_or_die(const char *infile, const char *outfile); |
Bill Richardson | 15dc6fc | 2014-09-02 14:45:44 -0700 | [diff] [blame] | 84 | |
| 85 | /* Wrapper for mmap/munmap. Returns 0 on success. Skips stupidly large files. */ |
Bill Richardson | e192e7f | 2014-09-23 12:49:26 -0700 | [diff] [blame] | 86 | #define MAP_RO 0 |
| 87 | #define MAP_RW 1 |
| 88 | int futil_map_file(int fd, int writeable, uint8_t **buf, uint32_t *len); |
| 89 | int futil_unmap_file(int fd, int writeable, uint8_t *buf, uint32_t len); |
| 90 | |
| 91 | /* The CPU architecture is occasionally important */ |
| 92 | enum arch_t { |
| 93 | ARCH_UNSPECIFIED, |
| 94 | ARCH_X86, |
| 95 | ARCH_ARM, |
| 96 | ARCH_MIPS |
| 97 | }; |
Bill Richardson | 15dc6fc | 2014-09-02 14:45:44 -0700 | [diff] [blame] | 98 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 99 | #endif /* VBOOT_REFERENCE_FUTILITY_H_ */ |