1feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson/* 2feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson * Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 3feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson * Use of this source code is governed by a BSD-style license that can be 4feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson * found in the LICENSE file. 5feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson */ 6feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson#ifndef VBOOT_REFERENCE_FUTILITY_H_ 7feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson#define VBOOT_REFERENCE_FUTILITY_H_ 8cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#include <stdint.h> 9cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson 10cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#include "vboot_common.h" 11cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#include "gbb_header.h" 12e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson#include "host_key.h" 13cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson 14cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson/* This program */ 15cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#define MYNAME "futility" 16feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson 17e1486c3234b7dc6fc5b58681b271a65a09141e20Bill Richardson/* Version string (autogenerated) */ 18e1486c3234b7dc6fc5b58681b271a65a09141e20Bill Richardsonextern const char futility_version[]; 19e1486c3234b7dc6fc5b58681b271a65a09141e20Bill Richardson 201eae873b6194db25781233d7a4aaee6a34160eecBill Richardson/* Bitfields indicating the struct/format versions supported by a command */ 211eae873b6194db25781233d7a4aaee6a34160eecBill Richardsonenum vboot_version { 221eae873b6194db25781233d7a4aaee6a34160eecBill Richardson /* 231eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * v1.0 is the original structs used since the dawn of time. 241eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * v2.0 can verify the firmware in smaller chunks, but there's 251eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * no difference in the on-device structs, so it's only 261eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * meaningful for the firmware API. Futility doesn't care. 271eae873b6194db25781233d7a4aaee6a34160eecBill Richardson */ 281eae873b6194db25781233d7a4aaee6a34160eecBill Richardson VBOOT_VERSION_1_0 = 0x00000001, 291eae873b6194db25781233d7a4aaee6a34160eecBill Richardson 301eae873b6194db25781233d7a4aaee6a34160eecBill Richardson /* 311eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * v2.1 uses new and different structs, and is what v2.0 would have 321eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * been if someone hadn't started using it before it was ready. 331eae873b6194db25781233d7a4aaee6a34160eecBill Richardson */ 341eae873b6194db25781233d7a4aaee6a34160eecBill Richardson VBOOT_VERSION_2_1 = 0x00000002, 351eae873b6194db25781233d7a4aaee6a34160eecBill Richardson 361eae873b6194db25781233d7a4aaee6a34160eecBill Richardson /* 371eae873b6194db25781233d7a4aaee6a34160eecBill Richardson * Everything we know about to date. 381eae873b6194db25781233d7a4aaee6a34160eecBill Richardson */ 391eae873b6194db25781233d7a4aaee6a34160eecBill Richardson VBOOT_VERSION_ALL = 0x00000003, 401eae873b6194db25781233d7a4aaee6a34160eecBill Richardson}; 411eae873b6194db25781233d7a4aaee6a34160eecBill Richardson 421eae873b6194db25781233d7a4aaee6a34160eecBill Richardson/* What's our preferred API & data format? */ 431eae873b6194db25781233d7a4aaee6a34160eecBill Richardsonenum vboot_version vboot_version; 441eae873b6194db25781233d7a4aaee6a34160eecBill Richardson 457d028c4f03c82307fc17dd1226c9949bdbbf38d1Bill Richardson/* Here's a structure to define the commands that futility implements. */ 467d028c4f03c82307fc17dd1226c9949bdbbf38d1Bill Richardsonstruct futil_cmd_t { 4731d95c2386df8d3d5ec619a077960645d052fa38Bill Richardson const char *const name; 4831d95c2386df8d3d5ec619a077960645d052fa38Bill Richardson int (*const handler) (int argc, char **argv); 491eae873b6194db25781233d7a4aaee6a34160eecBill Richardson enum vboot_version version; 5031d95c2386df8d3d5ec619a077960645d052fa38Bill Richardson const char *const shorthelp; 51779796f57e1e0236ea502248ede2cbea986fca21Bill Richardson void (*longhelp) (const char *cmd); 527d028c4f03c82307fc17dd1226c9949bdbbf38d1Bill Richardson}; 53feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson 54779796f57e1e0236ea502248ede2cbea986fca21Bill Richardson/* Macro to define a command */ 551eae873b6194db25781233d7a4aaee6a34160eecBill Richardson#define DECLARE_FUTIL_COMMAND(NAME, HANDLER, VERSION, SHORTHELP, LONGHELP) \ 561eae873b6194db25781233d7a4aaee6a34160eecBill Richardson const struct futil_cmd_t __cmd_##NAME = { \ 571eae873b6194db25781233d7a4aaee6a34160eecBill Richardson .name = #NAME, \ 581eae873b6194db25781233d7a4aaee6a34160eecBill Richardson .handler = HANDLER, \ 591eae873b6194db25781233d7a4aaee6a34160eecBill Richardson .version = VERSION, \ 601eae873b6194db25781233d7a4aaee6a34160eecBill Richardson .shorthelp = SHORTHELP, \ 611eae873b6194db25781233d7a4aaee6a34160eecBill Richardson .longhelp = LONGHELP, \ 62779796f57e1e0236ea502248ede2cbea986fca21Bill Richardson } 637d028c4f03c82307fc17dd1226c9949bdbbf38d1Bill Richardson 647d028c4f03c82307fc17dd1226c9949bdbbf38d1Bill Richardson/* This is the list of pointers to all commands. */ 6531d95c2386df8d3d5ec619a077960645d052fa38Bill Richardsonextern const struct futil_cmd_t *const futil_cmds[]; 66feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson 67e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson/* Size of an array */ 68e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#ifndef ARRAY_SIZE 69e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0])) 70e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#endif 71e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson 72e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson/* Test an important condition at compile time, not run time */ 73e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#ifndef BUILD_ASSERT 74e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#define _BA1_(cond, line) \ 75779796f57e1e0236ea502248ede2cbea986fca21Bill Richardson extern int __build_assertion_ ## line[1 - 2*!(cond)] \ 76779796f57e1e0236ea502248ede2cbea986fca21Bill Richardson __attribute__ ((unused)) 77e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#define _BA0_(c, x) _BA1_(c, x) 78e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__) 79e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson#endif 80e155044a7bbb9b8c6eb751af051726cfe1b411a4Bill Richardson 81cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson/* Fatal internal stupidness */ 82cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#ifndef DIE 83cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#define DIE do { \ 84cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson fprintf(stderr, MYNAME ": internal error at %s:%d\n", \ 85cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson __FILE__, __LINE__); \ 86cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson exit(1); \ 87cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson } while (0) 88cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson#endif 89cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson 90e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson/* Debug output (off by default) */ 91e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardsonextern int debugging_enabled; 92e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardsonvoid Debug(const char *format, ...); 93cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson 94cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson/* Returns true if this looks enough like a GBB header to proceed. */ 95cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardsonint futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len); 96cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson 97cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson/* 98cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson * Returns true if the gbb header is valid (and optionally updates *maxlen). 99cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson * This doesn't verify the contents, though. 100cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson */ 101cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardsonint futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len, 102cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson uint32_t *maxlen); 103cf6e78dbd54684ebba0c3bfc2524426f61193416Bill Richardson 1046df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardson/* For GBB v1.2 and later, update the hwid_digest */ 1056df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardsonvoid update_hwid_digest(GoogleBinaryBlockHeader *gbb); 1066df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardson 1076df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardson/* For GBB v1.2 and later, print the stored digest of the HWID (and whether 1086df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardson * it's correct). Return true if it is correct. */ 1096df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardsonint print_hwid_digest(GoogleBinaryBlockHeader *gbb, 1106df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardson const char *banner, const char *footer); 1116df3e33912baf2633ed27fce6fe166d87e2f04a8Bill Richardson 11215dc6fc5eabc1b1756aca1dbed38cb9d26259bf2Bill Richardson/* Copies a file or dies with an error message */ 113e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardsonvoid futil_copy_file_or_die(const char *infile, const char *outfile); 11415dc6fc5eabc1b1756aca1dbed38cb9d26259bf2Bill Richardson 1152559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson/* Possible file operation errors */ 1162559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardsonenum futil_file_err { 1172559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_NONE, 1182559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_STAT, 1192559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_SIZE, 1202559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_MMAP, 1212559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_MSYNC, 1222559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_MUNMAP, 1232559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_OPEN, 1242559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_CLOSE, 1252559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_DIR, 1262559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_CHR, 1272559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_FIFO, 1282559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson FILE_ERR_SOCK, 1292559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson}; 1302559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson 1312559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson/* Wrapper for mmap/munmap. Skips stupidly large files. */ 132e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson#define MAP_RO 0 133e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson#define MAP_RW 1 1342559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardsonenum futil_file_err futil_map_file(int fd, int writeable, 1352559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson uint8_t **buf, uint32_t *len); 1362559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardsonenum futil_file_err futil_unmap_file(int fd, int writeable, 1372559338dbdacbcbd3ae426337403196135bbc7e8Bill Richardson uint8_t *buf, uint32_t len); 138e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson 139e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson/* The CPU architecture is occasionally important */ 140e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardsonenum arch_t { 141e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson ARCH_UNSPECIFIED, 142e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson ARCH_X86, 143e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson ARCH_ARM, 144e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson ARCH_MIPS 145e192e7f6cea5f2c4556b729bf57ef1f552e0242aBill Richardson}; 14615dc6fc5eabc1b1756aca1dbed38cb9d26259bf2Bill Richardson 147feb2518166b1cd181e607c611cbb610f0c7300daBill Richardson#endif /* VBOOT_REFERENCE_FUTILITY_H_ */ 148