Many TLAs: MTP MSC FS SD SDFat LittleFS UsbMSCFat to work with each other 8)

Morning,

An interesting test will be with formatting as Ex and then back to Fat to see if it makes any... At this point I am not worried...

Yesterday I just started to hack up a version to format the whole disk... I am looking at:

Option 1: Have a function that creates an MBR with the one partition, Then generate a PFsVolume object which has the block device and partition number in it... And then call your PFsFatFormatter or Ex.. to do the work.

Option 1a: Like a, but have a new version of the format method, where you pass in the BlockDevice, Partition number, and current volume label. You then move most of the current format code into this, and have the current one where you pass in PFsVolume, extract the data, and call off to new version.

Option2: Pass something in on your format methods, to say do the whole thing... Which would then do the same stuff as the other format code and compute the starting sector and sector count from the disk size... And then most of the code would still be the same except the writeMbr functions would not read the sector in first but simply keep it cleared like it still doing with the memset.

Actually currently the memset is sort of not needed as you will overwerite the whole thing anyway with the read and if read failed, you bailed.
 
Morning,

An interesting test will be with formatting as Ex and then back to Fat to see if it makes any... At this point I am not worried...

Yesterday I just started to hack up a version to format the whole disk... I am looking at:

Option 1: Have a function that creates an MBR with the one partition, Then generate a PFsVolume object which has the block device and partition number in it... And then call your PFsFatFormatter or Ex.. to do the work.

Option 1a: Like a, but have a new version of the format method, where you pass in the BlockDevice, Partition number, and current volume label. You then move most of the current format code into this, and have the current one where you pass in PFsVolume, extract the data, and call off to new version.

Option2: Pass something in on your format methods, to say do the whole thing... Which would then do the same stuff as the other format code and compute the starting sector and sector count from the disk size... And then most of the code would still be the same except the writeMbr functions would not read the sector in first but simply keep it cleared like it still doing with the memset.

Actually currently the memset is sort of not needed as you will overwerite the whole thing anyway with the read and if read failed, you bailed.

Kind of busy with honey-do list this morning but was thinking about an addPartition function:
Code:
void addPartition(Blockdevice, beginSector, size)

then run slightly different format code. EDIT: Still have to work out a couple of details. But if you pass in beginSector = 0 and size = thumb drive size it should theoretically reformat the whole disk. Forgot - have to add FATtype
 
