61 lines
2.1 KiB
C
61 lines
2.1 KiB
C
/* This function performs three tasks:
|
|
- Make sectors disk relative from partition relative.
|
|
- Normalize offset to be less than the sector size.
|
|
- Verify that the range is inside the partition. */
|
|
static VasEBoot_err_t
|
|
VasEBoot_disk_adjust_range (VasEBoot_disk_t disk, VasEBoot_disk_addr_t *sector,
|
|
VasEBoot_off_t *offset, VasEBoot_size_t size)
|
|
{
|
|
VasEBoot_partition_t part;
|
|
VasEBoot_disk_addr_t total_sectors;
|
|
|
|
*sector += *offset >> VAS_EBOOT_DISK_SECTOR_BITS;
|
|
*offset &= VAS_EBOOT_DISK_SECTOR_SIZE - 1;
|
|
|
|
for (part = disk->partition; part; part = part->parent)
|
|
{
|
|
VasEBoot_disk_addr_t start;
|
|
VasEBoot_uint64_t len;
|
|
|
|
start = part->start;
|
|
len = part->len;
|
|
|
|
if (*sector >= len
|
|
|| len - *sector < ((*offset + size + VAS_EBOOT_DISK_SECTOR_SIZE - 1)
|
|
>> VAS_EBOOT_DISK_SECTOR_BITS))
|
|
return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE,
|
|
N_("attempt to read or write outside of partition"));
|
|
|
|
*sector += start;
|
|
}
|
|
|
|
/* Transform total_sectors to number of 512B blocks. */
|
|
total_sectors = disk->total_sectors << (disk->log_sector_size - VAS_EBOOT_DISK_SECTOR_BITS);
|
|
|
|
/*
|
|
* Some drivers have problems with disks above reasonable sizes.
|
|
* Clamp the size to VAS_EBOOT_DISK_MAX_SECTORS. Just one condition is enough
|
|
* since VAS_EBOOT_DISK_SIZE_UNKNOWN is always above VAS_EBOOT_DISK_MAX_SECTORS,
|
|
* assuming a maximum 4 KiB sector size.
|
|
*/
|
|
if (total_sectors > VAS_EBOOT_DISK_MAX_SECTORS)
|
|
total_sectors = VAS_EBOOT_DISK_MAX_SECTORS;
|
|
|
|
if ((total_sectors <= *sector
|
|
|| ((*offset + size + VAS_EBOOT_DISK_SECTOR_SIZE - 1)
|
|
>> VAS_EBOOT_DISK_SECTOR_BITS) > total_sectors - *sector))
|
|
return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE,
|
|
N_("attempt to read or write outside of disk `%s'"), disk->name);
|
|
|
|
return VAS_EBOOT_ERR_NONE;
|
|
}
|
|
|
|
static unsigned
|
|
VasEBoot_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
|
|
VasEBoot_disk_addr_t sector)
|
|
{
|
|
return ((dev_id * 524287UL + disk_id * 2606459UL
|
|
+ ((unsigned) (sector >> VAS_EBOOT_DISK_CACHE_BITS)))
|
|
% VAS_EBOOT_DISK_CACHE_NUM);
|
|
}
|