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 );
}
Exemplo n.º 2
0
/* fatfsPut():
 * Copy data or file to a FATFS file.
 *
 * Input:
 * char *fatfspath
 *	Pointer to FATFS file and path.
 * char *src
 *	Pointer to TFS filename or a addr,len string.
 *	The expected "addr,len" syntax is 0xADDR,LEN or 0xADDR,0xLEN.
 */
static int
fatfsPut(char *src, char *fatfspath)
{
	FILEINFO fi;
	char tfs, *p, *cp;
	uint32_t successcount, len, size, rc;

	if (strncmp(src,"0x",2) == 0) {
		tfs = 0;
		p = (char *)strtoul(src,&cp,0);
		cp++;
		len = strtoul(cp,0,0);
	}
	else {
#if INCLUDE_TFS
		TFILE *tfp;

		tfs = 1;
		if ((tfp = tfsstat(src)) == 0) {
			printf("Can't find file '%s' in TFS\n",src);
			return(CMD_FAILURE);
		}
		p = TFS_BASE(tfp);
		len = TFS_SIZE(tfp);
#else
		printf("TFS not built in\n");
		return(-1);
#endif
	}

	/* Copy 'len' bytes from 'p' to DOSFS file 'fatfspath'...
	 */
	if (DFS_OpenFile(&vi, (uint8_t *)fatfspath, DFS_WRITE, sector, &fi)) {
		printf("error opening '%s'\n",fatfspath);
		return(-1);
	}

	while(len > 0) {
		if (len >= SECTOR_SIZE)
			size = SECTOR_SIZE;
		else
			size = len;

		rc = DFS_WriteFile(&fi, sector2, (uint8_t *)p, &successcount, size);
		if ((rc != DFS_OK) || (successcount != size)) {
			printf("error writing '%s'\n",fatfspath);
			return(-1);
		}
		p += size;
		len -= size;
	}
	return(0);
}
Exemplo n.º 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);
}
Exemplo n.º 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);
}
Exemplo n.º 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;
}
Exemplo n.º 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;
}
Exemplo n.º 7
0
// function called by fopen
static int File_fopen(FILE *fp, const char *fname1, const char *mode)
{
    int retval;
    PFILEINFO fileinfo;
    char fname[MAX_PATH];
    uint8_t dfs_mode;

    if (!dfs_mountflag && dfs_mount_defaults() != DFS_OK)
    {
        errno = EIO;
        return -1;
    }

    dfs_resolve_path((char *)fname1, fname);

    fileinfo = malloc(sizeof(FILEINFO) + FILE_BUFFER_SIZE);

    if (!fileinfo)
    {
        errno = ENOMEM;
        return -1;
    }

    if (mode[1] == '+')
        dfs_mode = DFS_READ | DFS_WRITE;
    else if (mode[0] == 'r')
        dfs_mode = DFS_READ;
    else
        dfs_mode = DFS_WRITE;

    if (mode[0] == 'w')
    {
        retval = DFS_OpenFile(&dfs_volinfo, (uint8_t *)fname, DFS_READ, dfs_scratch, fileinfo);
        if (retval == DFS_OK)
            DFS_UnlinkFile(&dfs_volinfo, (uint8_t *)fname, dfs_scratch);
        retval = DFS_OpenFile(&dfs_volinfo, (uint8_t *)fname, dfs_mode, dfs_scratch, fileinfo);
    }
    else if (mode[0] == 'a')
    {
        retval = DFS_OpenFile(&dfs_volinfo, (uint8_t *)fname, dfs_mode, dfs_scratch, fileinfo);
    }
    else if (mode[0] == 'r')
    {
        retval = DFS_OpenFile(&dfs_volinfo, (uint8_t *)fname, DFS_READ, dfs_scratch, fileinfo);
        if (retval == DFS_OK && (dfs_mode & DFS_WRITE))
            retval = DFS_OpenFile(&dfs_volinfo, (uint8_t *)fname, dfs_mode, dfs_scratch, fileinfo);
    }
    else
    {
        errno = EINVAL;
        free(fileinfo);
        return -1;
    }

    if (retval != DFS_OK)
    {
        errno = dfs_stdio_errno(retval);
        free(fileinfo);
        return -1;
    }

    fp->drvarg[0] = (int)fileinfo;

    // Set up file buffer in the FILE struct
    fp->_ptr = fp->_base = ((void *)fileinfo) + sizeof(FILEINFO);
    fp->_bsiz = FILE_BUFFER_SIZE;
    fp->_flag |= _IOFREEBUF;

    return 0;
}
void telnet_touch( file_handle_t handle, char** argv, unsigned int argc )
{
	if( argc < 2 )
	{
		file_puts("touch [filename] [data to write] [...]\r\n", telnet_handle);
		return;	
	}
	u8_t* scratch = malloc(SECTOR_SIZE);
	VOLINFO* volinfo = malloc( sizeof(VOLINFO) );
	FILEINFO* file = malloc( sizeof(FILEINFO) );

	//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
	uint32_t result =   DFS_OpenFile( volinfo, (u8_t*)argv[1], DFS_WRITE, scratch, file );
	
	if(!( result == DFS_OK || result == DFS_EOF)) 
	{
		file_puts("Error opening file ", handle);
		file_puts( argv[1], handle );
		file_puts(". RC=", handle );
		file_putchar( (char)result + '0', handle);
		file_puts( CRLF, handle );
		goto exit;
	}
	uint16_t i;
	for( i = 2; i < argc; i++ )
	{
		uint32_t did_write;
		uint16_t len = strlen(argv[i]);
		DFS_WriteFile( file, scratch, argv[i], &did_write, len );
		if( did_write != len )
		{
			file_puts("Error writing to file ", handle);
			file_puts( argv[1], handle );
			file_puts( CRLF, handle );
			break;			
		}
		if( i < (argc-1) )
		{
			DFS_WriteFile( file, scratch, " ", &did_write, 1 );
			if( did_write != 1 )
			{
				file_puts("Error writing to file ", handle);
				file_puts( argv[1], handle );
				file_puts( CRLF, handle );
				break;			
			}
		}
	} 
exit:
	free( file );
	free( volinfo );
	free( scratch );
}