Skip to content

Commit 143501d

Browse files
committed
Moved FS structure printing into a separate tool.
Added large logical sector support to meet cluster size recommendations for SDXC cards with FAT32. Other minor bug fixes/changes.
1 parent af3ac7f commit 143501d

16 files changed

Lines changed: 275 additions & 179 deletions

File tree

include/blockdev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class BlockDev
7979
*
8080
* @return Returns 0 on success or errno.
8181
*/
82-
int discardAll(const bool secure = false) const noexcept;
82+
int eraseAll(const bool secure = false) const noexcept;
8383

8484
/**
8585
* @brief Closes the block device.

include/buffered_fs_writer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ class BufferedFsWriter final : private BlockDev
107107
*
108108
* @return Returns 0 on success or errno.
109109
*/
110-
int discardAll(const bool secure = false) noexcept
110+
int eraseAll(const bool secure = false) noexcept
111111
{
112-
memset(m_buf.get(), 0, m_pos & m_blkMask);
112+
//memset(m_buf.get(), 0, m_pos & m_blkMask);
113113
m_pos = 0;
114-
return BlockDev::discardAll(secure);
114+
return BlockDev::eraseAll(secure);
115115
}
116116

117117
/**

include/errors.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
enum
55
{
66
// Everything ok = 0.
7-
ERR_INVALID_ARG = 1,
8-
ERR_DEV_OPEN = 2,
9-
ERR_DEV_TOO_SMALL = 3,
10-
ERR_ERASE = 4,
11-
ERR_PARTITION = 5,
12-
ERR_FORMAT = 6,
13-
ERR_CLOSE_DEV = 7,
14-
ERR_EXCEPTION = 8,
15-
ERR_UNK_EXCEPTION = 9
7+
ERR_INVALID_ARG = 1,
8+
ERR_DEV_OPEN = 2,
9+
ERR_DEV_TOO_SMALL = 3,
10+
ERR_ERASE = 4,
11+
ERR_FORMAT_PARAMS = 5,
12+
ERR_PARTITION = 6,
13+
ERR_FORMAT = 7,
14+
ERR_CLOSE_DEV = 8,
15+
ERR_EXCEPTION = 9,
16+
ERR_UNK_EXCEPTION = 10
1617
};

include/exfat.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstddef>
44
#include "types.h"
5+
#include "format.h"
56

67
// References:
78
// exFAT: https://learn.microsoft.com/en-us/windows/win32/fileio/exfat-specification
@@ -10,9 +11,9 @@
1011
// Boot Sector.
1112
typedef struct
1213
{
13-
u8 jumpBoot[3]; // {0xEB, 0x76, 0x90}.
14+
u8 jumpBoot[3]; // {0xEB, 0x76, 0x90}.
1415
char fileSystemName[8]; // "EXFAT ".
15-
u8 mustBeZero[53];
16+
u8 mustBeZero[53];
1617
u64 partitionOffset; // Arbitrary value or 0 = ignore this field.
1718
u64 volumeLength; // Minimum "2^20 / 2^BytesPerSectorShift", maximum "2^64 - 1". If excess space sub-region size = 0 then max. is "ClusterHeapOffset + (2^32 - 11) * 2^SectorsPerClusterShift".
1819
u32 fatOffset; // Minimum "24", maximum "ClusterHeapOffset - (FatLength * NumberOfFats)".
@@ -28,8 +29,8 @@ typedef struct
2829
u8 numberOfFats; // "1" or "2" (TexFAT only).
2930
u8 driveSelect; // Arbitrary value. Recommended 0x80.
3031
u8 percentInUse; // 0-100 "percentage of allocated clusters in the Cluster Heap, rounded down to the nearest integer" or 0xFF if unknown.
31-
u8 reserved[7];
32-
u8 bootCode[390]; // Bootstrapping code or 0xF4 filled (x86 halt).
32+
u8 reserved[7];
33+
u8 bootCode[390]; // Bootstrapping code or 0xF4 filled (x86 halt).
3334
u16 bootSignature; // 0xAA55.
3435
//u8 excessSpace[(1u<<bytesPerSectorShift) - 512];
3536
} BootSector;
@@ -38,10 +39,10 @@ static_assert(offsetof(BootSector, bootSignature) == 510, "Member bootSignature
3839
// Extended Boot Sector.
3940
/*typedef struct
4041
{
41-
u8 extendedBootCode[(1u<<bytesPerSectorShift) - 4];
42+
u8 extendedBootCode[(1u<<bytesPerSectorShift) - 4];
4243
u32 extendedBootSignature; // 0xAA550000.
4344
} ExtendedBootSector;*/
4445

