source: uKadecot/trunk/tools/makefsdata/main.c@ 107

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

SHIFT_JISのコードにcharsetプロパティを付けた

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