Partition API

const char *disk_partition_type_guid(const struct disk_partition *info)

get partition type GUID

Parameters

const struct disk_partition *info

partition information

Description

By using this function to get the partition type GUID we can use ‘if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))’ instead of ‘#ifdef CONFIG_PARTITION_TYPE_GUID’.

Return

partition type GUID

void disk_partition_set_type_guid(struct disk_partition *info, const char *val)

set partition type GUID

Parameters

struct disk_partition *info

partition information

const char *val

partition type GUID as string

Description

By using this function to set the partition type GUID we can use ‘if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))’ instead of ‘#ifdef CONFIG_PARTITION_TYPE_GUID’.

struct blk_desc *blk_get_dev(const char *ifname, int dev)

get a pointer to a block device given its type and number

Parameters

const char *ifname

Interface name (e.g. “ide”, “scsi”)

int dev

Device number (0 for first device on that interface, 1 for second, etc.

Description

Each interface allocates its own devices and typically struct blk_desc is contained with the interface’s data structure. There is no global numbering for block devices, so the interface name must be provided.

Return

pointer to the block device, or NULL if not available, or an error occurred.

int part_get_info_by_type(struct blk_desc *desc, int part, int part_type, struct disk_partition *info)

Get partitions from a block device using a specific partition driver

Parameters

struct blk_desc *desc

Block device descriptor

int part

Partition number to read

int part_type

Partition driver to use, or PART_TYPE_UNKNOWN to automatically choose a driver

struct disk_partition *info

Returned partition information

Description

Each interface allocates its own devices and typically struct blk_desc is contained with the interface’s data structure. There is no global numbering for block devices, so the interface name must be provided.

Return

0 on success, negative errno on failure

int part_get_info_whole_disk(struct blk_desc *desc, struct disk_partition *info)

get partition info for the special case of a partition occupying the entire disk.

Parameters

struct blk_desc *desc

block device descriptor

struct disk_partition *info

returned partition information

Return

0 on success

int blk_get_device_by_str(const char *ifname, const char *dev_str, struct blk_desc **desc)

Get a block device given its interface/hw partition

Parameters

const char *ifname

Interface name (e.g. “ide”, “scsi”)

const char *dev_str

Device and optional hw partition. This can either be a string containing the device number (e.g. “2”) or the device number and hardware partition number (e.g. “2.4”) for devices that support it (currently only MMC).

struct blk_desc **desc

Returns a pointer to the block device on success

Description

Each interface allocates its own devices and typically struct blk_desc is contained with the interface’s data structure. There is no global numbering for block devices, so the interface name must be provided.

The hardware parition is not related to the normal software partitioning of a device - each hardware partition is effectively a separately accessible block device. When a hardware parition is selected on MMC the other hardware partitions become inaccessible. The same block device is used to access all hardware partitions, but its capacity may change when a different hardware partition is selected.

When a hardware partition number is given, the block device switches to that hardware partition.

Return

block device number (local to the interface), or -1 on error

int blk_get_device_part_str(const char *ifname, const char *dev_part_str, struct blk_desc **desc, struct disk_partition *info, int allow_whole_dev)

Get a block device and partition

Parameters

const char *ifname

Interface name (e.g. “ide”, “scsi”)

const char *dev_part_str

Device and partition string

struct blk_desc **desc

Returns a pointer to the block device on success

struct disk_partition *info

Returns partition information

int allow_whole_dev

true to allow the user to select partition 0 (which means the whole device), false to require a valid partition number >= 1

Description

This calls blk_get_device_by_str() to look up a device. It also looks up a partition and returns information about it.

dev_part_str is in the format <dev>.<hw_part>:<part> where

  • <dev> is the device number,

  • <hw_part> is the optional hardware partition number and

  • <part> is the partition number.

If ifname is “hostfs”, then this function returns the sandbox host block device.

If ifname is “ubi”, then this function returns 0, with info set to a special UBI device.

If dev_part_str is NULL or empty or “-”, then this function looks up the “bootdevice” environment variable and uses that string instead.

If the partition string is empty then the first partition is used. If the partition string is “auto” then the first bootable partition is used.

Return

partition number, or -1 on error

int part_get_info_by_name(struct blk_desc *desc, const char *name, struct disk_partition *info)

Search for a partition by name among all available registered partitions

Parameters

struct blk_desc *desc

block device descriptor

const char *name

the specified table entry name

struct disk_partition *info

returns the disk partition info

Return

the partition number on match (starting on 1), -1 on no match, otherwise error

int part_get_info_by_dev_and_name_or_num(const char *dev_iface, const char *dev_part_str, struct blk_desc **desc, struct disk_partition *part_info, int allow_whole_dev)

Get partition info from dev number and part name, or dev number and part number.

Parameters

const char *dev_iface

Device interface

const char *dev_part_str

Input partition description, like “0#misc” or “0:1”

struct blk_desc **desc

Place to store the device description pointer

struct disk_partition *part_info

Place to store the partition information

int allow_whole_dev

true to allow the user to select partition 0 (which means the whole device), false to require a valid partition number >= 1

Description

Parse a device number and partition description (either name or number) in the form of device number plus partition name separated by a “#” (like “device_num#partition_name”) or a device number plus a partition number separated by a “:”. For example both “0#misc” and “0:1” can be valid partition descriptions for a given interface. If the partition is found, sets desc and part_info accordingly with the information of the partition.

Return

the partition number on success, or negative errno on error

void part_set_generic_name(const struct blk_desc *desc, int part_num, char *name)

create generic partition like hda1 or sdb2

Parameters

const struct blk_desc *desc

pointer to the block device

int part_num

partition number for which the name is generated

char *name

buffer where the name is written

Description

Helper function for partition tables, which don’t hold partition names (DOS, ISO). Generates partition name out of the device type and partition number.

ulong disk_blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buffer)

