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
109110u32 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 (¶ms, dev) != 0 ) return 5 ;
149+ if (createMbrAndPartition (¶ms, 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}
0 commit comments