Hi @defragster and others...
Thought I would show so more stuff that is not working
Code:
// VolumeName.ino
// An example of how to retrieve Fat32 and ExFat volume names using SdFat.
// Works with SD cards and USB mass storage drives.
#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);
#define sdDrive 1
#define msDrive 2
#define SD_CONFIG SdioConfig(FIFO_SDIO)
// set up variables using the mscFS utility library functions:
UsbFs msc1;
SdFs sd;
//FatVolume partVol;
//create holding array for partions
uint8_t partitionTable[4];
int32_t dataStart[4];
// Get ExFat volume name.
bool getExFatVolumeLabel(uint8_t drvType, uint8_t part) {
uint8_t buf[32];
UsbExFat volName;
SdExFat volName1;
ExFatFile root;
msController *mscDrive;
DirLabel_t *dir;
ExFatVolume expartVol;
if (drvType == msDrive) {
mscDrive = &msDrive1;
if (!volName.begin(&msDrive1)) {
Serial.println("EXFat volName.begin failed");
return false;
}
expartVol.begin(msc1.usbDrive(), true, part);
expartVol.chvol();
if (!root.openRoot(&expartVol)) {
Serial.println("openRoot failed");
return false;
}
}
if (drvType == sdDrive) {
if (!volName1.begin(SD_CONFIG)) {
return false;
}
if (!root.openRoot(&volName1)) {
Serial.println("openRoot failed");
return false;
}
}
root.read(buf, 32);
dir = reinterpret_cast<DirLabel_t*>(buf);
Serial.print(F("Volume Name: "));
for (size_t i = 0; i < dir->labelLength; i++) {
Serial.write(dir->unicode[2 * i]);
}
Serial.println();
return true;
}
// Get Fat32 volume name.
bool getFat32VolumeLabel(uint8_t drvType, uint8_t part) {
FatVolume partVol;
uint8_t buf[512];
partVol.begin(msc1.usbDrive(), true, part);
partVol.chvol();
if (drvType == msDrive) {
msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
}
if (drvType == sdDrive) {
sd.card()->readSector(sd.dataStartSector(), buf);
}
Serial.print(F("Volume Name: "));
for (size_t i = 0; i < 11; i++) {
Serial.write(buf[i]);
}
Serial.println();
partVol.ls(LS_SIZE | LS_DATE | LS_R);
return true;
}
// Get Fat16 volume name.
bool getFat16VolumeLabel(uint8_t drvType, uint8_t part) {
FatVolume partVol;
MbrSector_t mbr;
uint8_t buf[512];
partVol.begin(msc1.usbDrive(), true, part);
partVol.chvol();
if (drvType == msDrive) {
// lets read in the Master boot record...
msc1.usbDrive()->readSector(0, (uint8_t*)&mbr);
MbrPart_t *pt = &mbr.part[part - 1];
uint32_t starting_sector = getLe32(pt->relativeSectors);
msc1.usbDrive()->readSector(starting_sector, buf);
Serial.printf("\nFAT first sector:(%x)\n", starting_sector);
print_hexbytes(buf, 512);
pbs_t* pbs = (pbs_t*)&buf;
BpbFat16_t *bpt16 = reinterpret_cast<BpbFat16_t*>(pbs->bpb);
Serial.print(F("Volume Name: "));
for (size_t i = 0; i < 11; i++) {
Serial.write(bpt16->volumeLabel[i]);
}
}
if (drvType == sdDrive) {
sd.card()->readSector(sd.dataStartSector(), buf);
print_hexbytes(buf, 512);
Serial.print(F("Volume Name: "));
for (size_t i = 0; i < 11; i++) {
Serial.write(buf[i]);
}
}
Serial.println();
partVol.ls(LS_SIZE | LS_DATE | LS_R);
return true;
}
bool mbrDmp() {
MbrSector_t mbr;
bool valid = true;
if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
Serial.print("\nread MBR failed.\n");
//errorPrint();
return false;
}
Serial.print("\nmsc1 Partition Table\n");
Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
for (uint8_t ip = 1; ip < 5; ip++) {
MbrPart_t *pt = &mbr.part[ip - 1];
// if ((pt->boot != 0 && pt->boot != 0X80) ||
// getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
// valid = false;
// }
switch (pt->type) {
case 4:
case 6:
case 0xe:
Serial.print("FAT16: ");
break;
case 11:
case 12:
Serial.print("FAT32: ");
break;
case 7:
Serial.print("exFAT: ");
break;
}
partitionTable[ip - 1] = pt->type;
dataStart[ip - 1] = getLe32(pt->relativeSectors);
Serial.print( int(ip)); Serial.print( ',');
Serial.print(int(pt->boot), HEX); Serial.print( ',');
for (int i = 0; i < 3; i++ ) {
Serial.print("0x"); Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
}
Serial.print("0x"); Serial.print(int(pt->type), HEX); Serial.print( ',');
for (int i = 0; i < 3; i++ ) {
Serial.print("0x"); Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
}
Serial.print(getLe32(pt->relativeSectors), DEC); Serial.print(',');
Serial.println(getLe32(pt->totalSectors));
}
return true;
}
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();
}
void loop(void) {
//--------------------------------------------------
myusb.Task();
if (!msDrive1) {
Serial.println("Waiting up to 5 seconds for USB drive");
elapsedMillis em = 0;
while (!msDrive1 && (em < 5000) ) myusb.Task();
}
if (msDrive1) {
Serial.printf("Initialize USB drive...");
if (!msc1.begin(&msDrive1)) {
msc1.errorPrint(&Serial);
Serial.println("initialization failed.\n");
} else {
Serial.println("USB drive 1 is present.\n");
}
mbrDmp();
/*
if(msc1.fatType() == 32) {
Serial.printf("Fat Type: Fat32\n");
if(!getFat32VolumeLabel(msDrive))
Serial.printf("Failed to get volume label\n");
} else {
Serial.printf("Fat Type: ExFat\n");
if(!getExFatVolumeLabel(msDrive,1))
Serial.printf("Failed to get volume label\n");
}
msc1.ls(LS_SIZE | LS_DATE | LS_R);
*/
for (uint8_t i = 1; i < 5; i++) {
switch (partitionTable[i - 1]) {
case 11:
case 12:
Serial.printf("\nFat Type: Fat32\n");
if (!getFat32VolumeLabel(msDrive, i))
Serial.printf("Failed to get volume label\n");
break;
case 4:
case 6:
case 0xe:
Serial.printf("\nFat Type: Fat16\n");
if (!getFat16VolumeLabel(msDrive, i))
Serial.printf("Failed to get volume label\n");
break;
case 7:
Serial.printf("\nFat Type: ExFat\n");
if (!getExFatVolumeLabel(msDrive, i))
Serial.printf("Failed to get volume label\n");
break;
default:
Serial.println("No or Not Supported Partition");
}
}
}
//--------------------------------------------------
Serial.printf("\nInitialize SD card...");
if (!sd.begin(SD_CONFIG)) {
Serial.println("initialization failed.\n");
} else {
Serial.println("SD card is present.\n");
}
if (sd.fatType() == 32) {
Serial.printf("Fat Type: Fat32\n");
if (!getFat32VolumeLabel(sdDrive, 1))
Serial.printf("Failed to get volume label\n");
} else {
Serial.printf("Fat Type: ExFAT\n");
if (!getExFatVolumeLabel(sdDrive, 1)) {
Serial.printf("Failed to get volume label\n");
}
}
//sd.ls(LS_SIZE | LS_DATE | LS_R);
Serial.println("done...");
Serial.println("Press any key to run again");
while (Serial.read() == -1);
while (Serial.read() != -1);
}
void print_hexbytes(const void *ptr, int len)
{
if (ptr == NULL || len <= 0) return;
const uint8_t *p = (const uint8_t *)ptr;
while (len) {
for (uint8_t i = 0; i < 32; i++) {
if (i > len) break;
Serial.printf("%02X ", p[i]);
}
Serial.print(":");
for (uint8_t i = 0; i < 32; i++) {
if (i > len) break;
Serial.printf("%c", ((p[i] >= ' ') && (p[i] <= '~')) ? p[i] : '.');
}
Serial.println();
p += 32;
len -= 32;
}
}
Code:
Waiting up to 5 seconds for USB drive
Initialize USB drive...FsVolume::begin(20001b70)
ExFatPartition::init(20001b70, 1)
FatPartition::init(20001b70, 1)
USB drive 1 is present.
msc1 Partition Table
part,boot,bgnCHS[3],type,endCHS[3],start,length
FAT32: 1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
FAT16: 2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
Fat Type: Fat32
FatPartition::init(20001b70, 1)
Volume Name: VOLFAT32
2010-03-22 07:11 3343737 DSC03357.JPG
2021-01-01 00:00 0 example.txt
2021-02-24 16:35 0 OnFat32.txt
Fat Type: Fat16
FatPartition::init(20001b70, 2)
FAT first sector:(7d0800)
EB 3C 90 4D 53 44 4F 53 35 2E 30 00 02 20 08 00 02 00 02 00 00 F8 00 01 3F 00 FF 00 00 08 7D 00 :.<.MSDOS5.0.. ..........?.....}.
00 00 20 00 80 00 29 19 4C 09 02 4E 4F 20 4E 41 4D 45 20 20 20 20 46 41 54 31 36 20 20 20 33 C9 :.. ...).L..NO NAME FAT16 3.
8E D1 BC F0 7B 8E D9 B8 00 20 8E C0 FC BD 00 7C 38 4E 24 7D 24 8B C1 99 E8 3C 01 72 1C 83 EB 3A :....{.... .....|8N$}$....<.r...:
66 A1 1C 7C 26 66 3B 07 26 8A 57 FC 75 06 80 CA 02 88 56 02 80 C3 10 73 EB 33 C9 8A 46 10 98 F7 :f..|&f;.&.W.u.....V....s.3..F...
66 16 03 46 1C 13 56 1E 03 46 0E 13 D1 8B 76 11 60 89 46 FC 89 56 FE B8 20 00 F7 E6 8B 5E 0B 03 :f..F..V..F....v.`.F..V.. ....^..
C3 48 F7 F3 01 46 FC 11 4E FE 61 BF 00 00 E8 E6 00 72 39 26 38 2D 74 17 60 B1 0B BE A1 7D F3 A6 :.H...F..N.a......r9&8-t.`....}..
61 74 32 4E 74 09 83 C7 20 3B FB 72 E6 EB DC A0 FB 7D B4 7D 8B F0 AC 98 40 74 0C 48 74 13 B4 0E :at2Nt... ;.r.....}.}....@t.Ht...
BB 07 00 CD 10 EB EF A0 FD 7D EB E6 A0 FC 7D EB E1 CD 16 CD 19 26 8B 55 1A 52 B0 01 BB 00 00 E8 :.........}....}......&.U.R......
3B 00 72 E8 5B 8A 56 24 BE 0B 7C 8B FC C7 46 F0 3D 7D C7 46 F4 29 7D 8C D9 89 4E F2 89 4E F6 C6 :;.r.[.V$..|...F.=}.F.)}...N..N..
06 96 7D CB EA 03 00 00 20 0F B6 C8 66 8B 46 F8 66 03 46 1C 66 8B D0 66 C1 EA 10 EB 5E 0F B6 C8 :..}..... ...f.F.f.F.f..f....^...
4A 4A 8A 46 0D 32 E4 F7 E2 03 46 FC 13 56 FE EB 4A 52 50 06 53 6A 01 6A 10 91 8B 46 18 96 92 33 :JJ.F.2....F..V..JRP.Sj.j...F...3
D2 F7 F6 91 F7 F6 42 87 CA F7 76 1A 8A F2 8A E8 C0 CC 02 0A CC B8 01 02 80 7E 02 0E 75 04 B4 42 :......B...v..............~..u..B
8B F4 8A 56 24 CD 13 61 61 72 0B 40 75 01 42 03 5E 0B 49 75 06 F8 C3 41 BB 00 00 60 66 6A 00 EB :...V$..aar.@u.B.^.Iu...A...`fj..
B0 42 4F 4F 54 4D 47 52 20 20 20 20 0D 0A 52 65 6D 6F 76 65 20 64 69 73 6B 73 20 6F 72 20 6F 74 :.BOOTMGR ..Remove disks or ot
68 65 72 20 6D 65 64 69 61 2E FF 0D 0A 44 69 73 6B 20 65 72 72 6F 72 FF 0D 0A 50 72 65 73 73 20 :her media....Disk error...Press
61 6E 79 20 6B 65 79 20 74 6F 20 72 65 73 74 61 72 74 0D 0A 00 00 00 00 00 00 00 AC CB D8 55 AA :any key to restart............U.
Volume Name: NO NAME
2021-01-13 17:56 340510 T4-Cardlike.jpg
2021-02-24 16:34 0 OnFat16.txt
Fat Type: ExFat
ExFatPartition::init(2006fb0c, 1)
EXFat volName.begin failed
Failed to get volume label
No or Not Supported Partition
Initialize SD card...FsVolume::begin(200033f0)
ExFatPartition::init(200033f0, 1)
FatPartition::init(200033f0, 1)
SD card is present.
Fat Type: ExFAT
ExFatPartition::init(2006ff9c, 1)
Failed to get volume label
done...
Press any key to run again
For the fun of it, I tried to pull the volume label for Fat16 out of the Master boot record, but as you see it says the name is: NO NAME...
And and it is not liking my exfat...
Still investigating