greiman / SdFat

Arduino FAT16/FAT32 exFAT Library
MIT License
1.06k stars 501 forks source link

FAT12 support #231

Open DriekdeGadgetfreak opened 3 years ago

DriekdeGadgetfreak commented 3 years ago

Hi Bill,

Nice work! I just upgraded from standard SD to SdFat. More functionality and much quicker. However, we have over hundred Mega2560's in the field with 128 MB SD cards. Some of these cards don't have a MBR, just a VBR. This is according to the standard. SdFat::begin fails. I made a small modification to the code in FatPartition::init: to fix this.

  if (!mbr || mp->type == 0 || (mp->boot != 0 && mp->boot != 0X80))
  {
    DBG_FAIL_MACRO;
    goto fail;
  }
  volumeStartSector = getLe32(mp->relativeSectors);

Changed to:

if (mbr->bootCode[54] != 'F') // hansl added
{
  if (!mbr || mp->type == 0 || (mp->boot != 0 && mp->boot != 0X80))
  {
    DBG_FAIL_MACRO;
    goto fail;
  }
  volumeStartSector = getLe32(mp->relativeSectors);
}

}

Regards,

Hans Leijsen LTS Solar Netherlands

greiman commented 3 years ago

The Arduino SD.h library is still a wrapper for an early 2009 version of SdFat. This is the copyright from Arduino IDE 1.8.13.

Arduino SdFat Library Copyright (C) 2009 by William Greiman

The SD specification has required a MBR since the first file system spec in 2000. This is a quote from the spec with this title page.

SD Memory Card Specifications Part 2 FILE SYSTEM SPECIFICATION Version 1.0 February 2000 SD Group Matsushita Electric Industrial Co., Ltd. (MEI) SanDisk Corporation Toshiba Corporation

Page 7 of the spec.

  1. Volume Structure The volume structure of the SD Memory Card is specified in this section. It defines the logical structure of the Data Area. For the identification of the Data Area as a partition, the first sector has Master Boot Record and Partition Table. And the SD Memory Card file system uses the FAT file system (ISO/IEC 9293) and supports both FAT12 and FAT16 as the file system type.

It can't be an SD card without the MBR to provide hidden sectors for the "Secure" part of its name. The card was derived from the MultiMediaCard (MMC) and provided digital rights management based on the Secure Digital Music Initiative (SDMI) standard and for the time, a high memory density device.

People discover they could reformat the cards and gain a bit of space. Now the MBR serves to align the huge flash pages so sector zero is not remapped or damaged by wear leveling.

Guess I will need to relent and still allow cards with no MBR.