Exemplo n.º 1
0
/* fatfsLs():
 * The incoming string is assumed to be some directory name.
 * If NULL, then it is assumed to be the CF's top-most level.
 */ 
static int
fatfsLs(char *dirname)
{
	int	ftot;
	DIRENT de;
	DIRINFO di;
	char *dir;

	if (dirname)
		dir = dirname;
	else
		dir = "";

	di.scratch = sector;
	if (DFS_OpenDir(&vi, (uint8_t *)dir, &di)) {
		printf("Can't open dir: <%s>\n",dirname);
		return(CMD_FAILURE);
	}

	ftot = 0;
	while (!DFS_GetNext(&vi, &di, &de)) {
		char tmp[32]; 

		if (de.name[0] && ((de.attr & ATTR_LONG_NAME) != ATTR_LONG_NAME)) {
			if ((de.attr & ATTR_VOLUME_ID) > 0) {
				de.name[8] = 0;
				printf("Volume:     %s\n",de.name);
				continue;
            }
			ftot++;

			printf("%02d/%02d/%4d  %02d:%02d   ",
				CREATE_MON(de), CREATE_DAY(de), CREATE_YEAR(de),
				CREATE_HR(de), CREATE_MIN(de));

			if ((de.attr & ATTR_DIRECTORY) > 0) {
				// truncate the extension
				de.name[8] = 0;

				printf("<DIR>            %-8s",de.name);
			}
			else {
				DFS_DirToCanonical((uint8_t *)tmp,de.name);
				printf("       %8d  %-12s",FILESIZE(de), tmp);
			}

			printf("  %c%c%c%c%c\n",
				(( de.attr & ATTR_VOLUME_ID) ? 'V' : ' '),
				(( de.attr & ATTR_READ_ONLY) ? 'R' : ' '),
				(( de.attr & ATTR_ARCHIVE  ) ? 'A' : ' '),
				(( de.attr & ATTR_SYSTEM   ) ? 'S' : ' '),
				(( de.attr & ATTR_HIDDEN   ) ? 'H' : ' '));
		}
	}
	shell_sprintf(FATFS_FTOT_STR,"%d",ftot);
	return(0);
}
Exemplo n.º 2
0
/* fatfsRm():
 * Remove the specified file from the FATFS.
 * If the path is a directory, then don't remove anything.
 */
