Home | History | Annotate | Download | only in futility
      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  */
      6 #ifndef VBOOT_REFERENCE_FUTILITY_H_
      7 #define VBOOT_REFERENCE_FUTILITY_H_
      8 #include <stdint.h>
      9 
     10 #include "vboot_common.h"
     11 #include "gbb_header.h"
     12 #include "host_key.h"
     13 
     14 /* This program */
     15 #define MYNAME "futility"
     16 
     17 /* Version string (autogenerated) */
     18 extern const char futility_version[];
     19 
     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 
     45 /* Here's a structure to define the commands that futility implements. */
     46 struct futil_cmd_t {
     47 	const char *const name;
     48 	int (*const handler) (int argc, char **argv);
     49 	enum vboot_version version;
     50 	const char *const shorthelp;
     51 	void (*longhelp) (const char *cmd);
     52 };
     53 
     54 /* Macro to define a command */
     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,					\
     62 	}
     63 
     64 /* This is the list of pointers to all commands. */
     65 extern const struct futil_cmd_t *const futil_cmds[];
     66 
     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) \
     75 	extern int __build_assertion_ ## line[1 - 2*!(cond)]	\
     76 	__attribute__ ((unused))
     77 #define _BA0_(c, x) _BA1_(c, x)
     78 #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
     79 #endif
     80 
     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 
     90 /* Debug output (off by default) */
     91 extern int debugging_enabled;
     92 void Debug(const char *format, ...);
     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 
    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 
    112 /* Copies a file or dies with an error message */
    113 void futil_copy_file_or_die(const char *infile, const char *outfile);
    114 
    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. */
    132 #define MAP_RO 0
    133 #define MAP_RW 1
    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);
    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 };
    146 
    147 #endif /* VBOOT_REFERENCE_FUTILITY_H_ */
    148