Soon I will test a hacked up one function to initialize a drive:
Code:
// Function to handle one MS Drive...
void InitializeUSBDrive(uint8_t drive_index, uint8_t fat_type)
{
  if (!msDrives[drive_index]) {
    Serial.println("Not a valid USB drive");
  }
  PFsVolume partVol;

  for (int ii = 0; ii < count_partVols; ii++) {
    if (partVols_drive_index[ii] == drive_index) {
      while (Serial.read() != -1) ;
      Serial.println("Warning it appears like this drive has valid partitions, continue: Y? ");
      int ch;
      while ((ch = Serial.read()) == -1) ;
      if (ch != 'Y') {
        Serial.println("Canceled");
        return;
      }
      break;
    }
  }
uint32_t sectorCount = msDrives[drive_index].msCapacity.Blocks;
#define SECTORS_2GB 4194304   // (2^30 * 2) / 512
#define SECTORS_32GB 67108864 // (2^30 * 32) / 512
#define SECTORS_127GB 266338304 // (2^30 * 32) / 512
  // Serial.printf("Blocks: %u Size: %u\n", msDrives[drive_index].msCapacity.Blocks, msDrives[drive_index].msCapacity.BlockSize);
  if ((fat_type == FAT_TYPE_EXFAT) && (sectorCount < 0X100000 )) fat_type = 0; // hack to handle later
  if ((fat_type == FAT_TYPE_FAT16) && (sectorCount >= SECTORS_2GB )) fat_type = 0; // hack to handle later
  if ((fat_type == FAT_TYPE_FAT32) && (sectorCount >= SECTORS_127GB )) fat_type = 0; // hack to handle later
  if (fat_type == 0)  {
    // assume 512 byte blocks here.. 
    if (sectorCount < SECTORS_2GB) fat_type = FAT_TYPE_FAT16;
    else if (sectorCount < SECTORS_32GB) fat_type = FAT_TYPE_FAT32;
    else fat_type = FAT_TYPE_EXFAT;
  }
  // lets generate a MBR for this type...
  memset(sectorBuffer, 0, 512); // lets clear out the area.
  MbrSector_t* mbr = reinterpret_cast<MbrSector_t*>(sectorBuffer);

  // make Master Boot Record.  Use fake CHS.
  // fill in common stuff. 
  mbr->part->beginCHS[0] = 1;
  mbr->part->beginCHS[1] = 1;
  mbr->part->beginCHS[2] = 0;
  mbr->part->type = 7;
  mbr->part->endCHS[0] = 0XFE;
  mbr->part->endCHS[1] = 0XFF;
  mbr->part->endCHS[2] = 0XFF;
  setLe16(mbr->signature, MBR_SIGNATURE);


  if (fat_type == FAT_TYPE_EXFAT) {
    uint32_t clusterCount;
    uint32_t clusterHeapOffset;
    uint32_t fatLength;
    uint32_t m;
    uint32_t partitionOffset;
    uint32_t volumeLength;
    uint8_t sectorsPerClusterShift;
    uint8_t vs;
    // Determine partition layout.
    for (m = 1, vs = 0; m && sectorCount > m; m <<= 1, vs++) {}
    sectorsPerClusterShift = vs < 29 ? 8 : (vs - 11)/2;
    fatLength = 1UL << (vs < 27 ? 13 : (vs + 1)/2);
    partitionOffset = 2*fatLength;
    clusterHeapOffset = 2*fatLength;
    clusterCount = (sectorCount - 4*fatLength) >> sectorsPerClusterShift;
    volumeLength = clusterHeapOffset + (clusterCount << sectorsPerClusterShift);

    setLe32(mbr->part->relativeSectors, partitionOffset);
    setLe32(mbr->part->totalSectors, volumeLength);
  
  } else {
    // Fat16 or fat32...
    uint16_t const BU16 = 128;
    uint16_t const BU32 = 8192;
    // Assume 512 byte sectors.
    const uint16_t BYTES_PER_SECTOR = 512;
    const uint16_t SECTORS_PER_MB = 0X100000/BYTES_PER_SECTOR;
    const uint16_t FAT16_ROOT_ENTRY_COUNT = 512;
    const uint16_t FAT16_ROOT_SECTOR_COUNT = 32*FAT16_ROOT_ENTRY_COUNT/BYTES_PER_SECTOR;

    uint32_t capacityMB = (sectorCount + SECTORS_PER_MB - 1)/SECTORS_PER_MB;
    uint32_t sectorsPerCluster = 0;
    uint32_t nc;
    uint32_t r;
    uint32_t dataStart;
    uint32_t fatSize;
    uint32_t reservedSectorCount;
    uint32_t relativeSectors;
    uint32_t totalSectors;
    uint8_t partType;

    if (capacityMB <= 6) {
      Serial.print("Card is too small.\r\n");
      return;
    } else if (capacityMB <= 16) {
      sectorsPerCluster = 2;
    } else if (capacityMB <= 32) {
      sectorsPerCluster = 4;
    } else if (capacityMB <= 64) {
      sectorsPerCluster = 8;
    } else if (capacityMB <= 128) {
      sectorsPerCluster = 16;
    } else if (capacityMB <= 1024) {
      sectorsPerCluster = 32;
    } else if (capacityMB <= 32768) {
      sectorsPerCluster = 64;
    } else {
      // SDXC cards
      sectorsPerCluster = 128;
    }

    // Fat16
    if (fat_type == FAT_TYPE_FAT16) {
  
      for (dataStart = 2*BU16; ; dataStart += BU16) {
        nc = (sectorCount - dataStart)/sectorsPerCluster;
        fatSize = (nc + 2 + (BYTES_PER_SECTOR/2) - 1)/(BYTES_PER_SECTOR/2);
        r = BU16 + 1 + 2*fatSize + FAT16_ROOT_SECTOR_COUNT;
        if (dataStart >= r) {
          relativeSectors = dataStart - r + BU16;
          break;
        }
      }
      reservedSectorCount = 1;
      totalSectors = nc*sectorsPerCluster
                       + 2*fatSize + reservedSectorCount + 32;
      if (totalSectors < 65536) {
        partType = 0X04;
      } else {
        partType = 0X06;
      }
    } else {
      // fat32...
      relativeSectors = BU32;
      for (dataStart = 2*BU32; ; dataStart += BU32) {
        nc = (sectorCount - dataStart)/sectorsPerCluster;
        fatSize = (nc + 2 + (BYTES_PER_SECTOR/4) - 1)/(BYTES_PER_SECTOR/4);
        r = relativeSectors + 9 + 2*fatSize;
        if (dataStart >= r) {
          break;
        }
      }
      reservedSectorCount = dataStart - relativeSectors - 2*fatSize;
      totalSectors = nc*sectorsPerCluster + dataStart - relativeSectors;
      // type depends on address of end sector
      // max CHS has lba = 16450560 = 1024*255*63
      if ((relativeSectors + totalSectors) <= 16450560) {
        // FAT32 with CHS and LBA
        partType = 0X0B;
      } else {
        // FAT32 with only LBA
        partType = 0X0C;
      }
    }
    mbr->part->type = partType;
    setLe32(mbr->part->relativeSectors, relativeSectors);
    setLe32(mbr->part->totalSectors, totalSectors);
  }

  // Bugbug:: we assume that the msc is already set for this...
  USBMSCDevice* dev = (USBMSCDevice*)msc[drive_index].usbDrive();
  dev->writeSector(0, sectorBuffer);

  // and lets setup a partition to use this area...
  partVol.begin(dev, 1); // blind faith it worked
    
  // now lets try calling formatter
  formatter(partVol, fat_type, false); 
 
}

