How to format USB drive with TexFAT programmatically

Hi, I’m the Toradex AI Assistant.

I might make mistakes. Our support team will step in if needed. If I answered your question, please tick the “Solution” checkbox. For follow-up questions to me, please use @ToradexAI in your post.

Did you like my answer? :+1: :-1:


Hello! Thank you for your clear and detailed description of your scenario.

Based on your requirements and the available documentation, here is guidance on checking the file system type of a USB drive and formatting it as TexFAT programmatically on your Colibri T20 and iMX6 running Windows CE 7.

1. Check the File System Type Programmatically

You can use the CeGetVolumeInfo() API to determine the current file system and to check if the CE_VOLUME_TRANSACTION_SAFE flag is set. This lets you identify whether the volume is formatted as TFAT (TexFAT).

Example Logic:

CE_VOLUME_INFO volInfo;
if (CeGetVolumeInfo(L"\\USB Disk", CE_VOLUME_INFO_LEVEL, &volInfo, sizeof(volInfo))) {
    if (volInfo.dwFlags & CE_VOLUME_TRANSACTION_SAFE) {
        // TexFAT or TFAT is active
    } else {
        // Regular FAT
    }
}

2. Programmatically Format the USB Drive as TexFAT

To format a volume programmatically, you would use the Storage Manager APIs:

However, the key to enabling TFAT is to set the proper flag when calling FormatPartition(). The documentation in the knowledge base highlights that while code samples are not provided, the process from the Storage Manager UI involves selecting the TFAT option, which must be mapped to the flag value in your function call.

Basic flow for formatting:

  1. Use FindFirstStore() / FindFirstPartition() to enumerate and identify your USB storage/partition.
  2. Use DismountPartition() as needed.
  3. Call FormatPartition() with the appropriate parameters and with the TFAT flag enabled.

Pseudocode Outline

// Open handle to storage manager
HANDLE hStore = OpenStore(storeName);
// Get partition info
HANDLE hPart = OpenPartition(hStore, partitionName);
// Unmount (dismount) partition before formatting
DismountPartition(hPart);
// Set TFAT flag and format
DWORD formatFlags = FORMAT_SKIP_CONFIRMATION | FORMAT_ENABLE_TFAT;
BOOL bResult = FormatPartition(hPart, formatFlags, NULL);
// Mount partition again
MountPartition(hPart);

Notes:

  • The actual value for FORMAT_ENABLE_TFAT may need to be defined. If not available in your header files, you may want to refer to platform-specific defines or Microsoft/ODM documentation.
  • Make sure to check your device’s documentation for the correct store and partition names for your USB device.

3. Additional References

4. Important Considerations

  • Backup Important Data: Formatting a drive will erase all data on it.
  • User Confirmation: For production systems, consider providing a way for the user to confirm the formatting action.

5. Example Discussion

Here’s a relevant discussion thread you might find helpful:
Issue SD Card Corruption - Toradex Community


In summary:

  • Use CeGetVolumeInfo() to detect TFAT.
  • Use the Storage Manager APIs (OpenStore, OpenPartition, FormatPartition with flag for TFAT) to format partitions programmatically.
  • The process is similar for both T20 and iMX6 running WinCE 7.

If you need a more detailed sample code, let me know and I can help structure it further.

Best regards,
Toradex AI Assistant.