gpt-utils.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2013,2016,2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef __GPT_UTILS_H__
  30. #define __GPT_UTILS_H__
  31. #include <vector>
  32. #include <string>
  33. #include <map>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. /******************************************************************************
  40. * GPT HEADER DEFINES
  41. ******************************************************************************/
  42. #define GPT_SIGNATURE "EFI PART"
  43. #define HEADER_SIZE_OFFSET 12
  44. #define HEADER_CRC_OFFSET 16
  45. #define PRIMARY_HEADER_OFFSET 24
  46. #define BACKUP_HEADER_OFFSET 32
  47. #define FIRST_USABLE_LBA_OFFSET 40
  48. #define LAST_USABLE_LBA_OFFSET 48
  49. #define PENTRIES_OFFSET 72
  50. #define PARTITION_COUNT_OFFSET 80
  51. #define PENTRY_SIZE_OFFSET 84
  52. #define PARTITION_CRC_OFFSET 88
  53. #define TYPE_GUID_OFFSET 0
  54. #define TYPE_GUID_SIZE 16
  55. #define PTN_ENTRY_SIZE 128
  56. #define UNIQUE_GUID_OFFSET 16
  57. #define FIRST_LBA_OFFSET 32
  58. #define LAST_LBA_OFFSET 40
  59. #define ATTRIBUTE_FLAG_OFFSET 48
  60. #define PARTITION_NAME_OFFSET 56
  61. #define MAX_GPT_NAME_SIZE 72
  62. /******************************************************************************
  63. * AB RELATED DEFINES
  64. ******************************************************************************/
  65. //Bit 48 onwords in the attribute field are the ones where we are allowed to
  66. //store our AB attributes.
  67. #define AB_FLAG_OFFSET (ATTRIBUTE_FLAG_OFFSET + 6)
  68. #define GPT_DISK_INIT_MAGIC 0xABCD
  69. #define AB_PARTITION_ATTR_SLOT_ACTIVE (0x1<<2)
  70. #define AB_PARTITION_ATTR_BOOT_SUCCESSFUL (0x1<<6)
  71. #define AB_PARTITION_ATTR_UNBOOTABLE (0x1<<7)
  72. #define AB_SLOT_ACTIVE_VAL 0x3F
  73. #define AB_SLOT_INACTIVE_VAL 0x0
  74. #define AB_SLOT_ACTIVE 1
  75. #define AB_SLOT_INACTIVE 0
  76. #define AB_SLOT_A_SUFFIX "_a"
  77. #define AB_SLOT_B_SUFFIX "_b"
  78. #define PTN_XBL "xbl"
  79. #define PTN_XBL_CFG "xbl_config"
  80. #define PTN_MULTIIMGOEM "multiimgoem"
  81. #define PTN_MULTIIMGQTI "multiimgqti"
  82. #define PTN_SWAP_LIST PTN_XBL, PTN_XBL_CFG, PTN_MULTIIMGOEM, PTN_MULTIIMGQTI, "sbl1", "rpm", "tz", "aboot", "abl", "hyp", "lksecapp", "keymaster", "cmnlib", "cmnlib32", "cmnlib64", "pmic", "apdp", "devcfg", "hosd", "keystore", "msadp", "mdtp", "mdtpsecapp", "dsp", "aop", "qupfw", "vbmeta", "dtbo", "imagefv", "ImageFv", "vm-bootsys", "shrm", "cpucp", "uefi", "aop_config", "uefisecapp", "featenabler", "vendor_boot", "recovery", "qweslicstore", "xbl_ramdump"
  83. #define AB_PTN_LIST PTN_SWAP_LIST, "boot", "system", "vendor", "odm", "modem", "bluetooth"
  84. #define BOOT_DEV_DIR "/dev/block/bootdevice/by-name"
  85. /******************************************************************************
  86. * HELPER MACROS
  87. ******************************************************************************/
  88. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  89. /******************************************************************************
  90. * TYPES
  91. ******************************************************************************/
  92. enum boot_update_stage {
  93. UPDATE_MAIN = 1,
  94. UPDATE_BACKUP,
  95. UPDATE_FINALIZE
  96. };
  97. enum gpt_instance {
  98. PRIMARY_GPT = 0,
  99. SECONDARY_GPT
  100. };
  101. enum boot_chain {
  102. NORMAL_BOOT = 0,
  103. BACKUP_BOOT
  104. };
  105. struct gpt_disk {
  106. //GPT primary header
  107. uint8_t *hdr;
  108. //primary header crc
  109. uint32_t hdr_crc;
  110. //GPT backup header
  111. uint8_t *hdr_bak;
  112. //backup header crc
  113. uint32_t hdr_bak_crc;
  114. //Partition entries array
  115. uint8_t *pentry_arr;
  116. //Partition entries array for backup table
  117. uint8_t *pentry_arr_bak;
  118. //Size of the pentry array
  119. uint32_t pentry_arr_size;
  120. //Size of each element in the pentry array
  121. uint32_t pentry_size;
  122. //CRC of the partition entry array
  123. uint32_t pentry_arr_crc;
  124. //CRC of the backup partition entry array
  125. uint32_t pentry_arr_bak_crc;
  126. //Path to block dev representing the disk
  127. char devpath[PATH_MAX];
  128. //Block size of disk
  129. uint32_t block_size;
  130. uint32_t is_initialized;
  131. };
  132. /******************************************************************************
  133. * FUNCTION PROTOTYPES
  134. ******************************************************************************/
  135. int prepare_boot_update(enum boot_update_stage stage);
  136. //GPT disk methods
  137. struct gpt_disk* gpt_disk_alloc();
  138. //Free previously allocated gpt_disk struct
  139. void gpt_disk_free(struct gpt_disk *disk);
  140. //Get the details of the disk holding the partition whose name
  141. //is passed in via dev
  142. int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk);
  143. //Get pointer to partition entry from a allocated gpt_disk structure
  144. uint8_t* gpt_disk_get_pentry(struct gpt_disk *disk,
  145. const char *partname,
  146. enum gpt_instance instance);
  147. //Update the crc fields of the modified disk structure
  148. int gpt_disk_update_crc(struct gpt_disk *disk);
  149. //Write the contents of struct gpt_disk back to the actual disk
  150. int gpt_disk_commit(struct gpt_disk *disk);
  151. //Return if the current device is UFS based or not
  152. int gpt_utils_is_ufs_device();
  153. //Swtich betwieen using either the primary or the backup
  154. //boot LUN for boot. This is required since UFS boot partitions
  155. //cannot have a backup GPT which is what we use for failsafe
  156. //updates of the other 'critical' partitions. This function will
  157. //not be invoked for emmc targets and on UFS targets is only required
  158. //to be invoked for XBL.
  159. //
  160. //The algorithm to do this is as follows:
  161. //- Find the real block device(eg: /dev/block/sdb) that corresponds
  162. // to the /dev/block/bootdevice/by-name/xbl(bak) symlink
  163. //
  164. //- Once we have the block device 'node' name(sdb in the above example)
  165. // use this node to to locate the scsi generic device that represents
  166. // it by checking the file /sys/block/sdb/device/scsi_generic/sgY
  167. //
  168. //- Once we locate sgY we call the query ioctl on /dev/sgy to switch
  169. //the boot lun to either LUNA or LUNB
  170. int gpt_utils_set_xbl_boot_partition(enum boot_chain chain);
  171. //Given a vector of partition names as a input and a reference to a map,
  172. //populate the map to indicate which physical disk each of the partitions
  173. //sits on. The key in the map is the path to the block device where the
  174. //partiton lies and the value is a vector of strings indicating which of
  175. //the passed in partiton names sits on that device.
  176. int gpt_utils_get_partition_map(std::vector<std::string>& partition_list,
  177. std::map<std::string,std::vector<std::string>>& partition_map);
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif /* __GPT_UTILS_H__ */