:: commit e2e5e6dcf4c81931b429a7e858e33687aad497b0

Mintsuki <mintsuki@protonmail.com> — 2026-02-04 06:40

parents: 36885711f7

drivers/disk: Normalise sect_count to always be in 512-byte blocks

diff --git a/common/drivers/disk.s2.c b/common/drivers/disk.s2.c
index 2c9ccbfe..fba40b09 100644
--- a/common/drivers/disk.s2.c
+++ b/common/drivers/disk.s2.c
@@ -271,13 +271,20 @@ void disk_create_index(void) {
         block->drive = drive;
         block->partition = 0;
         block->first_sect = 0;
-        block->sect_count = drive_params.lba_count;
         block->max_partition = -1;
 
         if (!detect_sector_size(block)) {
             continue;
         }
 
+        // Normalize sect_count to 512-byte sectors for consistency with partitions
+        // Preserve (uint64_t)-1 sentinel value (means "unknown size")
+        if (drive_params.lba_count == (uint64_t)-1 || drive_params.lba_count == 0) {
+            block->sect_count = (uint64_t)-1;
+        } else {
+            block->sect_count = drive_params.lba_count * (block->sector_size / 512);
+        }
+
         // Detect optical drives via DPTE ATAPI bit (bit 6) or sector size heuristic
         bool is_atapi = (dpte != NULL && (dpte->flags & (1 << 6)));
         block->is_optical = is_atapi || (block->sector_size == 2048 && is_removable);
@@ -654,7 +661,8 @@ static void find_unique_sectors(void) {
 
         size_t first_sect = (volume_index[i]->first_sect * 512) / volume_index[i]->sector_size;
 
-        if (volume_index[i]->sect_count * volume_index[i]->sector_size < UNIQUE_SECTOR_POOL_SIZE) {
+        // sect_count is always in 512-byte sectors
+        if (volume_index[i]->sect_count * 512 < UNIQUE_SECTOR_POOL_SIZE) {
             continue;
         }
 
@@ -769,7 +777,8 @@ fail:
         block->partition = 0;
         block->sector_size = drive->Media->BlockSize;
         block->first_sect = 0;
-        block->sect_count = drive->Media->LastBlock + 1;
+        // Normalize sect_count to 512-byte sectors for consistency with partitions
+        block->sect_count = (drive->Media->LastBlock + 1) * (drive->Media->BlockSize / 512);
         block->max_partition = -1;
 
         if (drive->Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION3) {
diff --git a/common/lib/part.s2.c b/common/lib/part.s2.c
index 2db4b07f..35d14856 100644
--- a/common/lib/part.s2.c
+++ b/common/lib/part.s2.c
@@ -75,8 +75,9 @@ bool volume_read(struct volume *volume, void *buffer, uint64_t loc, uint64_t cou
     }
 
     if (volume->sect_count != (uint64_t)-1) {
+        // sect_count is always in 512-byte sectors for both whole disks and partitions
         uint64_t part_size;
-        if (__builtin_mul_overflow(volume->sect_count, volume->sector_size, &part_size)) {
+        if (__builtin_mul_overflow(volume->sect_count, (uint64_t)512, &part_size)) {
             return false;
         }
         if (loc >= part_size || count > part_size - loc) {
tab: 248 wrap: offon