disk_common.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* This function performs three tasks:
  2. - Make sectors disk relative from partition relative.
  3. - Normalize offset to be less than the sector size.
  4. - Verify that the range is inside the partition. */
  5. static grub_err_t
  6. grub_disk_adjust_range (grub_disk_t disk, grub_disk_addr_t *sector,
  7. grub_off_t *offset, grub_size_t size)
  8. {
  9. grub_partition_t part;
  10. grub_disk_addr_t total_sectors;
  11. *sector += *offset >> GRUB_DISK_SECTOR_BITS;
  12. *offset &= GRUB_DISK_SECTOR_SIZE - 1;
  13. for (part = disk->partition; part; part = part->parent)
  14. {
  15. grub_disk_addr_t start;
  16. grub_uint64_t len;
  17. start = part->start;
  18. len = part->len;
  19. if (*sector >= len
  20. || len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
  21. >> GRUB_DISK_SECTOR_BITS))
  22. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  23. N_("attempt to read or write outside of partition"));
  24. *sector += start;
  25. }
  26. /* Transform total_sectors to number of 512B blocks. */
  27. total_sectors = disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
  28. /* Some drivers have problems with disks above reasonable.
  29. Treat unknown as 1EiB disk. While on it, clamp the size to 1EiB.
  30. Just one condition is enough since GRUB_DISK_UNKNOWN_SIZE << ls is always
  31. above 9EiB.
  32. */
  33. if (total_sectors > (1ULL << 51))
  34. total_sectors = (1ULL << 51);
  35. if ((total_sectors <= *sector
  36. || ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
  37. >> GRUB_DISK_SECTOR_BITS) > total_sectors - *sector))
  38. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  39. N_("attempt to read or write outside of disk `%s'"), disk->name);
  40. return GRUB_ERR_NONE;
  41. }
  42. static inline grub_disk_addr_t
  43. transform_sector (grub_disk_t disk, grub_disk_addr_t sector)
  44. {
  45. return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
  46. }
  47. static unsigned
  48. grub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
  49. grub_disk_addr_t sector)
  50. {
  51. return ((dev_id * 524287UL + disk_id * 2606459UL
  52. + ((unsigned) (sector >> GRUB_DISK_CACHE_BITS)))
  53. % GRUB_DISK_CACHE_NUM);
  54. }