static void *wrap_fs_opendir(struct _reent *r, char *path) { DirList *list = malloc(sizeof(DirList)); if (!list) { r->_errno = ENOMEM; return NULL; } if (ls_openDir(list, fs, path) != 0) { r->_errno = EACCES; return NULL; } return list; }
/** ******************************************************************************* Function Name : delete_file() Description : Input : Output : none Note : ******************************************************************************* */ U8 delete_file(U8 *name) { DirList list; U8 retval = FILE_DELETED_SUCCESS; ls_openDir(&list,&efs.myFs,"/"); if(rmfile(&efs.myFs,name) == -1) { retval = FILE_DELETED_NO_SUCH_FILE; } fs_umount(&efs.myFs); return retval; }
int cfs_opendir (struct cfs_dir *dirp, const char *name) { DirList *dirs; COMPILE_TIME_CHECK(sizeof(DirList*) <= sizeof(struct cfs_dir)); if (sdcard_efs.myCard.sectorCount == 0) return -1; dirs = find_free_dir_list(); if (!dirs) return -1; if (ls_openDir(dirs, &sdcard_efs.myFs, (eint8*)name) != 0) { dirs->fs = NULL; return -1; } *(DirList**)dirp = dirs; return 0; }
void ls_fnt(int argc, char** argv) { // MESSAGE("Directory of root\n\r"); if(argc != 2) { MESSAGE("usage: ls {path_name}\n\r"); return; } MESSAGE("listing directory %s:\n\r", argv[1]); ls_openDir( &list, &(efs.myFs) , argv[1]); while ( ls_getNext( &list ) == 0 ) { list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0'; MESSAGE( "%s ( %d bytes )\n\r", list.currentEntry.FileName,list.currentEntry.FileSize ) ; } return; }
/** * This function will set a hook function, which will be invoked when a memory * block is allocated from heap memory. * * @param hook the hook function */ int efs_lseek(struct dfs_fd* file, rt_off_t offset) { int result = 0; DirList* efsdir; efsl_fs* efsfs = (efsl_fs *)file->fs->data; /* parameter check */ RT_ASSERT(efsfs != RT_NULL); /* seek directory */ if(file->flags & DFS_O_DIRECTORY) { efsdir = (DirList*)file->data; /* only support offset equels zero */ if(offset == 0) { result = ls_openDir(efsdir, &efsfs->filesystem, file->path); if(result < 0) { dfs_log(DFS_DEBUG_INFO, ("open directory %s failed", file->path)); } } /* should implement in future version */ else { dfs_log(DFS_DEBUG_INFO, ("not implement")); } } /* seek file */ else { File* efsfile = file->data; /* parameter check */ if ( efsfile == RT_NULL) return -DFS_STATUS_EBADF; result = file_setpos(efsfile, offset); } return result; }
int main() { int8_t res; uint32_t n, m, p, cnt; uint32_t cclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU); uint32_t filesize = 0; uint32_t time_end; // SystemInit(); SysTick_Config(cclk/1000 - 1); /* Generate interrupt each 1 ms */ debug_frmwrk_init(); // UART0 xfunc_out = put_char; xfunc_in = get_char; xprintf("%s",mciFsMenu); xprintf("\nMMC/SD Card Filesystem Test (P:LPC1788 L:EFSL)\n"); xprintf("\nCARD init..."); // init file system if ( ( res = efs_init( &efs, 0 ) ) != 0 ) { xprintf("failed with %d\n",res); } else { xprintf("ok\n"); xprintf("Card type: "); switch (CardConfig.CardType) { case MCI_MMC_CARD: xprintf("MMC\n"); break; case MCI_SDSC_V1_CARD: xprintf("Version 1.x Standard Capacity SD card.\n"); break; case MCI_SDSC_V2_CARD: xprintf("Version 2.0 or later Standard Capacity SD card.\n"); break; case MCI_SDHC_SDXC_CARD: xprintf("Version 2.0 or later High/eXtended Capacity SD card.\n"); break; default: break; } xprintf("Sector size: %d bytes\n", CardConfig.SectorSize); xprintf("Sector count: %d\n", CardConfig.SectorCount); xprintf("Block size: %d sectors\n", CardConfig.BlockSize); xprintf("Card capacity: %d MByte\n\n", (((CardConfig.SectorCount >> 10) * CardConfig.SectorSize)) >> 10); xprintf("\nDirectory of 'root':\n"); /* list files in root directory */ ls_openDir( &list, &(efs.myFs) , "/"); while ( ls_getNext( &list ) == 0 ) { // list.currentEntry is the current file list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0'; xprintf("%s, 0x%x bytes\n", list.currentEntry.FileName, list.currentEntry.FileSize ) ; } #if READ_TEST_ENABLED!=0 /* Read test */ xprintf("\nFile read test:\n"); xprintf("Open file %s ...", FILE_NAME_R); xmemset(Buff,0,sizeof(Buff)); if (file_fopen( &filer, &efs.myFs , FILE_NAME_R , 'r' ) == 0 ) { xprintf(" OK. \nReading %lu bytes ...\n", FILE_RW_SIZE); n=FILE_RW_SIZE; m = 0; Timer = 0; xprintf("File's content:\n"); while (n) { if (n>=blen) {cnt = blen; n -= blen;} else {cnt = n; n = 0;} p = file_read( &filer, cnt, &Buff[m] ); xprintf("%s",&Buff[m]); m += p; if (p != cnt) break; } filesize = m; time_end = Timer; xprintf("\n%lu bytes read in %lu milisec.\n", m, time_end); file_fclose( &filer ); } else { xprintf (" Failed.\n"); } #endif #if WRITE_TEST_ENABLED!=0 /* Write test*/ xprintf("\nFile write test:\n"); xprintf("Open file %s ...", FILE_NAME_W); if (file_fopen( &filew, &efs.myFs , FILE_NAME_W , 'a' ) == 0 ) { xprintf(" OK. \nWriting %lu bytes ...\n", filesize); n=filesize; m = 0; Timer = 0; while (n) { if (n>=blen) { cnt = blen; n -= blen; } else { cnt = n; n = 0; } p = file_write( &filew, cnt, &Buff[m] ); m += p; if (p != cnt) break; } time_end = Timer; xprintf("%lu bytes written in %lu milisec.\n", m, time_end); file_fclose( &filew ); } else { xprintf (" Failed.\n"); } #endif #if READ_TEST_ENABLED!=0 /* Read test */ xprintf("\nFile read test:\n"); xprintf("Open file %s ...", FILE_NAME_W); xmemset(Buff,0,sizeof(Buff)); if (file_fopen( &filer, &efs.myFs , FILE_NAME_W , 'r' ) == 0 ) { xprintf(" OK. \nReading %lu bytes ...\n", FILE_RW_SIZE); n=FILE_RW_SIZE; m = 0; Timer = 0; xprintf("File's content:\n"); while (n) { if (n>=blen) {cnt = blen; n -= blen;} else {cnt = n; n = 0;} p = file_read( &filer, cnt, &Buff[m] ); xprintf("%s",&Buff[m]); m += p; if (p != cnt) break; } filesize = m; time_end = Timer; xprintf("\n%lu bytes read in %lumiliseconds.\n", m, time_end); file_fclose( &filer ); } else { xprintf (" Failed.\n"); } #endif /* close file system */ fs_umount( &efs.myFs ) ; } xprintf("\nEFSL test complete.\n"); while (1); }
/* for each new request (data in p->payload) from addr:port, * create a new port to serve the response, and start the response * process */ void process_tftp_request(struct pbuf *pkt_buf, struct ip_addr *addr, u16_t port) { tftp_opcode op = tftp_decode_op(pkt_buf->payload); char FileName[30]; struct udp_pcb *upcb; err_t err; u32_t IPaddress; u8_t iptxt[20]; volatile u8_t iptab[4]; IPaddress = addr->addr; printf("\n\rTFTP RRQ (read request) from: %d.%d.%d.%d\n\r", (u8_t)(IPaddress), (u8_t)(IPaddress >> 8),(u8_t)(IPaddress >> 16),(u8_t)(IPaddress >> 24)); /* read its IP address */ iptab[0] = (u8_t)(IPaddress >> 24); iptab[1] = (u8_t)(IPaddress >> 16); iptab[2] = (u8_t)(IPaddress >> 8); iptab[3] = (u8_t)(IPaddress); sprintf((char*)iptxt, "TFTP: %d.%d.%d.%d ", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_DisplayStringLine(Line7, iptxt); /* create new UDP PCB structure */ upcb = udp_new(); if (!upcb) { /* Error creating PCB. Out of Memory */ return; } /* bind to port 0 to receive next available free port */ /* NOTE: This is how TFTP works. There is a UDP PCB for the standard port * 69 which al transactions begin communication on, however, _all_ subsequent * transactions for a given "stream" occur on another port! */ err = udp_bind(upcb, IP_ADDR_ANY, 0); if (err != ERR_OK) { /* Unable to bind to port */ return; } switch (op) { case TFTP_RRQ: /* TFTP RRQ (read request) */ /* Read the name of the file asked by the client to be sent from the SD card */ tftp_extract_filename(FileName, pkt_buf->payload); printf("\n\rTFTP RRQ (read request)"); printf("\n\rONLY EFS filesystem(NTFS in WinXp) is support"); /* If Could not open filesystem */ if (efs_init(&efs1, 0) != 0) { printf("\n\rIf Could not open filesystem"); return; } printf("\n\rCould open filesystem\n\r"); /* If Could not open the selected directory */ if (ls_openDir(&list1, &(efs1.myFs), "/") != 0) { printf("\n\rIf Could not open the selected directory"); return; } /* Start the TFTP read mode*/ printf("\n\rStart the TFTP read mode...."); tftp_process_read(upcb, addr, port, FileName); break; case TFTP_WRQ: /* TFTP WRQ (write request) */ /* Read the name of the file asked by the client to received and writen in the SD card */ tftp_extract_filename(FileName, pkt_buf->payload); /* If Could not open filesystem */ if (efs_init(&efs2, 0) != 0) { return; } /* If Could not open the selected directory */ if (ls_openDir(&list2, &(efs2.myFs), "/") != 0) { return; } /* Start the TFTP write mode*/ tftp_process_write(upcb, addr, port, FileName); break; default: /* sEndTransfera generic access violation message */ tftp_send_error_message(upcb, addr, port, TFTP_ERR_ACCESS_VIOLATION); /* TFTP unknown request op */ /* no need to use tftp_cleanup_wr because no "tftp_connection_args" struct has been malloc'd */ udp_remove(upcb); break; } }
/** * This function will set a hook function, which will be invoked when a memory * block is allocated from heap memory. * * @param hook the hook function */ int efs_open(struct dfs_fd* file) { File* efsfile; DirList* efsdir; efsl_fs* efsfs = (efsl_fs*)(file->fs->data); int result = 0; /* parameter check */ if ( file == RT_NULL || file->fs == RT_NULL || file->fs->data == RT_NULL ) { dfs_log(DFS_DEBUG_INFO, ("Invalid argument")); return -DFS_STATUS_EINVAL; } /* open directory */ if(file->flags & DFS_O_DIRECTORY) { /* write directory is not supported */ if(file->flags & DFS_O_WRONLY || file->flags & DFS_O_RDWR) { dfs_log(DFS_DEBUG_INFO, ("write directory isn't supported")); return -DFS_STATUS_EISDIR; } /* create directory */ if(file->flags & DFS_O_CREAT) { dfs_log(DFS_DEBUG_INFO, ("create directory")); result = mk_dir(&efsfs->filesystem, file->path); if(result < 0) { dfs_log(DFS_DEBUG_INFO, ("directory %s has existed", file->path)); return -DFS_STATUS_EEXIST; } } efsdir = (DirList*)rt_malloc(sizeof(DirList)); if(efsdir == RT_NULL) { dfs_log(DFS_DEBUG_INFO, ("memory alloc failed")); return -DFS_STATUS_ENOMEM; } result = ls_openDir(efsdir, &efsfs->filesystem, file->path); if(result < 0) { dfs_log(DFS_DEBUG_INFO, ("open directory %s failed", file->path)); rt_free(efsdir); } else { file->data = efsdir; } } /* open file */ else { efsfile = (File *)rt_malloc(sizeof(File)); if (efsfile == RT_NULL) { dfs_log(DFS_DEBUG_INFO, ("memory alloc failed")); return -DFS_STATUS_ENOMEM; } result = file_fopen(efsfile, &efsfs->filesystem, file->path, file->flags); if(result < 0) { dfs_log(DFS_DEBUG_INFO, ("open file %s failed", file->path)); rt_free(efsfile); } else { file->pos = efsfile->FilePtr; file->size = efsfile->FileSize; file->data = efsfile; } } return result; }
int main() { int8_t res; uint32_t n, m, p, cnt; // SystemInit(); SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms */ LPC17xx_UART_Init(115200); // UART0 xfunc_out = LPC17xx_UART_PutChar; xfunc_in = LPC17xx_UART_GetChar; xprintf("\nMMC/SD Card Filesystem Test (P:LPC1768 L:EFSL)\n"); xprintf("\nCARD init..."); // init file system if ( ( res = efs_init( &efs, 0 ) ) != 0 ) { xprintf("failed with %i\n",res); } else { xprintf("ok\n"); xprintf("Card type: "); switch (CardType) { case CARDTYPE_MMC: xprintf("MMC\n"); break; case CARDTYPE_SDV1: xprintf("Version 1.x Standard Capacity SD card.\n"); break; case CARDTYPE_SDV2_SC: xprintf("Version 2.0 or later Standard Capacity SD card.\n"); break; case CARDTYPE_SDV2_HC: xprintf("Version 2.0 or later High/eXtended Capacity SD card.\n"); break; default: break; } xprintf("Sector size: %d bytes\n", CardConfig.sectorsize); xprintf("Sector count: %d\n", CardConfig.sectorcnt); xprintf("Block size: %d sectors\n", CardConfig.blocksize); xprintf("Card capacity: %d MByte\n\n", (((CardConfig.sectorcnt >> 10) * CardConfig.sectorsize)) >> 10); xprintf("\nDirectory of 'root':\n"); /* list files in root directory */ ls_openDir( &list, &(efs.myFs) , "/"); while ( ls_getNext( &list ) == 0 ) { // list.currentEntry is the current file list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0'; xprintf("%s, 0x%x bytes\n", list.currentEntry.FileName, list.currentEntry.FileSize ) ; } #if WRITE_TEST_ENABLED!=0 /* Write test*/ xprintf("\nFile write test:\n"); xprintf("Open file %s ...", FILE_NAME); if (file_fopen( &filew, &efs.myFs , FILE_NAME , 'a' ) == 0 ) { xprintf(" OK. \nWriting %lu bytes ...\n", FILE_RW_SIZE); xmemset(Buff, 0xAA, blen); n=FILE_RW_SIZE; m = 0; Timer = 0; while (n) { if (n>=blen) { cnt = blen; n -= blen; } else { cnt = n; n = 0; } p = file_write( &filew, cnt, Buff ); m += p; if (p != cnt) break; } xprintf("%lu bytes written with %lu kB/sec.\n", m, Timer ? (m / Timer) : 0); file_fclose( &filew ); } else { xprintf (" Failed.\n"); } #endif #if READ_TEST_ENABLED!=0 /* Read test */ xprintf("\nFile read test:\n"); xprintf("Open file %s ...", FILE_NAME); if (file_fopen( &filer, &efs.myFs , FILE_NAME , 'r' ) == 0 ) { xprintf(" OK. \nReading %lu bytes ...\n", FILE_RW_SIZE); n=FILE_RW_SIZE; m = 0; Timer = 0; while (n) { if (n>=blen) {cnt = blen; n -= blen;} else {cnt = n; n = 0;} p = file_read( &filer, cnt, Buff ); m += p; if (p != cnt) break; } xprintf("%lu bytes read with %lu kB/sec.\n", m, Timer ? (m / Timer) : 0); file_fclose( &filer ); } else { xprintf (" Failed.\n"); } #endif /* close file system */ fs_umount( &efs.myFs ) ; } xprintf("\nEFSL test complete.\n"); while (1); }
/* for each new request (data in p->payload) from addr:port, * create a new port to serve the response, and start the response * process */ void process_tftp_request(struct pbuf *pkt_buf, struct ip_addr *addr, u16_t port) { tftp_opcode op = tftp_decode_op(pkt_buf->payload); char FileName[30]; struct udp_pcb *upcb; err_t err; /* create new UDP PCB structure */ upcb = udp_new(); if (!upcb) { /* Error creating PCB. Out of Memory */ return; } /* bind to port 0 to receive next available free port */ /* NOTE: This is how TFTP works. There is a UDP PCB for the standard port * 69 which al transactions begin communication on, however, _all_ subsequent * transactions for a given "stream" occur on another port! */ err = udp_bind(upcb, IP_ADDR_ANY, 0); if (err != ERR_OK) { /* Unable to bind to port */ return; } switch (op) { case TFTP_RRQ: /* TFTP RRQ (read request) */ /* Read the name of the file asked by the client to be sent from the SD card */ tftp_extract_filename(FileName, pkt_buf->payload); /* If Could not open filesystem */ if (efs_init(&efs1, 0) != 0) { return; } /* If Could not open the selected directory */ if (ls_openDir(&list1, &(efs1.myFs), "/") != 0) { return; } /* Start the TFTP read mode*/ tftp_process_read(upcb, addr, port, FileName); break; case TFTP_WRQ: /* TFTP WRQ (write request) */ /* Read the name of the file asked by the client to received and writen in the SD card */ tftp_extract_filename(FileName, pkt_buf->payload); /* If Could not open filesystem */ if (efs_init(&efs2, 0) != 0) { return; } /* If Could not open the selected directory */ if (ls_openDir(&list2, &(efs2.myFs), "/") != 0) { return; } /* Start the TFTP write mode*/ tftp_process_write(upcb, addr, port, FileName); break; default: /* sEndTransfera generic access violation message */ tftp_send_error_message(upcb, addr, port, TFTP_ERR_ACCESS_VIOLATION); /* TFTP unknown request op */ /* no need to use tftp_cleanup_wr because no "tftp_connection_args" struct has been malloc'd */ udp_remove(upcb); break; } }
int main(void) { int ch; int8_t res; systemInit(); gpioInit(); uart1Init(UART_BAUD(BAUD), UART_8N1, UART_FIFO_8); // setup the UART uart1Puts("\r\nMMC/SD Card Filesystem Test (P:LPC2138 L:EFSL)\r\n"); uart1Puts("efsl LPC2000-port and this Demo-Application\r\n"); uart1Puts("done by Martin Thomas, Kaiserslautern, Germany\r\n"); /* init efsl debug-output */ lpc2000_debug_devopen(uart1Putch); ledToggle(); rprintf("CARD init..."); if ( ( res = efs_init( &efs, 0 ) ) != 0 ) { rprintf("failed with %i\n",res); } else { rprintf("ok\n"); rprintf("Directory of 'root':\n"); ls_openDir( &list, &(efs.myFs) , "/"); while ( ls_getNext( &list ) == 0 ) { list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0'; rprintf( "%s ( %li bytes )\n" , list.currentEntry.FileName, list.currentEntry.FileSize ) ; } if ( file_fopen( &filer, &efs.myFs , LogFileName , 'r' ) == 0 ) { rprintf("File %s open. Content:\n", LogFileName); while ( ( e = file_read( &filer, 512, buf ) ) != 0 ) { buf[e]='\0'; uart1Puts((char*)buf); } rprintf("\n"); file_fclose( &filer ); } if ( file_fopen( &filew, &efs.myFs , LogFileName , 'a' ) == 0 ) { rprintf("File %s open for append. Appending...", LogFileName); strcpy((char*)buf, "Martin hat's angehaengt\r\n"); if ( file_write( &filew, strlen((char*)buf), buf ) == strlen((char*)buf) ) { rprintf("ok\n"); } else { rprintf("fail\n"); } file_fclose( &filew ); } if ( file_fopen( &filer, &efs.myFs , LogFileName , 'r' ) == 0 ) { rprintf("File %s open. Content:\n", LogFileName); while ( ( e = file_read( &filer, 512, buf ) ) != 0 ) { buf[e]='\0'; uart1Puts((char*)buf); } rprintf("\n"); file_fclose( &filer ); } fs_umount( &efs.myFs ) ; } while(1) { if ((ch = uart1Getch()) >= 0) { uart1Puts("You pressed : "); uart1Putch(ch); uart1Puts("\r\n"); if ( ch == 'M' ) { rprintf("Creating FS\n"); mkfs_makevfat(&efs.myPart); } ledToggle(); } } return 0; /* never reached */ }