avb_property_descriptor.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  25. #error "Never include this file directly, include libavb.h instead."
  26. #endif
  27. #ifndef AVB_PROPERTY_DESCRIPTOR_H_
  28. #define AVB_PROPERTY_DESCRIPTOR_H_
  29. #include "avb_descriptor.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* A descriptor for properties (free-form key/value pairs).
  34. *
  35. * Following this struct are |key_num_bytes| bytes of key data,
  36. * followed by a NUL byte, then |value_num_bytes| bytes of value data,
  37. * followed by a NUL byte and then enough padding to make the combined
  38. * size a multiple of 8.
  39. */
  40. typedef struct AvbPropertyDescriptor {
  41. AvbDescriptor parent_descriptor;
  42. uint64_t key_num_bytes;
  43. uint64_t value_num_bytes;
  44. } AVB_ATTR_PACKED AvbPropertyDescriptor;
  45. /* Copies |src| to |dest| and validates, byte-swapping fields in the
  46. * process if needed. Returns true if valid, false if invalid.
  47. *
  48. * Data following the struct is not validated nor copied.
  49. */
  50. bool avb_property_descriptor_validate_and_byteswap(
  51. const AvbPropertyDescriptor* src,
  52. AvbPropertyDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
  53. /* Convenience function for looking up the value for a property with
  54. * name |key| in a vbmeta image. If |key_size| is 0, |key| must be
  55. * NUL-terminated.
  56. *
  57. * The |image_data| parameter must be a pointer to a vbmeta image of
  58. * size |image_size|.
  59. *
  60. * This function returns a pointer to the value inside the passed-in
  61. * image or NULL if not found. Note that the value is always
  62. * guaranteed to be followed by a NUL byte.
  63. *
  64. * If the value was found and |out_value_size| is not NULL, the size
  65. * of the value is returned there.
  66. *
  67. * This function is O(n) in number of descriptors so if you need to
  68. * look up a lot of values, you may want to build a more efficient
  69. * lookup-table by manually walking all descriptors using
  70. * avb_descriptor_foreach().
  71. *
  72. * Before using this function, you MUST verify |image_data| with
  73. * avb_vbmeta_image_verify() and reject it unless it's signed by a
  74. * known good public key.
  75. */
  76. const char* avb_property_lookup(const uint8_t* image_data,
  77. size_t image_size,
  78. const char* key,
  79. size_t key_size,
  80. size_t* out_value_size)
  81. AVB_ATTR_WARN_UNUSED_RESULT;
  82. /* Like avb_property_lookup() but parses the intial portions of the
  83. * value as an unsigned 64-bit integer. Both decimal and hexadecimal
  84. * representations (e.g. "0x2a") are supported. Returns false on
  85. * failure and true on success. On success, the parsed value is
  86. * returned in |out_value|.
  87. */
  88. bool avb_property_lookup_uint64(const uint8_t* image_data,
  89. size_t image_size,
  90. const char* key,
  91. size_t key_size,
  92. uint64_t* out_value)
  93. AVB_ATTR_WARN_UNUSED_RESULT;
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97. #endif /* AVB_PROPERTY_DESCRIPTOR_H_ */