Skip to content

Commit 5c96088

Browse files
committed
Add page and block size arguments
1 parent 8e2dbdb commit 5c96088

File tree

1 file changed

+101
-70
lines changed

1 file changed

+101
-70
lines changed

main.cpp

Lines changed: 101 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@
2121
#include "tclap/CmdLine.h"
2222
#include "tclap/UnlabeledValueArg.h"
2323

24-
#define LOG_PAGE_SIZE 256
25-
2624
static std::vector<uint8_t> s_flashmem;
25+
2726
static std::string s_dirName;
2827
static std::string s_imageName;
2928
static int s_imageSize;
29+
static int s_pageSize;
30+
static int s_blockSize;
3031

31-
enum Action { ACTION_NONE, ACTION_PACK, ACTION_LIST };
32+
enum Action { ACTION_NONE, ACTION_PACK, ACTION_LIST, ACTION_VISUALIZE };
3233
static Action s_action = ACTION_NONE;
3334

34-
static spiffs _filesystemStorageHandle;
35-
static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
36-
static u8_t spiffs_fds[32*4];
37-
static u8_t spiffs_cache[(LOG_PAGE_SIZE+32)*4];
35+
static spiffs s_fs;
36+
37+
static std::vector<uint8_t> s_spiffsWorkBuf;
38+
static std::vector<uint8_t> s_spiffsFds;
39+
static std::vector<uint8_t> s_spiffsCache;
3840

3941

4042
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst)
@@ -55,69 +57,63 @@ static s32_t api_spiffs_erase(u32_t addr, u32_t size)
5557
return SPIFFS_OK;
5658
}
5759