Where the loop function has extra sections:
Code:
        Serial.println("  R <USB Device> - Setup initial MBR and format disk *sledgehammer*");
...
      case 'R':
        Serial.printf("\n **** Try Sledgehammer on USB Drive %d ****\n", partVol_index);
        switch(ch) {
          case '1': fat_type = FAT_TYPE_FAT16; break;
          case '3': fat_type = FAT_TYPE_FAT32; break;
          case 'e': fat_type = FAT_TYPE_EXFAT; break;
        }
        InitializeUSBDrive(partVol_index, fat_type); 
        break;

It compiles but have not tried it yet. Have other things on my list as well ;)
 
@KurtE
Was just working on that myself but for creating a new partition from unallocated space (figure formatting the whole drive was the easy part (just as you showed:
Code:
bool FatFormatter::createPartition(BlockDevice* dev, uint8_t part, 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;
  m_part = part;

  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;
  }
  
   if(fat_type == 16 && fat_type < 0X400000 ) {
	writeMsg("format makeFAT16\r\n");  
	rtn = makeFat16();
  } else if(fat_type == 32) {
	writeMsg("format makeFAT32\r\n");  
	rtn = makeFat32();
  }	else {
	rtn = false;
  }
  
  if (rtn) {
    Serial.printf("free clusters after format: %u\n", partVol.freeClusterCount());
    // what happens if I tell the partion to begin again?
    partVol.begin(m_dev, true, m_part+1);  // need to 1 bias again...
    Serial.printf("free clusters after begin on partVol: %u\n", partVol.freeClusterCount());
	m_dev->syncDevice();
    writeMsg("Format Done\r\n");
  } else {
    writeMsg("Format Failed\r\n");
  }
    
}
Ran into one problem when wanting to specify new partition. In writeMBR function we specify:
Code:
  MbrSector_t* mbr = reinterpret_cast<MbrSector_t*>(m_secBuf);
  [COLOR="#FF0000"]MbrPart_t *pt = &mbr->part[/COLOR][m_part];
So as usual got stuck on if a new part how do we point it the new part? Or am I making it more complicated.

Yeah making it more complicated - just need to use the original writeMBR function I think. The writeSector should take care of where to write.
 
@all - just pushed up new branch on UsbMscFat - add_partition

That has the hacked up code mentioned above. WIP,
but it did appear to be able to add MBR for my 1gb thumb drive which I deleted everything earlier.

More testing needed and probably move code out of sketch into appropriate places.
 
@mjs513 - Not sure which is the harder...

But was glad it at least formatted one one drive, will try more later.

Back to working on drain :(

As for creating new partitions. I believe from my experiments, the partitions maybe need to be in Sector start order. So if you are filling in a whole between two
partitions, you need to move the other partitions down.

Of course then are partVols objects in list may no longer be valid. That is the partitions that were already there will have their index updated...

Probably not much of an issue.

Now back to :(
 
As a experiment (by the way figure it would work this way) I took my 4 part 128gb drive:
Code:
msc # Partition Table
	part,boot,bgnCHS[3],type,endCHS[3],start,length
FAT32:	1,0,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,61675520
	 < unused area starting at: 61677568 length 60817408 >
exFAT:	2,0,0xFE,0xFF,0xFF,0x7,0xFE,0xFF,0xFF,122494976,68661248
FAT32:	3,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,191156224,50868224
pt_#0:	4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
and ran my create partition code - well it sort of worked:
Code:
msc # Partition Table
	part,boot,bgnCHS[3],type,endCHS[3],start,length
	 < unused area starting at: 8192 length 61669376 >
FAT32:	1,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,61677568,60817408
pt_#0:	2,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
pt_#0:	3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
pt_#0:	4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
	 < unused area starting at: 122494976 length 119529471 >
drive 0 Partition 0 valid:1

***** Partition List *****
0(0:1):>> Fat32:  Partition Total Size:31130124288 Used:289112064 time us: 580250
done...
It did create the partition where it was suppose to but erased the rest of the MBR :) . So have to play with MBR code a bit as you mentioned. Ok off to other things for a while.
 
Question is which way to go.... I will wait to see what you have.

Or you might post the updates.

Wonder in the case you had, you did not read int the current MBr before you updated it?

The FatFormatter and the like blasts the whole thing out, so they don't read, they just zero out and fill in a few things.

Which is what my sledgehammer one did.

But in your case, you will probably need to Read in the MBR.
See that your range is now between 1 and 2,
so you need to copy 2 to 3
copy 1, 2

And then fill in the 1 slot with your new data and then write it out.
 
You are right I forgot to read in the MBR before I wrote it. Between chores but heard phone beep:)

Anyway this is what I have so far if you want to play. Basically I just called it at the before the end of the loop since I knew where the hole was.

Yep that was what I was working to make it general - but you know my programming skills :):
Code:
See that your range is now between 1 and 2,
so you need to copy 2 to 3
copy 1, 2
 

