host_misc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (c) 2011 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. /* TODO: change all 'return 0', 'return 1' into meaningful return codes */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include "host_common.h"
  13. #include "vboot_common.h"
  14. char* StrCopy(char* dest, const char* src, int dest_size)
  15. {
  16. strncpy(dest, src, dest_size);
  17. dest[dest_size - 1] = '\0';
  18. return dest;
  19. }
  20. uint8_t* ReadFile(const char* filename, uint64_t* sizeptr)
  21. {
  22. FILE* f;
  23. uint8_t* buf;
  24. long size;
  25. f = fopen(filename, "rb");
  26. if (!f) {
  27. fprintf(stderr, "Unable to open file %s\n", filename);
  28. return NULL;
  29. }
  30. fseek(f, 0, SEEK_END);
  31. size = ftell(f);
  32. if (size < 0) {
  33. fclose(f);
  34. return NULL;
  35. }
  36. rewind(f);
  37. buf = malloc(size);
  38. if (!buf) {
  39. fclose(f);
  40. return NULL;
  41. }
  42. if(1 != fread(buf, size, 1, f)) {
  43. fprintf(stderr, "Unable to read from file %s\n", filename);
  44. fclose(f);
  45. free(buf);
  46. return NULL;
  47. }
  48. fclose(f);
  49. if (sizeptr)
  50. *sizeptr = size;
  51. return buf;
  52. }
  53. char* ReadFileString(char* dest, int size, const char* filename)
  54. {
  55. char* got;
  56. FILE* f;
  57. f = fopen(filename, "rt");
  58. if (!f)
  59. return NULL;
  60. got = fgets(dest, size, f);
  61. fclose(f);
  62. return got;
  63. }
  64. int ReadFileInt(const char* filename, unsigned* value)
  65. {
  66. char buf[64];
  67. char* e = NULL;
  68. if (!ReadFileString(buf, sizeof(buf), filename))
  69. return -1;
  70. /* Convert to integer. Allow characters after the int ("123 blah"). */
  71. *value = (unsigned)strtoul(buf, &e, 0);
  72. if (e == buf)
  73. return -1; /* No characters consumed, so conversion failed */
  74. return 0;
  75. }
  76. int ReadFileBit(const char* filename, int bitmask)
  77. {
  78. unsigned value;
  79. if (ReadFileInt(filename, &value) < 0)
  80. return -1;
  81. else return (value & bitmask ? 1 : 0);
  82. }
  83. int WriteFile(const char* filename, const void *data, uint64_t size)
  84. {
  85. FILE *f = fopen(filename, "wb");
  86. if (!f) {
  87. fprintf(stderr, "Unable to open file %s\n", filename);
  88. return 1;
  89. }
  90. if (1 != fwrite(data, size, 1, f)) {
  91. fprintf(stderr, "Unable to write to file %s\n", filename);
  92. fclose(f);
  93. unlink(filename); /* Delete any partial file */
  94. return 1;
  95. }
  96. fclose(f);
  97. return 0;
  98. }