blob: fcb3c13dea4de6346ad98d6ce320ef36747dda85 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk012771d2002-03-08 21:31:05 +00002/*
wdenkc35ba4e2004-03-14 22:25:36 +00003 * (C) Copyright 2000-2004
wdenk012771d2002-03-08 21:31:05 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk012771d2002-03-08 21:31:05 +00005 */
6#ifndef _PART_H
7#define _PART_H
Grant Likelyffc2dd72007-02-20 09:04:34 +01008
Simon Glassac8162f2016-02-29 15:25:39 -07009#include <blk.h>
wdenk05939202004-04-18 17:39:38 +000010#include <ide.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010011#include <u-boot/uuid.h>
Corneliu Doban0c6776c2020-11-09 11:49:16 +053012#include <linker_lists.h>
AKASHI Takahiro7780a932022-04-19 10:01:52 +090013#include <linux/errno.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070014#include <linux/list.h>
wdenk012771d2002-03-08 21:31:05 +000015
Alexander Graf3b3d9152016-03-04 01:09:56 +010016struct block_drvr {
17 char *name;
Alexander Graf3b3d9152016-03-04 01:09:56 +010018 int (*select_hwpart)(int dev_num, int hwpart);
19};
20
Egbert Eich2eec2ab2013-04-09 21:11:56 +000021#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))
Egbert Eich071e4232013-04-09 06:03:36 +000025
wdenk012771d2002-03-08 21:31:05 +000026/* Part types */
wdenkc35ba4e2004-03-14 22:25:36 +000027#define PART_TYPE_UNKNOWN 0x00
wdenk012771d2002-03-08 21:31:05 +000028#define PART_TYPE_MAC 0x01
29#define PART_TYPE_DOS 0x02
30#define PART_TYPE_ISO 0x03
wdenkc35ba4e2004-03-14 22:25:36 +000031#define PART_TYPE_AMIGA 0x04
richardretanubune6745592008-09-26 11:13:22 -040032#define PART_TYPE_EFI 0x05
Alexey Romanovd5119672024-07-18 08:46:04 +030033#define PART_TYPE_MTD 0x06
Alexey Romanovc9f61132024-07-18 08:45:27 +030034#define PART_TYPE_UBI 0x07
wdenk452cfd62002-11-19 11:04:11 +000035
Petr Kulhavy712257e2016-09-09 10:27:15 +020036/* maximum number of partition entries supported by search */
37#define DOS_ENTRY_NUMBERS 8
38#define ISO_ENTRY_NUMBERS 64
39#define MAC_ENTRY_NUMBERS 64
40#define AMIGA_ENTRY_NUMBERS 8
Alexey Romanovd5119672024-07-18 08:46:04 +030041#define MTD_ENTRY_NUMBERS 64
Alexey Romanovc9f61132024-07-18 08:45:27 +030042#define UBI_ENTRY_NUMBERS UBI_MAX_VOLUMES
Alexey Romanovd5119672024-07-18 08:46:04 +030043
wdenkf12e3962003-06-29 21:03:46 +000044/*
45 * Type string for U-Boot bootable partitions
46 */
47#define BOOT_PART_TYPE "U-Boot" /* primary boot partition type */
48#define BOOT_PART_COMP "PPCBoot" /* PPCBoot compatibility type */
49
wdenk012771d2002-03-08 21:31:05 +000050/* device types */
wdenkf12e3962003-06-29 21:03:46 +000051#define DEV_TYPE_UNKNOWN 0xff /* not connected */
52#define DEV_TYPE_HARDDISK 0x00 /* harddisk */
wdenkc35ba4e2004-03-14 22:25:36 +000053#define DEV_TYPE_TAPE 0x01 /* Tape */
54#define DEV_TYPE_CDROM 0x05 /* CD-ROM */
55#define DEV_TYPE_OPDISK 0x07 /* optical disk */
wdenk012771d2002-03-08 21:31:05 +000056
Alison Chaiken462e2322017-06-25 16:43:18 -070057#define PART_NAME_LEN 32
58#define PART_TYPE_LEN 32
Heinrich Schuchardt72d78832022-01-11 16:03:38 +010059#define MAX_SEARCH_PARTITIONS 128
Alison Chaiken462e2322017-06-25 16:43:18 -070060
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010061#define PART_BOOTABLE ((int)BIT(0))
62#define PART_EFI_SYSTEM_PARTITION ((int)BIT(1))
63
Simon Glassc1c4a8f2020-05-10 11:39:57 -060064struct disk_partition {
Frederic Leroye7ee0282013-06-26 18:11:25 +020065 lbaint_t start; /* # of first block in partition */
66 lbaint_t size; /* number of blocks in partition */
wdenk012771d2002-03-08 21:31:05 +000067 ulong blksz; /* block size in bytes */
Alison Chaiken462e2322017-06-25 16:43:18 -070068 uchar name[PART_NAME_LEN]; /* partition name */
69 uchar type[PART_TYPE_LEN]; /* string type description */
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010070 /*
71 * The bootable is a bitmask with the following fields:
72 *
73 * PART_BOOTABLE the MBR bootable flag is set
74 * PART_EFI_SYSTEM_PARTITION the partition is an EFI system partition
75 */
76 int bootable;
Caleb Connolly6ea9b662024-10-12 15:57:18 +020077 u16 type_flags; /* top 16 bits of GPT partition attributes */
Patrick Delaunay73287092017-01-27 11:00:42 +010078#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Alison Chaiken2cb47e62017-06-25 16:43:17 -070079 char uuid[UUID_STR_LEN + 1]; /* filesystem UUID as string, if exists */
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +000080#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +010081#ifdef CONFIG_PARTITION_TYPE_GUID
Alison Chaiken2cb47e62017-06-25 16:43:17 -070082 char type_guid[UUID_STR_LEN + 1]; /* type GUID as string, if exists */
Patrick Delaunay8248c8d2015-10-27 11:00:27 +010083#endif
Dalon Westergreen8d770f42017-02-10 17:15:34 -080084#ifdef CONFIG_DOS_PARTITION
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020085 uchar sys_ind; /* partition type */
Dalon Westergreen8d770f42017-02-10 17:15:34 -080086#endif
Simon Glassc1c4a8f2020-05-10 11:39:57 -060087};
wdenk012771d2002-03-08 21:31:05 +000088
Simon Glass23e34532023-08-24 13:55:31 -060089/* Accessors for struct disk_partition field ->uuid */
90extern char *__invalid_use_of_disk_partition_uuid;
91
92static inline const char *disk_partition_uuid(const struct disk_partition *info)
93{
94#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
95 return info->uuid;
96#else
97 return __invalid_use_of_disk_partition_uuid;
98#endif
99}
100
101static inline void disk_partition_set_uuid(struct disk_partition *info,
102 const char *val)
103{
104#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
105 strlcpy(info->uuid, val, UUID_STR_LEN + 1);
106#endif
107}
108
109static inline void disk_partition_clr_uuid(struct disk_partition *info)
110{
111#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
112 *info->uuid = '\0';
113#endif
114}
115
Simon Glassb8d3a782023-08-24 13:55:32 -0600116/* Accessors for struct disk_partition field ->type_guid */
Heinrich Schuchardtf4b4c4e2023-09-02 09:35:21 +0200117extern char *__invalid_use_of_disk_partition_type_guid;
Simon Glassb8d3a782023-08-24 13:55:32 -0600118
Heinrich Schuchardtf4b4c4e2023-09-02 09:35:21 +0200119/**
120 * disk_partition_type_guid() - get partition type GUID
121 *
122 * By using this function to get the partition type GUID we can use
123 * 'if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))' instead of
124 * '#ifdef CONFIG_PARTITION_TYPE_GUID'.
125 *
126 * @info: partition information
127 * Return: partition type GUID
128 */
Simon Glassb8d3a782023-08-24 13:55:32 -0600129static inline const
Heinrich Schuchardtf4b4c4e2023-09-02 09:35:21 +0200130char *disk_partition_type_guid(const struct disk_partition *info)
Simon Glassb8d3a782023-08-24 13:55:32 -0600131{
132#ifdef CONFIG_PARTITION_TYPE_GUID
133 return info->type_guid;
134#else
Heinrich Schuchardtf4b4c4e2023-09-02 09:35:21 +0200135 return __invalid_use_of_disk_partition_type_guid;
Simon Glassb8d3a782023-08-24 13:55:32 -0600136#endif
137}
138
Heinrich Schuchardtf4b4c4e2023-09-02 09:35:21 +0200139/**
140 * disk_partition_set_type_guid() - set partition type GUID
141 *
142 * By using this function to set the partition type GUID we can use
143 * 'if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))' instead of
144 * '#ifdef CONFIG_PARTITION_TYPE_GUID'.
145 *
146 * @info: partition information
147 * @val: partition type GUID as string
148 */
Simon Glassb8d3a782023-08-24 13:55:32 -0600149static inline void disk_partition_set_type_guid(struct disk_partition *info,
150 const char *val)
151{
152#ifdef CONFIG_PARTITION_TYPE_GUID
153 strlcpy(info->type_guid, val, UUID_STR_LEN + 1);
154#endif
155}
156
157static inline void disk_partition_clr_type_guid(struct disk_partition *info)
158{
159#ifdef CONFIG_PARTITION_TYPE_GUID
160 *info->type_guid = '\0';
161#endif
162}
163
Simon Glassed0ec662023-08-24 13:55:33 -0600164/* Accessors for struct disk_partition field ->sys_ind */
165extern int __invalid_use_of_disk_partition_sys_ind;
166
167static inline uint disk_partition_sys_ind(const struct disk_partition *info)
168{
169#ifdef CONFIG_DOS_PARTITION
170 return info->sys_ind;
171#else
172 return __invalid_use_of_disk_partition_sys_ind;
173#endif
174}
175
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700176struct disk_part {
177 int partnum;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600178 struct disk_partition gpt_part_info;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700179 struct list_head list;
180};
181
Grant Likelyffc2dd72007-02-20 09:04:34 +0100182/* Misc _get_dev functions */
AKASHI Takahiro60bb6d72022-04-19 10:01:53 +0900183#if CONFIG_IS_ENABLED(PARTITIONS)
Simon Glassddb13252016-02-29 15:25:41 -0700184/**
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700185 * blk_get_dev() - get a pointer to a block device given its type and number
Simon Glassddb13252016-02-29 15:25:41 -0700186 *
187 * Each interface allocates its own devices and typically struct blk_desc is
188 * contained with the interface's data structure. There is no global
189 * numbering for block devices, so the interface name must be provided.
190 *
191 * @ifname: Interface name (e.g. "ide", "scsi")
192 * @dev: Device number (0 for first device on that interface, 1 for
193 * second, etc.
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200194 * Return:
195 * pointer to the block device, or NULL if not available, or an error occurred.
Simon Glassddb13252016-02-29 15:25:41 -0700196 */
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700197struct blk_desc *blk_get_dev(const char *ifname, int dev);
Simon Glassddb13252016-02-29 15:25:41 -0700198
Simon Glasse3394752016-02-29 15:25:34 -0700199struct blk_desc *mg_disk_get_dev(int dev);
Grant Likelyffc2dd72007-02-20 09:04:34 +0100200
Joshua Wattaa62d502023-07-03 08:39:54 -0500201/**
202 * part_get_info_by_type() - Get partitions from a block device using a specific
203 * partition driver
204 *
205 * Each interface allocates its own devices and typically struct blk_desc is
206 * contained with the interface's data structure. There is no global
207 * numbering for block devices, so the interface name must be provided.
208 *
Simon Glass54c709a2023-08-24 13:55:24 -0600209 * @desc: Block device descriptor
Joshua Wattaa62d502023-07-03 08:39:54 -0500210 * @part: Partition number to read
211 * @part_type: Partition driver to use, or PART_TYPE_UNKNOWN to automatically
212 * choose a driver
213 * @info: Returned partition information
214 *
215 * Return: 0 on success, negative errno on failure
216 */
Simon Glass54c709a2023-08-24 13:55:24 -0600217int part_get_info_by_type(struct blk_desc *desc, int part, int part_type,
Joshua Wattaa62d502023-07-03 08:39:54 -0500218 struct disk_partition *info);
Simon Glass54c709a2023-08-24 13:55:24 -0600219int part_get_info(struct blk_desc *desc, int part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600220 struct disk_partition *info);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400221/**
222 * part_get_info_whole_disk() - get partition info for the special case of
223 * a partition occupying the entire disk.
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200224 *
Simon Glass54c709a2023-08-24 13:55:24 -0600225 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200226 * @info: returned partition information
227 * Return: 0 on success
Rob Clark2b7bfd92017-09-09 13:15:55 -0400228 */
Simon Glass54c709a2023-08-24 13:55:24 -0600229int part_get_info_whole_disk(struct blk_desc *desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600230 struct disk_partition *info);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400231
Simon Glass54c709a2023-08-24 13:55:24 -0600232void part_print(struct blk_desc *desc);
233void part_init(struct blk_desc *desc);
234void dev_print(struct blk_desc *desc);
Simon Glasse6649a62016-02-29 15:25:43 -0700235
236/**
237 * blk_get_device_by_str() - Get a block device given its interface/hw partition
238 *
239 * Each interface allocates its own devices and typically struct blk_desc is
240 * contained with the interface's data structure. There is no global
241 * numbering for block devices, so the interface name must be provided.
242 *
243 * The hardware parition is not related to the normal software partitioning
244 * of a device - each hardware partition is effectively a separately
245 * accessible block device. When a hardware parition is selected on MMC the
246 * other hardware partitions become inaccessible. The same block device is
247 * used to access all hardware partitions, but its capacity may change when a
248 * different hardware partition is selected.
249 *
250 * When a hardware partition number is given, the block device switches to
251 * that hardware partition.
252 *
253 * @ifname: Interface name (e.g. "ide", "scsi")
254 * @dev_str: Device and optional hw partition. This can either be a string
255 * containing the device number (e.g. "2") or the device number
256 * and hardware partition number (e.g. "2.4") for devices that
257 * support it (currently only MMC).
Simon Glass54c709a2023-08-24 13:55:24 -0600258 * @desc: Returns a pointer to the block device on success
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100259 * Return: block device number (local to the interface), or -1 on error
Simon Glasse6649a62016-02-29 15:25:43 -0700260 */
261int blk_get_device_by_str(const char *ifname, const char *dev_str,
Simon Glass54c709a2023-08-24 13:55:24 -0600262 struct blk_desc **desc);
Simon Glasse76ee972016-02-29 15:25:44 -0700263
264/**
265 * blk_get_device_part_str() - Get a block device and partition
266 *
267 * This calls blk_get_device_by_str() to look up a device. It also looks up
268 * a partition and returns information about it.
269 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200270 * @dev_part_str is in the format <dev>.<hw_part>:<part> where
271 *
272 * * <dev> is the device number,
273 *
274 * * <hw_part> is the optional hardware partition number and
Simon Glasse76ee972016-02-29 15:25:44 -0700275 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200276 * * <part> is the partition number.
277 *
278 * If @ifname is "hostfs", then this function returns the sandbox host block
Simon Glasse76ee972016-02-29 15:25:44 -0700279 * device.
280 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200281 * If @ifname is "ubi", then this function returns 0, with @info set to a
Simon Glasse76ee972016-02-29 15:25:44 -0700282 * special UBI device.
283 *
284 * If @dev_part_str is NULL or empty or "-", then this function looks up
285 * the "bootdevice" environment variable and uses that string instead.
286 *
287 * If the partition string is empty then the first partition is used. If the
288 * partition string is "auto" then the first bootable partition is used.
289 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200290 * @ifname: Interface name (e.g. "ide", "scsi")
Simon Glasse76ee972016-02-29 15:25:44 -0700291 * @dev_part_str: Device and partition string
Simon Glass54c709a2023-08-24 13:55:24 -0600292 * @desc: Returns a pointer to the block device on success
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200293 * @info: Returns partition information
Simon Glasse76ee972016-02-29 15:25:44 -0700294 * @allow_whole_dev: true to allow the user to select partition 0
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200295 * (which means the whole device), false to require a valid
296 * partition number >= 1
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100297 * Return: partition number, or -1 on error
Simon Glasse76ee972016-02-29 15:25:44 -0700298 *
299 */
300int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glass54c709a2023-08-24 13:55:24 -0600301 struct blk_desc **desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600302 struct disk_partition *info, int allow_whole_dev);
Petr Kulhavy712257e2016-09-09 10:27:15 +0200303
304/**
305 * part_get_info_by_name() - Search for a partition by name
306 * among all available registered partitions
307 *
Simon Glass54c709a2023-08-24 13:55:24 -0600308 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200309 * @name: the specified table entry name
310 * @info: returns the disk partition info
Petr Kulhavy712257e2016-09-09 10:27:15 +0200311 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200312 * Return: the partition number on match (starting on 1), -1 on no match,
Alex Deymoe2f689f2017-04-02 01:49:50 -0700313 * otherwise error
Petr Kulhavy712257e2016-09-09 10:27:15 +0200314 */
Simon Glass54c709a2023-08-24 13:55:24 -0600315int part_get_info_by_name(struct blk_desc *desc, const char *name,
316 struct disk_partition *info);
Petr Kulhavy712257e2016-09-09 10:27:15 +0200317
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200318/**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200319 * part_get_info_by_dev_and_name_or_num() - Get partition info from dev number
320 * and part name, or dev number and
321 * part number.
Ruslan Trofymenko5e171ef2019-07-05 15:37:31 +0300322 *
323 * Parse a device number and partition description (either name or number)
324 * in the form of device number plus partition name separated by a "#"
325 * (like "device_num#partition_name") or a device number plus a partition number
326 * separated by a ":". For example both "0#misc" and "0:1" can be valid
327 * partition descriptions for a given interface. If the partition is found, sets
Simon Glass54c709a2023-08-24 13:55:24 -0600328 * desc and part_info accordingly with the information of the partition.
Ruslan Trofymenko5e171ef2019-07-05 15:37:31 +0300329 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200330 * @dev_iface: Device interface
331 * @dev_part_str: Input partition description, like "0#misc" or "0:1"
Simon Glass54c709a2023-08-24 13:55:24 -0600332 * @desc: Place to store the device description pointer
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200333 * @part_info: Place to store the partition information
334 * @allow_whole_dev: true to allow the user to select partition 0
335 * (which means the whole device), false to require a valid
336 * partition number >= 1
337 * Return: the partition number on success, or negative errno on error
Ruslan Trofymenko5e171ef2019-07-05 15:37:31 +0300338 */
339int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
340 const char *dev_part_str,
Simon Glass54c709a2023-08-24 13:55:24 -0600341 struct blk_desc **desc,
Sean Andersonc81028d2021-02-05 09:38:56 -0500342 struct disk_partition *part_info,
343 int allow_whole_dev);
Ruslan Trofymenko5e171ef2019-07-05 15:37:31 +0300344
345/**
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200346 * part_set_generic_name() - create generic partition like hda1 or sdb2
347 *
348 * Helper function for partition tables, which don't hold partition names
349 * (DOS, ISO). Generates partition name out of the device type and partition
350 * number.
351 *
Simon Glass54c709a2023-08-24 13:55:24 -0600352 * @desc: pointer to the block device
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200353 * @part_num: partition number for which the name is generated
354 * @name: buffer where the name is written
355 */
Simon Glass54c709a2023-08-24 13:55:24 -0600356void part_set_generic_name(const struct blk_desc *desc, int part_num,
357 char *name);
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200358
Alexander Graf3b3d9152016-03-04 01:09:56 +0100359extern const struct block_drvr block_drvr[];
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000360#else
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700361static inline struct blk_desc *blk_get_dev(const char *ifname, int dev)
Rob Herringd586cec2012-09-21 04:08:17 +0000362{ return NULL; }
Simon Glasse3394752016-02-29 15:25:34 -0700363static inline struct blk_desc *mg_disk_get_dev(int dev) { return NULL; }
wdenk012771d2002-03-08 21:31:05 +0000364
Simon Glass54c709a2023-08-24 13:55:24 -0600365static inline int part_get_info(struct blk_desc *desc, int part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600366 struct disk_partition *info) { return -1; }
Simon Glass54c709a2023-08-24 13:55:24 -0600367static inline int part_get_info_whole_disk(struct blk_desc *desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600368 struct disk_partition *info)
Rob Clark2b7bfd92017-09-09 13:15:55 -0400369{ return -1; }
Simon Glass54c709a2023-08-24 13:55:24 -0600370static inline void part_print(struct blk_desc *desc) {}
371static inline void part_init(struct blk_desc *desc) {}
372static inline void dev_print(struct blk_desc *desc) {}
Simon Glasse6649a62016-02-29 15:25:43 -0700373static inline int blk_get_device_by_str(const char *ifname, const char *dev_str,
Simon Glass54c709a2023-08-24 13:55:24 -0600374 struct blk_desc **desc)
Stephen Warrenafd909c2012-09-21 09:50:56 +0000375{ return -1; }
Simon Glasse76ee972016-02-29 15:25:44 -0700376static inline int blk_get_device_part_str(const char *ifname,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600377 const char *dev_part_str,
Simon Glass54c709a2023-08-24 13:55:24 -0600378 struct blk_desc **desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600379 struct disk_partition *info,
380 int allow_whole_dev)
Simon Glass54c709a2023-08-24 13:55:24 -0600381{ *desc = NULL; return -1; }
AKASHI Takahiro140c59c2022-04-19 10:01:54 +0900382
Simon Glass54c709a2023-08-24 13:55:24 -0600383static inline int part_get_info_by_name(struct blk_desc *desc, const char *name,
AKASHI Takahiro140c59c2022-04-19 10:01:54 +0900384 struct disk_partition *info)
385{
386 return -ENOENT;
387}
388
Sean Andersonf476b4a2021-04-12 18:53:05 -0400389static inline int
390part_get_info_by_dev_and_name_or_num(const char *dev_iface,
391 const char *dev_part_str,
Simon Glass54c709a2023-08-24 13:55:24 -0600392 struct blk_desc **desc,
Sean Andersonf476b4a2021-04-12 18:53:05 -0400393 struct disk_partition *part_info,
394 int allow_whole_dev)
395{
Simon Glass54c709a2023-08-24 13:55:24 -0600396 *desc = NULL;
Sean Andersonf476b4a2021-04-12 18:53:05 -0400397 return -ENOSYS;
398}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000399#endif
wdenk012771d2002-03-08 21:31:05 +0000400
AKASHI Takahiroefb9a9d2022-04-19 10:05:09 +0900401struct udevice;
402/**
Simon Glass4e93eea2022-10-20 18:22:52 -0600403 * disk_blk_read() - read blocks from a disk partition
404 *
405 * @dev: Device to read from (UCLASS_PARTITION)
406 * @start: Start block number to read in the partition (0=first)
407 * @blkcnt: Number of blocks to read
408 * @buffer: Destination buffer for data read
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200409 * Return: number of blocks read, or -ve error number (see the
Simon Glass4e93eea2022-10-20 18:22:52 -0600410 * IS_ERR_VALUE() macro
411 */
412ulong disk_blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
413 void *buffer);
414
415/**
416 * disk_blk_write() - write to a disk partition
417 *
418 * @dev: Device to write to (UCLASS_PARTITION)
419 * @start: Start block number to write in the partition (0=first)
420 * @blkcnt: Number of blocks to write
421 * @buffer: Source buffer for data to write
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200422 * Return: number of blocks written, or -ve error number (see the
Simon Glass4e93eea2022-10-20 18:22:52 -0600423 * IS_ERR_VALUE() macro
424 */
425ulong disk_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
426 const void *buffer);
427
428/**
429 * disk_blk_erase() - erase a section of a disk partition
430 *
431 * @dev: Device to (partially) erase (UCLASS_PARTITION)
432 * @start: Start block number to erase in the partition (0=first)
433 * @blkcnt: Number of blocks to erase
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200434 * Return: number of blocks erased, or -ve error number (see the
Simon Glass4e93eea2022-10-20 18:22:52 -0600435 * IS_ERR_VALUE() macro
436 */
437ulong disk_blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +0900438
Simon Glass83ce5632016-02-29 15:25:47 -0700439/*
440 * We don't support printing partition information in SPL and only support
441 * getting partition information in a few cases.
442 */
Simon Glass209ae762024-09-29 19:49:49 -0600443#ifdef CONFIG_XPL_BUILD
Simon Glass83ce5632016-02-29 15:25:47 -0700444# define part_print_ptr(x) NULL
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800445# if defined(CONFIG_SPL_FS_EXT4) || defined(CONFIG_SPL_FS_FAT) || \
Simon Glass83ce5632016-02-29 15:25:47 -0700446 defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
447# define part_get_info_ptr(x) x
448# else
449# define part_get_info_ptr(x) NULL
450# endif
451#else
452#define part_print_ptr(x) x
453#define part_get_info_ptr(x) x
wdenk012771d2002-03-08 21:31:05 +0000454#endif
455
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200456/**
457 * struct part_driver - partition driver
458 */
Simon Glass83ce5632016-02-29 15:25:47 -0700459struct part_driver {
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200460 /** @name: partition name */
Simon Glass83ce5632016-02-29 15:25:47 -0700461 const char *name;
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200462 /** @part_type: (MBR) partition type */
Simon Glass83ce5632016-02-29 15:25:47 -0700463 int part_type;
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200464 /** @max_entries: maximum number of partition table entries */
465 const int max_entries;
Simon Glass83ce5632016-02-29 15:25:47 -0700466 /**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200467 * @get_info: Get information about a partition
Simon Glass83ce5632016-02-29 15:25:47 -0700468 *
Simon Glass54c709a2023-08-24 13:55:24 -0600469 * @get_info.desc: Block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200470 * @get_info.part: Partition number (1 = first)
471 * @get_info.info: Returns partition information
Simon Glass83ce5632016-02-29 15:25:47 -0700472 */
Simon Glass54c709a2023-08-24 13:55:24 -0600473 int (*get_info)(struct blk_desc *desc, int part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600474 struct disk_partition *info);
Simon Glass83ce5632016-02-29 15:25:47 -0700475
476 /**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200477 * @print: Print partition information
Simon Glass83ce5632016-02-29 15:25:47 -0700478 *
Simon Glass54c709a2023-08-24 13:55:24 -0600479 * @print.desc: Block device descriptor
Simon Glass83ce5632016-02-29 15:25:47 -0700480 */
Simon Glass54c709a2023-08-24 13:55:24 -0600481 void (*print)(struct blk_desc *desc);
Simon Glass83ce5632016-02-29 15:25:47 -0700482
483 /**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200484 * @test: Test if a device contains this partition type
Simon Glass83ce5632016-02-29 15:25:47 -0700485 *
Simon Glass54c709a2023-08-24 13:55:24 -0600486 * @test.desc: Block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200487 * @test.Return:
488 * 0 if the block device appears to contain this partition type,
489 * -ve if not
Simon Glass83ce5632016-02-29 15:25:47 -0700490 */
Simon Glass54c709a2023-08-24 13:55:24 -0600491 int (*test)(struct blk_desc *desc);
Simon Glass83ce5632016-02-29 15:25:47 -0700492};
493
494/* Declare a new U-Boot partition 'driver' */
495#define U_BOOT_PART_TYPE(__name) \
496 ll_entry_declare(struct part_driver, __name, part_driver)
wdenk452cfd62002-11-19 11:04:11 +0000497
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100498#include <part_efi.h>
Rob Clark224ee842017-09-13 18:05:24 -0400499
500#if CONFIG_IS_ENABLED(EFI_PARTITION)
richardretanubune6745592008-09-26 11:13:22 -0400501/* disk/part_efi.c */
Steve Raef7f10b82014-05-26 11:52:24 -0700502/**
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100503 * write_gpt_table() - Write the GUID Partition Table to disk
504 *
Simon Glass54c709a2023-08-24 13:55:24 -0600505 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200506 * @gpt_h: pointer to GPT header representation
507 * @gpt_e: pointer to GPT partition table entries
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100508 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200509 * Return: zero on success, otherwise error
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100510 */
Simon Glass54c709a2023-08-24 13:55:24 -0600511int write_gpt_table(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100512
513/**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200514 * gpt_fill_pte() - Fill the GPT partition table entry
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100515 *
Simon Glass54c709a2023-08-24 13:55:24 -0600516 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200517 * @gpt_h: GPT header representation
518 * @gpt_e: GPT partition table entries
519 * @partitions: list of partitions
520 * @parts: number of partitions
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100521 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200522 * Return: zero on success
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100523 */
Simon Glass54c709a2023-08-24 13:55:24 -0600524int gpt_fill_pte(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600525 struct disk_partition *partitions, int parts);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100526
527/**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200528 * gpt_fill_header() - Fill the GPT header
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100529 *
Simon Glass54c709a2023-08-24 13:55:24 -0600530 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200531 * @gpt_h: GPT header representation
532 * @str_guid: disk guid string representation
533 * @parts_count: number of partitions
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100534 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200535 * Return: error on str_guid conversion error
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100536 */
Simon Glass54c709a2023-08-24 13:55:24 -0600537int gpt_fill_header(struct blk_desc *desc, gpt_header *gpt_h, char *str_guid,
538 int parts_count);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100539
540/**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200541 * gpt_restore() - Restore GPT partition table
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100542 *
Simon Glass54c709a2023-08-24 13:55:24 -0600543 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200544 * @str_disk_guid: disk GUID
545 * @partitions: list of partitions
546 * @parts_count: number of partitions
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100547 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200548 * Return: 0 on success
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100549 */
Simon Glass54c709a2023-08-24 13:55:24 -0600550int gpt_restore(struct blk_desc *desc, char *str_disk_guid,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600551 struct disk_partition *partitions, const int parts_count);
Steve Rae7d059342014-12-12 15:51:54 -0800552
553/**
554 * is_valid_gpt_buf() - Ensure that the Primary GPT information is valid
555 *
Simon Glass54c709a2023-08-24 13:55:24 -0600556 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200557 * @buf: buffer which contains the MBR and Primary GPT info
Steve Rae7d059342014-12-12 15:51:54 -0800558 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200559 * Return: 0 on success, otherwise error
Steve Rae7d059342014-12-12 15:51:54 -0800560 */
Simon Glass54c709a2023-08-24 13:55:24 -0600561int is_valid_gpt_buf(struct blk_desc *desc, void *buf);
Steve Rae7d059342014-12-12 15:51:54 -0800562
563/**
564 * write_mbr_and_gpt_partitions() - write MBR, Primary GPT and Backup GPT
565 *
Simon Glass54c709a2023-08-24 13:55:24 -0600566 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200567 * @buf: buffer which contains the MBR and Primary GPT info
Steve Rae7d059342014-12-12 15:51:54 -0800568 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200569 * Return: 0 on success, otherwise error
Steve Rae7d059342014-12-12 15:51:54 -0800570 */
Simon Glass54c709a2023-08-24 13:55:24 -0600571int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100572
573/**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200574 * gpt_verify_headers() - Read and check CRC32 of the GPT's header
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100575 * and partition table entries (PTE)
576 *
577 * As a side effect if sets gpt_head and gpt_pte so they point to GPT data.
578 *
Simon Glass54c709a2023-08-24 13:55:24 -0600579 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200580 * @gpt_head: pointer to GPT header data read from medium
581 * @gpt_pte: pointer to GPT partition table enties read from medium
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100582 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200583 * Return: 0 on success, otherwise error
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100584 */
Simon Glass54c709a2023-08-24 13:55:24 -0600585int gpt_verify_headers(struct blk_desc *desc, gpt_header *gpt_head,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100586 gpt_entry **gpt_pte);
587
588/**
Philippe Reynes8b054f52022-04-22 17:46:48 +0200589 * gpt_repair_headers() - Function to repair the GPT's header
590 * and partition table entries (PTE)
591 *
Simon Glass54c709a2023-08-24 13:55:24 -0600592 * @desc: block device descriptor
Philippe Reynes8b054f52022-04-22 17:46:48 +0200593 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200594 * Return: 0 on success, otherwise error
Philippe Reynes8b054f52022-04-22 17:46:48 +0200595 */
Simon Glass54c709a2023-08-24 13:55:24 -0600596int gpt_repair_headers(struct blk_desc *desc);
Philippe Reynes8b054f52022-04-22 17:46:48 +0200597
598/**
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100599 * gpt_verify_partitions() - Function to check if partitions' name, start and
600 * size correspond to '$partitions' env variable
601 *
602 * This function checks if on medium stored GPT data is in sync with information
603 * provided in '$partitions' environment variable. Specificially, name, start
604 * and size of the partition is checked.
605 *
Simon Glass54c709a2023-08-24 13:55:24 -0600606 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200607 * @partitions: partition data read from '$partitions' env variable
608 * @parts: number of partitions read from '$partitions' env variable
609 * @gpt_head: pointer to GPT header data read from medium
610 * @gpt_pte: pointer to GPT partition table enties read from medium
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100611 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200612 * Return: 0 on success, otherwise error
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100613 */
Simon Glass54c709a2023-08-24 13:55:24 -0600614int gpt_verify_partitions(struct blk_desc *desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600615 struct disk_partition *partitions, int parts,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100616 gpt_header *gpt_head, gpt_entry **gpt_pte);
Alison Chaikene4222582017-06-25 16:43:23 -0700617
Alison Chaikene4222582017-06-25 16:43:23 -0700618/**
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200619 * get_disk_guid() - Read the GUID string from a device's GPT
Alison Chaikene4222582017-06-25 16:43:23 -0700620 *
621 * This function reads the GUID string from a block device whose descriptor
622 * is provided.
623 *
Simon Glass54c709a2023-08-24 13:55:24 -0600624 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200625 * @guid: pre-allocated string in which to return the GUID
Alison Chaikene4222582017-06-25 16:43:23 -0700626 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200627 * Return: 0 on success, otherwise error
Alison Chaikene4222582017-06-25 16:43:23 -0700628 */
Simon Glass54c709a2023-08-24 13:55:24 -0600629int get_disk_guid(struct blk_desc *desc, char *guid);
Alison Chaikene4222582017-06-25 16:43:23 -0700630
richardretanubune6745592008-09-26 11:13:22 -0400631#endif
632
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100633#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200634/**
635 * is_valid_dos_buf() - Ensure that a DOS MBR image is valid
636 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200637 * @buf: buffer which contains the MBR
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200638 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200639 * Return: 0 on success, otherwise error
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200640 */
641int is_valid_dos_buf(void *buf);
642
643/**
Marek Szyprowski29ee06b2020-12-23 13:55:10 +0100644 * write_mbr_sector() - write DOS MBR
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200645 *
Simon Glass54c709a2023-08-24 13:55:24 -0600646 * @desc: block device descriptor
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200647 * @buf: buffer which contains the MBR
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200648 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200649 * Return: 0 on success, otherwise error
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200650 */
Simon Glass54c709a2023-08-24 13:55:24 -0600651int write_mbr_sector(struct blk_desc *desc, void *buf);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200652
Marek Szyprowski6a60b672020-12-23 13:55:14 +0100653int write_mbr_partitions(struct blk_desc *dev,
654 struct disk_partition *p, int count, unsigned int disksig);
655int layout_mbr_partitions(struct disk_partition *p, int count,
656 lbaint_t total_sectors);
657
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200658#endif
659
AKASHI Takahiro60bb6d72022-04-19 10:01:53 +0900660#if CONFIG_IS_ENABLED(PARTITIONS)
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530661/**
662 * part_driver_get_count() - get partition driver count
663 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200664 * Return: number of partition drivers
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530665 */
666static inline int part_driver_get_count(void)
667{
668 return ll_entry_count(struct part_driver, part_driver);
669}
670
671/**
672 * part_driver_get_first() - get first partition driver
673 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200674 * Return: pointer to first partition driver on success, otherwise NULL
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530675 */
676static inline struct part_driver *part_driver_get_first(void)
677{
678 return ll_entry_start(struct part_driver, part_driver);
679}
680
Simon Glassed338b72023-07-15 21:38:47 -0600681/**
682 * part_get_type_by_name() - Get partition type by name
683 *
Heinrich Schuchardt4a45c722023-08-15 12:30:19 +0200684 * @name: Name of partition type to look up (not case-sensitive)
685 * Return:
686 * Corresponding partition type (PART\_TYPE\_...) or PART\_TYPE\_UNKNOWN
Simon Glassed338b72023-07-15 21:38:47 -0600687 */
688int part_get_type_by_name(const char *name);
689
Simon Glass1e7f4472023-08-24 13:55:42 -0600690/**
691 * part_get_bootable() - Find the first bootable partition
692 *
Heinrich Schuchardt04044f92024-01-16 15:00:20 +0100693 * @desc: Block-device descriptor
694 * Return: first bootable partition, or 0 if there is none
Simon Glass1e7f4472023-08-24 13:55:42 -0600695 */
696int part_get_bootable(struct blk_desc *desc);
697
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530698#else
699static inline int part_driver_get_count(void)
700{ return 0; }
701
702static inline struct part_driver *part_driver_get_first(void)
703{ return NULL; }
Simon Glass1e7f4472023-08-24 13:55:42 -0600704
705static inline bool part_get_bootable(struct blk_desc *desc)
706{ return false; }
707
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530708#endif /* CONFIG_PARTITIONS */
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200709
wdenk012771d2002-03-08 21:31:05 +0000710#endif /* _PART_H */