futility.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "vboot_common.h"
  10. #include "gbb_header.h"
  11. #include "host_key.h"
  12. /* This program */
  13. #define MYNAME "futility"
  14. /* Version string (autogenerated) */
  15. extern const char futility_version[];
  16. /* Bitfields indicating the struct/format versions supported by a command */
  17. enum vboot_version {
  18. /*
  19. * v1.0 is the original structs used since the dawn of time.
  20. * v2.0 can verify the firmware in smaller chunks, but there's
  21. * no difference in the on-device structs, so it's only
  22. * meaningful for the firmware API. Futility doesn't care.
  23. */
  24. VBOOT_VERSION_1_0 = 0x00000001,
  25. /*
  26. * v2.1 uses new and different structs, and is what v2.0 would have
  27. * been if someone hadn't started using it before it was ready.
  28. */
  29. VBOOT_VERSION_2_1 = 0x00000002,
  30. /*
  31. * Everything we know about to date.
  32. */
  33. VBOOT_VERSION_ALL = 0x00000003,
  34. };
  35. /* What's our preferred API & data format? */
  36. enum vboot_version vboot_version;
  37. /* Here's a structure to define the commands that futility implements. */
  38. struct futil_cmd_t {
  39. /* String used to invoke this command */
  40. const char *const name;
  41. /* Function to do the work. Returns 0 on success.
  42. * Called with argv[0] == "name".
  43. * It should handle its own "--help" option. */
  44. int (*const handler) (int argc, char **argv);
  45. /* Supported ABIs */
  46. enum vboot_version version;
  47. /* One-line summary of what it does */
  48. const char *const shorthelp;
  49. };
  50. /* Macro to define a command */
  51. #define DECLARE_FUTIL_COMMAND(NAME, HANDLER, VERSION, SHORTHELP) \
  52. const struct futil_cmd_t __cmd_##NAME = { \
  53. .name = #NAME, \
  54. .handler = HANDLER, \
  55. .version = VERSION, \
  56. .shorthelp = SHORTHELP, \
  57. }
  58. /* This is the list of pointers to all commands. */
  59. extern const struct futil_cmd_t *const futil_cmds[];
  60. /* Size of an array */
  61. #ifndef ARRAY_SIZE
  62. #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
  63. #endif
  64. /* Test an important condition at compile time, not run time */
  65. #ifndef BUILD_ASSERT
  66. #define _BA1_(cond, line) \
  67. extern int __build_assertion_ ## line[1 - 2*!(cond)] \
  68. __attribute__ ((unused))
  69. #define _BA0_(c, x) _BA1_(c, x)
  70. #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
  71. #endif
  72. /* Fatal internal stupidness */
  73. #ifndef DIE
  74. #define DIE do { \
  75. fprintf(stderr, MYNAME ": internal error at %s:%d\n", \
  76. __FILE__, __LINE__); \
  77. exit(1); \
  78. } while (0)
  79. #endif
  80. /* Debug output (off by default) */
  81. extern int debugging_enabled;
  82. void Debug(const char *format, ...);
  83. /* Returns true if this looks enough like a GBB header to proceed. */
  84. int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len);
  85. /*
  86. * Returns true if the gbb header is valid (and optionally updates *maxlen).
  87. * This doesn't verify the contents, though.
  88. */
  89. int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len,
  90. uint32_t *maxlen);
  91. /* For GBB v1.2 and later, update the hwid_digest */
  92. void update_hwid_digest(GoogleBinaryBlockHeader *gbb);
  93. /* For GBB v1.2 and later, print the stored digest of the HWID (and whether
  94. * it's correct). Return true if it is correct. */
  95. int print_hwid_digest(GoogleBinaryBlockHeader *gbb,
  96. const char *banner, const char *footer);
  97. /* Copies a file or dies with an error message */
  98. void futil_copy_file_or_die(const char *infile, const char *outfile);
  99. /* Update ryu root key header in the image */
  100. int fill_ryu_root_header(uint8_t *ptr, size_t size,
  101. const GoogleBinaryBlockHeader *gbb);
  102. /* Verify ryu root key header */
  103. int verify_ryu_root_header(uint8_t *ptr, size_t size,
  104. const GoogleBinaryBlockHeader *gbb);
  105. /* Possible file operation errors */
  106. enum futil_file_err {
  107. FILE_ERR_NONE,
  108. FILE_ERR_STAT,
  109. FILE_ERR_SIZE,
  110. FILE_ERR_MMAP,
  111. FILE_ERR_MSYNC,
  112. FILE_ERR_MUNMAP,
  113. FILE_ERR_OPEN,
  114. FILE_ERR_CLOSE,
  115. FILE_ERR_DIR,
  116. FILE_ERR_CHR,
  117. FILE_ERR_FIFO,
  118. FILE_ERR_SOCK,
  119. };
  120. /* Wrapper for mmap/munmap. Skips stupidly large files. */
  121. #define MAP_RO 0
  122. #define MAP_RW 1
  123. enum futil_file_err futil_map_file(int fd, int writeable,
  124. uint8_t **buf, uint32_t *len);
  125. enum futil_file_err futil_unmap_file(int fd, int writeable,
  126. uint8_t *buf, uint32_t len);
  127. /* The CPU architecture is occasionally important */
  128. enum arch_t {
  129. ARCH_UNSPECIFIED,
  130. ARCH_X86,
  131. ARCH_ARM,
  132. ARCH_MIPS
  133. };
  134. #endif /* VBOOT_REFERENCE_FUTILITY_H_ */