host_misc.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (c) 2010 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-side misc functions for verified boot.
  6. */
  7. #ifndef VBOOT_REFERENCE_HOST_MISC_H_
  8. #define VBOOT_REFERENCE_HOST_MISC_H_
  9. #include "utility.h"
  10. #include "vboot_struct.h"
  11. /* Copy up to dest_size-1 characters from src to dest, ensuring null
  12. termination (which strncpy() doesn't do). Returns the destination
  13. string. */
  14. char* StrCopy(char* dest, const char* src, int dest_size);
  15. /* Read data from [filename]. Store the size of returned data in [size].
  16. *
  17. * Returns the data buffer, which the caller must Free(), or NULL if
  18. * error. */
  19. uint8_t* ReadFile(const char* filename, uint64_t* size);
  20. /* Read a string from a file. Passed the destination, dest size, and
  21. * filename to read.
  22. *
  23. * Returns the destination, or NULL if error. */
  24. char* ReadFileString(char* dest, int size, const char* filename);
  25. /* Read an unsigned integer from a file and save into passed pointer.
  26. *
  27. * Returns 0 if success, -1 if error. */
  28. int ReadFileInt(const char* filename, unsigned* value);
  29. /* Check if a bit is set in a file which contains an integer.
  30. *
  31. * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
  32. int ReadFileBit(const char* filename, int bitmask);
  33. /* Writes [size] bytes of [data] to [filename].
  34. *
  35. * Returns 0 if success, 1 if error. */
  36. int WriteFile(const char* filename, const void *data, uint64_t size);
  37. /**
  38. * Read data from a file into a newly allocated buffer.
  39. *
  40. * @param filename Name of file to read from
  41. * @param data_ptr On exit, pointer to newly allocated buffer with data
  42. * will be stored here. Caller must free() the buffer
  43. * when done with it.
  44. * @param size_ptr On exit, size of data will be stored here.
  45. * @return VB2_SUCCESS, or non-zero if error.
  46. */
  47. int vb2_read_file(const char *filename, uint8_t **data_ptr, uint32_t *size_ptr);
  48. /**
  49. * Write data to a file from a buffer.
  50. *
  51. * @param filename Name of file to write to
  52. * @param buf Buffer to write
  53. * @param size Number of bytes of data to write
  54. * @return VB2_SUCCESS, or non-zero if error.
  55. */
  56. int vb2_write_file(const char *filename, const void *buf, uint32_t size);
  57. /**
  58. * Write a buffer which starts with a standard vb21_struct_common header.
  59. *
  60. * Determines the buffer size from the common header total size field.
  61. *
  62. * @param filename Name of file to write to
  63. * @param buf Buffer to write
  64. * @return VB2_SUCCESS, or non-zero if error.
  65. */
  66. int vb21_write_object(const char *filename, const void *buf);
  67. /**
  68. * Round up a size to a multiple of 32 bits (4 bytes).
  69. */
  70. static __inline const uint32_t roundup32(uint32_t v)
  71. {
  72. return (v + 3) & ~3;
  73. }
  74. /**
  75. * Return the buffer size required to hold a description string.
  76. *
  77. * If the string is NULL or empty, the size is zero. Otherwise, it is the
  78. * size of a buffer which can hold the string and its null terminator,
  79. * rounded up to the nerest multiple of 32 bits.
  80. *
  81. * @param desc Description string
  82. * @return The buffer size in bytes.
  83. */
  84. uint32_t vb2_desc_size(const char *desc);
  85. #endif /* VBOOT_REFERENCE_HOST_MISC_H_ */