avb_descriptor.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_DESCRIPTOR_H_
  28. #define AVB_DESCRIPTOR_H_
  29. #include "avb_sysdeps.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Well-known descriptor tags.
  34. *
  35. * AVB_DESCRIPTOR_TAG_PROPERTY: see |AvbPropertyDescriptor| struct.
  36. * AVB_DESCRIPTOR_TAG_HASHTREE: see |AvbHashtreeDescriptor| struct.
  37. * AVB_DESCRIPTOR_TAG_HASH: see |AvbHashDescriptor| struct.
  38. * AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: see |AvbKernelCmdlineDescriptor| struct.
  39. * AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: see |AvbChainPartitionDescriptor| struct.
  40. */
  41. typedef enum {
  42. AVB_DESCRIPTOR_TAG_PROPERTY,
  43. AVB_DESCRIPTOR_TAG_HASHTREE,
  44. AVB_DESCRIPTOR_TAG_HASH,
  45. AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE,
  46. AVB_DESCRIPTOR_TAG_CHAIN_PARTITION,
  47. } AvbDescriptorTag;
  48. /* The header for a serialized descriptor.
  49. *
  50. * A descriptor always have two fields, a |tag| (denoting its type,
  51. * see the |AvbDescriptorTag| enumeration) and the size of the bytes
  52. * following, |num_bytes_following|.
  53. *
  54. * For padding, |num_bytes_following| is always a multiple of 8.
  55. */
  56. typedef struct AvbDescriptor {
  57. uint64_t tag;
  58. uint64_t num_bytes_following;
  59. } AVB_ATTR_PACKED AvbDescriptor;
  60. /* Copies |src| to |dest| and validates, byte-swapping fields in the
  61. * process if needed. Returns true if valid, false if invalid.
  62. *
  63. * Data following the struct is not validated nor copied.
  64. */
  65. bool avb_descriptor_validate_and_byteswap(
  66. const AvbDescriptor* src, AvbDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
  67. /* Signature for callback function used in avb_descriptor_foreach().
  68. * The passed in descriptor is given by |descriptor| and the
  69. * |user_data| passed to avb_descriptor_foreach() function is in
  70. * |user_data|. Return true to continue iterating, false to stop
  71. * iterating.
  72. *
  73. * Note that |descriptor| points into the image passed to
  74. * avb_descriptor_foreach() - all fields need to be byteswapped!
  75. */
  76. typedef bool AvbDescriptorForeachFunc(const AvbDescriptor* descriptor,
  77. void* user_data);
  78. /* Convenience function to iterate over all descriptors in an vbmeta
  79. * image.
  80. *
  81. * The function given by |foreach_func| will be called for each
  82. * descriptor. The given function should return true to continue
  83. * iterating, false to stop.
  84. *
  85. * The |user_data| parameter will be passed to |foreach_func|.
  86. *
  87. * Returns false if the iteration was short-circuited, that is if
  88. * an invocation of |foreach_func| returned false.
  89. *
  90. * Before using this function, you MUST verify |image_data| with
  91. * avb_vbmeta_image_verify() and reject it unless it's signed by a known
  92. * good public key. Additionally, |image_data| must be word-aligned.
  93. */
  94. bool avb_descriptor_foreach(const uint8_t* image_data,
  95. size_t image_size,
  96. AvbDescriptorForeachFunc foreach_func,
  97. void* user_data);
  98. /* Gets all descriptors in a vbmeta image.
  99. *
  100. * The return value is a NULL-pointer terminated array of
  101. * AvbDescriptor pointers. Free with avb_free() when you are done with
  102. * it. If |out_num_descriptors| is non-NULL, the number of descriptors
  103. * will be returned there.
  104. *
  105. * Note that each AvbDescriptor pointer in the array points into
  106. * |image_data| - all fields need to be byteswapped!
  107. *
  108. * Before using this function, you MUST verify |image_data| with
  109. * avb_vbmeta_image_verify() and reject it unless it's signed by a known
  110. * good public key. Additionally, |image_data| must be word-aligned.
  111. */
  112. const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
  113. size_t image_size,
  114. size_t* out_num_descriptors)
  115. AVB_ATTR_WARN_UNUSED_RESULT;
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif /* AVB_DESCRIPTOR_H_ */