4546

4647

47-
void calcFormatExFat(void); // (const u64 totSec, FormatParams *const paramsOut)
48+
void calcFormatExFat(FormatParams &params);

include/fat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ static_assert(offsetof(FatDir, fileSize) == 28, "Member fileSize of FatDir not a
9898

9999

100100

101-
void calcFormatFat(const u32 totSec, FormatParams &params);
102-
void calcFormatFat32(const u32 totSec, FormatParams &params);
101+
void calcFormatFat(FormatParams &params);
102+
void calcFormatFat32(FormatParams &params);
103103
int makeFsFat(const FormatParams &params, BufferedFsWriter &dev, const std::string &label);

include/format.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
#pragma once
22

3+
#include <string>
34
#include "types.h"
45

56

7+
// The smallest card we can format without running into issues is 64 KiB.
8+
#define MIN_CAPACITY (1024u * 64 / 512)
9+
#define MAX_CAPACITY_FAT32 (0xFFFFFFFFu)
10+
11+
612
union ArgFlags
713
{
814
struct
915
{
10-
u8 dryRun : 1;
11-
u8 erase : 1;
12-
u8 secErase : 1;
13-
u8 forceFat32 : 1;
14-
u8 verbose : 1;
16+
u8 bigClusters : 1;
17+
//u8 dryRun : 1;
18+
u8 erase : 1;
19+
u8 forceFat32 : 1;
20+
u8 secErase : 1;
21+
u8 verbose : 1;
1522
};
1623
u8 allFlags;
1724
};
1825

26+
// Note: Unless specified otherwise everything is in logical sectors.
1927
typedef struct
2028
{
2129
// TODO: union with exFAT vars.
2230
u64 totSec;
31+
u16 bytesPerSec;
2332
u8 heads;
2433
u8 secPerTrk;
2534
u8 fatBits;
26-
u32 alignment;
35+
u32 alignment; // In logical sectors.
2736
u32 secPerClus;
2837
u32 rsvdSecCnt;
2938
u32 secPerFat;
30-
u32 fsAreaSize;
31-
u32 partStart;
32-
u32 maxClus;
39+
u32 fsAreaSize; // In logical sectors.
40+
u32 partStart; // In logical sectors.
41+
u32 maxClus; // Logical clusters.
3342
} FormatParams;
3443

3544

3645

37-
u32 formatSd(const char *const path, const char *const label, const ArgFlags flags, const u64 overrTotSec);
46+
u32 formatSd(const char *const path, const std::string &label, const ArgFlags flags, const u64 overrTotSec);

include/mbr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ static_assert(offsetof(Mbr, bootSig) == 510, "Member bootSig of Mbr not at offse
2929

3030

3131

32-
int createMbrAndPartition(const FormatParams *const params, BufferedFsWriter &dev);
32+
int createMbrAndPartition(const FormatParams &params, BufferedFsWriter &dev);

source/blockdev.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ int BlockDev::write(const u8 *buf, const u64 sector, const u64 count) noexcept
192192
return 0;
193193
}
194194

195-
int BlockDev::discardAll(const bool secure) const noexcept
195+
int BlockDev::eraseAll(const bool secure) const noexcept
196196
{
197197
int res = 0;
198198
const u64 wholeRange[2] = {0, m_sectors * m_sectorSize};

source/exfat.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ u32 calcExFatBootChecksum(const u8 *data, const u16 bytesPerSector)
1717
return checksum;
1818
}
1919

20-
void calcFormatExFat(void) // (const u64 totSec, FormatParams *const paramsOut)
20+
void calcFormatExFat(FormatParams &params)
2121
{
22+
(void)params;
23+
2224
// TODO
2325
fputs("exFAT is not supported yet.\n", stderr);
2426
}

0 commit comments

Comments
 (0)