host_misc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Host functions for verified boot.
  6. */
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include "2sysincludes.h"
  12. #include "2common.h"
  13. #include "2sha.h"
  14. #include "vb21_common.h"
  15. #include "host_common.h"
  16. #include "host_misc2.h"
  17. int vb2_read_file(const char *filename, uint8_t **data_ptr, uint32_t *size_ptr)
  18. {
  19. FILE *f;
  20. uint8_t *buf;
  21. long size;
  22. *data_ptr = NULL;
  23. *size_ptr = 0;
  24. f = fopen(filename, "rb");
  25. if (!f) {
  26. VB2_DEBUG("Unable to open file %s\n", filename);
  27. return VB2_ERROR_READ_FILE_OPEN;
  28. }
  29. fseek(f, 0, SEEK_END);
  30. size = ftell(f);
  31. rewind(f);
  32. if (size < 0 || size > UINT32_MAX) {
  33. fclose(f);
  34. return VB2_ERROR_READ_FILE_SIZE;
  35. }
  36. buf = malloc(size);
  37. if (!buf) {
  38. fclose(f);
  39. return VB2_ERROR_READ_FILE_ALLOC;
  40. }
  41. if(1 != fread(buf, size, 1, f)) {
  42. VB2_DEBUG("Unable to read file %s\n", filename);
  43. fclose(f);
  44. free(buf);
  45. return VB2_ERROR_READ_FILE_DATA;
  46. }
  47. fclose(f);
  48. *data_ptr = buf;
  49. *size_ptr = size;
  50. return VB2_SUCCESS;
  51. }
  52. int vb2_write_file(const char *filename, const void *buf, uint32_t size)
  53. {
  54. FILE *f = fopen(filename, "wb");
  55. if (!f) {
  56. VB2_DEBUG("Unable to open file %s\n", filename);
  57. return VB2_ERROR_WRITE_FILE_OPEN;
  58. }
  59. if (1 != fwrite(buf, size, 1, f)) {
  60. VB2_DEBUG("Unable to write to file %s\n", filename);
  61. fclose(f);
  62. unlink(filename); /* Delete any partial file */
  63. return VB2_ERROR_WRITE_FILE_DATA;
  64. }
  65. fclose(f);
  66. return VB2_SUCCESS;
  67. }
  68. int vb21_write_object(const char *filename, const void *buf)
  69. {
  70. const struct vb21_struct_common *cptr = buf;
  71. return vb2_write_file(filename, buf, cptr->total_size);
  72. }
  73. uint32_t vb2_desc_size(const char *desc)
  74. {
  75. if (!desc || !*desc)
  76. return 0;
  77. return roundup32(strlen(desc) + 1);
  78. }
  79. static const char *onedigit(const char *str, uint8_t *vptr)
  80. {
  81. uint8_t val = 0;
  82. char c;
  83. for (; (c = *str++) && !isxdigit(c);)
  84. ;
  85. if (!c)
  86. return 0;
  87. if (c >= '0' && c <= '9')
  88. val = c - '0';
  89. else if (c >= 'A' && c <= 'F')
  90. val = 10 + c - 'A';
  91. else if (c >= 'a' && c <= 'f')
  92. val = 10 + c - 'a';
  93. *vptr = val;
  94. return str;
  95. }
  96. static const char *onebyte(const char *str, uint8_t *vptr)
  97. {
  98. uint8_t val;
  99. uint8_t digit;
  100. str = onedigit(str, &digit);
  101. if (!str)
  102. return 0;
  103. val = digit << 4;
  104. str = onedigit(str, &digit);
  105. if (!str)
  106. return 0;
  107. val |= digit;
  108. *vptr = val;
  109. return str;
  110. }
  111. int vb2_str_to_id(const char *str, struct vb2_id *id)
  112. {
  113. uint8_t val = 0;
  114. int i;
  115. if (!str)
  116. return VB2_ERROR_STR_TO_ID;
  117. memset(id, 0, sizeof(*id));
  118. for (i = 0; i < VB2_ID_NUM_BYTES; i++) {
  119. str = onebyte(str, &val);
  120. if (!str)
  121. break;
  122. id->raw[i] = val;
  123. }
  124. /* If we get at least one valid byte, that's good enough. */
  125. return i ? VB2_SUCCESS : VB2_ERROR_STR_TO_ID;
  126. }