Attachments

  • PFsLib.zip
    6 KB · Views: 43
@mjs513 looks like progress..

Wondering about test for partition == 0 when the user maybe created 3 partitions and had deleted the first one...

Also curious about how we use the count of sectors... I know with the 1 partition system, it calculates how many sectors per cluster and then computes the count of sectors off of that... Do we need to do similar for new partitions?
 
Already doing that in createPartition, it runs the same test:
Code:
  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;
  }

Just struggling on the code to implement the algorithm to update MBR.

Edit: Looks like I would have to manipulate m_secbuf and rearrange - argh - thats going to drive me nuts.
 
Last edited:
@mjs513 ;) :D I know the feeling.

Will wait until you finish as mine is sort of limited version of yours.
The fun with all of these test are also again:
Code:
msc # Partition Table
	part,boot,bgnCHS[3],type,endCHS[3],start,length
FAT32:	1,0,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,61675520
	 < unused area starting at: 61677568 length 60817408 >
exFAT:	2,0,0xFE,0xFF,0xFF,0x7,0xFE,0xFF,0xFF,122494976,68661248
FAT32:	3,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,191156224,50868224
pt_#0:	4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
So you pass in the 61677568 and size 60817408

In this case we know that it can not be FAT16 (> 2GB) and by the chart: On Fat32 cluster size is 32kb and on ExFat cluster size is 128kb.
So currently the free space is 120464 sectors. which I believe at 64 sectors per cluster is 1882.25 clusters. But you do have extra sectors for other stuff... So question is does the MBR need to have the number of sectors to exactly fit the overhead + clusters *count of sectors per cluster or will it not really mater just the extra sectors may not get used?

Not sure if I asked this overly well? Also will the different partition codes or format code leave gaps between the partitions... Probably I need to reread that pdf...
 
