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 ); }
/* fatfsInit(): * Start up the fs by reading in volume information. * This function will return if already initialized, unless force * is nonzero. */ static int fatfsInit(int force, int verbose) { uint8_t pactive, ptype; uint32_t pstart, psize; if ((fatfs_initialized == 1) && (force == 0)) return(0); /* 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); } if (verbose) { printf("Partition 0...\n"); printf(" Start sector 0x%08lx\n",pstart); printf(" Active %d\n",pactive); printf(" Type 0x%x\n",ptype); printf(" Size 0x%lx\n",psize); } if (DFS_GetVolInfo(0, sector, pstart, &vi)) { printf("Error getting volume information\n"); return(-1); } if (verbose) { printf("Volume label '%-11s'\n", vi.label); printf(" %d sector(s) per cluster\n",vi.secperclus); printf(" %d reserved sector(s)\n",vi.reservedsecs); printf(" %d total volume sector(s)\n", vi.numsecs); printf(" %d sectors per FAT\n",vi.secperfat); printf(" first FAT at sector #%d\n",vi.fat1); printf(" root dir at #%d\n",vi.rootdir); printf("For.. FAT32 rootdir is CLUSTER #,\n"); printf(" FAT12/16 rootdir is a SECTOR #.\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"); } fatfs_initialized = 1; return(0); }
void telnet_rm( file_handle_t handle, char** argv, unsigned int argc ) { if( argc != 2 ) { file_puts("rm [filename]\r\n", telnet_handle); return; } u8_t* scratch = malloc(SECTOR_SIZE); VOLINFO* volinfo = malloc( sizeof(VOLINFO) ); //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; } //unlink file uint32_t result = DFS_UnlinkFile( volinfo, (u8_t*)argv[1], scratch ); if( result != DFS_OK ) { file_puts("Error unlinking file ", handle); file_puts( argv[1], handle ); file_puts(". RC=", handle ); file_putchar( (char)result + '0', handle); file_puts( CRLF, handle ); } exit: free( volinfo ); free( scratch ); }
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; }
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(¶ms); 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; }
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 ); } }
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 ); }