read blocks from a disk partition

Parameters

struct udevice *dev

Device to read from (UCLASS_PARTITION)

lbaint_t start

Start block number to read in the partition (0=first)

lbaint_t blkcnt

Number of blocks to read

void *buffer

Destination buffer for data read

Return

number of blocks read, or -ve error number (see the IS_ERR_VALUE() macro

ulong disk_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, const void *buffer)

write to a disk partition

Parameters

struct udevice *dev

Device to write to (UCLASS_PARTITION)

lbaint_t start

Start block number to write in the partition (0=first)

lbaint_t blkcnt

Number of blocks to write

const void *buffer

Source buffer for data to write

Return

number of blocks written, or -ve error number (see the IS_ERR_VALUE() macro

ulong disk_blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)

erase a section of a disk partition

Parameters

struct udevice *dev

Device to (partially) erase (UCLASS_PARTITION)

lbaint_t start

Start block number to erase in the partition (0=first)

lbaint_t blkcnt

Number of blocks to erase

Return

number of blocks erased, or -ve error number (see the IS_ERR_VALUE() macro

struct part_driver

partition driver

Definition

struct part_driver {
  const char *name;
  int part_type;
  const int max_entries;
  int (*get_info)(struct blk_desc *desc, int part, struct disk_partition *info);
  void (*print)(struct blk_desc *desc);
  int (*test)(struct blk_desc *desc);
};

Members

name

partition name

part_type

(MBR) partition type

max_entries

maximum number of partition table entries

get_info

Get information about a partition

get_info.desc: Block device descriptor get_info.part: Partition number (1 = first) get_info.info: Returns partition information

print

Print partition information

print.desc: Block device descriptor

test

Test if a device contains this partition type

test.desc: Block device descriptor test.Return: 0 if the block device appears to contain this partition type, -ve if not

int write_gpt_table(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e)

Write the GUID Partition Table to disk

Parameters

struct blk_desc *desc

block device descriptor

gpt_header *gpt_h

pointer to GPT header representation

gpt_entry *gpt_e

pointer to GPT partition table entries

Return

zero on success, otherwise error

int gpt_fill_pte(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e, struct disk_partition *partitions, int parts)

Fill the GPT partition table entry

Parameters

struct blk_desc *desc

block device descriptor

gpt_header *gpt_h

GPT header representation

gpt_entry *gpt_e

GPT partition table entries

struct disk_partition *partitions

list of partitions

int parts

number of partitions

Return

zero on success

int gpt_fill_header(struct blk_desc *desc, gpt_header *gpt_h, char *str_guid, int parts_count)

Fill the GPT header

Parameters

struct blk_desc *desc

block device descriptor

gpt_header *gpt_h

GPT header representation

char *str_guid

disk guid string representation

int parts_count

number of partitions

Return

error on str_guid conversion error

int gpt_restore(struct blk_desc *desc, char *str_disk_guid, struct disk_partition *partitions, const int parts_count)

Restore GPT partition table

Parameters

struct blk_desc *desc

block device descriptor

char *str_disk_guid

disk GUID

struct disk_partition *partitions

list of partitions

const int parts_count

number of partitions

Return

0 on success

int is_valid_gpt_buf(struct blk_desc *desc, void *buf)

Ensure that the Primary GPT information is valid

Parameters

struct blk_desc *desc

block device descriptor

void *buf

buffer which contains the MBR and Primary GPT info

Return

0 on success, otherwise error

int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)

