Skip to content

Commit e71fe81

Browse files
authored
Merge pull request #23 from 2427054102/v2
Fix Partition Rounding, Alignment, and Erase Logic
2 parents 93ad1e5 + 14117d8 commit e71fe81

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

ptool.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def CreateErasingRawProgramFiles():
397397
temp.append(Comment('NOTE: Sector size is %ibytes'%SECTOR_SIZE_IN_BYTES))
398398

399399
CreateFileOfZeros("zeros_33sectors.bin",33)
400-
UpdateRawProgram(temp,0, 0.5, i, 0, 1, "zeros_33sectors.bin", "false", "Overwrite MBR sector")
400+
UpdateRawProgram(temp,0, 1*SECTOR_SIZE_IN_BYTES/1024.0, i, 0, 1, "zeros_1sector.bin", "false", "Overwrite MBR sector")
401401
UpdateRawProgram(temp,1, BackupGPTNumLBAs*SECTOR_SIZE_IN_BYTES/1024.0, i, 0, BackupGPTNumLBAs, "zeros_%dsectors.bin" % BackupGPTNumLBAs, "false", "Overwrite Primary GPT Sectors")
402402

403403
backup_gpt_lba = -BackupGPTNumLBAs
@@ -493,23 +493,19 @@ 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))
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
498500

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
501+
##import pdb; pdb.set_trace() ## verifying sizes
504502

505-
##import pdb; pdb.set_trace() ## verifying sizes
506-
507-
if HashInstructions['PERFORMANCE_BOUNDARY_IN_KB']>0 and HashInstructions['ALIGN_PARTITIONS_TO_PERFORMANCE_BOUNDARY'] is False:
503+
if HashInstructions['PERFORMANCE_BOUNDARY_IN_KB']>0:
508504
PrintBigWarning("WARNING: HashInstructions['PERFORMANCE_BOUNDARY_IN_KB'] is %i KB\n\tbut HashInstructions['ALIGN_PARTITIONS_TO_PERFORMANCE_BOUNDARY'] is FALSE!!\n\n" % HashInstructions['PERFORMANCE_BOUNDARY_IN_KB'])
509505
PrintBigWarning("WARNING: This means partitions will *NOT* be aligned to a HashInstructions['PERFORMANCE_BOUNDARY_IN_KB'] of %i KB !!\n\n" % HashInstructions['PERFORMANCE_BOUNDARY_IN_KB'])
510506
print("To correct this, partition.xml should look like this\n")
511507
print("\t<parser_instructions>")
512-
print("\t\tPERFORMANCE_BOUNDARY_IN_KB = %i" % Partition['PERFORMANCE_BOUNDARY_IN_KB'])
508+
print("\t\tPERFORMANCE_BOUNDARY_IN_KB = %i" % HashInstructions['PERFORMANCE_BOUNDARY_IN_KB'])
513509
print("\t\tALIGN_PARTITIONS_TO_PERFORMANCE_BOUNDARY=true")
514510
print("\t</parser_instructions>\n\n")
515511

@@ -784,7 +780,7 @@ def CreateGPTPartitionTable(PhysicalPartitionNumber,UserProvided=False):
784780
PhysicalPartitionNumber,
785781
FileOffset[z],
786782
ConvertKBtoSectors(PhyPartition[k][j]['original_size_in_kb'])+LastLBA-FirstLBA+1-FilePartitionOffset[z],
787-
"zeros_33sectors.bin",
783+
"zeros_%dsectors.bin" % BackupGPTNumLBAs,
788784
"false",
789785
PartitionLabel,
790786
PhyPartition[k][j]['readbackverify'],
@@ -796,14 +792,14 @@ def CreateGPTPartitionTable(PhysicalPartitionNumber,UserProvided=False):
796792
PhysicalPartitionNumber,
797793
FileOffset[z],
798794
LastLBA-FirstLBA+1-FilePartitionOffset[z], # num_partition_sectors
799-
"zeros_33sectors.bin",
795+
"zeros_%dsectors.bin" % BackupGPTNumLBAs,
800796
"false",
801797
PartitionLabel,
802798
PhyPartition[k][j]['readbackverify'],
803799
PhyPartition[k][j]['partofsingleimage'])
804800

805801
if j==0:
806-
UpdateRawProgram(RawProgramXML_Blank,0, 33*SECTOR_SIZE_IN_BYTES/1024.0, PhysicalPartitionNumber, FileOffset[z], 33, "gpt_empty%d.bin" % k, "false", "PrimaryGPT", "false", "false")
802+
UpdateRawProgram(RawProgramXML_Blank,0, PrimaryGPTNumLBAs*SECTOR_SIZE_IN_BYTES/1024.0, PhysicalPartitionNumber, FileOffset[z], PrimaryGPTNumLBAs, "gpt_empty%d.bin" % k, "false", "PrimaryGPT", "false", "false")
807803

808804

809805
LastLBA += 1 ## move to the next free sector, also, 0 to 9 inclusive means it's 10
@@ -1004,7 +1000,7 @@ def CreateGPTPartitionTable(PhysicalPartitionNumber,UserProvided=False):
10041000
#ShowBackupGPT(32)
10051001

10061002
UpdateRawProgram(RawProgramXML,0, PrimaryGPTNumLBAs*SECTOR_SIZE_IN_BYTES/1024.0, PhysicalPartitionNumber, 0, PrimaryGPTNumLBAs, os.path.basename(GPTMAIN), 'false', 'PrimaryGPT','false','true')
1007-
UpdateRawProgram(RawProgramXML_Wipe,0, 1*SECTOR_SIZE_IN_BYTES/1024.0, PhysicalPartitionNumber, 0, 1, "zeros_33sectors.bin", 'false', 'PrimaryGPT','false','true')
1003+
UpdateRawProgram(RawProgramXML_Wipe,0, 1*SECTOR_SIZE_IN_BYTES/1024.0, PhysicalPartitionNumber, 0, 1, "zeros_1sector.bin", 'false', 'PrimaryGPT-MBR','false','true')
10081004
UpdateRawProgram(RawProgramXML_Wipe,1, BackupGPTNumLBAs*SECTOR_SIZE_IN_BYTES/1024.0, PhysicalPartitionNumber, 0, BackupGPTNumLBAs, "zeros_%dsectors.bin" % BackupGPTNumLBAs, 'false', 'PrimaryGPT','false','true')
10091005

10101006
#print "szStartSector=%s" % szStartSector
@@ -1359,7 +1355,7 @@ def ParseXML(XMLFile):
13591355

13601356
##timmy
13611357

1362-
TempSizeInBytes = int(value)*1024
1358+
TempSizeInBytes = math.ceil(float(value)) * 1024
13631359
if TempSizeInBytes < SECTOR_SIZE_IN_BYTES:
13641360
## smaller than a sector, which is possible if sector size is 4KB
13651361
TempSizeInBytes = SECTOR_SIZE_IN_BYTES

0 commit comments

Comments
 (0)