void telnet_cat( file_handle_t handle, char** argv, unsigned int argc )
{
	if( argc != 2 )
	{
		file_puts("cat [filename]\r\n", telnet_handle);
		return;	
	}
	u8_t* scratch = malloc(SECTOR_SIZE);
	VOLINFO* volinfo = malloc( sizeof(VOLINFO) );
	FILEINFO* file = malloc( sizeof(FILEINFO) );
	u8_t* read_buf = malloc( SEND_BUF_SIZE );

	//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 file
	if( DFS_OpenFile( volinfo, (u8_t*)argv[1], DFS_READ, scratch, file ) ) 
	{
		file_puts("Error opening file ", handle);
		file_puts( argv[1], handle );
		file_puts( CRLF, handle );
		goto exit;
	}
	//read it to the output handle
	uint32_t read = 0;
	//TODO - would like the use the send buffer as THE buffer
	while( read  < file->filelen ) 
	{
		uint32_t did_read;
		DFS_ReadFile( file, scratch, read_buf, &did_read, SEND_BUF_SIZE );
		if( did_read == 0 )
		{
			file_puts("Error reading file ", handle);
			file_puts( argv[1], handle );
			file_puts( CRLF, handle );
			goto exit;
		}
		u16_t i = 0;
		for( ; i < did_read; i++ )
		{
			file_putchar( read_buf[i], handle );
		}
		read += did_read;
	} 
exit:
	free( read_buf );
	free( file );
	free( volinfo );
	free( scratch );
}
Example #2
0
// function called by fread
static int File_read(FILE *fp, unsigned char *buf, int count)
{
    int retval;
    uint32_t successcount;
    PFILEINFO fileinfo = (PFILEINFO)fp->drvarg[0];

    retval = DFS_ReadFile(fileinfo, dfs_scratch, buf, &successcount, count);
    if (retval != DFS_OK)
    {
        successcount = 0;
        errno = dfs_stdio_errno(retval);
    }
    return successcount;
}
Example #3
0
/* fatfsGet():
 * Retreive a file from the FATFS and place it in the specified destination.
 * The destination may be either a TFS filename or a hex address
 * (indicated by a leading "0x").
 *
 * Input:
 * char *fatfspath
 *	Pointer to FATFS file and path.
 * char *dest
 *	Pointer to TFS filename or a addr,len string.
 *	The expected "addr,len" syntax is 0xADDR,LEN or 0xADDR,0xLEN.
 */
static int
fatfsGet(char *fatfile, char *dest)
{
	FILEINFO fi;
	char *p, tfs;
	uint32_t i;

	if (DFS_OpenFile(&vi, (uint8_t *)fatfile, DFS_READ, sector, &fi)) {
		printf("error opening file\n");
		return(-1);
	}
	if (strncmp(dest,"0x",2) == 0) {
		tfs = 0;
		p = (char *)strtoul(dest,0,0);
	}
	else {
		tfs = 1;
		p = fatfs_tmp_space;
	}

	DFS_ReadFile(&fi, sector, (uint8_t *)p, &i, fi.filelen);
	if (i != fi.filelen) {
		printf("read %d, expected %d\n",i,fi.filelen);
		return(-1);
	}
	if (tfs) {
#if INCLUDE_TFS
		int	tfserr;
		char *flags, *info;

		flags = info = (char *)0;
		if ((flags = strchr(dest,','))) {
			*flags++ = 0;
			if ((info = strchr(flags,',')))
				*info++ = 0;
		}
		tfserr = tfsadd(dest,info,flags,(uchar *)p,fi.filelen);
		if (tfserr != TFS_OKAY) {
			printf("TFS error: %s\n",tfserrmsg(tfserr));
			return(-1);
		}
#else
		printf("TFS not built in\n");
		return(-1);
#endif
	}
	return(0);
}
Example #4
0
/* fatfsCat():
 * Dump the content of the specified fatfs file to the console.
 * This function assumes the data is ASCII.
 */