static int
fatfsRm(char *fatfspath)
{
	DIRINFO di;

	di.scratch = sector;
	if (DFS_OpenDir(&vi, (uint8_t *)fatfspath, &di) == 0) {
		printf("can't remove directory '%s'\n",fatfspath);
		return(-1);
	}
	if (DFS_UnlinkFile(&vi, (uint8_t *)fatfspath, sector)) {
		printf("error unlinking file %s\n",fatfspath);
		return(-1);
	}
	return(0);
}
Exemplo n.º 3
0
int main(int _argc, char *_argv[])
{
	uint32_t pstart, psize, i;
	uint8_t pactive, ptype;
	uint8_t sector[SECTOR_SIZE], sector2[SECTOR_SIZE];
	VOLINFO vi;
	DIRINFO di;
	DIRENT de;
	uint32_t cache;
	FILEINFO fi;
	uint8_t *p;

	if (_argc < 2) {
		printf("Usage: %s [image_file]\n", _argv[0]);
		return -1;
	}	

	// Attach user-specified image file
	if (DFS_HostAttach(_argv[1])) {
		printf("Cannot attach image file '%s'\n", _argv[1]);
		return -1;
	}

	// Obtain pointer to first partition on first (only) unit
	pstart = DFS_GetPtnStart(0, sector, 0, &pactive, &ptype, &psize);
	if (pstart == 0xffffffff) {
		printf("Cannot find first partition\n");
		return -1;
	}

	printf("Partition 0 start sector 0x%-08.8lX active %-02.2hX type %-02.2hX size %-08.8lX\n", pstart, pactive, ptype, psize);

	if (DFS_GetVolInfo(0, sector, pstart, &vi)) {
		printf("Error getting volume information\n");
		return -1;
	}
	printf("Volume label '%-11.11s'\n", vi.label);
	printf("%d sector/s per cluster, %d reserved sector/s, volume total %d sectors.\n", vi.secperclus, vi.reservedsecs, vi.numsecs);
	printf("%d sectors per FAT, first FAT at sector #%d, root dir at #%d.\n",vi.secperfat,vi.fat1,vi.rootdir);
	printf("(For FAT32, the root dir is a CLUSTER number, FAT12/16 it is a SECTOR number)\n");
	printf("%d root dir entries, data area commences at sector #%d.\n",vi.rootentries,vi.dataarea);
	printf("%d clusters (%d bytes) in data area, filesystem IDd as ", vi.numclusters, vi.numclusters * vi.secperclus * SECTOR_SIZE);
	if (vi.filesystem == FAT12)
		printf("FAT12.\n");
	else if (vi.filesystem == FAT16)
		printf("FAT16.\n");
	else if (vi.filesystem == FAT32)
		printf("FAT32.\n");
	else
		printf("[unknown]\n");

//------------------------------------------------------------
// Directory enumeration test
	di.scratch = sector;
//	if (DFS_OpenDir(&vi, "", &di)) {
//		printf("Error opening root directory\n");
//		return -1;
//	}
	if (DFS_OpenDir(&vi, "MYDIR1", &di)) {
		printf("error opening subdirectory\n");
		return -1;
	}
	while (!DFS_GetNext(&vi, &di, &de)) {
		if (de.name[0])
			printf("file: '%-11.11s'\n", de.name);
	}


//------------------------------------------------------------
// Unlink test
//	cache = 0;
//	printf("*** FAT BEFORE ***\n");
//  	for (i=0;i<vi.numclusters;i++) {
//		printf("entry %-08.8x, %-08.8X\n", i, DFS_GetFAT(&vi, sector, &cache, i));
//	}
//	if (DFS_UnlinkFile(&vi, "MYDIR1/SUBDIR1.2/README.TXT", sector)) {
//		printf("error unlinking file\n");
//	}
//	printf("*** FAT AFTER ***\n");
//  	for (i=0;i<vi.numclusters;i++) {
//		printf("entry %-08.8x, %-08.8X\n", i, DFS_GetFAT(&vi, sector, &cache, i));
//	}

//------------------------------------------------------------
// File write test
	if (DFS_OpenFile(&vi, "MYDIR1/WRTEST.TXT", DFS_WRITE, sector, &fi)) {
		printf("error opening file\n");
		return -1;
	}
	for (i=0;i<18;i++) {
		memset(sector2, 128+i, SECTOR_SIZE);
		DFS_WriteFile(&fi, sector, sector2, &cache, SECTOR_SIZE/2);
		memset(sector2+256, 255-i, SECTOR_SIZE/2);
		DFS_WriteFile(&fi, sector, sector2+256, &cache, SECTOR_SIZE/2);
	}
	sprintf(sector2, "test string at the end...");
	DFS_WriteFile(&fi, sector, sector2, &cache, strlen(sector2));

//------------------------------------------------------------
// File read test
	printf("Readback test\n");
	if (DFS_OpenFile(&vi, "MYDIR1/WRTEST.TXT", DFS_READ, sector, &fi)) {
		printf("error opening file\n");
		return -1;
	}
	p = (void *) malloc(fi.filelen+512);
	memset(p, 0xaa, fi.filelen+512);

	DFS_ReadFile(&fi, sector, p, &i, fi.filelen);
printf("read complete %d bytes (expected %d) pointer %d\n", i, fi.filelen, fi.pointer);

	{
	FILE *fp;
	fp=fopen("test.txt","wb");
	fwrite(p, fi.filelen+512, 1, fp);
	fclose(fp);
	}

	return 0;
}
Exemplo n.º 4
0
bool
walkfs(int pass)
{
    int a;
    int e;
    int ms;
    uint32 i;
    uint32 j;
    uint32 rv;
    uint32 pstart;
    uint32 rfilesize;
    VOLINFO vi;
    DIRINFO di;
    DIRENT de;
    bool skip;
    bool first;
    int wfilesize;
    uint32 namei[DEPTH];
    uint32 skips[DEPTH];
    static char dirname[MAX_PATH];
    static char rfilename[MAX_PATH];
    static char wfilename[MAX_PATH];
    static char tfilename[MAX_PATH];
    static byte scratch[SECTOR_SIZE];
    
    ms = ticks;
    
    printf("walkfs\n");

#if ! _WIN32
    while (adc_result[0] < 20000) {
        DFS_HostFlush(0);
        led_sad(code_battery);
        delay(1000);
    }
    led_unknown();
#endif
    
    params_get(&params);
    assert(params.roflip == true || params.roflip == false);

    memset(scratch, -1, sizeof(scratch));  // remove
    pstart = DFS_GetPtnStart(0, scratch, 0, NULL, NULL, NULL);
    if (pstart == DFS_ERRMISC) {
        return 0;
    }

    memset(scratch, -1, sizeof(scratch));  // remove
    if (DFS_GetVolInfo(0, scratch, pstart, &vi)) {
        return 0;
    }
    
    if (! vi.secperclus || (vi.secperclus & (vi.secperclus-1))) {
        printf("invalid fs cluster size %d\n", vi.secperclus);
        return 0;
    }
    
    printf("fs cluster size %d\n", vi.secperclus);

    i = 0;
    namei[i] = 0;
    skips[i] = 0;
    strcpy(dirname, "");
    
    first = 1;
    for (;;) {
XXX_LOOP_XXX:        
        if (panic) {
            return 0;
        }
        
        // open a new directory
        memset(scratch, -1, sizeof(scratch));  // remove
        di.scratch = scratch;
        if (DFS_OpenDir(&vi, (unsigned char *)dirname, &di)) {
            return 0;
        }

        memset(&de, 0, sizeof(de));

        skip = 0;
        for (j = 0; j < skips[i]; j++) {
            if (DFS_GetNext(&vi, &di, &de)) {
                skip = 1;
            }
        }

        // enumerate the directory
        if (! skip && ! DFS_GetNext(&vi, &di, &de)) {
            skips[i]++;
         
            if (! de.name[0] || de.name[0] == '.') {
                continue;
            }

#if ! _WIN32
            while (adc_result[0] < 20000) {
                DFS_HostFlush(0);
                led_sad(code_battery);
                delay(1000);
            }
            led_unknown();

            while ((MCF_GPIO_SETNQ & 0x10) == 0) {
                // we're paused
                DFS_HostFlush(0);
            }
#endif
            
            if (de.attr & ATTR_DIRECTORY) {
                if (i+1 < DEPTH) {
                    namei[i] = strlen(dirname);
                    if (namei[i]) {
                        strcat(dirname, "/");
                    }
                    strncat(dirname, (char *)de.name, 8);
                    tailtrim(dirname);
                    if (strncmp((char *)de.name+8, "   ", 3)) {
                        strcat(dirname, ".");
                        strncat(dirname, (char *)de.name+8, 3);
                        tailtrim(dirname);
                    }
                    i++;
                    skips[i] = 0;
                    printf("dir: '%s'\n", dirname);
                    goto XXX_LOOP_XXX;
                }
            } else if (! (de.attr & (ATTR_HIDDEN|ATTR_SYSTEM|ATTR_VOLUME_ID)) && !!(de.attr & ATTR_READ_ONLY) == params.roflip) {
                // force upper case
                for (a = 0; a < sizeof(de.name); a++) {
                    if (de.name[a] >= 'a' && de.name[a] <= 'z') {
                        de.name[a] = de.name[a] - 'a' + 'A';
                    }
                }
                
                if (pass == 0) {
                    // pass 0: check for extension match for tmp file
                    if (! strncmp("TMP", (char *)de.name+8, 3)) {
                        strcpy(rfilename, dirname);
                        if (strlen(rfilename)) {
                            strcat(rfilename, "/");
                        }
                        strncat(rfilename, (char *)de.name, 8);
                        tailtrim(rfilename);
                        if (strncmp((char *)de.name+8, "   ", 3)) {
                            strcat(rfilename, ".");
                            strncat(rfilename, (char *)de.name+8, 3);
                            tailtrim(rfilename);
                        }

                        // rfilename needs to be purged!
                        
                        rfilesize = (de.filesize_3<<24)|(de.filesize_2<<16)|(de.filesize_1<<8)|de.filesize_0;
                        printf("temp: '%s (%d bytes)'\n", rfilename, rfilesize);

                        // we have to rewrite rfilename
                        rv = encrypt_file(&vi, scratch, NULL, 0, rfilename, rfilesize);
                        if (! rv) {
                            printf("rewrite failed\n");
                            return 0;
                        }
                        
                        // and unlink it
                        rv = DFS_UnlinkFile(&vi, (unsigned char *)rfilename, scratch);
                        if (rv) {
                            printf("unlink failed\n");
                            return 0;
                        }

                        DFS_HostFlush(0);
                    }
                } else {
                    assert(pass == 1);
                    
                    // pass 1: check for extension match for image file
                    for (e = 0; e < NEXTS; e++) {
                        if (params.extensions[e][0] != 0 && params.extensions[e][0] != (char)-1) {
                            if (! strncmp(params.extensions[e], (char *)de.name+8, 3)) {
                                break;
                            }
                        }
                    }
                    
                    if (e != NEXTS) {
                        strcpy(rfilename, dirname);
                        if (strlen(rfilename)) {
                            strcat(rfilename, "/");
                        }
                        strncat(rfilename, (char *)de.name, 8);
                        tailtrim(rfilename);
                        if (strncmp((char *)de.name+8, "   ", 3)) {
                            strcat(rfilename, ".");
                            strncat(rfilename, (char *)de.name+8, 3);
                            tailtrim(rfilename);
                        }
                        
                        // rfilename needs to be encrypted!
                        
                        rfilesize = (de.filesize_3<<24)|(de.filesize_2<<16)|(de.filesize_1<<8)|de.filesize_0;
                        printf("encrypt: '%s (%d bytes)'\n", rfilename, rfilesize);
                        
                        if (first) {
                            // for the first rfilename we create a new (unallocated) .REC file directly
                            output_file(rfilename, ".REC", wfilename);
                            wfilesize = 0;
                        }
                        
                        // we'll encrypt to wfilename
                        
                        rv = encrypt_file(&vi, scratch, rfilename, rfilesize, wfilename, wfilesize);
                        if (! rv) {
                            printf("encryption failed\n");
                            return 0;
                        }
                        
                        // and then we rename wfilename to rfilename with .REC
                        if (! first) {
                            // and then rename the old file
                            output_file(rfilename, ".REC", tfilename);
                            rv = DFS_RenameFile(&vi, (unsigned char *)wfilename, (unsigned char *)tfilename, scratch);
                            if (rv) {
                                printf("rename failed\n");
                                return 0;
                            }
                        }
                        
                        // and rename rfilename to rfilename with .TMP
                        output_file(rfilename, ".TMP", wfilename);
                        wfilesize = rfilesize;
                        rv = DFS_RenameFile(&vi, (unsigned char *)rfilename, (unsigned char *)wfilename, scratch);
                        if (rv) {
                            printf("rename failed\n");
                            return 0;
                        }
                        
                        // we reuse an old (allocated) file
                        first = 0;
                    }
                }
            }
        } else {
            skip = 1;
        }

        if (! skip) {
            // we have more files in this directory
            goto XXX_LOOP_XXX;
        }

        // we're leaving this directory        
        if (pass == 1 && ! first) {
            // lastly we have to rewrite wfilename
            rv = encrypt_file(&vi, scratch, NULL, 0, wfilename, wfilesize);
            if (! rv) {
                printf("rewrite failed\n");
                return 0;
            }
            
            // and unlink it
            rv = DFS_UnlinkFile(&vi, (unsigned char *)wfilename, scratch);
            if (rv) {
                printf("unlink failed\n");
                return 0;
            }

            DFS_HostFlush(0);
            
            first = 1;
        }

        // if this is the root dirtectory...        
        if (! i) {
            // we're done
            break;
        }

        // continue a previous directory
        i--;
        dirname[namei[i]] = '\0';
    }
    
    DFS_HostFlush(0);
    total_ticks += ticks-ms;
    
    return 1;
}
Exemplo n.º 5
0
static int
fatfsQry(char *fatfspath, int verbose)
{
	DIRENT de;
	DIRINFO di;
	char *lastslash, *fname, *dir, pathcopy[80], matchname[80];
	int flen, flen1, fsize, pathlen, match, ftot;

	if (!fatfspath)
		return(-1);

	/* Prior to each 'ls', clear the content of the name and
	 * size shell variables...
	 */
	setenv(FATFS_FNAME_STR,0);
	setenv(FATFS_FSIZE_STR,0);
	setenv(FATFS_FTOT_STR,0);

	pathlen = strlen(fatfspath);
	if (pathlen > sizeof(pathcopy)) {
		printf("path too big\n");
		return(-1);
	}
	strcpy(pathcopy, fatfspath);
	lastslash = strrchr(pathcopy,DIR_SEPARATOR);

	if (lastslash == 0) {
		dir = "";
		fname = pathcopy;
	}
	else {
		*lastslash = 0;
		dir = pathcopy;
		fname = lastslash+1;
	}

	flen = strlen(fname);
	if (verbose > 1)
		printf("Qry opening dir <%s>, fname <%s>...\n",dir,fname);

	di.scratch = sector;
	if (DFS_OpenDir(&vi, (uint8_t *)dir, &di)) {
		printf("error opening subdirectory\n");
		return(CMD_FAILURE);
	}

	match = fsize = ftot = flen1 = 0;
	while (!DFS_GetNext(&vi, &di, &de)) {
		int i;
		char dosname[16];

		memset(dosname,0,sizeof(dosname));
		if (de.name[0] && ((de.attr & ATTR_LONG_NAME) != ATTR_LONG_NAME)) {
			if ((de.attr & ATTR_VOLUME_ID) || (de.attr & ATTR_DIRECTORY)) {
				for(i=0;i<8;i++) {
					if (de.name[i] != ' ') {
						dosname[i] = de.name[i];
					}
					else {
						dosname[i] = 0;
						break;
					}
				}
				dosname[8] = 0;
			}
			else
				DFS_DirToCanonical((uint8_t *)dosname,de.name);
	
			flen1 = strlen(dosname);

			if ((fname[0] == '*') && (fname[flen-1] == '*')) {
				fname[flen-1] = 0;
				if (strstr(dosname,fname+1))
					match = 1;
				fname[flen-1] = '*';
			}
			else if (fname[0] == '*') {
				if (!strcmp(dosname+(flen1-flen+1),fname+1))
					match = 1;
			}
			else if (fname[flen-1] == '*') {
				fname[flen-1] = 0;
				if (!strncmp(dosname,fname,flen-1))
					match = 1;
				fname[flen-1] = '*';
			}
			else if (!strcmp(dosname,fname)) {
				match = 1;
			}
			if (match) {
				strcpy(matchname,dosname);
				fsize = FILESIZE(de);
				ftot++;
				match = 0;
				if (verbose)
					printf("  %s (%d)\n",dosname,fsize);
			}
		}
	}
	shell_sprintf(FATFS_FTOT_STR,"%d",ftot);
	if (ftot) {
		shell_sprintf(FATFS_FSIZE_STR,"%d",fsize);
		setenv(FATFS_FNAME_STR,matchname);
	}
	return(0);
}
void telnet_ls( file_handle_t handle, char** argv, unsigned int argc )
{
	if( argc != 2 )
	{
		file_puts("ls [path]\r\n", telnet_handle);
		return;	
	}
	u8_t* scratch = malloc(SECTOR_SIZE);
	VOLINFO* volinfo = malloc( sizeof(VOLINFO) );
	DIRINFO* dir = malloc( sizeof(DIRINFO) );
	DIRENT* dirent = malloc( sizeof(DIRENT) );

	//get partition start sector
	uint32_t startsector = DFS_GetPtnStart( 0, scratch, 0 , NULL, NULL, NULL );
	if( startsector == DFS_ERRMISC )
	{
		file_puts("Error finding partition start\r\n", handle);
		goto exit;
	}
	//get volume info
	if( DFS_GetVolInfo(0,scratch,startsector,volinfo) ) 
	{
		file_puts("Error getting volume info\r\n", handle);
		goto exit;
	}
	//open dir
	dir->scratch = scratch;
	if( DFS_OpenDir( volinfo, (u8_t*)argv[1], dir ) ) 
	{
		file_puts("Error opening dir ", handle);
		file_puts( argv[1], handle );
		file_puts( CRLF, handle );
		goto exit;
	}
	file_puts("Directory of ", handle );
	file_puts( argv[1] , handle );
	file_puts( CRLF, handle );
	char* tmp = malloc(64);
	while( DFS_GetNext(volinfo, dir, dirent) == DFS_OK )
	{
		if( dirent->name[0] != '\0' )
		{	
			char* t= tmp;
			uint32_t file_size =dirent->filesize_3;
			file_size <<= 8;
			file_size+=dirent->filesize_2;
			file_size <<= 8;
			file_size+=dirent->filesize_1;
		 	file_size <<= 8;
			file_size+=dirent->filesize_0; 

			register uint16_t date = (dirent->wrtdate_h <<8) + dirent->wrtdate_l;
			register uint16_t time = (dirent->wrttime_h <<8) + dirent->wrttime_l;
			register uint8_t attr = dirent->attr;
			t+=sprintf(t,"%04hu-%02hu-%02hu %02hu:%02hu:%02hu  ",
				(date >> 9) + 1980,
				(date & 0x01e0) >> 5,
				(date & 0x1f),
				(time >> 11),
				(time & 0x07e0) >>5,
				(time & 0x1f) << 1);

			t+=sprintf(t,"%c%c%c%c%c  %10lu  ",
				(attr & ATTR_READ_ONLY ? 'r':'-'),
				(attr & ATTR_HIDDEN ? 'h':'-'),
				(attr & ATTR_SYSTEM ? 's':'-'),
				(attr & ATTR_DIRECTORY ? 'd':'-'),
				(attr & ATTR_ARCHIVE ? 'a':'-'),
				file_size);
			sprintf(t,"%s",dirent->name);
			t[12] = '\0';
			t[11] = t[10];
			t[10] = t[9];
			t[9] = t[8];
			t[8] = (attr & ATTR_DIRECTORY ? ' ':'.');
			file_puts( tmp, handle );
			file_puts( CRLF, handle );
		}
	}