CardSight 0.1.0
C API reference for the CardSight firmware and application
Loading...
Searching...
No Matches
blkdev.h File Reference

Block devices: disks made by a driver, the partitions found in their table, and the checked transfers on them. More...

#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#include <hal/device.h>

Go to the source code of this file.

Data Structures

struct  block_device_operations
 Operations a disk driver provides to the block layer. More...
struct  block_device
 One openable view of a disk: the whole disk or one partition. More...
struct  gendisk
 A disk as a driver describes it to the block layer. More...
struct  blk_partition_layout_entry
 One partition to create, in disk order. More...

Macros

#define DISK_NAME_LEN   16
 Longest disk name, including the terminating zero.
#define BDEV_LABEL_LEN   32
 Longest partition label in bytes.
#define GENHD_FL_RDONLY   0x02
 Disk flag: write and discard are refused.

Enumerations

enum  blk_partition_table_type { BLK_PARTITION_TABLE_MBR , BLK_PARTITION_TABLE_GPT }
 Partition table format written by blk_write_partition_layout(). More...
enum  blk_partition_content { BLK_PARTITION_FAT32 , BLK_PARTITION_EXFAT , BLK_PARTITION_EXT4 }
 File system marker stored in a created partition. More...

Functions

struct gendisk * alloc_disk (void)
 Allocate a zeroed disk with its whole-disk block device.
void put_disk (struct gendisk *disk)
 Drop the caller's reference to a disk.
int add_disk (struct gendisk *disk)
 Register the disk and read its partition table.
int del_gendisk (struct gendisk *disk)
 Take the disk and its partitions away.
void del_gendisk_force (struct gendisk *disk)
 Remove the disk without checking its mounts.
struct block_device * bdev_open_by_name (const char *name)
 Open a registered block device by name.
void bdev_close (struct block_device *bdev)
 Drop one opener of a block device.
int bdev_read (struct block_device *bdev, uint64_t sector, void *buffer, uint32_t count)
 Read sectors through a block device view.
int bdev_write (struct block_device *bdev, uint64_t sector, const void *buffer, uint32_t count)
 Write sectors through a block device view.
int bdev_flush (struct block_device *bdev)
 Ask the disk behind a block device to write out its cached writes.
bool bdev_read_only (const struct block_device *bdev)
 Report whether a block device refuses writes.
uint64_t bdev_nr_sectors (const struct block_device *bdev)
 Length of a block device in sectors.
uint32_t bdev_sector_size (const struct block_device *bdev)
 Logical sector size of the disk behind a block device.
bool bdev_is_whole (const struct block_device *bdev)
 Report whether a block device covers a whole disk.
const char * bdev_name (const struct block_device *bdev)
 Name of a block device as registered.
int bdev_claim_mount (struct block_device *bdev)
 Claim a block device for one file system instance.
void bdev_release_mount (struct block_device *bdev)
 Release the mount claim of a block device.
int blk_erase_partition_table (struct gendisk *disk)
 Erase the MBR and both GPT copies.
int blk_write_partition_layout (struct gendisk *disk, enum blk_partition_table_type table, const struct blk_partition_layout_entry *entries, uint32_t count)
 Write a fresh partition table and rescan it.

Detailed Description

Block devices: disks made by a driver, the partitions found in their table, and the checked transfers on them.

A disk driver fills a struct gendisk from alloc_disk() and registers it with add_disk(); the whole disk is "part0" and the partitions are "<disk>p<N>". Access goes through a struct block_device opened by name.

The calls are synchronous and run in the caller's task; read and write buffers must be 64-byte aligned (the card driver does DMA on them).

Enumeration Type Documentation

◆ blk_partition_content

File system marker stored in a created partition.

Enumerator
BLK_PARTITION_FAT32 

FAT32 partition type.

BLK_PARTITION_EXFAT 

exFAT partition type.

BLK_PARTITION_EXT4 

Linux ext4 partition type.

◆ blk_partition_table_type

Partition table format written by blk_write_partition_layout().

Enumerator
BLK_PARTITION_TABLE_MBR 

Master boot record, four primary entries.

BLK_PARTITION_TABLE_GPT 

GUID partition table, primary and backup copies.

Function Documentation

◆ add_disk()

int add_disk ( struct gendisk * disk)

Register the disk and read its partition table.

The name and operations are validated, then the whole-disk view is registered and the MBR or GPT is scanned. A table that cannot be read is logged and the disk stays without partitions.

Parameters
[in]diskDisk to register.
Returns
0 on success, -EINVAL for a bad description, or -EEXIST when the name is taken.

◆ alloc_disk()

struct gendisk * alloc_disk ( void )

Allocate a zeroed disk with its whole-disk block device.

Returns
The disk, with part0 ready, or NULL when out of memory.

◆ bdev_claim_mount()

int bdev_claim_mount ( struct block_device * bdev)

Claim a block device for one file system instance.

A file system keeps a device claimed from its mount to its unmount.

