blob: 40abd790ad70e7702a8d8e419dea6e69bec6d585
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 | |
10 | #include <ide.h> |
11 | #include <common.h> |
12 | #include <uuid.h> |
13 | #include <linux/list.h> |
14 | |
15 | #ifndef EINVAL |
16 | #define EINVAL 22 |
17 | #endif |
18 | |
19 | #define CONFIG_PARTITION_UUIDS |
20 | #define CONFIG_PARTITION_TYPE_GUID |
21 | //#define CONFIG_EFI_PARTITION |
22 | |
23 | typedef struct block_dev_desc { |
24 | int if_type; /* type of the interface */ |
25 | int dev; /* device number */ |
26 | unsigned char part_type; /* partition type */ |
27 | unsigned char target; /* target SCSI ID */ |
28 | unsigned char lun; /* target LUN */ |
29 | unsigned char type; /* device type */ |
30 | unsigned char removable; /* removable device */ |
31 | #ifdef CONFIG_LBA48 |
32 | unsigned char lba48; /* device can use 48bit addr (ATA/ATAPI v7) */ |
33 | #endif |
34 | lbaint_t lba; /* number of blocks */ |
35 | unsigned long blksz; /* block size */ |
36 | int log2blksz; /* for convenience: log2(blksz) */ |
37 | char vendor [40+1]; /* IDE model, SCSI Vendor */ |
38 | char product[20+1]; /* IDE Serial no, SCSI product */ |
39 | char revision[8+1]; /* firmware revision */ |
40 | unsigned long (*block_read)(int dev, |
41 | lbaint_t start, |
42 | lbaint_t blkcnt, |
43 | void *buffer); |
44 | unsigned long (*block_write)(int dev, |
45 | lbaint_t start, |
46 | lbaint_t blkcnt, |
47 | const void *buffer); |
48 | unsigned long (*block_erase)(int dev, |
49 | lbaint_t start, |
50 | lbaint_t blkcnt); |
51 | void *priv; /* driver private struct pointer */ |
52 | }block_dev_desc_t; |
53 | |
54 | #define BLOCK_CNT(size, block_dev_desc) (PAD_COUNT(size, block_dev_desc->blksz)) |
55 | #define PAD_TO_BLOCKSIZE(size, block_dev_desc) \ |
56 | (PAD_SIZE(size, block_dev_desc->blksz)) |
57 | #define LOG2(x) (((x & 0xaaaaaaaa) ? 1 : 0) + ((x & 0xcccccccc) ? 2 : 0) + \ |
58 | ((x & 0xf0f0f0f0) ? 4 : 0) + ((x & 0xff00ff00) ? 8 : 0) + \ |
59 | ((x & 0xffff0000) ? 16 : 0)) |
60 | #define LOG2_INVALID(type) ((type)((sizeof(type)<<3)-1)) |
61 | |
62 | /* Interface types: */ |
63 | #define IF_TYPE_UNKNOWN 0 |
64 | #define IF_TYPE_IDE 1 |
65 | #define IF_TYPE_SCSI 2 |
66 | #define IF_TYPE_ATAPI 3 |
67 | #define IF_TYPE_USB 4 |
68 | #define IF_TYPE_DOC 5 |
69 | #define IF_TYPE_MMC 6 |
70 | #define IF_TYPE_SD 7 |
71 | #define IF_TYPE_SATA 8 |
72 | #define IF_TYPE_HOST 9 |
73 | #define IF_TYPE_MAX 10 /* Max number of IF_TYPE_* supported */ |
74 | |
75 | /* Part types */ |
76 | #define PART_TYPE_UNKNOWN 0x00 |
77 | #define PART_TYPE_MAC 0x01 |
78 | #define PART_TYPE_DOS 0x02 |
79 | #define PART_TYPE_ISO 0x03 |
80 | #define PART_TYPE_AMIGA 0x04 |
81 | #define PART_TYPE_EFI 0x05 |
82 | #define PART_TYPE_AML 0x06 |
83 | |
84 | /* |
85 | * Type string for U-Boot bootable partitions |
86 | */ |
87 | #define BOOT_PART_TYPE "U-Boot" /* primary boot partition type */ |
88 | #define BOOT_PART_COMP "PPCBoot" /* PPCBoot compatibility type */ |
89 | |
90 | /* device types */ |
91 | #define DEV_TYPE_UNKNOWN 0xff /* not connected */ |
92 | #define DEV_TYPE_HARDDISK 0x00 /* harddisk */ |
93 | #define DEV_TYPE_TAPE 0x01 /* Tape */ |
94 | #define DEV_TYPE_CDROM 0x05 /* CD-ROM */ |
95 | #define DEV_TYPE_OPDISK 0x07 /* optical disk */ |
96 | |
97 | #define PART_NAME_LEN 32 |
98 | #define PART_TYPE_LEN 32 |
99 | #define MAX_SEARCH_PARTITIONS 64 |
100 | #define UUID_STR_LEN 36 |
101 | |
102 | typedef struct disk_partition { |
103 | lbaint_t start; /* # of first block in partition */ |
104 | lbaint_t size; /* number of blocks in partition */ |
105 | ulong blksz; /* block size in bytes */ |
106 | uchar name[32]; /* partition name */ |
107 | uchar type[32]; /* string type description */ |
108 | int bootable; /* Active/Bootable flag is set */ |
109 | #ifdef CONFIG_PARTITION_UUIDS |
110 | char uuid[37]; /* filesystem UUID as string, if exists */ |
111 | #endif |
112 | #ifdef CONFIG_PARTITION_TYPE_GUID |
113 | char type_guid[UUID_STR_LEN + 1]; /* type GUID as string, if exists*/ |
114 | #endif |
115 | #ifdef CONFIG_DOS_PARTITION |
116 | uchar sys_ind; /* partition type */ |
117 | #endif |
118 | } disk_partition_t; |
119 | |
120 | struct disk_part { |
121 | int partnum; |
122 | disk_partition_t gpt_part_info; |
123 | struct list_head list; |
124 | }; |
125 | |
126 | /* Misc _get_dev functions */ |
127 | #ifdef CONFIG_PARTITIONS |
128 | block_dev_desc_t *get_dev(const char *ifname, int dev); |
129 | block_dev_desc_t *ide_get_dev(int dev); |
130 | block_dev_desc_t *sata_get_dev(int dev); |
131 | block_dev_desc_t *scsi_get_dev(int dev); |
132 | block_dev_desc_t *usb_stor_get_dev(int dev); |
133 | block_dev_desc_t *mmc_get_dev(int dev); |
134 | int mmc_select_hwpart(int dev_num, int hwpart); |
135 | block_dev_desc_t *systemace_get_dev(int dev); |
136 | block_dev_desc_t *mg_disk_get_dev(int dev); |
137 | block_dev_desc_t *host_get_dev(int dev); |
138 | int host_get_dev_err(int dev, block_dev_desc_t **blk_devp); |
139 | |
140 | /* disk/part.c */ |
141 | int get_partition_info(block_dev_desc_t *dev_desc, int part, |
142 | disk_partition_t *info); |
143 | void print_part(block_dev_desc_t *dev_desc); |
144 | void init_part(block_dev_desc_t *dev_desc); |
145 | void dev_print(block_dev_desc_t *dev_desc); |
146 | int get_device(const char *ifname, const char *dev_str, |
147 | block_dev_desc_t **dev_desc); |
148 | int get_device_and_partition(const char *ifname, const char *dev_part_str, |
149 | block_dev_desc_t **dev_desc, |
150 | disk_partition_t *info, int allow_whole_dev); |
151 | #else |
152 | static inline block_dev_desc_t *get_dev(const char *ifname, int dev) |
153 | { return NULL; } |
154 | static inline block_dev_desc_t *ide_get_dev(int dev) { return NULL; } |
155 | static inline block_dev_desc_t *sata_get_dev(int dev) { return NULL; } |
156 | static inline block_dev_desc_t *scsi_get_dev(int dev) { return NULL; } |
157 | static inline block_dev_desc_t *usb_stor_get_dev(int dev) { return NULL; } |
158 | static inline block_dev_desc_t *mmc_get_dev(int dev) { return NULL; } |
159 | static inline int mmc_select_hwpart(int dev_num, int hwpart) { return -1; } |
160 | static inline block_dev_desc_t *systemace_get_dev(int dev) { return NULL; } |
161 | static inline block_dev_desc_t *mg_disk_get_dev(int dev) { return NULL; } |
162 | static inline block_dev_desc_t *host_get_dev(int dev) { return NULL; } |
163 | |
164 | static inline int get_partition_info(block_dev_desc_t *dev_desc, int part, |
165 | disk_partition_t *info) { return -1; } |
166 | static inline void print_part(block_dev_desc_t *dev_desc) {} |
167 | static inline void init_part(block_dev_desc_t *dev_desc) {} |
168 | static inline void dev_print(block_dev_desc_t *dev_desc) {} |
169 | static inline int get_device(const char *ifname, const char *dev_str, |
170 | block_dev_desc_t **dev_desc) |
171 | { return -1; } |
172 | static inline int get_device_and_partition(const char *ifname, |
173 | const char *dev_part_str, |
174 | block_dev_desc_t **dev_desc, |
175 | disk_partition_t *info, |
176 | int allow_whole_dev) |
177 | { *dev_desc = NULL; return -1; } |
178 | #endif |
179 | |
180 | #ifdef CONFIG_MAC_PARTITION |
181 | /* disk/part_mac.c */ |
182 | int get_partition_info_mac(block_dev_desc_t *dev_desc, int part, |
183 | disk_partition_t *info); |
184 | void print_part_mac(block_dev_desc_t *dev_desc); |
185 | int test_part_mac(block_dev_desc_t *dev_desc); |
186 | #endif |
187 | |
188 | #ifdef CONFIG_DOS_PARTITION |
189 | /* disk/part_dos.c */ |
190 | int get_partition_info_dos(block_dev_desc_t *dev_desc, int part, |
191 | disk_partition_t *info); |
192 | void print_part_dos(block_dev_desc_t *dev_desc); |
193 | int test_part_dos(block_dev_desc_t *dev_desc); |
194 | #endif |
195 | |
196 | #ifdef CONFIG_ISO_PARTITION |
197 | /* disk/part_iso.c */ |
198 | int get_partition_info_iso(block_dev_desc_t *dev_desc, int part, |
199 | disk_partition_t *info); |
200 | void print_part_iso(block_dev_desc_t *dev_desc); |
201 | int test_part_iso(block_dev_desc_t *dev_desc); |
202 | #endif |
203 | |
204 | #ifdef CONFIG_AMIGA_PARTITION |
205 | /* disk/part_amiga.c */ |
206 | int get_partition_info_amiga(block_dev_desc_t *dev_desc, int part, |
207 | disk_partition_t *info); |
208 | void print_part_amiga(block_dev_desc_t *dev_desc); |
209 | int test_part_amiga(block_dev_desc_t *dev_desc); |
210 | #endif |
211 | |
212 | #ifdef CONFIG_AML_PARTITION |
213 | /* disk/part_aml.c, for aml emmc only */ |
214 | int get_partition_info_aml(block_dev_desc_t *dev_desc, |
215 | int part_num, disk_partition_t * info); |
216 | int get_partition_info_aml_by_name(block_dev_desc_t *dev_desc, |
217 | const char *name, disk_partition_t *info); |
218 | void print_part_aml(block_dev_desc_t *dev_desc); |
219 | int test_part_aml(block_dev_desc_t *dev_desc); |
220 | #endif |
221 | |
222 | /* |
223 | * We don't support printing partition information in SPL and only support |
224 | * getting partition information in a few cases. |
225 | */ |
226 | #ifdef CONFIG_SPL_BUILD |
227 | # define part_print_ptr(x) NULL |
228 | # if defined(CONFIG_SPL_EXT_SUPPORT) || defined(CONFIG_SPL_FAT_SUPPORT) || \ |
229 | defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) |
230 | # define part_get_info_ptr(x) x |
231 | # else |
232 | # define part_get_info_ptr(x) NULL |
233 | # endif |
234 | #else |
235 | #define part_print_ptr(x) x |
236 | #define part_get_info_ptr(x) x |
237 | #endif |
238 | |
239 | |
240 | struct part_driver { |
241 | const char *name; |
242 | int part_type; |
243 | const int max_entries; /* maximum number of entries to search */ |
244 | |
245 | /** |
246 | * get_info() - Get information about a partition |
247 | * |
248 | * @dev_desc: Block device descriptor |
249 | * @part: Partition number (1 = first) |
250 | * @info: Returns partition information |
251 | */ |
252 | int (*get_info)(block_dev_desc_t *dev_desc, int part, |
253 | disk_partition_t *info); |
254 | |
255 | /** |
256 | * print() - Print partition information |
257 | * |
258 | * @dev_desc: Block device descriptor |
259 | */ |
260 | void (*print)(block_dev_desc_t *dev_desc); |
261 | |
262 | /** |
263 | * test() - Test if a device contains this partition type |
264 | * |
265 | * @dev_desc: Block device descriptor |
266 | * @return 0 if the block device appears to contain this partition |
267 | * type, -ve if not |
268 | */ |
269 | int (*test)(block_dev_desc_t *dev_desc); |
270 | }; |
271 | |
272 | /* Declare a new U-Boot partition 'driver' */ |
273 | #define U_BOOT_PART_TYPE(__name) \ |
274 | ll_entry_declare(struct part_driver, __name, part_driver) |
275 | |
276 | |
277 | #ifdef CONFIG_EFI_PARTITION |
278 | #include <part_efi.h> |
279 | /* disk/part_efi.c */ |
280 | int get_partition_info_efi(block_dev_desc_t *dev_desc, |
281 | int part, disk_partition_t *info); |
282 | /** |
283 | * get_partition_info_efi_by_name() - Find the specified GPT partition table entry |
284 | * |
285 | * @param dev_desc - block device descriptor |
286 | * @param gpt_name - the specified table entry name |
287 | * @param info - returns the disk partition info |
288 | * |
289 | * @return - '0' on match, '-1' on no match, otherwise error |
290 | */ |
291 | int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc, |
292 | const char *name, disk_partition_t *info); |
293 | void print_part_efi(block_dev_desc_t *dev_desc); |
294 | int test_part_efi(block_dev_desc_t *dev_desc); |
295 | |
296 | /** |
297 | * write_gpt_table() - Write the GUID Partition Table to disk |
298 | * |
299 | * @param dev_desc - block device descriptor |
300 | * @param gpt_h - pointer to GPT header representation |
301 | * @param gpt_e - pointer to GPT partition table entries |
302 | * |
303 | * @return - zero on success, otherwise error |
304 | */ |
305 | int write_gpt_table(block_dev_desc_t *dev_desc, |
306 | gpt_header *gpt_h, gpt_entry *gpt_e); |
307 | |
308 | /** |
309 | * gpt_fill_pte(): Fill the GPT partition table entry |
310 | * |
311 | * @param gpt_h - GPT header representation |
312 | * @param gpt_e - GPT partition table entries |
313 | * @param partitions - list of partitions |
314 | * @param parts - number of partitions |
315 | * |
316 | * @return zero on success |
317 | */ |
318 | int gpt_fill_pte(block_dev_desc_t *dev_desc, gpt_header *gpt_h, |
319 | gpt_entry *gpt_e, disk_partition_t *partitions, int parts); |
320 | |
321 | /** |
322 | * gpt_fill_header(): Fill the GPT header |
323 | * |
324 | * @param dev_desc - block device descriptor |
325 | * @param gpt_h - GPT header representation |
326 | * @param str_guid - disk guid string representation |
327 | * @param parts_count - number of partitions |
328 | * |
329 | * @return - error on str_guid conversion error |
330 | */ |
331 | int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h, |
332 | char *str_guid, int parts_count); |
333 | |
334 | /** |
335 | * gpt_restore(): Restore GPT partition table |
336 | * |
337 | * @param dev_desc - block device descriptor |
338 | * @param str_disk_guid - disk GUID |
339 | * @param partitions - list of partitions |
340 | * @param parts - number of partitions |
341 | * |
342 | * @return zero on success |
343 | */ |
344 | int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid, |
345 | disk_partition_t *partitions, const int parts_count); |
346 | |
347 | /** |
348 | * is_valid_gpt_buf() - Ensure that the Primary GPT information is valid |
349 | * |
350 | * @param dev_desc - block device descriptor |
351 | * @param buf - buffer which contains the MBR and Primary GPT info |
352 | * |
353 | * @return - '0' on success, otherwise error |
354 | */ |
355 | int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf); |
356 | |
357 | /** |
358 | * write_mbr_and_gpt_partitions() - write MBR, Primary GPT and Backup GPT |
359 | * |
360 | * @param dev_desc - block device descriptor |
361 | * @param buf - buffer which contains the MBR and Primary GPT info |
362 | * |
363 | * @return - '0' on success, otherwise error |
364 | */ |
365 | int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf); |
366 | /** |
367 | * gpt_verify_headers() - Function to read and CRC32 check of the GPT's header |
368 | * and partition table entries (PTE) |
369 | * |
370 | * As a side effect if sets gpt_head and gpt_pte so they point to GPT data. |
371 | * |
372 | * @param dev_desc - block device descriptor |
373 | * @param gpt_head - pointer to GPT header data read from medium |
374 | * @param gpt_pte - pointer to GPT partition table enties read from medium |
375 | * |
376 | * @return - '0' on success, otherwise error |
377 | */ |
378 | int gpt_verify_headers(block_dev_desc_t *dev_desc, gpt_header *gpt_head, |
379 | gpt_entry **gpt_pte); |
380 | |
381 | /** |
382 | * gpt_verify_partitions() - Function to check if partitions' name, start and |
383 | * size correspond to '$partitions' env variable |
384 | * |
385 | * This function checks if on medium stored GPT data is in sync with information |
386 | * provided in '$partitions' environment variable. Specificially, name, start |
387 | * and size of the partition is checked. |
388 | * |
389 | * @param dev_desc - block device descriptor |
390 | * @param partitions - partition data read from '$partitions' env variable |
391 | * @param parts - number of partitions read from '$partitions' env variable |
392 | * @param gpt_head - pointer to GPT header data read from medium |
393 | * @param gpt_pte - pointer to GPT partition table enties read from medium |
394 | * |
395 | * @return - '0' on success, otherwise error |
396 | */ |
397 | int gpt_verify_partitions(block_dev_desc_t *dev_desc, |
398 | disk_partition_t *partitions, int parts, |
399 | gpt_header *gpt_head, gpt_entry **gpt_pte); |
400 | |
401 | |
402 | /** |
403 | * get_disk_guid() - Function to read the GUID string from a device's GPT |
404 | * |
405 | * This function reads the GUID string from a block device whose descriptor |
406 | * is provided. |
407 | * |
408 | * @param dev_desc - block device descriptor |
409 | * @param guid - pre-allocated string in which to return the GUID |
410 | * |
411 | * @return - '0' on success, otherwise error |
412 | */ |
413 | int get_disk_guid(block_dev_desc_t *dev_desc, char *guid); |
414 | |
415 | #endif |
416 | |
417 | #if CONFIG_DOS_PARTITION |
418 | /** |
419 | * is_valid_dos_buf() - Ensure that a DOS MBR image is valid |
420 | * |
421 | * @param buf - buffer which contains the MBR |
422 | * |
423 | * @return - '0' on success, otherwise error |
424 | */ |
425 | int is_valid_dos_buf(void *buf); |
426 | |
427 | /** |
428 | * write_mbr_partition() - write DOS MBR |
429 | * |
430 | * @param dev_desc - block device descriptor |
431 | * @param buf - buffer which contains the MBR |
432 | * |
433 | * @return - '0' on success, otherwise error |
434 | */ |
435 | int write_mbr_partition(block_dev_desc_t *dev_desc, void *buf); |
436 | |
437 | #endif |
438 | |
439 | #endif /* _PART_H */ |
440 |