Skip to content

Commit af3ac7f

Browse files
committed
Minor fixes/changes.
Moved fsPrinter to a standalone tool.
1 parent 9429509 commit af3ac7f

8 files changed

Lines changed: 203 additions & 70 deletions

File tree

include/errors.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
4+
enum
5+
{
6+
// 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
16+
};

include/format.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ union ArgFlags
1111
u8 erase : 1;
1212
u8 secErase : 1;
1313
u8 forceFat32 : 1;
14-
u8 printFs : 1;
1514
u8 verbose : 1;
1615
};
1716
u8 allFlags;

include/fs_printer.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

source/blockdev.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int BlockDev::open(const char *const path, const bool rw) noexcept
119119
#endif
120120

121121
m_fd = fd;
122-
m_sectors = diskSize / 512;
122+
m_sectors = diskSize / m_sectorSize;
123123
} while(0);
124124

125125
if(res != 0)
@@ -134,8 +134,8 @@ int BlockDev::read(u8 *buf, const u64 sector, const u64 count) const noexcept
134134
{
135135
int res = 0;
136136
const int fd = m_fd;
137-
off_t offset = sector * 512;
138-
u64 totSize = count * 512;
137+
off_t offset = sector * m_sectorSize;
138+
u64 totSize = count * m_sectorSize;
139139
while(totSize > 0)
140140
{
141141
// Limit of 1 GiB chunks.
@@ -168,8 +168,8 @@ int BlockDev::write(const u8 *buf, const u64 sector, const u64 count) noexcept
168168

169169
int res = 0;
170170
const int fd = m_fd;
171-
off_t offset = sector * 512;
172-
u64 totSize = count * 512;
171+
off_t offset = sector * m_sectorSize;
172+
u64 totSize = count * m_sectorSize;
173173
while(totSize > 0)
174174
{
175175
// Limit of 1 GiB chunks.
@@ -195,7 +195,7 @@ int BlockDev::write(const u8 *buf, const u64 sector, const u64 count) noexcept
195195
int BlockDev::discardAll(const bool secure) const noexcept
196196
{
197197
int res = 0;
198-
const u64 wholeRange[2] = {0, m_sectors * 512};
198+
const u64 wholeRange[2] = {0, m_sectors * m_sectorSize};
199199
if(ioctl(m_fd, (secure ? BLKSECDISCARD : BLKDISCARD), wholeRange) == -1)
200200
{
201201
res = errno;

source/format.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#include "mbr.h"
77
#include "exfat.h"
88
#include "fat.h"
9+
#include "errors.h"
910
#include "buffered_fs_writer.h"
1011
#include "verbose_printf.h"
1112
#include "privileges.h"
1213

1314

1415

1516
// TODO: fatBits is determined from SC and TS.
16-
static bool getFormatParams(const u64 totSec, const bool forceSdxcFat32, FormatParams *const paramsOut)
17+
static bool getFormatParams(const u64 totSec, const bool forceSdxcFat32, FormatParams &paramsOut)
1718
{
1819
if(totSec == 0) return false;
1920
if(totSec >= 1ull<<32 && forceSdxcFat32) return false;
@@ -59,12 +60,12 @@ static bool getFormatParams(const u64 totSec, const bool forceSdxcFat32, FormatP
5960
{ 0, 0, 0, 0} // Higher is not supported (yet).
6061
};
6162

62-
paramsOut->totSec = totSec;
63+
paramsOut.totSec = totSec;
6364

6465
const GeometryData *geometryData = geometryTable;
6566
while(geometryData->cap != 0 && totSec>>11 > geometryData->cap) geometryData++;
66-
paramsOut->heads = geometryData->heads;
67-
paramsOut->secPerTrk = geometryData->secPerTrk;
67+
paramsOut.heads = geometryData->heads;
68+
paramsOut.secPerTrk = geometryData->secPerTrk;
6869

6970
const AlignData *alignParams = alignTable;
7071
while(alignParams->capLog2 != 0 && totSec > 1ull<<alignParams->capLog2) alignParams++;
@@ -75,41 +76,41 @@ static bool getFormatParams(const u64 totSec, const bool forceSdxcFat32, FormatP
7576
}
7677

7778
const u8 fatBits = (alignParams->capLog2 > 26 && forceSdxcFat32 ? 32 : alignParams->fatBits);
78-
paramsOut->fatBits = fatBits;
79-
paramsOut->alignment = alignParams->alignment;
80-
paramsOut->secPerClus = alignParams->secPerClus;
79+
paramsOut.fatBits = fatBits;
80+
paramsOut.alignment = alignParams->alignment;
81+
paramsOut.secPerClus = alignParams->secPerClus;
8182

82-
if(fatBits <= 16) calcFormatFat((u32)totSec, *paramsOut);
83-
else if(fatBits == 32) calcFormatFat32((u32)totSec, *paramsOut);
83+
if(fatBits <= 16) calcFormatFat((u32)totSec, paramsOut);
84+
else if(fatBits == 32) calcFormatFat32((u32)totSec, paramsOut);
8485
else calcFormatExFat(/*totSec, *paramsOut*/); // TODO
8586

8687
return true;
8788
}
8889

89-
static void printFormatParams(const FormatParams *const params)
90+
static void printFormatParams(const FormatParams &params)
9091
{
9192
const char *fsName;
92-
if(params->fatBits == 12) fsName = "FAT12";
93-
else if(params->fatBits == 16) fsName = "FAT16";
94-
else if(params->fatBits == 32) fsName = "FAT32";
95-
else fsName = "exFAT";
93+
if(params.fatBits == 12) fsName = "FAT12";
94+
else if(params.fatBits == 16) fsName = "FAT16";
95+
else if(params.fatBits == 32) fsName = "FAT32";
96+
else fsName = "exFAT";
9697

9798
printf("Filesystem type: %s\n", fsName);
98-
printf("Heads: %" PRIu8 "\n", params->heads);
99-
printf("Sectors per track: %" PRIu8 "\n", params->secPerTrk);
100-
printf("Alignment: %" PRIu32 "\n", params->alignment);
101-
printf("Reserved sectors: %" PRIu32 "\n", params->rsvdSecCnt);
102-
printf("Sectors per cluster: %" PRIu32 "\n", params->secPerClus);
103-
printf("Sectors per FAT: %" PRIu32 "\n", params->secPerFat);
104-
printf("Filesystem area size: %" PRIu32 "\n", params->fsAreaSize);
105-
printf("Partition start: %" PRIu32 "\n", params->partStart);
106-
printf("Maximum clusters: %" PRIu32 "\n", params->maxClus);
99+
printf("Heads: %" PRIu8 "\n", params.heads);
100+
printf("Sectors per track: %" PRIu8 "\n", params.secPerTrk);
101+
printf("Alignment: %" PRIu32 "\n", params.alignment);
102+
printf("Reserved sectors: %" PRIu32 "\n", params.rsvdSecCnt);
103+
printf("Sectors per cluster: %" PRIu32 "\n", params.secPerClus);
104+
printf("Sectors per FAT: %" PRIu32 "\n", params.secPerFat);
105+
printf("Filesystem area size: %" PRIu32 "\n", params.fsAreaSize);
106+
printf("Partition start: %" PRIu32 "\n", params.partStart);
107+
printf("Maximum clusters: %" PRIu32 "\n", params.maxClus);
107108
}
108109

109110
u32 formatSd(const char *const path, const char *const label, const ArgFlags flags, const u64 overrTotSec)
110111
{
111112
BufferedFsWriter dev;
112-
if(dev.open(path) != 0) return 2;
113+
if(dev.open(path) != 0) return ERR_DEV_OPEN;
113114
dropPrivileges();
114115

115116
// The smallest card we can format without running into issues is 64 KiB.
@@ -118,7 +119,7 @@ u32 formatSd(const char *const path, const char *const label, const ArgFlags fla
118119
if(totSec < (1024 * 64 / 512))
119120
{
120121
fputs("SD card capacity too small.\n", stderr);
121-
return 3;
122+
return ERR_DEV_TOO_SMALL;
122123
}
123124

124125
// Allow overriding the capacity only if the new capacity is lower.
@@ -137,28 +138,28 @@ u32 formatSd(const char *const path, const char *const label, const ArgFlags fla
137138
{
138139
fputs("SD card can't be erased. Ignoring.\n", stderr);
139140
}
140-
else if(discardRes != 0) return 4;
141+
else if(discardRes != 0) return ERR_ERASE;
141142
}
142143

143144
FormatParams params{};
144-
getFormatParams(totSec, flags.forceFat32, &params);
145+
getFormatParams(totSec, flags.forceFat32, params);
145146

146147
// Create a new Master Boot Record and partition.
147148
verbosePuts("Creating new partition table and partition...");
148-
if(createMbrAndPartition(&params, dev) != 0) return 5;
149+
if(createMbrAndPartition(&params, dev) != 0) return ERR_PARTITION;
149150

150151
// Clear filesystem areas and write a new Volume Boot Record.
151152
// TODO: Label should be upper case and some chars are not allowed. Implement conversion + checks.
152153
// mkfs.fat allows lower case but warns about it.
153154
verbosePuts("Formatting the partition...");
154155
// TODO: Depending on FS type call the format function here.
155-
if(makeFsFat(params, dev, label) != 0) return 6;
156+
if(makeFsFat(params, dev, label) != 0) return ERR_FORMAT;
156157

157158
// Explicitly close dev to get the result.
158-
if(dev.close() != 0) return 7;
159+
if(dev.close() != 0) return ERR_CLOSE_DEV;
159160

160161
puts("Successfully formatted the card.");
161-
printFormatParams(&params);
162+
printFormatParams(params);
162163

163164
return 0;
164165
}

source/main.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <cstring>
44
#include <exception>
55
#include <getopt.h>
6-
#include "fs_printer.h"
6+
#include "errors.h"
77
#include "format.h"
88
#include "verbose_printf.h"
99

@@ -23,7 +23,6 @@ static void printHelp(void)
2323
" -f, --force-fat32 Force format SDXC cards as FAT32.\n"
2424
" No effect on other types of SD cards.\n"
2525
" -l, --label LABEL Volume label. Maximum 11 uppercase characters.\n"
26-
" -p, --print-fs Don't format. Print the filesystem structure.\n"
2726
" -v, --verbose Show format details.\n"
2827
" -h, --help Output this help.\n");
2928
}
@@ -36,7 +35,6 @@ int main(const int argc, char *const argv[])
3635
{ "erase", required_argument, NULL, 'e'},
3736
{"force-fat32", no_argument, NULL, 'f'},
3837
{ "label", required_argument, NULL, 'l'},
39-
{ "print-fs", no_argument, NULL, 'p'},
4038
{ "verbose", no_argument, NULL, 'v'},
4139
{ "help", no_argument, NULL, 'h'},
4240
{ NULL, 0, NULL, 0}};
@@ -46,7 +44,7 @@ int main(const int argc, char *const argv[])
4644
char label[12]{};
4745
while(1)
4846
{
49-
const int c = getopt_long(argc, argv, "c:de:fl:pvh", long_options, NULL);
47+
const int c = getopt_long(argc, argv, "c:" /*d*/ "e:fl:vh", long_options, NULL);
5048
if(c == -1) break;
5149

5250
switch(c)
@@ -57,7 +55,7 @@ int main(const int argc, char *const argv[])
5755
if(overrTotSec == 0 || overrTotSec > 1ull<<32) // Max 2 TiB.
5856
{
5957
fputs("Error: Image size 0 or out of range.\n", stderr);
60-
return 1;
58+
return ERR_INVALID_ARG;
6159
}
6260
}
6361
break;
@@ -66,14 +64,15 @@ int main(const int argc, char *const argv[])
6664
break;*/
6765
case 'e':
6866
{
67+
// TODO: Support full overwrite?
6968
if(strcmp(optarg, "trim") == 0)
7069
flags.erase = 1;
7170
else if(strcmp(optarg, "secure") == 0)
7271
flags.secErase = 1;
7372
else
7473
{
7574
fprintf(stderr, "Error: Invalid erase type '%s'.\n", optarg);
76-
return 1;
75+
return ERR_INVALID_ARG;
7776
}
7877
}
7978
break;
@@ -85,7 +84,7 @@ int main(const int argc, char *const argv[])
8584
if(strlen(optarg) > 11)
8685
{
8786
fputs("Error: Label is longer than 11 characters.\n", stderr);
88-
return 1;
87+
return ERR_INVALID_ARG;
8988
}
9089
strncpy(label, optarg, 11);
9190
/*for(u32 i = 0; i < 11; i++)
@@ -99,17 +98,14 @@ int main(const int argc, char *const argv[])
9998
lc == 0x7C)
10099
{
101100
fputs("Error: Label contains invalid characters.\n", stderr);
102-
return 1;
101+
return ERR_INVALID_ARG;
103102
}
104103
// TODO: The label should be encoded in the system's DOS code page. Convert from UTF-8 to DOS code page.
105104
// TODO: Check for uppercase chars and give a warning.
106105
// TODO: Do not allow the "NO NAME" label?
107106
}*/
108107
}
109108
break;
110-
case 'p':
111-
flags.printFs = 1;
112-
break;
113109
case 'v':
114110
flags.verbose = 1;
115111
break;
@@ -121,36 +117,32 @@ int main(const int argc, char *const argv[])
121117
// Fallthrough.
122118
default:
123119
printHelp();
124-
return 1;
120+
return ERR_INVALID_ARG;
125121
}
126122
}
127123

128124
if(argc - optind < 1 || argc - optind > 1)
129125
{
130126
printHelp();
131-
return 1;
127+
return ERR_INVALID_ARG;
132128
}
133129

134130
const char *const devPath = argv[optind];
135131
int res;
136132
try
137133
{
138-
if(!flags.printFs)
139-
{
140-
setVerboseMode(flags.verbose);
141-
res = formatSd(devPath, (*label != 0 ? label : NULL), flags, overrTotSec);
142-
}
143-
else res = printDiskInfo(devPath);
134+
setVerboseMode(flags.verbose);
135+
res = formatSd(devPath, (*label != 0 ? label : NULL), flags, overrTotSec);
144136
}
145-
catch(const std::exception& e)
137+
catch(const std::exception &e)
146138
{
147139
fprintf(stderr, "An exception occurred: what(): '%s'\n", e.what());
148-
res = 8;
140+
res = ERR_EXCEPTION;
149141
}
150142
catch(...)
151143
{
152144
fprintf(stderr, "Unknown exception. Aborting...\n");
153-
res = 9;
145+
res = ERR_UNK_EXCEPTION;
154146
}
155147

156148
return res;

0 commit comments

Comments
 (0)