58-
spiffs_config spiffs_get_storage_config()
59-
{
60-
spiffs_config cfg = {0};
61-
62-
cfg.phys_addr = 0x0000;
63-
cfg.phys_size = (u32_t) s_flashmem.size();
64-
65-
const int flash_sector_size = 4096;
66-
const int log_page_size = 256;
67-
68-
cfg.phys_erase_block = flash_sector_size; // according to datasheet
69-
cfg.log_block_size = flash_sector_size * 2; // Important to make large
70-
cfg.log_page_size = log_page_size; // as we said
71-
return cfg;
72-
}
73-
74-
void check_callback(spiffs_check_type type, spiffs_check_report report,
60+
void checkCallback(spiffs_check_type type, spiffs_check_report report,
7561
u32_t arg1, u32_t arg2)
7662
{
7763
}
7864

79-
void spiffs_mount(bool init)
65+
void spiffsMount(bool init)
8066
{
81-
spiffs_config cfg = spiffs_get_storage_config();
67+
spiffs_config cfg = {0};
8268

83-
// debugf("fs.start:%x, size:%d Kb\n", cfg.phys_addr, cfg.phys_size / 1024);
69+
cfg.phys_addr = 0x0000;
70+
cfg.phys_size = (u32_t) s_flashmem.size();
71+
72+
cfg.phys_erase_block = s_blockSize;
73+
cfg.log_block_size = s_blockSize;
74+
cfg.log_page_size = s_pageSize;
8475

8576
cfg.hal_read_f = api_spiffs_read;
8677
cfg.hal_write_f = api_spiffs_write;
8778
cfg.hal_erase_f = api_spiffs_erase;
8879

89-
int res = SPIFFS_mount(&_filesystemStorageHandle,
80+
const int maxOpenFiles = 4;
81+
s_spiffsWorkBuf.resize(s_pageSize * 2);
82+
s_spiffsFds.resize(32 * maxOpenFiles);
83+
s_spiffsCache.resize((32 + s_pageSize) * maxOpenFiles);
84+
85+
86+
int res = SPIFFS_mount(&s_fs,
9087
&cfg,
91-
spiffs_work_buf,
92-
spiffs_fds,
93-
sizeof(spiffs_fds),
94-
spiffs_cache,
95-
sizeof(spiffs_cache),
88+
&s_spiffsWorkBuf[0],
89+
&s_spiffsFds[0],
90+
s_spiffsFds.size(),
91+
&s_spiffsCache[0],
92+
s_spiffsCache.size(),
9693
NULL);
9794

9895
// debugf("mount res: %d\n", res);
9996
if (init) {
100-
spiffs_file fd = SPIFFS_open(&_filesystemStorageHandle, "tmp", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
97+
spiffs_file fd = SPIFFS_open(&s_fs, "tmp", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
10198
uint8_t tmp = 0xff;
102-
SPIFFS_write(&_filesystemStorageHandle, fd, &tmp, 1);
103-
SPIFFS_fremove(&_filesystemStorageHandle, fd);
104-
SPIFFS_close(&_filesystemStorageHandle, fd);
99+
SPIFFS_write(&s_fs, fd, &tmp, 1);
100+
SPIFFS_fremove(&s_fs, fd);
101+
SPIFFS_close(&s_fs, fd);
105102
} else {
106-
_filesystemStorageHandle.check_cb_f = &check_callback;
107-
//SPIFFS_check(&_filesystemStorageHandle);
103+
s_fs.check_cb_f = &checkCallback;
104+
//SPIFFS_check(&s_fs);
108105
}
109106
}
110107

111-
void spiffs_unmount()
108+
void spiffsUnmount()
112109
{
113110
uint32_t total, used;
114-
SPIFFS_info(&_filesystemStorageHandle, &total, &used);
115-
SPIFFS_vis(&_filesystemStorageHandle);
111+
SPIFFS_info(&s_fs, &total, &used);
116112
std::cerr << "total: " << total << ", used: " << used << std::endl;
117-
SPIFFS_unmount(&_filesystemStorageHandle);
113+
SPIFFS_unmount(&s_fs);
118114
}
119115

120-
int add_file(char* name, const char* path) {
116+
int addFile(char* name, const char* path) {
121117
const size_t write_size = 512;
122118
uint8_t write_block[write_size];
123119

@@ -127,7 +123,7 @@ int add_file(char* name, const char* path) {
127123
return 1;
128124
}
129125

130-
spiffs_file dst = SPIFFS_open(&_filesystemStorageHandle, name, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
126+
spiffs_file dst = SPIFFS_open(&s_fs, name, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
131127

132128
fseek(src, 0, SEEK_END);
133129
size_t size = ftell(src);
@@ -138,24 +134,29 @@ int add_file(char* name, const char* path) {
138134
{
139135
size_t will_write = std::min(left, write_size);
140136
if (will_write != fread(write_block, 1, will_write, src)) {
141-
std::cerr << "error: failed to read from file";
142137
fclose(src);
143-
SPIFFS_close(&_filesystemStorageHandle, dst);
138+
SPIFFS_close(&s_fs, dst);
139+
return 1;
140+
}
141+
int res = SPIFFS_write(&s_fs, dst, write_block, write_size);
142+
if (res < 0) {
143+
fclose(src);
144+
SPIFFS_close(&s_fs, dst);
144145
return 1;
145146
}
146-
SPIFFS_write(&_filesystemStorageHandle, dst, write_block, write_size);
147147
left -= will_write;
148148
}
149149

150-
SPIFFS_close(&_filesystemStorageHandle, dst);
150+
SPIFFS_close(&s_fs, dst);
151151
fclose(src);
152152
return 0;
153153
}
154154

155-
int add_files(const char* dirname)
155+
int addFiles(const char* dirname)
156156
{
157157
DIR *dir;
158158
struct dirent *ent;
159+
bool error = false;
159160
if ((dir = opendir (dirname)) != NULL) {
160161
while ((ent = readdir (dir)) != NULL) {
161162
if (ent->d_name[0] == '.')
@@ -170,24 +171,27 @@ int add_files(const char* dirname)
170171
continue;
171172
}
172173

173-
std::cerr << "adding " << ent->d_name << std::endl;
174-
if (add_file(ent->d_name, fullpath.c_str()) != 0) {
174+
std::cerr << "adding " << ent->d_name;
175+
if (addFile(ent->d_name, fullpath.c_str()) != 0) {
176+
std::cerr << " - error!" << std::endl;
177+
error = true;
175178
break;
176179
}
180+
std::cerr << std::endl;
177181
}
178182
closedir (dir);
179183
} else {
180184
std::cerr << "warning: can't read source directory" << std::endl;
181185
return 1;
182186
}
183-
return 0;
187+
return (error) ? 1 : 0;
184188
}
185189

186190
void listFiles() {
187191
spiffs_DIR dir;
188192
spiffs_dirent ent;
189193

190-
spiffs_DIR* res = SPIFFS_opendir(&_filesystemStorageHandle, 0, &dir);
194+
spiffs_DIR* res = SPIFFS_opendir(&s_fs, 0, &dir);
191195
spiffs_dirent* it;
192196
while (true) {
193197
it = SPIFFS_readdir(&dir, &ent);
@@ -210,16 +214,14 @@ int actionPack() {
210214
return 1;
211215
}
212216

213-
spiffs_mount(true);
214-
add_files(s_dirName.c_str());
215-
spiffs_unmount();
216-
217+
spiffsMount(true);
218+
int result = addFiles(s_dirName.c_str());
219+
spiffsUnmount();
217220

218221
fwrite(&s_flashmem[0], 4, s_flashmem.size()/4, fdres);
219-
220222
fclose(fdres);
221223

222-
return 0;
224+
return result;
223225
}
224226

225227

@@ -235,23 +237,48 @@ int actionList() {
235237
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc);
236238
fclose(fdsrc);
237239

238-
spiffs_mount(false);
240+
spiffsMount(false);
239241
listFiles();
240-
spiffs_unmount();
242+
spiffsUnmount();
241243

242244
return 0;
243245
}
244246

245-
int processArgs(int argc, const char** argv) {
247+
int actionVisualize() {
248+
s_flashmem.resize(s_imageSize, 0xff);
249+
250+
FILE* fdsrc = fopen(s_imageName.c_str(), "rb");
251+
if (!fdsrc) {
252+
std::cerr << "error: failed to open image file" << std::endl;
253+
return 1;
254+
}
255+
256+
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc);
257+
fclose(fdsrc);
258+
259+
spiffsMount(false);
260+
SPIFFS_vis(&s_fs);
261+
spiffsUnmount();
262+
263+
return 0;
264+
}
265+
266+
void processArgs(int argc, const char** argv) {
246267

247268
TCLAP::CmdLine cmd("", ' ', "0.1");
248269
TCLAP::ValueArg<std::string> packArg( "c", "create", "create spiffs image from a directory", true, "", "pack_dir");
249-
TCLAP::SwitchArg listArg( "l", "list", "list spiffs image to a directory", false);
270+
TCLAP::SwitchArg listArg( "l", "list", "list files in spiffs image", false);
271+
TCLAP::SwitchArg visualizeArg( "i", "visualize", "visualize spiffs image", false);
250272
TCLAP::UnlabeledValueArg<std::string> outNameArg( "image_file", "spiffs image file", true, "", "image_file" );
251-
TCLAP::ValueArg<int> sizeArg( "s", "fs_size", "fs image size, in bytes", false, 0xb000, "number" );
273+
TCLAP::ValueArg<int> imageSizeArg( "s", "size", "fs image size, in bytes", false, 0x10000, "number" );
274+
TCLAP::ValueArg<int> pageSizeArg( "p", "page", "fs page size, in bytes", false, 256, "number" );
275+
TCLAP::ValueArg<int> blockSizeArg( "b", "block", "fs block size, in bytes", false, 4096, "number" );
252276

253-
cmd.add( sizeArg );
254-
cmd.xorAdd( packArg, listArg );
277+
cmd.add( imageSizeArg );
278+
cmd.add( pageSizeArg );
279+
cmd.add( blockSizeArg );
280+
std::vector<TCLAP::Arg*> args = {&packArg, &listArg, &visualizeArg};
281+
cmd.xorAdd( args );
255282
cmd.add( outNameArg );
256283
cmd.parse( argc, argv );
257284

@@ -262,11 +289,14 @@ int processArgs(int argc, const char** argv) {
262289
else if (listArg.isSet()) {
263290
s_action = ACTION_LIST;
264291
}
292+
else if (visualizeArg.isSet()) {
293+
s_action = ACTION_VISUALIZE;
294+
}
265295

266296
s_imageName = outNameArg.getValue();
267-
s_imageSize = sizeArg.getValue();
268-
269-
return 0;
297+
s_imageSize = imageSizeArg.getValue();
298+
s_pageSize = pageSizeArg.getValue();
299+
s_blockSize = blockSizeArg.getValue();
270300
}
271301

272302
int main(int argc, const char * argv[]) {
@@ -283,6 +313,7 @@ int main(int argc, const char * argv[]) {
283313
switch (s_action) {
284314
case ACTION_PACK: return actionPack();
285315
case ACTION_LIST: return actionList();
316+
case ACTION_VISUALIZE: return actionVisualize();
286317
default: ;
287318
}
288319

0 commit comments

Comments
 (0)