source: EcnlProtoTool/trunk/tools/makefsdata/main.c@ 445

Last change on this file since 445 was 445, checked in by coas-nagasima, 4 years ago

ROMファイルシステムのWebコンテンツをwwwフォルダ下に移動。
etcフォルダ追加。

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 13.5 KB
Line 
1/*------------------------------------------------------------------------/
2/ The Main Development Bench of FatFs Module
3/-------------------------------------------------------------------------/
4/
5/ Copyright (C) 2014, ChaN, all right reserved.
6/
7/ * This software is a free software and there is NO WARRANTY.
8/ * No restriction on use. You can use, modify and redistribute it for
9/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
10/ * Redistributions of source code must retain the above copyright notice.
11/
12/-------------------------------------------------------------------------*/
13
14
15#include <string.h>
16#include <stdarg.h>
17#include <stdio.h>
18#include <locale.h>
19#include "diskio.h"
20#include "ff.h"
21#include "zlib.h"
22
23int SZ_RAMDISK = 1536; /* 1.5[MB] */
24int SS_RAMDISK = 512;
25
26int assign_drives(void);
27
28
29#ifdef UNICODE
30#if !_LFN_UNICODE
31#error Configuration mismatch. _LFN_UNICODE must be 1.
32#endif
33#else
34#if _LFN_UNICODE
35#error Configuration mismatch. _LFN_UNICODE must be 0.
36#endif
37#endif
38
39
40
41#if _MULTI_PARTITION /* Volume - partition resolution table (Example) */
42PARTITION VolToPart[] = {
43 { 0, 0 }, /* "0:" <== Disk# 0, auto detect */
44 { 1, 0 }, /* "1:" <== Disk# 1, auto detect */
45 { 2, 0 }, /* "2:" <== Disk# 2, auto detect */
46 { 3, 1 }, /* "3:" <== Disk# 3, 1st partition */
47 { 3, 2 }, /* "4:" <== Disk# 3, 2nd partition */
48 { 3, 3 }, /* "5:" <== Disk# 3, 3rd partition */
49 { 4, 0 }, /* "6:" <== Disk# 4, auto detect */
50 { 5, 0 } /* "7:" <== Disk# 5, auto detect */
51};
52#endif
53
54
55/*---------------------------------------------------------*/
56/* Work Area */
57/*---------------------------------------------------------*/
58
59
60LONGLONG AccSize; /* Work register for scan_files() */
61WORD AccFiles, AccDirs;
62FILINFO Finfo;
63#if _USE_LFN
64TCHAR LFName[256];
65#endif
66
67TCHAR Line[300]; /* Console input/output buffer */
68HANDLE hCon, hKey;
69
70FATFS FatFs[_VOLUMES]; /* File system object for logical drive */
71BYTE InBuff[4 * 1024 * 1024]; /* Working buffer */
72BYTE OutBuff[4 * 1024 * 1024]; /* Working buffer */
73
74#if _USE_FASTSEEK
75DWORD SeekTbl[16]; /* Link map table for fast seek feature */
76#endif
77
78
79/*---------------------------------------------------------*/
80/* User Provided RTC Function for FatFs module */
81/*---------------------------------------------------------*/
82/* This is a real time clock service to be called from */
83/* FatFs module. Any valid time must be returned even if */
84/* the system does not support an RTC. */
85/* This function is not required in read-only cfg. */
86
87DWORD get_fattime(void)
88{
89 SYSTEMTIME tm;
90
91 /* Get local time */
92 GetLocalTime(&tm);
93
94 /* Pack date and time into a DWORD variable */
95 return ((DWORD)(tm.wYear - 1980) << 25)
96 | ((DWORD)tm.wMonth << 21)
97 | ((DWORD)tm.wDay << 16)
98 | (WORD)(tm.wHour << 11)
99 | (WORD)(tm.wMinute << 5)
100 | (WORD)(tm.wSecond >> 1);
101}
102
103
104
105/*--------------------------------------------------------------------------*/
106/* Monitor */
107
108/*----------------------------------------------*/
109/* Get a value of the string */
110/*----------------------------------------------*/
111/* "123 -5 0x3ff 0b1111 0377 w "
112^ 1st call returns 123 and next ptr
113^ 2nd call returns -5 and next ptr
114^ 3rd call returns 1023 and next ptr
115^ 4th call returns 15 and next ptr
116^ 5th call returns 255 and next ptr
117^ 6th call fails and returns 0
118*/
119
120int xatoi( /* 0:Failed, 1:Successful */
121 TCHAR **str, /* Pointer to pointer to the string */
122 long *res /* Pointer to a valiable to store the value */
123 )
124{
125 unsigned long val;
126 unsigned char r, s = 0;
127 TCHAR c;
128
129
130 *res = 0;
131 while((c = **str) == ' ') (*str)++; /* Skip leading spaces */
132
133 if(c == '-') { /* negative? */
134 s = 1;
135 c = *(++(*str));
136 }
137
138 if(c == '0') {
139 c = *(++(*str));
140 switch(c) {
141 case 'x': /* hexdecimal */
142 r = 16; c = *(++(*str));
143 break;
144 case 'b': /* binary */
145 r = 2; c = *(++(*str));
146 break;
147 default:
148 if(c <= ' ') return 1; /* single zero */
149 if(c < '0' || c > '9') return 0; /* invalid char */
150 r = 8; /* octal */
151 }
152 }
153 else {
154 if(c < '0' || c > '9') return 0; /* EOL or invalid char */
155 r = 10; /* decimal */
156 }
157
158 val = 0;
159 while(c > ' ') {
160 if(c >= 'a') c -= 0x20;
161 c -= '0';
162 if(c >= 17) {
163 c -= 7;
164 if(c <= 9) return 0; /* invalid char */
165 }
166 if(c >= r) return 0; /* invalid char for current radix */
167 val = val * r + c;
168 c = *(++(*str));
169 }
170 if(s) val = 0 - val; /* apply sign if needed */
171
172 *res = val;
173 return 1;
174}
175
176
177/*----------------------------------------------*/
178/* Dump a block of byte array */
179
180void put_dump(
181 const unsigned char* buff, /* Pointer to the byte array to be dumped */
182 unsigned long addr, /* Heading address value */
183 int cnt /* Number of bytes to be dumped */
184 )
185{
186 int i;
187
188
189 _tprintf(_T("%08lX:"), addr);
190
191 for(i = 0; i < cnt; i++)
192 _tprintf(_T(" %02X"), buff[i]);
193
194 _puttchar(' ');
195 for(i = 0; i < cnt; i++)
196 _puttchar((TCHAR)((buff[i] >= ' ' && buff[i] <= '~') ? buff[i] : '.'));
197
198 _puttchar('\n');
199}
200
201void put_rc(const TCHAR *func, FRESULT rc)
202{
203 const TCHAR *p =
204 _T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
205 _T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
206 _T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
207 _T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");
208 FRESULT i;
209
210 for(i = 0; i != rc && *p; i++) {
211 while(*p++);
212 }
213 _tprintf(_T("%s() =>%u FR_%s\n"), func, (UINT)rc, p);
214}
215
216void xcopy(TCHAR *path)
217{
218 TCHAR subpath[_MAX_PATH];
219 TCHAR temp[_MAX_PATH];
220 WIN32_FIND_DATA lp;
221 FRESULT res;
222 FIL file[2]; /* File objects */
223 DWORD len, wlen;
224 UINT s2, cnt;
225 HANDLE h;
226 FILINFO fi;
227 SYSTEMTIME st;
228 z_stream stream;
229
230 _tcscpy_s(temp, sizeof(temp), path);
231 h = FindFirstFile(temp, &lp);
232 if(h == INVALID_HANDLE_VALUE)
233 return;
234
235 temp[strlen(temp) - 1] = '\0';
236 do
237 {
238 if((lp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
239 && strcmp(lp.cFileName, "..") != 0 && strcmp(lp.cFileName, ".") != 0)
240 {
241 if((res = f_mkdir(lp.cFileName)) != RES_OK)
242 put_rc("f_mkdir", res);
243
244 FileTimeToSystemTime(&lp.ftLastWriteTime, &st);
245 fi.fdate = (WORD)(((st.wYear - 1980) * 512U) | st.wMonth * 32U | st.wDay);
246 fi.ftime = (WORD)(st.wHour * 2048U | st.wMinute * 32U | st.wSecond / 2U);
247 if((res = f_utime(lp.cFileName, &fi)) != RES_OK)
248 put_rc("f_utime", res);
249
250 _stprintf_s(subpath, sizeof(subpath), _T("%s%s\\*"), temp, lp.cFileName);
251 if((res = f_chdir(lp.cFileName)) != RES_OK)
252 put_rc("f_chdir", res);
253 xcopy(subpath);
254 if((res = f_chdir(_T(".."))) != RES_OK)
255 put_rc("f_chdir", res);
256 }
257 if((lp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
258 {
259 HANDLE fh;
260
261 res = -1;
262 _stprintf_s(subpath, sizeof(subpath), "%s%s", temp, lp.cFileName);
263 fh = CreateFile(subpath, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
264 if(fh != INVALID_HANDLE_VALUE) {
265 if(ReadFile(fh, InBuff, sizeof InBuff, &len, 0))
266 res = FR_OK;
267 CloseHandle(fh);
268
269 if (len == sizeof InBuff)
270 _tprintf(_T("file too big %s > %d bytes"), subpath, len);
271 }
272
273 while (res == FR_OK) {
274 if (strncmp(temp, "rom_fs\\www\\", 11) == 0) {
275 /* ˆ³k */
276 memset(&stream, 0, sizeof(stream));
277 /* allocate deflate memory, set up for gzip compression */
278 stream.zalloc = Z_NULL;
279 stream.zfree = Z_NULL;
280 stream.opaque = Z_NULL;
281 stream.next_in = InBuff;
282 stream.avail_in = len;
283 stream.next_out = OutBuff;
284 stream.avail_out = sizeof(OutBuff);
285 res = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
286 MAX_WBITS + 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
287 if (res != Z_OK) {
288 _tprintf(_T("out of memory %d"), res);
289 break;
290 }
291
292 res = deflate(&stream, Z_FULL_FLUSH);
293 if (res == Z_STREAM_ERROR) {
294 _tprintf(_T("internal error: deflate stream corrupt %d"), res);
295 break;
296 }
297
298 deflateEnd(&stream);
299
300 len = stream.total_out;
301 }
302 else {
303 memcpy(OutBuff, InBuff, len);
304 }
305 break;
306 }
307
308 if(res == FR_OK){
309 if((res = f_open(&file[0], lp.cFileName, FA_CREATE_ALWAYS | FA_WRITE)) != RES_OK)
310 put_rc("f_open", res);
311 wlen = 0;
312 while(len) {
313 if((UINT)len >= sizeof OutBuff) {
314 cnt = sizeof OutBuff;
315 len -= sizeof OutBuff;
316 }
317 else {
318 cnt = len;
319 len = 0;
320 }
321 res = f_write(&file[0], OutBuff, cnt, &s2);
322 if(res != FR_OK) {
323 put_rc("f_write", res);
324 break;
325 }
326 wlen += s2;
327 if(cnt != s2)
328 break;
329 }
330 _tprintf(_T("%lu bytes written.\n"), wlen);
331 if((res = f_close(&file[0])) != RES_OK)
332 put_rc("f_close", res);
333
334 if((res = f_chmod(lp.cFileName, AM_RDO | AM_ARC, AM_RDO | AM_ARC | AM_SYS | AM_HID)) != RES_OK)
335 put_rc("f_chmod", res);
336
337 FileTimeToSystemTime(&lp.ftLastWriteTime, &st);
338 fi.fdate = (WORD)(((st.wYear - 1980) * 512U) | st.wMonth * 32U | st.wDay);
339 fi.ftime = (WORD)(st.wHour * 2048U | st.wMinute * 32U | st.wSecond / 2U);
340 if((res = f_utime(lp.cFileName, &fi)) != RES_OK)
341 put_rc("f_utime", res);
342 }
343 }
344 } while(FindNextFile(h, &lp));
345
346 FindClose(h);
347}
348
349FRESULT scan_files (
350 char* path, /* Start node to be scanned (also used as work area) */
351 int size
352)
353{
354 FRESULT res;
355 FILINFO fno;
356 DIR dir;
357 int i;
358 char *fn; /* This function assumes non-Unicode configuration */
359#if _USE_LFN
360 static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
361 fno.lfname = lfn;
362 fno.lfsize = sizeof lfn;
363#endif
364
365 res = f_opendir(&dir, path); /* Open the directory */
366 if (res == FR_OK) {
367 i = strlen(path);
368 for (;;) {
369 res = f_readdir(&dir, &fno); /* Read a directory item */
370 if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
371 if (fno.fname[0] == '.') continue; /* Ignore dot entry */
372#if _USE_LFN
373 fn = *fno.lfname ? fno.lfname : fno.fname;
374#else
375 fn = fno.fname;
376#endif
377 if (fno.fattrib & AM_DIR) { /* It is a directory */
378 sprintf_s(&path[i], size -i, "/%s", fn);
379 res = scan_files(path, size);
380 path[i] = 0;
381 if (res != FR_OK) break;
382 } else { /* It is a file. */
383 printf("%s/%s\n", path, fn);
384 }
385 }
386 f_closedir(&dir);
387 }
388
389 return res;
390}
391
392int _tmain(int argc, TCHAR *argv[])
393{
394 TCHAR pool[50];
395 FRESULT res;
396 BYTE pdrv = 0;
397 WORD w;
398 DWORD dw;
399 char path[256] = ".";
400 FATFS *fs;
401 DWORD fre_clust, fre_sect, tot_sect;
402
403 _stprintf_s(pool, sizeof(pool), _T(".%u"), _CODE_PAGE);
404 _tsetlocale(LC_CTYPE, pool);
405
406 _tprintf(_T("FatFs module test monitor (%s, CP:%u/%s)\n\n"),
407 _USE_LFN ? _T("LFN") : _T("SFN"),
408 _CODE_PAGE,
409 _LFN_UNICODE ? _T("Unicode") : _T("ANSI"));
410
411 _stprintf_s(pool, sizeof(pool), _T("FatFs debug console (%s, CP:%u/%s)"),
412 _USE_LFN ? _T("LFN") : _T("SFN"),
413 _CODE_PAGE,
414 _LFN_UNICODE ? _T("Unicode") : _T("ANSI"));
415 SetConsoleTitle(pool);
416
417 assign_drives(); /* Find physical drives on the PC */
418
419#if _MULTI_PARTITION
420 _tprintf(_T("\nMultiple partition feature is enabled. Each logical drive is tied to the patition as follows:\n"));
421 for(cnt = 0; cnt < sizeof VolToPart / sizeof(PARTITION); cnt++) {
422 const TCHAR *pn[] = { _T("auto detect"), _T("1st partition"), _T("2nd partition"), _T("3rd partition"), _T("4th partition") };
423
424 _tprintf(_T("\"%u:\" <== Disk# %u, %s\n"), cnt, VolToPart[cnt].pd, pn[VolToPart[cnt].pt]);
425 }
426 _tprintf(_T("\n"));
427#else
428 _tprintf(_T("\nMultiple partition feature is disabled.\nEach logical drive is tied to the same physical drive number.\n\n"));
429#endif
430
431#if _USE_LFN
432 Finfo.lfname = LFName;
433 Finfo.lfsize = sizeof LFName;
434#endif
435
436 res = disk_initialize(pdrv);
437 _tprintf(_T("rc=%d\n"), res);
438 if(disk_ioctl(pdrv, GET_SECTOR_SIZE, &w) == RES_OK)
439 _tprintf(_T("Sector size = %u\n"), w);
440 if(disk_ioctl(pdrv, GET_SECTOR_COUNT, &dw) == RES_OK)
441 _tprintf(_T("Number of sectors = %u\n"), dw);
442
443 if((res = f_mount(&FatFs[0], _T(""), 0)) != RES_OK)
444 put_rc("f_mount", res);
445
446 if((res = f_mkfs(_T(""), 1, 0)) != RES_OK)
447 put_rc("f_mkfs", res);
448
449 xcopy(_T("rom_fs\\*"));
450
451 if(disk_ioctl(0, 201, _T("src\\rom_fs.bin")) == RES_OK)
452 _tprintf(_T("Ok\n"));
453
454 if((res = scan_files(path, sizeof(path))) != RES_OK)
455 put_rc("scan_files", res);
456
457 /* Get volume information and free clusters of drive 1 */
458 if(f_getfree(_T("0:"), &fre_clust, &fs) == RES_OK){
459 /* Get total sectors and free sectors */
460 tot_sect = (fs->n_fatent - 2) * fs->csize;
461 fre_sect = fre_clust * fs->csize;
462
463 /* Print the free space (assuming 512 bytes/sector) */
464 _tprintf(_T("%10lu KB total drive space.\n%10lu KB available.\n"),
465 tot_sect / 2, fre_sect / 2);;
466 }
467
468 char temp[100];
469 int len;
470 HANDLE sf, df;
471 sf = CreateFile("src\\rom_fs.bin", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
472 if (sf == INVALID_HANDLE_VALUE)
473 return;
474 DWORD fileLen = GetFileSize(sf, NULL);
475 df = CreateFile("src\\rom_fs.c", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
476 if (df == INVALID_HANDLE_VALUE)
477 return;
478
479 len = sprintf(temp, "const unsigned char RamDisk[] = {\n");
480 WriteFile(df, temp, len, NULL, NULL);
481
482 DWORD rlen = 0;
483 for (int p = 0; p < fileLen; p += rlen) {
484 char c[16];
485 if (ReadFile(sf, c, sizeof(c), &rlen, NULL) != TRUE) {
486 break;
487 }
488
489 len = sprintf(temp, "\t");
490 WriteFile(df, temp, len, NULL, NULL);
491
492 for (int i = 0; i < rlen; i++) {
493 len = sprintf(temp, "0x%02X,", (unsigned char)c[i]);
494 WriteFile(df, temp, len, NULL, NULL);
495 }
496 len = sprintf(temp, "\n");
497 WriteFile(df, temp, len, NULL, NULL);
498 }
499 len = sprintf(temp, "};\n");
500 WriteFile(df, temp, len, NULL, NULL);
501
502 CloseHandle(sf);
503
504 CloseHandle(df);
505}
Note: See TracBrowser for help on using the repository browser.