gpt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-05 linzhenxing first version
  9. */
  10. #ifndef __GPT_H
  11. #define __GPT_H
  12. #include <rtthread.h>
  13. #include <stdint.h>
  14. typedef struct
  15. {
  16. uint8_t b[16]; /* GUID 16 bytes*/
  17. } guid_t;
  18. #define MSDOS_MBR_SIGNATURE 0xaa55
  19. #define EFI_PMBR_OSTYPE_EFI 0xEF
  20. #define EFI_PMBR_OSTYPE_EFI_GPT 0xEE
  21. #define GPT_MBR_PROTECTIVE 1
  22. #define GPT_MBR_HYBRID 2
  23. #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
  24. #define GPT_HEADER_REVISION_V1 0x00010000
  25. #define GPT_PRIMARY_PARTITION_TABLE_LBA 1
  26. typedef guid_t gpt_guid_t __attribute__ ((aligned (4)));
  27. #define EFI_GUID(a, b, c, d...) (gpt_guid_t){ { \
  28. (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
  29. (b) & 0xff, ((b) >> 8) & 0xff, \
  30. (c) & 0xff, ((c) >> 8) & 0xff, d } }
  31. #define NULL_GUID \
  32. EFI_GUID(0x00000000, 0x0000, 0x0000,\
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
  34. #define PARTITION_SYSTEM_GUID \
  35. EFI_GUID( 0xC12A7328, 0xF81F, 0x11d2, \
  36. 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B)
  37. #define LEGACY_MBR_PARTITION_GUID \
  38. EFI_GUID( 0x024DEE41, 0x33E7, 0x11d3, \
  39. 0x9D, 0x69, 0x00, 0x08, 0xC7, 0x81, 0xF3, 0x9F)
  40. #define PARTITION_MSFT_RESERVED_GUID \
  41. EFI_GUID( 0xE3C9E316, 0x0B5C, 0x4DB8, \
  42. 0x81, 0x7D, 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE)
  43. #define PARTITION_BASIC_DATA_GUID \
  44. EFI_GUID( 0xEBD0A0A2, 0xB9E5, 0x4433, \
  45. 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7)
  46. #define PARTITION_LINUX_RAID_GUID \
  47. EFI_GUID( 0xa19d880f, 0x05fc, 0x4d3b, \
  48. 0xa0, 0x06, 0x74, 0x3f, 0x0f, 0x84, 0x91, 0x1e)
  49. #define PARTITION_LINUX_SWAP_GUID \
  50. EFI_GUID( 0x0657fd6d, 0xa4ab, 0x43c4, \
  51. 0x84, 0xe5, 0x09, 0x33, 0xc8, 0x4b, 0x4f, 0x4f)
  52. #define PARTITION_LINUX_LVM_GUID \
  53. EFI_GUID( 0xe6d6d379, 0xf507, 0x44c2, \
  54. 0xa2, 0x3c, 0x23, 0x8f, 0x2a, 0x3d, 0xf9, 0x28)
  55. #pragma pack(push, 1)
  56. typedef struct _gpt_header
  57. {
  58. uint64_t signature;
  59. uint32_t revision;
  60. uint32_t header_size;
  61. uint32_t header_crc32;
  62. uint32_t reserved1;
  63. uint64_t start_lba; /*GPT head start sector*/
  64. uint64_t alternate_lba; /*GPT head alternate sector*/
  65. uint64_t first_usable_lba;
  66. uint64_t last_usable_lba;
  67. gpt_guid_t disk_guid;
  68. uint64_t partition_entry_lba;
  69. uint32_t num_partition_entries;
  70. uint32_t sizeof_partition_entry;
  71. uint32_t partition_entry_array_crc32;
  72. /* The rest of the logical block is reserved by UEFI and must be zero.
  73. * EFI standard handles this by:
  74. *
  75. * uint8_t reserved2[ BlockSize - 92 ];
  76. */
  77. } gpt_header;
  78. typedef struct _gpt_entry_attributes
  79. {
  80. uint64_t required_to_function:1;
  81. uint64_t reserved:47;
  82. uint64_t type_guid_specific:16;
  83. } gpt_entry_attributes;
  84. typedef struct _gpt_entry
  85. {
  86. gpt_guid_t partition_type_guid;
  87. gpt_guid_t unique_partition_guid;
  88. uint64_t starting_lba;
  89. uint64_t ending_lba;
  90. gpt_entry_attributes attributes;
  91. uint16_t partition_name[72/sizeof(uint16_t)];
  92. } gpt_entry;
  93. typedef struct _gpt_mbr_record
  94. {
  95. uint8_t boot_indicator; /* unused by EFI, set to 0x80 for bootable */
  96. uint8_t start_head; /* unused by EFI, pt start in CHS */
  97. uint8_t start_sector; /* unused by EFI, pt start in CHS */
  98. uint8_t start_track;
  99. uint8_t os_type; /* EFI and legacy non-EFI OS types */
  100. uint8_t end_head; /* unused by EFI, pt end in CHS */
  101. uint8_t end_sector; /* unused by EFI, pt end in CHS */
  102. uint8_t end_track; /* unused by EFI, pt end in CHS */
  103. uint32_t starting_lba; /* used by EFI - start addr of the on disk pt */
  104. uint32_t size_in_lba; /* used by EFI - size of pt in LBA */
  105. } gpt_mbr_record;
  106. typedef struct _legacy_mbr
  107. {
  108. uint8_t boot_code[440];
  109. uint32_t unique_mbr_signature;
  110. uint16_t unknown;
  111. gpt_mbr_record partition_record[4];
  112. uint16_t signature;
  113. } legacy_mbr;
  114. #pragma pack(pop)
  115. int check_gpt(struct rt_mmcsd_card *card);
  116. int gpt_get_partition_param(struct rt_mmcsd_card *card, struct dfs_partition *part, uint32_t pindex);
  117. void gpt_free(void);
  118. #endif /*__GPT_H*/