1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2000-2004
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 #ifndef _PART_H
7 #define _PART_H
8 
9 #include <blk.h>
10 #include <ide.h>
11 #include <uuid.h>
12 #include <linker_lists.h>
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 
16 struct block_drvr {
17 	char *name;
18 	int (*select_hwpart)(int dev_num, int hwpart);
19 };
20 
21 #define LOG2(x) (((x & 0xaaaaaaaa) ? 1 : 0) + ((x & 0xcccccccc) ? 2 : 0) + \
22 		 ((x & 0xf0f0f0f0) ? 4 : 0) + ((x & 0xff00ff00) ? 8 : 0) + \
23 		 ((x & 0xffff0000) ? 16 : 0))
24 #define LOG2_INVALID(type) ((type)((sizeof(type)<<3)-1))
25 
26 /* Part types */
27 #define PART_TYPE_UNKNOWN	0x00
28 #define PART_TYPE_MAC		0x01
29 #define PART_TYPE_DOS		0x02
30 #define PART_TYPE_ISO		0x03
31 #define PART_TYPE_AMIGA		0x04
32 #define PART_TYPE_EFI		0x05
33 
34 /* maximum number of partition entries supported by search */
35 #define DOS_ENTRY_NUMBERS	8
36 #define ISO_ENTRY_NUMBERS	64
37 #define MAC_ENTRY_NUMBERS	64
38 #define AMIGA_ENTRY_NUMBERS	8
39 /*
40  * Type string for U-Boot bootable partitions
41  */
42 #define BOOT_PART_TYPE	"U-Boot"	/* primary boot partition type	*/
43 #define BOOT_PART_COMP	"PPCBoot"	/* PPCBoot compatibility type	*/
44 
45 /* device types */
46 #define DEV_TYPE_UNKNOWN	0xff	/* not connected */
47 #define DEV_TYPE_HARDDISK	0x00	/* harddisk */
48 #define DEV_TYPE_TAPE		0x01	/* Tape */
49 #define DEV_TYPE_CDROM		0x05	/* CD-ROM */
50 #define DEV_TYPE_OPDISK		0x07	/* optical disk */
51 
52 #define PART_NAME_LEN 32
53 #define PART_TYPE_LEN 32
54 #define MAX_SEARCH_PARTITIONS 128
55 
56 #define PART_BOOTABLE			((int)BIT(0))
57 #define PART_EFI_SYSTEM_PARTITION	((int)BIT(1))
58 
59 struct disk_partition {
60 	lbaint_t	start;	/* # of first block in partition	*/
61 	lbaint_t	size;	/* number of blocks in partition	*/
62 	ulong	blksz;		/* block size in bytes			*/
63 	uchar	name[PART_NAME_LEN];	/* partition name			*/
64 	uchar	type[PART_TYPE_LEN];	/* string type description		*/
65 	/*
66 	 * The bootable is a bitmask with the following fields:
67 	 *
68 	 * PART_BOOTABLE		the MBR bootable flag is set
69 	 * PART_EFI_SYSTEM_PARTITION	the partition is an EFI system partition
70 	 */
71 	int	bootable;
72 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
73 	char	uuid[UUID_STR_LEN + 1];	/* filesystem UUID as string, if exists	*/
74 #endif
75 #ifdef CONFIG_PARTITION_TYPE_GUID
76 	char	type_guid[UUID_STR_LEN + 1];	/* type GUID as string, if exists	*/
77 #endif
78 #ifdef CONFIG_DOS_PARTITION
79 	uchar	sys_ind;	/* partition type			*/
80 #endif
81 };
82 
83 struct disk_part {
84 	int partnum;
85 	struct disk_partition gpt_part_info;
86 	struct list_head list;
87 };
88 
89 /* Misc _get_dev functions */
90 #if CONFIG_IS_ENABLED(PARTITIONS)
91 /**
92  * blk_get_dev() - get a pointer to a block device given its type and number
93  *
94  * Each interface allocates its own devices and typically struct blk_desc is
95  * contained with the interface's data structure. There is no global
96  * numbering for block devices, so the interface name must be provided.
97  *
98  * @ifname:	Interface name (e.g. "ide", "scsi")
99  * @dev:	Device number (0 for first device on that interface, 1 for
100  *		second, etc.
101  * Return: pointer to the block device, or NULL if not available, or an
102  *	   error occurred.
103  */
104 struct blk_desc *blk_get_dev(const char *ifname, int dev);
105 
106 struct blk_desc *mg_disk_get_dev(int dev);
107 
108 /* disk/part.c */
109 int part_get_info(struct blk_desc *dev_desc, int part,
110 		  struct disk_partition *info);
111 /**
112  * part_get_info_whole_disk() - get partition info for the special case of
113  * a partition occupying the entire disk.
114  */
115 int part_get_info_whole_disk(struct blk_desc *dev_desc,
116 			     struct disk_partition *info);
117 
118 void part_print(struct blk_desc *dev_desc);
119 void part_init(struct blk_desc *dev_desc);
120 void dev_print(struct blk_desc *dev_desc);
121 
122 /**
123  * blk_get_device_by_str() - Get a block device given its interface/hw partition
124  *
125  * Each interface allocates its own devices and typically struct blk_desc is
126  * contained with the interface's data structure. There is no global
127  * numbering for block devices, so the interface name must be provided.
128  *
129  * The hardware parition is not related to the normal software partitioning
130  * of a device - each hardware partition is effectively a separately
131  * accessible block device. When a hardware parition is selected on MMC the
132  * other hardware partitions become inaccessible. The same block device is
133  * used to access all hardware partitions, but its capacity may change when a
134  * different hardware partition is selected.
135  *
136  * When a hardware partition number is given, the block device switches to
137  * that hardware partition.
138  *
139  * @ifname:	Interface name (e.g. "ide", "scsi")
140  * @dev_str:	Device and optional hw partition. This can either be a string
141  *		containing the device number (e.g. "2") or the device number
142  *		and hardware partition number (e.g. "2.4") for devices that
143  *		support it (currently only MMC).
144  * @dev_desc:	Returns a pointer to the block device on success
145  * Return: block device number (local to the interface), or -1 on error
146  */
147 int blk_get_device_by_str(const char *ifname, const char *dev_str,
148 			  struct blk_desc **dev_desc);
149 
150 /**
151  * blk_get_device_part_str() - Get a block device and partition
152  *
153  * This calls blk_get_device_by_str() to look up a device. It also looks up
154  * a partition and returns information about it.
155  *
156  * @dev_part_str is in the format:
157  *	<dev>.<hw_part>:<part> where <dev> is the device number,
158  *	<hw_part> is the optional hardware partition number and
159  *	<part> is the partition number
160  *
161  * If ifname is "hostfs" then this function returns the sandbox host block
162  * device.
163  *
164  * If ifname is ubi, then this function returns 0, with @info set to a
165  * special UBI device.
166  *
167  * If @dev_part_str is NULL or empty or "-", then this function looks up
168  * the "bootdevice" environment variable and uses that string instead.
169  *
170  * If the partition string is empty then the first partition is used. If the
171  * partition string is "auto" then the first bootable partition is used.
172  *
173  * @ifname:	Interface name (e.g. "ide", "scsi")
174  * @dev_part_str:	Device and partition string
175  * @dev_desc:	Returns a pointer to the block device on success
176  * @info:	Returns partition information
177  * @allow_whole_dev:	true to allow the user to select partition 0
178  *		(which means the whole device), false to require a valid
179  *		partition number >= 1
180  * Return: partition number, or -1 on error
181  *
182  */
183 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
184 			    struct blk_desc **dev_desc,
185 			    struct disk_partition *info, int allow_whole_dev);
186 
187 /**
188  * part_get_info_by_name_type() - Search for a partition by name
189  *                                for only specified partition type
190  *
191  * @param dev_desc - block device descriptor
192  * @param gpt_name - the specified table entry name
193  * @param info - returns the disk partition info
194  * @param part_type - only search in partitions of this type
195  *
196  * Return: - the partition number on match (starting on 1), -1 on no match,
197  * otherwise error
198  */
199 int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
200 			       struct disk_partition *info, int part_type);
201 
202 /**
203  * part_get_info_by_name() - Search for a partition by name
204  *                           among all available registered partitions
205  *
206  * @param dev_desc - block device descriptor
207  * @param gpt_name - the specified table entry name
208  * @param info - returns the disk partition info
209  *
210  * Return: - the partition number on match (starting on 1), -1 on no match,
211  * otherwise error
212  */
213 int part_get_info_by_name(struct blk_desc *dev_desc,
214 			      const char *name, struct disk_partition *info);
215 
216 /**
217  * Get partition info from dev number + part name, or dev number + part number.
218  *
219  * Parse a device number and partition description (either name or number)
220  * in the form of device number plus partition name separated by a "#"
221  * (like "device_num#partition_name") or a device number plus a partition number
222  * separated by a ":". For example both "0#misc" and "0:1" can be valid
223  * partition descriptions for a given interface. If the partition is found, sets
224  * dev_desc and part_info accordingly with the information of the partition.
225  *
226  * @param[in] dev_iface	Device interface
227  * @param[in] dev_part_str Input partition description, like "0#misc" or "0:1"
228  * @param[out] dev_desc	Place to store the device description pointer
229  * @param[out] part_info Place to store the partition information
230  * @param[in] allow_whole_dev true to allow the user to select partition 0
231  *		(which means the whole device), false to require a valid
232  *		partition number >= 1
233  * Return: the partition number on success, or negative errno on error
234  */
235 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
236 					 const char *dev_part_str,
237 					 struct blk_desc **dev_desc,
238 					 struct disk_partition *part_info,
239 					 int allow_whole_dev);
240 
241 /**
242  * part_set_generic_name() - create generic partition like hda1 or sdb2
243  *
244  * Helper function for partition tables, which don't hold partition names
245  * (DOS, ISO). Generates partition name out of the device type and partition
246  * number.
247  *
248  * @dev_desc:	pointer to the block device
249  * @part_num:	partition number for which the name is generated
250  * @name:	buffer where the name is written
251  */
252 void part_set_generic_name(const struct blk_desc *dev_desc,
253 	int part_num, char *name);
254 
255 extern const struct block_drvr block_drvr[];
256 #else
blk_get_dev(const char * ifname,int dev)257 static inline struct blk_desc *blk_get_dev(const char *ifname, int dev)
258 { return NULL; }
mg_disk_get_dev(int dev)259 static inline struct blk_desc *mg_disk_get_dev(int dev) { return NULL; }
260 
part_get_info(struct blk_desc * dev_desc,int part,struct disk_partition * info)261 static inline int part_get_info(struct blk_desc *dev_desc, int part,
262 				struct disk_partition *info) { return -1; }
part_get_info_whole_disk(struct blk_desc * dev_desc,struct disk_partition * info)263 static inline int part_get_info_whole_disk(struct blk_desc *dev_desc,
264 					   struct disk_partition *info)
265 { return -1; }
part_print(struct blk_desc * dev_desc)266 static inline void part_print(struct blk_desc *dev_desc) {}
part_init(struct blk_desc * dev_desc)267 static inline void part_init(struct blk_desc *dev_desc) {}
dev_print(struct blk_desc * dev_desc)268 static inline void dev_print(struct blk_desc *dev_desc) {}
blk_get_device_by_str(const char * ifname,const char * dev_str,struct blk_desc ** dev_desc)269 static inline int blk_get_device_by_str(const char *ifname, const char *dev_str,
270 					struct blk_desc **dev_desc)
271 { return -1; }
blk_get_device_part_str(const char * ifname,const char * dev_part_str,struct blk_desc ** dev_desc,struct disk_partition * info,int allow_whole_dev)272 static inline int blk_get_device_part_str(const char *ifname,
273 					  const char *dev_part_str,
274 					  struct blk_desc **dev_desc,
275 					  struct disk_partition *info,
276 					  int allow_whole_dev)
277 { *dev_desc = NULL; return -1; }
278 
part_get_info_by_name_type(struct blk_desc * dev_desc,const char * name,struct disk_partition * info,int part_type)279 static inline int part_get_info_by_name_type(struct blk_desc *dev_desc,
280 					     const char *name,
281 					     struct disk_partition *info,
282 					     int part_type)
283 {
284 	return -ENOENT;
285 }
286 
part_get_info_by_name(struct blk_desc * dev_desc,const char * name,struct disk_partition * info)287 static inline int part_get_info_by_name(struct blk_desc *dev_desc,
288 					const char *name,
289 					struct disk_partition *info)
290 {
291 	return -ENOENT;
292 }
293 
294 static inline int
part_get_info_by_dev_and_name_or_num(const char * dev_iface,const char * dev_part_str,struct blk_desc ** dev_desc,struct disk_partition * part_info,int allow_whole_dev)295 part_get_info_by_dev_and_name_or_num(const char *dev_iface,
296 				     const char *dev_part_str,
297 				     struct blk_desc **dev_desc,
298 				     struct disk_partition *part_info,
299 				     int allow_whole_dev)
300 {
301 	*dev_desc = NULL;
302 	return -ENOSYS;
303 }
304 #endif
305 
306 /**
307  * part_get_bootable() - Find the first bootable partition
308  *
309  * @desc: Block-device descriptor
310  * @return first bootable partition, or 0 if there is none
311  */
312 int part_get_bootable(struct blk_desc *desc);
313 
314 struct udevice;
315 /**
316  * part_create_block_devices - Create block devices for disk partitions
317  *
318  * Create UCLASS_PARTITION udevices for each of disk partitions in @parent
319  *
320  * @blk_dev:	Whole disk device
321  */
322 int part_create_block_devices(struct udevice *blk_dev);
323 
324 /**
325  * disk_blk_read() - read blocks from a disk partition
326  *
327  * @dev:	Device to read from (UCLASS_PARTITION)
328  * @start:	Start block number to read in the partition (0=first)
329  * @blkcnt:	Number of blocks to read
330  * @buffer:	Destination buffer for data read
331  * Returns: number of blocks read, or -ve error number (see the
332  * IS_ERR_VALUE() macro
333  */
334 ulong disk_blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
335 		    void *buffer);
336 
337 /**
338  * disk_blk_write() - write to a disk partition
339  *
340  * @dev:	Device to write to (UCLASS_PARTITION)
341  * @start:	Start block number to write in the partition (0=first)
342  * @blkcnt:	Number of blocks to write
343  * @buffer:	Source buffer for data to write
344  * Returns: number of blocks written, or -ve error number (see the
345  * IS_ERR_VALUE() macro
346  */
347 ulong disk_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
348 		     const void *buffer);
349 
350 /**
351  * disk_blk_erase() - erase a section of a disk partition
352  *
353  * @dev:	Device to (partially) erase (UCLASS_PARTITION)
354  * @start:	Start block number to erase in the partition (0=first)
355  * @blkcnt:	Number of blocks to erase
356  * Returns: number of blocks erased, or -ve error number (see the
357  * IS_ERR_VALUE() macro
358  */
359 ulong disk_blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
360 
361 /*
362  * We don't support printing partition information in SPL and only support
363  * getting partition information in a few cases.
364  */
365 #ifdef CONFIG_SPL_BUILD
366 # define part_print_ptr(x)	NULL
367 # if defined(CONFIG_SPL_FS_EXT4) || defined(CONFIG_SPL_FS_FAT) || \
368 	defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
369 #  define part_get_info_ptr(x)	x
370 # else
371 #  define part_get_info_ptr(x)	NULL
372 # endif
373 #else
374 #define part_print_ptr(x)	x
375 #define part_get_info_ptr(x)	x
376 #endif
377 
378 
379 struct part_driver {
380 	const char *name;
381 	int part_type;
382 	const int max_entries;	/* maximum number of entries to search */
383 
384 	/**
385 	 * get_info() - Get information about a partition
386 	 *
387 	 * @dev_desc:	Block device descriptor
388 	 * @part:	Partition number (1 = first)
389 	 * @info:	Returns partition information
390 	 */
391 	int (*get_info)(struct blk_desc *dev_desc, int part,
392 			struct disk_partition *info);
393 
394 	/**
395 	 * print() - Print partition information
396 	 *
397 	 * @dev_desc:	Block device descriptor
398 	 */
399 	void (*print)(struct blk_desc *dev_desc);
400 
401 	/**
402 	 * test() - Test if a device contains this partition type
403 	 *
404 	 * @dev_desc:	Block device descriptor
405 	 * @return 0 if the block device appears to contain this partition
406 	 *	   type, -ve if not
407 	 */
408 	int (*test)(struct blk_desc *dev_desc);
409 };
410 
411 /* Declare a new U-Boot partition 'driver' */
412 #define U_BOOT_PART_TYPE(__name)					\
413 	ll_entry_declare(struct part_driver, __name, part_driver)
414 
415 #include <part_efi.h>
416 
417 #if CONFIG_IS_ENABLED(EFI_PARTITION)
418 /* disk/part_efi.c */
419 /**
420  * write_gpt_table() - Write the GUID Partition Table to disk
421  *
422  * @param dev_desc - block device descriptor
423  * @param gpt_h - pointer to GPT header representation
424  * @param gpt_e - pointer to GPT partition table entries
425  *
426  * Return: - zero on success, otherwise error
427  */
428 int write_gpt_table(struct blk_desc *dev_desc,
429 		  gpt_header *gpt_h, gpt_entry *gpt_e);
430 
431 /**
432  * gpt_fill_pte(): Fill the GPT partition table entry
433  *
434  * @param dev_desc - block device descriptor
435  * @param gpt_h - GPT header representation
436  * @param gpt_e - GPT partition table entries
437  * @param partitions - list of partitions
438  * @param parts - number of partitions
439  *
440  * Return: zero on success
441  */
442 int gpt_fill_pte(struct blk_desc *dev_desc,
443 		 gpt_header *gpt_h, gpt_entry *gpt_e,
444 		 struct disk_partition *partitions, int parts);
445 
446 /**
447  * gpt_fill_header(): Fill the GPT header
448  *
449  * @param dev_desc - block device descriptor
450  * @param gpt_h - GPT header representation
451  * @param str_guid - disk guid string representation
452  * @param parts_count - number of partitions
453  *
454  * Return: - error on str_guid conversion error
455  */
456 int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
457 		char *str_guid, int parts_count);
458 
459 /**
460  * gpt_restore(): Restore GPT partition table
461  *
462  * @param dev_desc - block device descriptor
463  * @param str_disk_guid - disk GUID
464  * @param partitions - list of partitions
465  * @param parts - number of partitions
466  *
467  * Return: zero on success
468  */
469 int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
470 		struct disk_partition *partitions, const int parts_count);
471 
472 /**
473  * is_valid_gpt_buf() - Ensure that the Primary GPT information is valid
474  *
475  * @param dev_desc - block device descriptor
476  * @param buf - buffer which contains the MBR and Primary GPT info
477  *
478  * Return: - '0' on success, otherwise error
479  */
480 int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf);
481 
482 /**
483  * write_mbr_and_gpt_partitions() - write MBR, Primary GPT and Backup GPT
484  *
485  * @param dev_desc - block device descriptor
486  * @param buf - buffer which contains the MBR and Primary GPT info
487  *
488  * Return: - '0' on success, otherwise error
489  */
490 int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf);
491 
492 /**
493  * gpt_verify_headers() - Function to read and CRC32 check of the GPT's header
494  *                        and partition table entries (PTE)
495  *
496  * As a side effect if sets gpt_head and gpt_pte so they point to GPT data.
497  *
498  * @param dev_desc - block device descriptor
499  * @param gpt_head - pointer to GPT header data read from medium
500  * @param gpt_pte - pointer to GPT partition table enties read from medium
501  *
502  * Return: - '0' on success, otherwise error
503  */
504 int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
505 		       gpt_entry **gpt_pte);
506 
507 /**
508  * gpt_repair_headers() - Function to repair the GPT's header
509  *                        and partition table entries (PTE)
510  *
511  * @param dev_desc - block device descriptor
512  *
513  * Return: - '0' on success, otherwise error
514  */
515 int gpt_repair_headers(struct blk_desc *dev_desc);
516 
517 /**
518  * gpt_verify_partitions() - Function to check if partitions' name, start and
519  *                           size correspond to '$partitions' env variable
520  *
521  * This function checks if on medium stored GPT data is in sync with information
522  * provided in '$partitions' environment variable. Specificially, name, start
523  * and size of the partition is checked.
524  *
525  * @param dev_desc - block device descriptor
526  * @param partitions - partition data read from '$partitions' env variable
527  * @param parts - number of partitions read from '$partitions' env variable
528  * @param gpt_head - pointer to GPT header data read from medium
529  * @param gpt_pte - pointer to GPT partition table enties read from medium
530  *
531  * Return: - '0' on success, otherwise error
532  */
533 int gpt_verify_partitions(struct blk_desc *dev_desc,
534 			  struct disk_partition *partitions, int parts,
535 			  gpt_header *gpt_head, gpt_entry **gpt_pte);
536 
537 
538 /**
539  * get_disk_guid() - Function to read the GUID string from a device's GPT
540  *
541  * This function reads the GUID string from a block device whose descriptor
542  * is provided.
543  *
544  * @param dev_desc - block device descriptor
545  * @param guid - pre-allocated string in which to return the GUID
546  *
547  * Return: - '0' on success, otherwise error
548  */
549 int get_disk_guid(struct blk_desc *dev_desc, char *guid);
550 
551 #endif
552 
553 #if CONFIG_IS_ENABLED(DOS_PARTITION)
554 /**
555  * is_valid_dos_buf() - Ensure that a DOS MBR image is valid
556  *
557  * @param buf - buffer which contains the MBR
558  *
559  * Return: - '0' on success, otherwise error
560  */
561 int is_valid_dos_buf(void *buf);
562 
563 /**
564  * write_mbr_sector() - write DOS MBR
565  *
566  * @param dev_desc - block device descriptor
567  * @param buf - buffer which contains the MBR
568  *
569  * Return: - '0' on success, otherwise error
570  */
571 int write_mbr_sector(struct blk_desc *dev_desc, void *buf);
572 
573 int write_mbr_partitions(struct blk_desc *dev,
574 		struct disk_partition *p, int count, unsigned int disksig);
575 int layout_mbr_partitions(struct disk_partition *p, int count,
576 			  lbaint_t total_sectors);
577 
578 #endif
579 
580 #if CONFIG_IS_ENABLED(PARTITIONS)
581 /**
582  * part_driver_get_count() - get partition driver count
583  *
584  * Return: - number of partition drivers
585  */
part_driver_get_count(void)586 static inline int part_driver_get_count(void)
587 {
588 	return ll_entry_count(struct part_driver, part_driver);
589 }
590 
591 /**
592  * part_driver_get_first() - get first partition driver
593  *
594  * Return: - pointer to first partition driver on success, otherwise NULL
595  */
part_driver_get_first(void)596 static inline struct part_driver *part_driver_get_first(void)
597 {
598 	return ll_entry_start(struct part_driver, part_driver);
599 }
600 
601 #else
part_driver_get_count(void)602 static inline int part_driver_get_count(void)
603 { return 0; }
604 
part_driver_get_first(void)605 static inline struct part_driver *part_driver_get_first(void)
606 { return NULL; }
607 #endif /* CONFIG_PARTITIONS */
608 
609 #endif /* _PART_H */
610