21
21
#include " tclap/CmdLine.h"
22
22
#include " tclap/UnlabeledValueArg.h"
23
23
24
- #define LOG_PAGE_SIZE 256
25
-
26
24
static std::vector<uint8_t > s_flashmem;
25
+
27
26
static std::string s_dirName;
28
27
static std::string s_imageName;
29
28
static int s_imageSize;
29
+ static int s_pageSize;
30
+ static int s_blockSize;
30
31
31
- enum Action { ACTION_NONE, ACTION_PACK, ACTION_LIST };
32
+ enum Action { ACTION_NONE, ACTION_PACK, ACTION_LIST, ACTION_VISUALIZE };
32
33
static Action s_action = ACTION_NONE;
33
34
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;
38
40
39
41
40
42
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)
55
57
return SPIFFS_OK;
56
58
}
57
59
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,
75
61
u32_t arg1, u32_t arg2)
76
62
{
77
63
}
78
64
79
- void spiffs_mount (bool init)
65
+ void spiffsMount (bool init)
80
66
{
81
- spiffs_config cfg = spiffs_get_storage_config () ;
67
+ spiffs_config cfg = { 0 } ;
82
68
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;
84
75
85
76
cfg.hal_read_f = api_spiffs_read;
86
77
cfg.hal_write_f = api_spiffs_write;
87
78
cfg.hal_erase_f = api_spiffs_erase;
88
79
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,
90
87
&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 ( ),
96
93
NULL );
97
94
98
95
// debugf("mount res: %d\n", res);
99
96
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 );
101
98
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);
105
102
} else {
106
- _filesystemStorageHandle .check_cb_f = &check_callback ;
107
- // SPIFFS_check(&_filesystemStorageHandle );
103
+ s_fs .check_cb_f = &checkCallback ;
104
+ // SPIFFS_check(&s_fs );
108
105
}
109
106
}
110
107
111
- void spiffs_unmount ()
108
+ void spiffsUnmount ()
112
109
{
113
110
uint32_t total, used;
114
- SPIFFS_info (&_filesystemStorageHandle, &total, &used);
115
- SPIFFS_vis (&_filesystemStorageHandle);
111
+ SPIFFS_info (&s_fs, &total, &used);
116
112
std::cerr << " total: " << total << " , used: " << used << std::endl;
117
- SPIFFS_unmount (&_filesystemStorageHandle );
113
+ SPIFFS_unmount (&s_fs );
118
114
}
119
115
120
- int add_file (char * name, const char * path) {
116
+ int addFile (char * name, const char * path) {
121
117
const size_t write_size = 512 ;
122
118
uint8_t write_block[write_size];
123
119
@@ -127,7 +123,7 @@ int add_file(char* name, const char* path) {
127
123
return 1 ;
128
124
}
129
125
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 );
131
127
132
128
fseek (src, 0 , SEEK_END);
133
129
size_t size = ftell (src);
@@ -138,24 +134,29 @@ int add_file(char* name, const char* path) {
138
134
{
139
135
size_t will_write = std::min (left, write_size);
140
136
if (will_write != fread (write_block, 1 , will_write, src)) {
141
- std::cerr << " error: failed to read from file" ;
142
137
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);
144
145
return 1 ;
145
146
}
146
- SPIFFS_write (&_filesystemStorageHandle, dst, write_block, write_size);
147
147
left -= will_write;
148
148
}
149
149
150
- SPIFFS_close (&_filesystemStorageHandle , dst);
150
+ SPIFFS_close (&s_fs , dst);
151
151
fclose (src);
152
152
return 0 ;
153
153
}
154
154
155
- int add_files (const char * dirname)
155
+ int addFiles (const char * dirname)
156
156
{
157
157
DIR *dir;
158
158
struct dirent *ent;
159
+ bool error = false ;
159
160
if ((dir = opendir (dirname)) != NULL ) {
160
161
while ((ent = readdir (dir)) != NULL ) {
161
162
if (ent->d_name [0 ] == ' .' )
@@ -170,24 +171,27 @@ int add_files(const char* dirname)
170
171
continue ;
171
172
}
172
173
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 ;
175
178
break ;
176
179
}
180
+ std::cerr << std::endl;
177
181
}
178
182
closedir (dir);
179
183
} else {
180
184
std::cerr << " warning: can't read source directory" << std::endl;
181
185
return 1 ;
182
186
}
183
- return 0 ;
187
+ return (error) ? 1 : 0 ;
184
188
}
185
189
186
190
void listFiles () {
187
191
spiffs_DIR dir;
188
192
spiffs_dirent ent;
189
193
190
- spiffs_DIR* res = SPIFFS_opendir (&_filesystemStorageHandle , 0 , &dir);
194
+ spiffs_DIR* res = SPIFFS_opendir (&s_fs , 0 , &dir);
191
195
spiffs_dirent* it;
192
196
while (true ) {
193
197
it = SPIFFS_readdir (&dir, &ent);
@@ -210,16 +214,14 @@ int actionPack() {
210
214
return 1 ;
211
215
}
212
216
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 ();
217
220
218
221
fwrite (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdres);
219
-
220
222
fclose (fdres);
221
223
222
- return 0 ;
224
+ return result ;
223
225
}
224
226
225
227
@@ -235,23 +237,48 @@ int actionList() {
235
237
fread (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdsrc);
236
238
fclose (fdsrc);
237
239
238
- spiffs_mount (false );
240
+ spiffsMount (false );
239
241
listFiles ();
240
- spiffs_unmount ();
242
+ spiffsUnmount ();
241
243
242
244
return 0 ;
243
245
}
244
246
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) {
246
267
247
268
TCLAP::CmdLine cmd (" " , ' ' , " 0.1" );
248
269
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 );
250
272
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" );
252
276
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 );
255
282
cmd.add ( outNameArg );
256
283
cmd.parse ( argc, argv );
257
284
@@ -262,11 +289,14 @@ int processArgs(int argc, const char** argv) {
262
289
else if (listArg.isSet ()) {
263
290
s_action = ACTION_LIST;
264
291
}
292
+ else if (visualizeArg.isSet ()) {
293
+ s_action = ACTION_VISUALIZE;
294
+ }
265
295
266
296
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 () ;
270
300
}
271
301
272
302
int main (int argc, const char * argv[]) {
@@ -283,6 +313,7 @@ int main(int argc, const char * argv[]) {
283
313
switch (s_action) {
284
314
case ACTION_PACK: return actionPack ();
285
315
case ACTION_LIST: return actionList ();
316
+ case ACTION_VISUALIZE: return actionVisualize ();
286
317
default : ;
287
318
}
288
319
0 commit comments