/* 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); }
// function called by fwrite static int File_write(FILE *fp, unsigned char *buf, int count) { int retval; uint32_t successcount; PFILEINFO fileinfo = (PFILEINFO)fp->drvarg[0]; retval = DFS_WriteFile(fileinfo, dfs_scratch, buf, &successcount, count); if (retval != DFS_OK) { successcount = 0; errno = dfs_stdio_errno(retval); } return successcount; }
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; }
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; }
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 ); }