@mjs513 @ KurtE - Great progress:) I have been trying to keep up and test. So far all seems to work. I have been working ext4 and have found a version of lwext4 that was setup to use a USB drive instead of an image file. The key is Block Device interface. I hope to have something I can run basic tests with soon. The other consideration is it is written in C. I really hate wrappers. I remember you guy's going through RA8876 display driver and re-working it into C++. Once I have lwext4 talking to USB drives on the T4.1 I am going to attempt to convert it to C++.
 
Morning as well - already been playing,

Kind of gave up trying to create a partition in the middle but any I can add new partitions at the end. Starting with 1 partition:
Code:
msc # Partition Table
	part,boot,bgnCHS[3],type,endCHS[3],start,length
FAT32:	1,0,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,62621696
pt_#0:	2,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
pt_#0:	3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
pt_#0:	4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
	 < unused area starting at: 62623744 length 179400703 >
drive 0 Partition 0 valid:1

***** Partition List *****
0(0:1):>> Fat32: Volume name:(v1) Partition Total Size:32053919744 Used:32571392 time us: 606498
done...
I added 3 partitions:
Code:
        FatFormatter.createPartition((USBMSCDevice*)msc[0].usbDrive(), 32, 62623744 , 57700352, sectorBuffer, &Serial); 
        delay(1000);
        FatFormatter.createPartition((USBMSCDevice*)msc[0].usbDrive(), 32, 120324096 , 60678144, sectorBuffer, &Serial);
        delay(1000); 
        FatFormatter.createPartition((USBMSCDevice*)msc[0].usbDrive(), 32, 181002240 , 61022207, sectorBuffer, &Serial);

And wound up with:
Code:
msc # Partition Table
	part,boot,bgnCHS[3],type,endCHS[3],start,length
FAT32:	1,0,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,62621696
FAT32:	2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,62623744,57700352
FAT32:	3,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,120324096,60678144
FAT32:	4,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,181002240,61022207
drive 0 Partition 0 valid:1
drive 0 Partition 1 valid:1
drive 0 Partition 2 valid:1
drive 0 Partition 3 valid:1

***** Partition List *****
0(0:1):>> Fat32: Volume name:(v1) Partition Total Size:32053919744 Used:32571392 time us: 597623
1(0:2):>> Fat32:  Partition Total Size:29534191616 Used:110460928 time us: 550875
2(0:3):>> Fat32:  Partition Total Size:31058821120 Used:114262016 time us: 579000
3(0:4):>> Fat32:  Partition Total Size:31234949120 Used:114720768 time us: 582375
done...
took the disk and put it back in windows and it seems to like it :)

Am attaching what I have now in case you want to play.
 

Attachments

  • PFsLib.zip
    6.6 KB · Views: 45
Morning again @mjs513 - I do like hitting head with hammer ;)

I tried out your previous version on Thumb drive with 4 partitions, where I deleted 2nd and 4th..

Tried filling in 2nd and the others below it no longer worked... Will retry with new stuff.

I edited the new stuff to add a couple returns to avoid warnings...

I also have hacked up version of sketch with new command to create a partition, plus added helper function to read next number...

Zip file has the minor updates plus sketch.
Note: the sketch folder also has my sublime test project files, as I build within the project and have it show the different libraries I use in the sketch.
Secondary note: I am using latest beta of SublimeText Version 4, which is sort of fun.
Their completion stuff works better now, plus how the error/warning messages are shown looks better.
 

Attachments

  • MSCFormatter.zip
    96.8 KB · Views: 42
@KurE
You have to see my head - have the dents from the hammer.

Tried filling in 2nd and the others below it no longer worked... Will retry with new stuff.
Will still have the same problem -if you try to fill in the hole it deletes all the other partitions. Really not sure how to fix that one.

Have to get more coffee before I play with your updates :)

Next up is I guess try to make it more generic.

BTW from the class how do I get the size of the thumb drive?
 
I will play more with the fill in the holes code...
Actually it looks like you did maybe preserve some with previous code. Will setup the test again and see what happens with new stuff...
screenshot.jpg

In this image the G: was the one I think was the new second drive, V2 is still there from before and the last one is still free... Will again investigate more.

As for getting the size of the Thumb drive: I am only so far doing it on the USB side, have not looked at SDCards yet...

I have played with it two different ways: where msDrives is msController
The easy way: uint32_t sectorCount = msDrives[drive_index].msCapacity.Blocks;

