avb_property_descriptor.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  6. #error "Never include this file directly, include libavb.h instead."
  7. #endif
  8. #ifndef AVB_PROPERTY_DESCRIPTOR_H_
  9. #define AVB_PROPERTY_DESCRIPTOR_H_
  10. #include "avb_descriptor.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /* A descriptor for properties (free-form key/value pairs).
  15. *
  16. * Following this struct are |key_num_bytes| bytes of key data,
  17. * followed by a NUL byte, then |value_num_bytes| bytes of value data,
  18. * followed by a NUL byte and then enough padding to make the combined
  19. * size a multiple of 8.
  20. */
  21. typedef struct AvbPropertyDescriptor {
  22. AvbDescriptor parent_descriptor;
  23. uint64_t key_num_bytes;
  24. uint64_t value_num_bytes;
  25. } AVB_ATTR_PACKED AvbPropertyDescriptor;
  26. /* Copies |src| to |dest| and validates, byte-swapping fields in the
  27. * process if needed. Returns true if valid, false if invalid.
  28. *
  29. * Data following the struct is not validated nor copied.
  30. */
  31. bool avb_property_descriptor_validate_and_byteswap(
  32. const AvbPropertyDescriptor* src,
  33. AvbPropertyDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
  34. /* Convenience function for looking up the value for a property with
  35. * name |key| in a vbmeta image. If |key_size| is 0, |key| must be
  36. * NUL-terminated.
  37. *
  38. * The |image_data| parameter must be a pointer to a vbmeta image of
  39. * size |image_size|.
  40. *
  41. * This function returns a pointer to the value inside the passed-in
  42. * image or NULL if not found. Note that the value is always
  43. * guaranteed to be followed by a NUL byte.
  44. *
  45. * If the value was found and |out_value_size| is not NULL, the size
  46. * of the value is returned there.
  47. *
  48. * This function is O(n) in number of descriptors so if you need to
  49. * look up a lot of values, you may want to build a more efficient
  50. * lookup-table by manually walking all descriptors using
  51. * avb_descriptor_foreach().
  52. *
  53. * Before using this function, you MUST verify |image_data| with
  54. * avb_vbmeta_image_verify() and reject it unless it's signed by a
  55. * known good public key.
  56. */
  57. const char* avb_property_lookup(const uint8_t* image_data,
  58. size_t image_size,
  59. const char* key,
  60. size_t key_size,
  61. size_t* out_value_size)
  62. AVB_ATTR_WARN_UNUSED_RESULT;
  63. /* Like avb_property_lookup() but parses the intial portions of the
  64. * value as an unsigned 64-bit integer. Both decimal and hexadecimal
  65. * representations (e.g. "0x2a") are supported. Returns false on
  66. * failure and true on success. On success, the parsed value is
  67. * returned in |out_value|.
  68. */
  69. bool avb_property_lookup_uint64(const uint8_t* image_data,
  70. size_t image_size,
  71. const char* key,
  72. size_t key_size,
  73. uint64_t* out_value)
  74. AVB_ATTR_WARN_UNUSED_RESULT;
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif /* AVB_PROPERTY_DESCRIPTOR_H_ */