-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredsimg.c
333 lines (288 loc) · 7.78 KB
/
redsimg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* REDSFS Image creator and reader tool
*
* Author: Farran Rebbeck
* https://github.com/frebbles/redsfs
*
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include "redsfs.h"
static int retcode = 0;
static uint8_t *flash;
// Die with an error message
void die(char * msg)
{
printf("ERROR: %s \r\n", msg);
exit(-1);
}
// Mapped function for reading (for micros this is usually a SPI/FLASH read function call)
uint32_t linux_fs_read ( uint32_t addr, uint32_t size, uint8_t * dest )
{
memcpy (dest, flash + addr, size );
return 0;
}
// Mapped function for writing (for micros this is usually a SPI/FLASH write function call)
uint32_t linux_fs_write ( uint32_t addr, uint32_t size, uint8_t * src )
{
//printf("Writing to addr %x \r\n", addr);
memcpy ( flash + addr, src, size );
return 0;
}
int import_file ( char * dir, char * path )
{
int n;
char buf[256];
int filepathlen;
int pathlen;
int dirlen;
char * filepath;
// Open reds file for writing
int file = redsfs_open( path, MODE_WRITE );
if (file < 0) return -1;
// Open file for reading to copy into redsfs
filepathlen = strlen(dir) + strlen(path) + 2;
pathlen = strlen(path);
dirlen = strlen(dir);
filepath = malloc( filepathlen );
memset( filepath, 0, filepathlen );
memcpy( filepath, dir, dirlen );
memcpy( filepath+dirlen, "/", 1);
memcpy( filepath+dirlen+1, path, pathlen);
int fin = open( filepath, O_RDONLY );
if (fin < 0) return fin;
while ((n = read(fin, buf, sizeof(buf)))) {
retcode = redsfs_write ( buf, n );
if (retcode < 0) die("Write issue (out of space?)\r\n");
}
// Close the reds file (complete the copy);
redsfs_close();
close(fin);
free(filepath);
return 0;
}
int import_dir ( char * path )
{
// Open directory
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, "..") && strcmp(dir->d_name, "."))
{
printf("Importing file : %s\n", dir->d_name);
import_file(path, dir->d_name);
}
}
closedir(d);
}
return 0;
}
int export_file ( char * dir, char * path )
{
int n;
char buf[256];
int filepathlen;
int pathlen;
int dirlen;
char * filepath;
// Open reds file for writing
int file = redsfs_open( path, MODE_WRITE );
if (file < 0) return -1;
// Open file for reading to copy into redsfs
filepathlen = strlen(dir) + strlen(path) + 2;
pathlen = strlen(path);
dirlen = strlen(dir);
filepath = malloc( filepathlen );
memset( filepath, 0, filepathlen );
memcpy( filepath, dir, dirlen );
memcpy( filepath+dirlen, "/", 1);
memcpy( filepath+dirlen+1, path, pathlen);
FILE * fout = fopen( filepath, "w" );
if (fout < 0) return -1;
ssize_t fSize = redsfs_cur_file_size();
printf("Size=%zd\r\n", fSize);
while ((n = redsfs_read( buf, sizeof(buf)))) {
retcode = fwrite ( buf, 1, n, fout );
}
// Close the reds file (complete the copy);
redsfs_close();
fclose(fout);
free(filepath);
return 0;
}
int export_dir ( char * path )
{
uint8_t file;
char * fname;
// Check our export directory has been created
struct stat st = {0};
if (stat(path, &st) == -1) {
mkdir(path, 0755);
}
// Start export/list
printf("Exporting/Listing files...\r\n");
while ( (fname = redsfs_next_file()) != NULL ) {
printf("Exporting file: %s \r\n", fname);
export_file( path, fname );
}
return 0;
}
void list_files()
{
uint8_t file;
char * fname;
printf("Files in redsfs image:\r\n");
while ( ( fname = redsfs_next_file() ) != NULL ) {
printf(" - %s\r\n", fname);
}
}
int readwrite_test()
{
char buf[256];
int bufSz;
printf ("Opening a file to write...\r\n");
int file = redsfs_open( "test.txt", MODE_APPEND );
printf ("Writing to file\r\n");
if (redsfs_write("The quick brown fox jumps over the lazy dog... ", 47) == 0)
{
redsfs_close();
redsfs_unmount();
die("Couldn't write to redsfs...");
}
printf("Closing file\r\n");
redsfs_close();
printf("Reopening read to read contents back...\r\n");
file = redsfs_open("test.txt", MODE_READ );
while ( (bufSz = redsfs_read(buf, 255)) > 0) {
if (bufSz > 0)
{
printf("READ: %d bytes: %s\r\n", bufSz, buf);
} else {
redsfs_close();
redsfs_unmount();
die("Could not read from redsfs...");
}
memset(buf, 0, 256);
}
printf ("Opening a file to append some more\r\n");
file = redsfs_open( "test.txt", MODE_APPEND );
printf ("Writing to file\r\n");
if (redsfs_write("The slow wharty sloth crawls under the cosy blanket... ", 55) == 0)
{
redsfs_close();
redsfs_unmount();
die("Couldn't write to redsfs...");
}
printf("Closing file\r\n");
redsfs_close();
printf("Reopening to read appended text\r\n");
file = redsfs_open("test.txt", MODE_READ );
memset(buf, 0, 256);
while ( (bufSz = redsfs_read(buf, 255)) > 0) {
if (bufSz > 0)
{
printf("READ: %d bytes: %s\r\n", bufSz, buf);
} else {
redsfs_close();
redsfs_unmount();
die("Could not read from redsfs...");
}
memset(buf, 0, 256);
}
//printf("Deleting file\r\n");
//redsfs_delete("test.txt");
printf("Read/Write tests passed\r\n");
return 0;
}
int main( int argc, char *argv[] )
{
int opt;
const char *fname = 0;
bool create = false;
enum { CMD_NONE, CMD_LIST, CMD_IMPORT, CMD_EXPORT, CMD_TEST } command = CMD_NONE;
size_t sz = 0;
char *imp_dir = 0;
char *exp_dir = 0;
while ((opt = getopt (argc, argv, "f:c:li:e:t")) != -1)
{
switch (opt)
{
case 'f': fname = optarg; break;
case 'c': create = true; sz = strtoul (optarg, 0, 0); break;
case 'l': command = CMD_LIST; break;
case 'i': command = CMD_IMPORT; imp_dir = optarg; break;
case 'e': command = CMD_EXPORT; exp_dir = optarg; break;
case 't': command = CMD_TEST; break;
default: die("no options");
}
}
int fd = open (fname, (create ? (O_CREAT | O_TRUNC) : 0) | O_RDWR, 0660);
if (fd == -1)
die ("File not opened");
if (create)
{
if (lseek (fd, sz -1, SEEK_SET) == -1)
die ("lseek");
if (write (fd, "", 1) != 1)
die ("write");
}
else if (!sz)
{
off_t offs = lseek (fd, 0, SEEK_END);
if (offs == -1)
die ("lseek");
sz = offs;
}
if (sz & (BLK_SIZE -1))
die ("file size not multiple of page size");
flash = mmap (0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (!flash)
die ("mmap");
if (create) {
memset (flash, 0, sz);
}
redsfs_fs redsfs_mnt;
redsfs_mnt.fs_start = 0;
redsfs_mnt.fs_block_size = 256;
redsfs_mnt.call_read_f = linux_fs_read;
redsfs_mnt.call_write_f = linux_fs_write;;
redsfs_mnt.fs_end = sz;
printf("Mounting redsfs...\r\n");
int rfmt = redsfs_mount( &redsfs_mnt );
if (command == CMD_IMPORT)
{
import_dir( imp_dir );
}
if (command == CMD_EXPORT)
{
export_dir( exp_dir );
}
if (command == CMD_LIST)
{
list_files();
}
if (command == CMD_TEST)
{
readwrite_test();
}
printf("Unmounting... \r\n");
redsfs_unmount();
munmap(flash, sz);
close(fd);
return 0;
}