Parameters
[in]bdevDevice to claim.
Returns
0 on success, -ENODEV when the disk is gone, or -EBUSY when already mounted.

◆ bdev_close()

void bdev_close ( struct block_device * bdev)

Drop one opener of a block device.

The last close of a live disk flushes it, so writes it held back reach the media.

Parameters
[in]bdevDevice to close; NULL is ignored.

◆ bdev_flush()

int bdev_flush ( struct block_device * bdev)

Ask the disk behind a block device to write out its cached writes.

Parameters
[in]bdevDevice to flush.
Returns
0 on success, or -ENODEV when the device or its disk is gone.

◆ bdev_is_whole()

bool bdev_is_whole ( const struct block_device * bdev)

Report whether a block device covers a whole disk.

Parameters
[in]bdevDevice to query.
Returns
true for the disk's partition 0, false for a partition.

◆ bdev_name()

const char * bdev_name ( const struct block_device * bdev)

Name of a block device as registered.

Parameters
[in]bdevDevice to query.
Returns
The device name, without the "/dev/" prefix.

◆ bdev_nr_sectors()

uint64_t bdev_nr_sectors ( const struct block_device * bdev)

Length of a block device in sectors.

Parameters
[in]bdevDevice to query.
Returns
The number of sectors in the view.

◆ bdev_open_by_name()

struct block_device * bdev_open_by_name ( const char * name)

Open a registered block device by name.

Parameters
[in]nameDevice name such as "mmcblk0", "mmcblk0p1", or "/dev/...".
Returns
The block device with a reference, or NULL when no live device matches.

◆ bdev_read()

int bdev_read ( struct block_device * bdev,
uint64_t sector,
void * buffer,
uint32_t count )

Read sectors through a block device view.

Parameters
[in]bdevDevice to read from.
[in]sectorFirst sector inside the device.
[out]bufferDestination buffer; 64-byte aligned.
[in]countNumber of sectors.
Returns
0 on success, or a negative errno when the device is gone or the range is invalid.

◆ bdev_read_only()

bool bdev_read_only ( const struct block_device * bdev)

Report whether a block device refuses writes.

Parameters
[in]bdevDevice to query.
Returns
true when the view or the whole disk is read-only.

◆ bdev_sector_size()

uint32_t bdev_sector_size ( const struct block_device * bdev)

Logical sector size of the disk behind a block device.

Parameters
[in]bdevDevice to query.
Returns
The block size in bytes.

◆ bdev_write()

int bdev_write ( struct block_device * bdev,
uint64_t sector,
const void * buffer,
uint32_t count )

Write sectors through a block device view.

Parameters
[in]bdevDevice to write to.
[in]sectorFirst sector inside the device.
[in]bufferSource buffer; 64-byte aligned.
[in]countNumber of sectors.
Returns
0 on success, -EROFS for a read-only device, or another negative errno.

◆ blk_erase_partition_table()

int blk_erase_partition_table ( struct gendisk * disk)

Erase the MBR and both GPT copies.

Zeroes the first 512 KiB (some cards carry a boot image in front of the table and the SoC would try to run it) and the backup GPT copy at the end of the disk, then rescans, leaving the card without a partition table. File data itself is left where it is.

Parameters
[in]diskRegistered disk to erase.
Returns
0 on success, -ENODEV for an unregistered disk, -ENOTSUP for a non-512-byte sector size, or -EBUSY while a partition is mounted.

◆ blk_write_partition_layout()

int blk_write_partition_layout ( struct gendisk * disk,
enum blk_partition_table_type table,
const struct blk_partition_layout_entry * entries,
uint32_t count )

Write a fresh partition table and rescan it.

Entries are placed with 1 MiB alignment, at most four of them for the MBR; the first entry starts at sector 2048. The table is flushed and rescanned before the call returns.

Parameters
[in]diskRegistered disk to write.
[in]tableTable format to write.
[in]entriesPartitions to create, in disk order.
[in]countNumber of entries in entries.
Returns
0 on success, -ENODEV for an unregistered disk, -ENOTSUP for a non-512-byte sector size, -EINVAL for an unknown table, -ENOSPC when the sizes do not fit, or -EBUSY while a partition is mounted.

◆ del_gendisk()

int del_gendisk ( struct gendisk * disk)

Take the disk and its partitions away.

Parameters
[in]diskDisk to remove.
Returns
0 on success, -ENODEV when it is not registered, or -EBUSY while one of its block devices is mounted.

◆ del_gendisk_force()

void del_gendisk_force ( struct gendisk * disk)

Remove the disk without checking its mounts.

The same as del_gendisk() but without the -EBUSY, for a card that is gone.

Parameters
[in]diskDisk to remove.

◆ put_disk()

void put_disk ( struct gendisk * disk)

Drop the caller's reference to a disk.

Call after del_gendisk(), or after a failed add_disk().

Parameters
[in]diskDisk to release; NULL is ignored.