efi.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * 2023-02-25 GuEe-GUI make blk interface
  10. */
  11. #ifndef __PARTITIONS_EFI_H__
  12. #define __PARTITIONS_EFI_H__
  13. #include "../blk_partition.h"
  14. #include <drivers/misc.h>
  15. #include <drivers/byteorder.h>
  16. #define MSDOS_MBR_SIGNATURE 0xaa55
  17. #define EFI_PMBR_OSTYPE_EFI 0xef
  18. #define EFI_PMBR_OSTYPE_EFI_GPT 0xee
  19. #define GPT_MBR_PROTECTIVE 1
  20. #define GPT_MBR_HYBRID 2
  21. #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
  22. #define GPT_HEADER_REVISION_V1 0x00010000
  23. #define GPT_PRIMARY_PARTITION_TABLE_LBA 1
  24. #ifndef __UUID_H__
  25. #define UUID_SIZE 16
  26. rt_packed(struct _guid_t
  27. {
  28. rt_uint8_t b[UUID_SIZE];
  29. });
  30. typedef struct _guid_t guid_t;
  31. #endif /* __UUID_H__ */
  32. #ifndef __EFI_H__
  33. typedef guid_t efi_guid_t rt_align(4);
  34. #define EFI_GUID(a, b, c, d...) (efi_guid_t) \
  35. {{ \
  36. (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
  37. (b) & 0xff, ((b) >> 8) & 0xff, \
  38. (c) & 0xff, ((c) >> 8) & 0xff, \
  39. d \
  40. }}
  41. #define NULL_GUID \
  42. EFI_GUID(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
  43. rt_inline int efi_guidcmp(efi_guid_t left, efi_guid_t right)
  44. {
  45. return rt_memcmp(&left, &right, sizeof (efi_guid_t));
  46. }
  47. #endif /* __EFI_H__ */
  48. #define PARTITION_SYSTEM_GUID \
  49. EFI_GUID(0xc12a7328, 0xf81f, 0x11d2, 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b)
  50. #define LEGACY_MBR_PARTITION_GUID \
  51. EFI_GUID(0x024dee41, 0x33e7, 0x11d3, 0x9d, 0x69, 0x00, 0x08, 0xc7, 0x81, 0xf3, 0x9f)
  52. #define PARTITION_MSFT_RESERVED_GUID \
  53. EFI_GUID(0xe3c9e316, 0x0b5c, 0x4db8, 0x81, 0x7d, 0xf9, 0x2d, 0xf0, 0x02, 0x15, 0xae)
  54. #define PARTITION_BASIC_DATA_GUID \
  55. EFI_GUID(0xebd0a0a2, 0xb9e5, 0x4433, 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7)
  56. rt_packed(struct _gpt_header
  57. {
  58. rt_le64_t signature;
  59. rt_le32_t revision;
  60. rt_le32_t header_size;
  61. rt_le32_t header_crc32;
  62. rt_le32_t reserved1;
  63. rt_le64_t start_lba;
  64. rt_le64_t alternate_lba;
  65. rt_le64_t first_usable_lba;
  66. rt_le64_t last_usable_lba;
  67. efi_guid_t disk_guid;
  68. rt_le64_t partition_entry_lba;
  69. rt_le32_t num_partition_entries;
  70. rt_le32_t sizeof_partition_entry;
  71. rt_le32_t partition_entry_array_crc32;
  72. /*
  73. * The rest of the logical block is reserved by UEFI and must be zero.
  74. * EFI standard handles this by:
  75. *
  76. * uint8_t reserved2[BlockSize - 92];
  77. */
  78. });
  79. typedef struct _gpt_header gpt_header;
  80. rt_packed(struct _gpt_entry_attributes
  81. {
  82. rt_uint64_t required_to_function:1;
  83. rt_uint64_t reserved:47;
  84. rt_uint64_t type_guid_specific:16;
  85. });
  86. typedef struct _gpt_entry_attributes gpt_entry_attributes;
  87. rt_packed(struct _gpt_entry
  88. {
  89. efi_guid_t partition_type_guid;
  90. efi_guid_t unique_partition_guid;
  91. rt_le64_t starting_lba;
  92. rt_le64_t ending_lba;
  93. gpt_entry_attributes attributes;
  94. rt_le16_t partition_name[72/sizeof(rt_le16_t)];
  95. });
  96. typedef struct _gpt_entry gpt_entry;
  97. rt_packed(struct _gpt_mbr_record
  98. {
  99. rt_uint8_t boot_indicator; /* unused by EFI, set to 0x80 for bootable */
  100. rt_uint8_t start_head; /* unused by EFI, pt start in CHS */
  101. rt_uint8_t start_sector; /* unused by EFI, pt start in CHS */
  102. rt_uint8_t start_track;
  103. rt_uint8_t os_type; /* EFI and legacy non-EFI OS types */
  104. rt_uint8_t end_head; /* unused by EFI, pt end in CHS */
  105. rt_uint8_t end_sector; /* unused by EFI, pt end in CHS */
  106. rt_uint8_t end_track; /* unused by EFI, pt end in CHS */
  107. rt_le32_t starting_lba; /* used by EFI - start addr of the on disk pt */
  108. rt_le32_t size_in_lba; /* used by EFI - size of pt in LBA */
  109. });
  110. typedef struct _gpt_mbr_record gpt_mbr_record;
  111. rt_packed(struct _legacy_mbr
  112. {
  113. rt_uint8_t boot_code[440];
  114. rt_le32_t unique_mbr_signature;
  115. rt_le16_t unknown;
  116. gpt_mbr_record partition_record[4];
  117. rt_le16_t signature;
  118. });
  119. typedef struct _legacy_mbr legacy_mbr;
  120. #endif /* __PARTITIONS_EFI_H__ */