write MBR, Primary GPT and Backup GPT

Parameters

struct blk_desc *desc

block device descriptor

void *buf

buffer which contains the MBR and Primary GPT info

Return

0 on success, otherwise error

int gpt_verify_headers(struct blk_desc *desc, gpt_header *gpt_head, gpt_entry **gpt_pte)

Read and check CRC32 of the GPT’s header and partition table entries (PTE)

Parameters

struct blk_desc *desc

block device descriptor

gpt_header *gpt_head

pointer to GPT header data read from medium

gpt_entry **gpt_pte

pointer to GPT partition table enties read from medium

Description

As a side effect if sets gpt_head and gpt_pte so they point to GPT data.

Return

0 on success, otherwise error

int gpt_repair_headers(struct blk_desc *desc)

Function to repair the GPT’s header and partition table entries (PTE)

Parameters

struct blk_desc *desc

block device descriptor

Return

0 on success, otherwise error

int gpt_verify_partitions(struct blk_desc *desc, struct disk_partition *partitions, int parts, gpt_header *gpt_head, gpt_entry **gpt_pte)

Function to check if partitions’ name, start and size correspond to ‘$partitions’ env variable

Parameters

struct blk_desc *desc

block device descriptor

struct disk_partition *partitions

partition data read from ‘$partitions’ env variable

int parts

number of partitions read from ‘$partitions’ env variable

gpt_header *gpt_head

pointer to GPT header data read from medium

gpt_entry **gpt_pte

pointer to GPT partition table enties read from medium

Description

This function checks if on medium stored GPT data is in sync with information provided in ‘$partitions’ environment variable. Specificially, name, start and size of the partition is checked.

Return

0 on success, otherwise error

int get_disk_guid(struct blk_desc *desc, char *guid)

Read the GUID string from a device’s GPT

Parameters

struct blk_desc *desc

block device descriptor

char *guid

pre-allocated string in which to return the GUID

Description

This function reads the GUID string from a block device whose descriptor is provided.

Return

0 on success, otherwise error

int is_valid_dos_buf(void *buf)

Ensure that a DOS MBR image is valid

Parameters

void *buf

buffer which contains the MBR

Return

0 on success, otherwise error

int write_mbr_sector(struct blk_desc *desc, void *buf)

write DOS MBR

Parameters

struct blk_desc *desc

block device descriptor

void *buf

buffer which contains the MBR

Return

0 on success, otherwise error

int part_driver_get_count(void)

get partition driver count

Parameters

void

no arguments

Return

number of partition drivers

struct part_driver *part_driver_get_first(void)

get first partition driver

Parameters

void

no arguments

Return

pointer to first partition driver on success, otherwise NULL

int part_get_type_by_name(const char *name)

Get partition type by name

Parameters

const char *name

Name of partition type to look up (not case-sensitive)

Return

Corresponding partition type (PART_TYPE_…) or PART_TYPE_UNKNOWN

int part_get_bootable(struct blk_desc *desc)

Find the first bootable partition

Parameters

struct blk_desc *desc

Block-device descriptor

Return

first bootable partition, or 0 if there is none