static int
fatfsCat(char *filepath)
{
	uint32_t i, j;
	FILEINFO fi;
	uint8_t *p;

	if (DFS_OpenFile(&vi, (uint8_t *)filepath, DFS_READ, sector, &fi)) {
		printf("error opening '%s'\n",filepath);
		return(-1);
	}
	p = (uint8_t *)fatfs_tmp_space;
	DFS_ReadFile(&fi, sector, p, &i, fi.filelen);
	for(j=0;j<fi.filelen;j++)
		putchar(*p++);
	putchar('\n');
	return(0);
}
Example #5
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;
}
Example #6
0
static
bool
encrypt_file(PVOLINFO vi, byte *scratch, char *rfilename, int rfilesize, char *wfilename, int wfilesize)
{
    int ms;
    int ms2;
    int filesize;
    FILEINFO rfi;
    FILEINFO wfi;
    uint32 actual;
    uint32 result;
    uint32 initial;
    header_t *header;
    byte chain[16];
    
    ms = ticks;
    initial = seconds;    
    
    // open the input file, if specified
    if (rfilename && DFS_OpenFile(vi, (unsigned char *)rfilename, DFS_READ, scratch, &rfi)) {
        return false;
    }
    
    // open the output file
    if (DFS_OpenFile(vi, (unsigned char *)wfilename, DFS_WRITE, scratch, &wfi)) {
        return false;
    }
    
    filesize = 0;
    if (rfilename) {
        // first encrypt the header
        memset(big_buffer, 0, sizeof(big_buffer));
        header = (header_t *)big_buffer;
        header->filesize = rfilesize;
        strcpy(header->filename, rfilename);
        
        aes_pre_enc(params.aesbits, params.aeskey, chain);
        
        // encrypt the next block of data
        aes_enc(chain, sizeof(big_buffer), big_buffer);

        ms2 = ticks;
        if (DFS_WriteFile(&wfi, scratch, big_buffer, &actual, sizeof(big_buffer))) {
            return false;
        }
        write_ticks += ticks-ms2;
        assert(actual == sizeof(big_buffer));
        filesize += actual;
        encrypt_bytes += actual;
    
        // then read all the data of the file
        for (;;) {
            if (panic) {
                return false;
            }
            
            ms2 = ticks;
            result = DFS_ReadFile(&rfi, scratch, big_buffer, &actual, sizeof(big_buffer));
            if (result == DFS_EOF || (! result && ! actual)) {
                break;
            }
            read_ticks += ticks-ms2;
            
            if (result) {
                return false;
            }
            
            assert(actual && actual <= sizeof(big_buffer));
            memset(big_buffer+actual, 0, sizeof(big_buffer)-actual);
            
            // encrypt the next block of data
            aes_enc(chain, sizeof(big_buffer), big_buffer);
            
            ms2 = ticks;
            result = DFS_WriteFile(&wfi, scratch, big_buffer, &actual, sizeof(big_buffer));
            if (result) {
                return false;
            }
            write_ticks += ticks-ms2;
            
            assert(actual == sizeof(big_buffer));
            filesize += actual;
            encrypt_bytes += actual;
        }
    }
    
    // wipe the input file up to its original length
    memset(big_buffer, 0, sizeof(big_buffer));
    while (filesize < wfilesize) {
        if (panic) {
            return false;
        }
        
        ms2 = ticks;
        if (DFS_WriteFile(&wfi, scratch, big_buffer, &actual, sizeof(big_buffer))) {
            return false;
        }
        write_ticks += ticks-ms2;
        
        assert(actual == sizeof(big_buffer));
        filesize += actual;
    }
    
    DFS_HostFlush(0);
    
    encrypt_ticks += ticks-ms;
    
#if ! _WIN32
    led_happy_progress();
#endif
    printf("  %d seconds\n", seconds-initial);
    
    return true;
}