KurtE
Senior Member+
@mjs513 - I started hitting head with larger hammer
I was playing with creating the partition in the MBR before you actually do the format.
One reason is maybe the MBR is full or ...
I just finished hacking up pass 1 of it. Still some holes and have not run it yet...
So I hacked up the start of create partition like:
The new function:
As I mentioned in BUGBUG: I detected one form of overlap of the partitions, but not the other yet. Also potentially could modify that says something like:
Start at X and use what is available. Both for holes and at end...
Does any of this make sense? Or maybe the wrong rabbit hole
I was playing with creating the partition in the MBR before you actually do the format.
One reason is maybe the MBR is full or ...
I just finished hacking up pass 1 of it. Still some holes and have not run it yet...
So I hacked up the start of create partition like:
Code:
bool PFsFatFormatter::createPartition(BlockDevice* dev, uint8_t fat_type, uint32_t startSector, uint32_t sectorCount, uint8_t* secBuf, print_t* pr) {
bool rtn;
m_dev = dev;
m_secBuf = secBuf;
m_pr = pr;
m_sectorCount = sectorCount;
m_capacityMB = (m_sectorCount + SECTORS_PER_MB - 1)/SECTORS_PER_MB;
m_part_relativeSectors = startSector;
newPart = 1;
if (m_capacityMB <= 6) {
writeMsg("Card is too small.\r\n");
return false;
} else if (m_capacityMB <= 16) {
m_sectorsPerCluster = 2;
} else if (m_capacityMB <= 32) {
m_sectorsPerCluster = 4;
} else if (m_capacityMB <= 64) {
m_sectorsPerCluster = 8;
} else if (m_capacityMB <= 128) {
m_sectorsPerCluster = 16;
} else if (m_capacityMB <= 1024) {
m_sectorsPerCluster = 32;
} else if (m_capacityMB <= 32768) {
m_sectorsPerCluster = 64;
} else {
// SDXC cards
m_sectorsPerCluster = 128;
}
[COLOR="#FF0000"]m_part = addPartitionToMbr(startSector, sectorCount);
if (m_part == 0xff) return false; // error in adding a partition to the MBR[/COLOR]
#if defined(DBG_Print)
Serial.println(m_part);
The new function:
Code:
uint8_t PFsFatFormatter::addPartitionToMbr(uint32_t startSector, uint32_t sectorCount) {
uint32_t endSector = startSector + sectorCount;
MbrSector_t* mbr = reinterpret_cast<MbrSector_t*>(m_secBuf);
if (!m_dev->readSector(0, m_secBuf)) {
writeMsg("Didn't read MBR Sector !!!\n");
return 0xff; // did not read the sector.
}
uint8_t part_index = 3; // zero index;
MbrPart_t *pt = &mbr->part[part_index];
uint32_t part_sector_start = getLe32(pt->relativeSectors);
uint32_t part_total_sectors = getLe32(pt->totalSectors);
uint16_t sig = getLe16(mbr->signature);
if (sig != MBR_SIGNATURE) {
// not valid we will use the whole thing
memset(m_secBuf, 0, 512);
part_index = 0;
} else {
// lets look through to see if we have any empty slots
if (pt->type && part_sector_start && part_total_sectors) return 0xff; // We don't have any room in the MBR
// loop through the blank ones
while (part_index && (pt->type==0) && (part_sector_start== 0) && (part_total_sectors == 0)) {
part_index--;
pt = &mbr->part[part_index];
part_sector_start = getLe32(pt->relativeSectors);
part_total_sectors = getLe32(pt->totalSectors);
}
// was empty.
if ((part_index==0) && (pt->type==0) && (part_sector_start== 0) && (part_total_sectors == 0)) return 0; // empty mbr...
// Now see if we found the spot or if we need to move items down.
while (part_index && (startSector < part_sector_start)) {
// Should we check for overlaps?
if (endSector > part_sector_start) {
Serial.println("Partion overlaps existing partition");
return 0xff;
}
//move that item down
memcpy((void*)pt, (void*)&mbr->part[part_index+1], sizeof(MbrPart_t));
part_index--;
pt = &mbr->part[part_index];
part_sector_start = getLe32(pt->relativeSectors);
part_total_sectors = getLe32(pt->totalSectors);
}
// Now see if we are at the start or...
if (part_index != 0) part_index++; // back up to the one before
else if (startSector > part_sector_start) part_index++; // likewise need to backup.
// BUGBUG:: should probably test that we don't overlap on the other side...
// probably don't need this, but:
pt = &mbr->part[part_index];
memset(pt, 0, sizeof(MbrPart_t));
}
m_dev->writeSector(0, m_secBuf);
return part_index;
}
Start at X and use what is available. Both for holes and at end...
Does any of this make sense? Or maybe the wrong rabbit hole