Skip to content

Commit 1cb091b

Browse files
committed
ptool: Fix partition sector rounding behavior
Fix partition sector rounding to avoid decimal sector counts. Ensure partition memory size is rounded up to the next whole sector. Prevent insufficient space allocation due to fractional sector values. Signed-off-by: Xiaoming Zhao <[email protected]>
1 parent e0a6c52 commit 1cb091b

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

ptool.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,10 @@ def CreateGPTPartitionTable(PhysicalPartitionNumber,UserProvided=False):
493493

494494
print("\n"+"="*78)
495495

496-
PhyPartition[k][j]['size_in_kb'] = int(PhyPartition[k][j]['size_in_kb'])
497-
print("\n\n%d of %d \"%s\" (readonly=%s) and size=%dKB (%dMB) (%i sectors with %i bytes/sector)" %(j+1, len(PhyPartition[k]), PhyPartition[k][j]['label'], PhyPartition[k][j]['readonly'], PhyPartition[k][j]['size_in_kb'], PhyPartition[k][j]['size_in_kb']/1024, ConvertKBtoSectors(PhyPartition[k][j]['size_in_kb']), SECTOR_SIZE_IN_BYTES))
498-
499-
if (PhyPartition[k][j]['size_in_kb']*1024)%SECTOR_SIZE_IN_BYTES>0:
500-
## Have a remainder, need to round up to next full sector
501-
TempResult = (PhyPartition[k][j]['size_in_kb']*1024)/SECTOR_SIZE_IN_BYTES
502-
TempResult +=1
503-
PhyPartition[k][j]['size_in_kb'] = (TempResult * SECTOR_SIZE_IN_BYTES)/1024
496+
size_kb = float(PhyPartition[k][j]['size_in_kb'])
497+
size_bytes = math.ceil(size_kb * 1024) #Count the number of bytes
498+
sectors = math.ceil(size_bytes / SECTOR_SIZE_IN_BYTES) #Calculate the number of sectors rounded up
499+
PhyPartition[k][j]['size_in_kb'] = sectors * SECTOR_SIZE_IN_BYTES / 1024
504500

505501
##import pdb; pdb.set_trace() ## verifying sizes
506502

0 commit comments

Comments
 (0)