Which cached in the values when it was created:
Or: you could do it like I did it in the sketch with the processMSDrive:
Code:
    msSCSICapacity_t mscap;
    uint8_t status = msDrive.msReadDeviceCapacity(&mscap);
    Serial.printf("Read Capacity: status: %u Blocks: %u block Size: %u\n", status, mscap.Blocks, mscap.BlockSize);
    Serial.printf("From object: Blocks: %u Size: %u\n", msDrive.msCapacity.Blocks, msDrive.msCapacity.BlockSize);

Again for SDFat I assume there is function to call as well. But I don't believe it is in BlockDeviceInterface...
 
Not sure what you mean by previous code. The code I deleted from writeNewMbr was the same as for writeMBR. So all I did was call off to that after doing the MBR manipulation in writeNewMbr. Did leave the other version initFatDir in though just commented out.
 
Sorry, I meant to say the code from your previous zip file (#360), I have not tried it yet with the code from the new zip...
 
@wwatson @mjs513 If you really want to run on the wild side, I added the X command to the sketch:
Which will delete partitions out of the MBR. This way I don't have to keep going back and forth to PC.

Not fully tested out yet but did delete one partition so far...
 

Attachments

  • MSCFormatter.zip
    97.2 KB · Views: 46
I will play more with the fill in the holes code...
Actually it looks like you did maybe preserve some with previous code. Will setup the test again and see what happens with new stuff...
View attachment 24216

In this image the G: was the one I think was the new second drive, V2 is still there from before and the last one is still free... Will again investigate more.

As for getting the size of the Thumb drive: I am only so far doing it on the USB side, have not looked at SDCards yet...

I have played with it two different ways: where msDrives is msController
The easy way: uint32_t sectorCount = msDrives[drive_index].msCapacity.Blocks;

Which cached in the values when it was created:
Or: you could do it like I did it in the sketch with the processMSDrive:
Code:
    msSCSICapacity_t mscap;
    uint8_t status = msDrive.msReadDeviceCapacity(&mscap);
    Serial.printf("Read Capacity: status: %u Blocks: %u block Size: %u\n", status, mscap.Blocks, mscap.BlockSize);
    Serial.printf("From object: Blocks: %u Size: %u\n", msDrive.msCapacity.Blocks, msDrive.msCapacity.BlockSize);

Again for SDFat I assume there is function to call as well. But I don't believe it is in BlockDeviceInterface...

KurtE - Not sure if this is what you were looking for but you can get the sector count from the block device this way:
Code:
/*
  MSC USB Drive test 
   
#include "Arduino.h"
#include "mscFS.h"

// Setup USBHost_t36 and as many HUB ports as needed.
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
USBHub hub3(myusb);
USBHub hub4(myusb);

// Setup MSC for the number of USB Drives you are using. (Two for this example)
// Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
// delay after a USB MSC device is plugged in. This is waiting for initialization
// but after it is initialized ther should be no delay.
msController msDrive1(myusb);
msController msDrive2(myusb);

// set up variables using the mscFS utility library functions:
UsbFs msc1;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    SysCall::yield(); // wait for serial port to connect.
  }

  // Start USBHost_t36, HUB(s) and USB devices.
  myusb.begin();

  // Initialize USB drive 1
  Serial.print("\nInitializing USB MSC drive 1...");
  if (!msc1.begin(&msDrive1)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a USB drive connected?");
  } else {
     Serial.println("USB drive 1 is present.");
  }
  Serial.printf("msc1.usbDrive()->sectorCount() = %d\n",[COLOR="#FF0000"]msc1.usbDrive()->sectorCount()[/COLOR]);
}

void loop(void) {
}
 
@wwatson @mjs513 If you really want to run on the wild side, I added the X command to the sketch:
Which will delete partitions out of the MBR. This way I don't have to keep going back and forth to PC.

Not fully tested out yet but did delete one partition so far...

Great idea. Was getting tired of going back and forth myself.

Just loaded the latest version for a quick test drive. I like the new menu items:
Code:
  *** Danger Zone ***
  N <USB Device> start_addr <length> - Add a new partition to a disk
  R <USB Device> - Setup initial MBR and format disk *sledgehammer*
  X <partition> [d <usb device> - Delete a partition
 
Back
Top