part.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * (C) Copyright 2000-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _PART_H
  8. #define _PART_H
  9. #include <ide.h>
  10. #include <common.h>
  11. typedef struct block_dev_desc block_dev_desc_t;
  12. struct block_dev_desc {
  13. int if_type; /* type of the interface */
  14. int dev; /* device number */
  15. unsigned char part_type; /* partition type */
  16. unsigned char target; /* target SCSI ID */
  17. unsigned char lun; /* target LUN */
  18. unsigned char type; /* device type */
  19. unsigned char removable; /* removable device */
  20. #ifdef CONFIG_LBA48
  21. unsigned char lba48; /* device can use 48bit addr (ATA/ATAPI v7) */
  22. #endif
  23. lbaint_t lba; /* number of blocks */
  24. unsigned long blksz; /* block size */
  25. int log2blksz; /* for convenience: log2(blksz) */
  26. char vendor [40+1]; /* IDE model, SCSI Vendor */
  27. char product[20+1]; /* IDE Serial no, SCSI product */
  28. char revision[8+1]; /* firmware revision */
  29. unsigned long (*block_read)(block_dev_desc_t *block_dev,
  30. lbaint_t start,
  31. lbaint_t blkcnt,
  32. void *buffer);
  33. unsigned long (*block_write)(block_dev_desc_t *block_dev,
  34. lbaint_t start,
  35. lbaint_t blkcnt,
  36. const void *buffer);
  37. unsigned long (*block_erase)(block_dev_desc_t *block_dev,
  38. lbaint_t start,
  39. lbaint_t blkcnt);
  40. void *priv; /* driver private struct pointer */
  41. };
  42. #define BLOCK_CNT(size, block_dev_desc) (PAD_COUNT(size, block_dev_desc->blksz))
  43. #define PAD_TO_BLOCKSIZE(size, block_dev_desc) \
  44. (PAD_SIZE(size, block_dev_desc->blksz))
  45. #define LOG2(x) (((x & 0xaaaaaaaa) ? 1 : 0) + ((x & 0xcccccccc) ? 2 : 0) + \
  46. ((x & 0xf0f0f0f0) ? 4 : 0) + ((x & 0xff00ff00) ? 8 : 0) + \
  47. ((x & 0xffff0000) ? 16 : 0))
  48. #define LOG2_INVALID(type) ((type)((sizeof(type)<<3)-1))
  49. /* Interface types: */
  50. #define IF_TYPE_UNKNOWN 0
  51. #define IF_TYPE_IDE 1
  52. #define IF_TYPE_SCSI 2
  53. #define IF_TYPE_ATAPI 3
  54. #define IF_TYPE_USB 4
  55. #define IF_TYPE_DOC 5
  56. #define IF_TYPE_MMC 6
  57. #define IF_TYPE_SD 7
  58. #define IF_TYPE_SATA 8
  59. #define IF_TYPE_HOST 9
  60. #define IF_TYPE_MAX 10 /* Max number of IF_TYPE_* supported */
  61. /* Part types */
  62. #define PART_TYPE_UNKNOWN 0x00
  63. #define PART_TYPE_MAC 0x01
  64. #define PART_TYPE_DOS 0x02
  65. #define PART_TYPE_ISO 0x03
  66. #define PART_TYPE_AMIGA 0x04
  67. #define PART_TYPE_EFI 0x05
  68. /*
  69. * Type string for U-Boot bootable partitions
  70. */
  71. #define BOOT_PART_TYPE "U-Boot" /* primary boot partition type */
  72. #define BOOT_PART_COMP "PPCBoot" /* PPCBoot compatibility type */
  73. /* device types */
  74. #define DEV_TYPE_UNKNOWN 0xff /* not connected */
  75. #define DEV_TYPE_HARDDISK 0x00 /* harddisk */
  76. #define DEV_TYPE_TAPE 0x01 /* Tape */
  77. #define DEV_TYPE_CDROM 0x05 /* CD-ROM */
  78. #define DEV_TYPE_OPDISK 0x07 /* optical disk */
  79. typedef struct disk_partition {
  80. lbaint_t start; /* # of first block in partition */
  81. lbaint_t size; /* number of blocks in partition */
  82. ulong blksz; /* block size in bytes */
  83. uchar name[32]; /* partition name */
  84. uchar type[32]; /* string type description */
  85. int bootable; /* Active/Bootable flag is set */
  86. #ifdef CONFIG_PARTITION_UUIDS
  87. char uuid[37]; /* filesystem UUID as string, if exists */
  88. #endif
  89. #ifdef CONFIG_PARTITION_TYPE_GUID
  90. char type_guid[37]; /* type GUID as string, if exists */
  91. #endif
  92. } disk_partition_t;
  93. /* Misc _get_dev functions */
  94. #ifdef CONFIG_PARTITIONS
  95. block_dev_desc_t *get_dev(const char *ifname, int dev);
  96. block_dev_desc_t* ide_get_dev(int dev);
  97. block_dev_desc_t* sata_get_dev(int dev);
  98. block_dev_desc_t* scsi_get_dev(int dev);
  99. block_dev_desc_t* usb_stor_get_dev(int dev);
  100. block_dev_desc_t* mmc_get_dev(int dev);
  101. int mmc_select_hwpart(int dev_num, int hwpart);
  102. block_dev_desc_t* systemace_get_dev(int dev);
  103. block_dev_desc_t* mg_disk_get_dev(int dev);
  104. block_dev_desc_t *host_get_dev(int dev);
  105. int host_get_dev_err(int dev, block_dev_desc_t **blk_devp);
  106. /* disk/part.c */
  107. int get_partition_info (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
  108. void print_part (block_dev_desc_t *dev_desc);
  109. void init_part (block_dev_desc_t *dev_desc);
  110. void dev_print(block_dev_desc_t *dev_desc);
  111. int get_device(const char *ifname, const char *dev_str,
  112. block_dev_desc_t **dev_desc);
  113. int get_device_and_partition(const char *ifname, const char *dev_part_str,
  114. block_dev_desc_t **dev_desc,
  115. disk_partition_t *info, int allow_whole_dev);
  116. #else
  117. static inline block_dev_desc_t *get_dev(const char *ifname, int dev)
  118. { return NULL; }
  119. static inline block_dev_desc_t* ide_get_dev(int dev) { return NULL; }
  120. static inline block_dev_desc_t* sata_get_dev(int dev) { return NULL; }
  121. static inline block_dev_desc_t* scsi_get_dev(int dev) { return NULL; }
  122. static inline block_dev_desc_t* usb_stor_get_dev(int dev) { return NULL; }
  123. static inline block_dev_desc_t* mmc_get_dev(int dev) { return NULL; }
  124. static inline int mmc_select_hwpart(int dev_num, int hwpart) { return -1; }
  125. static inline block_dev_desc_t* systemace_get_dev(int dev) { return NULL; }
  126. static inline block_dev_desc_t* mg_disk_get_dev(int dev) { return NULL; }
  127. static inline block_dev_desc_t *host_get_dev(int dev) { return NULL; }
  128. static inline int get_partition_info (block_dev_desc_t * dev_desc, int part,
  129. disk_partition_t *info) { return -1; }
  130. static inline void print_part (block_dev_desc_t *dev_desc) {}
  131. static inline void init_part (block_dev_desc_t *dev_desc) {}
  132. static inline void dev_print(block_dev_desc_t *dev_desc) {}
  133. static inline int get_device(const char *ifname, const char *dev_str,
  134. block_dev_desc_t **dev_desc)
  135. { return -1; }
  136. static inline int get_device_and_partition(const char *ifname,
  137. const char *dev_part_str,
  138. block_dev_desc_t **dev_desc,
  139. disk_partition_t *info,
  140. int allow_whole_dev)
  141. { *dev_desc = NULL; return -1; }
  142. #endif
  143. #ifdef CONFIG_MAC_PARTITION
  144. /* disk/part_mac.c */
  145. int get_partition_info_mac (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
  146. void print_part_mac (block_dev_desc_t *dev_desc);
  147. int test_part_mac (block_dev_desc_t *dev_desc);
  148. #endif
  149. #ifdef CONFIG_DOS_PARTITION
  150. /* disk/part_dos.c */
  151. int get_partition_info_dos (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
  152. void print_part_dos (block_dev_desc_t *dev_desc);
  153. int test_part_dos (block_dev_desc_t *dev_desc);
  154. #endif
  155. #ifdef CONFIG_ISO_PARTITION
  156. /* disk/part_iso.c */
  157. int get_partition_info_iso (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
  158. void print_part_iso (block_dev_desc_t *dev_desc);
  159. int test_part_iso (block_dev_desc_t *dev_desc);
  160. #endif
  161. #ifdef CONFIG_AMIGA_PARTITION
  162. /* disk/part_amiga.c */
  163. int get_partition_info_amiga (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
  164. void print_part_amiga (block_dev_desc_t *dev_desc);
  165. int test_part_amiga (block_dev_desc_t *dev_desc);
  166. #endif
  167. #ifdef CONFIG_EFI_PARTITION
  168. #include <part_efi.h>
  169. /* disk/part_efi.c */
  170. int get_partition_info_efi (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
  171. /**
  172. * get_partition_info_efi_by_name() - Find the specified GPT partition table entry
  173. *
  174. * @param dev_desc - block device descriptor
  175. * @param gpt_name - the specified table entry name
  176. * @param info - returns the disk partition info
  177. *
  178. * @return - '0' on match, '-1' on no match, otherwise error
  179. */
  180. int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc,
  181. const char *name, disk_partition_t *info);
  182. void print_part_efi (block_dev_desc_t *dev_desc);
  183. int test_part_efi (block_dev_desc_t *dev_desc);
  184. /**
  185. * write_gpt_table() - Write the GUID Partition Table to disk
  186. *
  187. * @param dev_desc - block device descriptor
  188. * @param gpt_h - pointer to GPT header representation
  189. * @param gpt_e - pointer to GPT partition table entries
  190. *
  191. * @return - zero on success, otherwise error
  192. */
  193. int write_gpt_table(block_dev_desc_t *dev_desc,
  194. gpt_header *gpt_h, gpt_entry *gpt_e);
  195. /**
  196. * gpt_fill_pte(): Fill the GPT partition table entry
  197. *
  198. * @param gpt_h - GPT header representation
  199. * @param gpt_e - GPT partition table entries
  200. * @param partitions - list of partitions
  201. * @param parts - number of partitions
  202. *
  203. * @return zero on success
  204. */
  205. int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
  206. disk_partition_t *partitions, int parts);
  207. /**
  208. * gpt_fill_header(): Fill the GPT header
  209. *
  210. * @param dev_desc - block device descriptor
  211. * @param gpt_h - GPT header representation
  212. * @param str_guid - disk guid string representation
  213. * @param parts_count - number of partitions
  214. *
  215. * @return - error on str_guid conversion error
  216. */
  217. int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
  218. char *str_guid, int parts_count);
  219. /**
  220. * gpt_restore(): Restore GPT partition table
  221. *
  222. * @param dev_desc - block device descriptor
  223. * @param str_disk_guid - disk GUID
  224. * @param partitions - list of partitions
  225. * @param parts - number of partitions
  226. *
  227. * @return zero on success
  228. */
  229. int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
  230. disk_partition_t *partitions, const int parts_count);
  231. /**
  232. * is_valid_gpt_buf() - Ensure that the Primary GPT information is valid
  233. *
  234. * @param dev_desc - block device descriptor
  235. * @param buf - buffer which contains the MBR and Primary GPT info
  236. *
  237. * @return - '0' on success, otherwise error
  238. */
  239. int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf);
  240. /**
  241. * write_mbr_and_gpt_partitions() - write MBR, Primary GPT and Backup GPT
  242. *
  243. * @param dev_desc - block device descriptor
  244. * @param buf - buffer which contains the MBR and Primary GPT info
  245. *
  246. * @return - '0' on success, otherwise error
  247. */
  248. int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf);
  249. /**
  250. * gpt_verify_headers() - Function to read and CRC32 check of the GPT's header
  251. * and partition table entries (PTE)
  252. *
  253. * As a side effect if sets gpt_head and gpt_pte so they point to GPT data.
  254. *
  255. * @param dev_desc - block device descriptor
  256. * @param gpt_head - pointer to GPT header data read from medium
  257. * @param gpt_pte - pointer to GPT partition table enties read from medium
  258. *
  259. * @return - '0' on success, otherwise error
  260. */
  261. int gpt_verify_headers(block_dev_desc_t *dev_desc, gpt_header *gpt_head,
  262. gpt_entry **gpt_pte);
  263. /**
  264. * gpt_verify_partitions() - Function to check if partitions' name, start and
  265. * size correspond to '$partitions' env variable
  266. *
  267. * This function checks if on medium stored GPT data is in sync with information
  268. * provided in '$partitions' environment variable. Specificially, name, start
  269. * and size of the partition is checked.
  270. *
  271. * @param dev_desc - block device descriptor
  272. * @param partitions - partition data read from '$partitions' env variable
  273. * @param parts - number of partitions read from '$partitions' env variable
  274. * @param gpt_head - pointer to GPT header data read from medium
  275. * @param gpt_pte - pointer to GPT partition table enties read from medium
  276. *
  277. * @return - '0' on success, otherwise error
  278. */
  279. int gpt_verify_partitions(block_dev_desc_t *dev_desc,
  280. disk_partition_t *partitions, int parts,
  281. gpt_header *gpt_head, gpt_entry **gpt_pte);
  282. #endif
  283. #endif /* _PART_H */