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