cmd_validate_rec_mrc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2016 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. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <getopt.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include "futility.h"
  16. enum {
  17. OPT_HELP = 1000,
  18. OPT_OFFSET,
  19. };
  20. static const struct option long_opts[] = {
  21. {"help", 0, 0, OPT_HELP},
  22. {"offset", 1, 0, OPT_OFFSET},
  23. {NULL, 0, NULL, 0},
  24. };
  25. static void print_help(int argc, char *argv[])
  26. {
  27. printf("\nUsage: " MYNAME " %s FILE [OPTIONS]\n", argv[0]);
  28. printf("\nOptions:\n");
  29. printf(" --offset <offset> Offset of cache within FILE\n");
  30. printf("\n");
  31. }
  32. struct mrc_metadata {
  33. uint32_t signature;
  34. uint32_t data_size;
  35. uint16_t data_checksum;
  36. uint16_t header_checksum;
  37. uint32_t version;
  38. } __attribute__((packed));
  39. #define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
  40. #define REGF_BLOCK_SHIFT 4
  41. #define REGF_UNALLOCATED_BLOCK 0xffff
  42. unsigned long compute_ip_checksum(const void *addr, unsigned long length)
  43. {
  44. const uint8_t *ptr;
  45. volatile union {
  46. uint8_t byte[2];
  47. uint16_t word;
  48. } value;
  49. unsigned long sum;
  50. unsigned long i;
  51. /* In the most straight forward way possible,
  52. * compute an ip style checksum.
  53. */
  54. sum = 0;
  55. ptr = addr;
  56. for(i = 0; i < length; i++) {
  57. unsigned long v;
  58. v = ptr[i];
  59. if (i & 1) {
  60. v <<= 8;
  61. }
  62. /* Add the new value */
  63. sum += v;
  64. /* Wrap around the carry */
  65. if (sum > 0xFFFF) {
  66. sum = (sum + (sum >> 16)) & 0xFFFF;
  67. }
  68. }
  69. value.byte[0] = sum & 0xff;
  70. value.byte[1] = (sum >> 8) & 0xff;
  71. return (~value.word) & 0xFFFF;
  72. }
  73. static int verify_mrc_slot(struct mrc_metadata *md, unsigned long slot_len)
  74. {
  75. uint32_t header_checksum;
  76. if (md->signature != MRC_DATA_SIGNATURE) {
  77. fprintf(stderr, "MRC signature mismatch\n");
  78. return 1;
  79. }
  80. fprintf(stderr, "MRC signature match.. successful\n");
  81. if (md->data_size > slot_len) {
  82. fprintf(stderr, "MRC cache size overflow\n");
  83. return 1;
  84. }
  85. header_checksum = md->header_checksum;
  86. md->header_checksum = 0;
  87. if (header_checksum != compute_ip_checksum(md, sizeof(*md))) {
  88. fprintf(stderr, "MRC metadata header checksum mismatch\n");
  89. return 1;
  90. }
  91. md->header_checksum = header_checksum;
  92. fprintf(stderr, "MRC metadata header checksum.. verified!\n");
  93. if (md->data_checksum != compute_ip_checksum(&md[1], md->data_size)) {
  94. fprintf(stderr, "MRC data checksum mismatch\n");
  95. return 1;
  96. }
  97. fprintf(stderr, "MRC data checksum.. verified!\n");
  98. return 0;
  99. }
  100. static int get_mrc_data_slot(uint16_t *mb, uint32_t *data_offset,
  101. uint32_t *data_size)
  102. {
  103. /*
  104. * First block offset in metadata block tells the total number of
  105. * metadata blocks.
  106. * Currently, we expect only 1 metadata block to be present.
  107. */
  108. if (*mb != 1) {
  109. fprintf(stderr, "Only 1 metadata block is expected. "
  110. "Actual %x\n", *mb);
  111. return 1;
  112. }
  113. /*
  114. * RECOVERY_MRC_CACHE is expected to contain only one slot. Thus, there
  115. * should be only one block offset present, indicating size of the MRC
  116. * cache slot.
  117. */
  118. mb++;
  119. *data_offset = (1 << REGF_BLOCK_SHIFT);
  120. *data_size = (*mb - 1) << REGF_BLOCK_SHIFT;
  121. mb++;
  122. if (*mb != REGF_UNALLOCATED_BLOCK) {
  123. fprintf(stderr, "More than 1 slot in recovery mrc cache.\n");
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. static int do_validate_rec_mrc(int argc, char *argv[])
  129. {
  130. char *infile = NULL;
  131. int parse_error = 0;
  132. int fd, i, ret = 1;
  133. uint32_t file_size;
  134. uint8_t *buff;
  135. uint32_t offset = 0;
  136. uint32_t data_offset;
  137. uint32_t data_size;
  138. char *e;
  139. while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
  140. !parse_error) {
  141. switch (i) {
  142. case OPT_HELP:
  143. print_help(argc, argv);
  144. return 0;
  145. case OPT_OFFSET:
  146. offset = strtoul(optarg, &e, 0);
  147. if (!*optarg || (e && *e)) {
  148. fprintf(stderr, "Invalid --offset\n");
  149. parse_error = 1;
  150. }
  151. break;
  152. default:
  153. case '?':
  154. parse_error = 1;
  155. break;
  156. }
  157. }
  158. if (parse_error) {
  159. print_help(argc, argv);
  160. return 1;
  161. }
  162. if ((argc - optind) < 1) {
  163. fprintf(stderr, "You must specify an input FILE!\n");
  164. print_help(argc, argv);
  165. return 1;
  166. } else if ((argc - optind) != 1) {
  167. fprintf(stderr, "Unexpected arguments!\n");
  168. print_help(argc, argv);
  169. return 1;
  170. }
  171. infile = argv[optind++];
  172. fd = open(infile, O_RDONLY);
  173. if (fd < 0) {
  174. fprintf(stderr, "Cannot open %s:%s\n", infile, strerror(errno));
  175. return 1;
  176. }
  177. if (futil_map_file(fd, MAP_RO, &buff, &file_size) != FILE_ERR_NONE) {
  178. fprintf(stderr, "Cannot map file %s\n", infile);
  179. close(fd);
  180. return 1;
  181. }
  182. if (get_mrc_data_slot((uint16_t *)(buff + offset), &data_offset,
  183. &data_size)) {
  184. fprintf(stderr, "Metadata block error\n");
  185. futil_unmap_file(fd, MAP_RO, buff, file_size);
  186. close(fd);
  187. return 1;
  188. }
  189. offset += data_offset;
  190. if ((file_size > offset) && ((file_size - offset) >= data_size))
  191. ret = verify_mrc_slot((struct mrc_metadata *)(buff + offset),
  192. data_size);
  193. else
  194. fprintf(stderr, "Offset or data size greater than file size: "
  195. "offset=0x%x, file size=0x%x, data_size=0x%x\n",
  196. offset, file_size, data_size);
  197. if (futil_unmap_file(fd, MAP_RO, buff, file_size) != FILE_ERR_NONE) {
  198. fprintf(stderr, "Failed to unmap file %s\n", infile);
  199. ret = 1;
  200. }
  201. close(fd);
  202. return ret;
  203. }
  204. DECLARE_FUTIL_COMMAND(validate_rec_mrc, do_validate_rec_mrc, VBOOT_VERSION_ALL,
  205. "Validates content of Recovery MRC cache");