66 lines
2.2 KiB
C
66 lines
2.2 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 >> VasEBoot_DISK_SECTOR_BITS;
|
|
*offset &= VasEBoot_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 + VasEBoot_DISK_SECTOR_SIZE - 1)
|
|
>> VasEBoot_DISK_SECTOR_BITS))
|
|
return VasEBoot_error (VasEBoot_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 - VasEBoot_DISK_SECTOR_BITS);
|
|
|
|
/* Some drivers have problems with disks above reasonable.
|
|
Treat unknown as 1EiB disk. While on it, clamp the size to 1EiB.
|
|
Just one condition is enough since VasEBoot_DISK_UNKNOWN_SIZE << ls is always
|
|
above 9EiB.
|
|
*/
|
|
if (total_sectors > (1ULL << 51))
|
|
total_sectors = (1ULL << 51);
|
|
|
|
if ((total_sectors <= *sector
|
|
|| ((*offset + size + VasEBoot_DISK_SECTOR_SIZE - 1)
|
|
>> VasEBoot_DISK_SECTOR_BITS) > total_sectors - *sector))
|
|
return VasEBoot_error (VasEBoot_ERR_OUT_OF_RANGE,
|
|
N_("attempt to read or write outside of disk `%s'"), disk->name);
|
|
|
|
return VasEBoot_ERR_NONE;
|
|
}
|
|
|
|
static inline VasEBoot_disk_addr_t
|
|
transform_sector (VasEBoot_disk_t disk, VasEBoot_disk_addr_t sector)
|
|
{
|
|
return sector >> (disk->log_sector_size - VasEBoot_DISK_SECTOR_BITS);
|
|
}
|
|
|
|
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 >> VasEBoot_DISK_CACHE_BITS)))
|
|
% VasEBoot_DISK_CACHE_NUM);
|
|
}
|