gpt.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (c) 2018 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4. /* See the file LICENSE for further information */
  5. #ifndef _LIBRARIES_GPT_H
  6. #define _LIBRARIES_GPT_H
  7. #define GPT_GUID_SIZE 16
  8. #ifndef __ASSEMBLER__
  9. #include "comdef.h"
  10. #define GPT_HEADER_LBA 1
  11. #define GPT_HEADER_BYTES 92
  12. typedef struct
  13. {
  14. uint8_t bytes[GPT_GUID_SIZE];
  15. } gpt_guid;
  16. typedef struct
  17. {
  18. uint64_t signature;
  19. uint32_t revision;
  20. uint32_t header_size;
  21. uint32_t header_crc;
  22. uint32_t reserved;
  23. uint64_t current_lba;
  24. uint64_t backup_lba;
  25. uint64_t first_usable_lba;
  26. uint64_t last_usable_lba;
  27. gpt_guid disk_guid;
  28. uint64_t partition_entries_lba;
  29. uint32_t num_partition_entries;
  30. uint32_t partition_entry_size;
  31. uint32_t partition_array_crc;
  32. // gcc will pad this struct to an alignment the matches the alignment of the
  33. // maximum member size, i.e. an 8-byte alignment.
  34. uint32_t padding;
  35. } gpt_header;
  36. #if 1
  37. #define _ASSERT_SIZEOF(type, size) \
  38. _Static_assert(sizeof(type) == (size), #type " must be " #size " bytes wide")
  39. #define _ASSERT_OFFSETOF(type, member, offset) \
  40. _Static_assert(offsetof(type, member) == (offset), #type "." #member " must be at offset " #offset)
  41. _ASSERT_SIZEOF(gpt_header, 96);
  42. _ASSERT_OFFSETOF(gpt_header, disk_guid, 0x38);
  43. _ASSERT_OFFSETOF(gpt_header, partition_array_crc, 0x58);
  44. #undef _ASSERT_SIZEOF
  45. #undef _ASSERT_OFFSETOF
  46. #endif
  47. // If either field is zero, the range is invalid (partitions can't be at LBA 0).
  48. typedef struct
  49. {
  50. uint64_t first_lba;
  51. uint64_t last_lba; // Inclusive
  52. } gpt_partition_range;
  53. gpt_partition_range gpt_find_partition_by_guid(const void* entries, const gpt_guid* guid, uint32_t num_entries);
  54. static inline gpt_partition_range gpt_invalid_partition_range()
  55. {
  56. return (gpt_partition_range) { .first_lba = 0, .last_lba = 0 };
  57. }
  58. static inline int gpt_is_valid_partition_range(gpt_partition_range range)
  59. {
  60. return range.first_lba != 0 && range.last_lba != 0;
  61. }
  62. #endif /* !__ASSEMBLER__ */
  63. #endif /* _LIBRARIES_GPT_H */