int main() { int32_t rc; uint32_t numBlks, blkSize; uint8_t inquiryResult[INQUIRY_LENGTH]; /* SystemClockUpdate() updates the SystemFrequency variable */ SystemClockUpdate(); UART_Init(57600); /* Initialize the serial port to view the log messages */ Host_Init(); /* Initialize the lpc17xx host controller */ rc = Host_EnumDev(); /* Enumerate the device connected */ if (rc == OK) { /* Initialize the mass storage and scsi interfaces */ rc = MS_Init( &blkSize, &numBlks, inquiryResult ); if (rc == OK) { rc = FAT_Init(); /* Initialize the FAT16 file system */ if (rc == OK) { Main_Copy(); /* Call the application */ } else { return (0); } } else { return (0); } } else { return (0); } while(1); }
void initfs() { // reset_floppy_controller(0); //init floppy printf("Initing hard disk...\n"); resetHd(0); printf("Hard disk inited.\nIniting Fat...\n"); FAT_Init(); //get fat infomation struct printf("Fat inited.\n"); }
int main() { USB_INT32S rc; USB_INT32U numBlks, blkSize; USB_INT08U inquiryResult[INQUIRY_LENGTH]; // Code Red - if CMSIS 1.3 setup is being used, then SystemInit() routine // will be called by startup code rather than in application's main() #ifndef __USE_CMSIS SystemInit(); /* initialize clocks */ #endif UART_Init(115200); /* Initialize the serial port to view the log messages */ Host_Init(); /* Initialize the lpc17xx host controller */ rc = Host_EnumDev(); /* Enumerate the device connected */ if (rc == OK) { /* Initialize the mass storage and scsi interfaces */ rc = MS_Init( &blkSize, &numBlks, inquiryResult ); if (rc == OK) { rc = FAT_Init(); /* Initialize the FAT16 file system */ if (rc == OK) { Main_Copy(); /* Call the application */ } else { while ( 1 ); return (0); } } else { while ( 1 ); return (0); } } else { while ( 1 ); return (0); } while(1); }
s32 handleFFSIoctl(ipcmessage *msg) { s32 ret = 0; u32 cmd = msg->ioctl.command; u32 length_io = msg->ioctl.length_io; u32 *buffer_io = msg->ioctl.buffer_io; u32 *buffer_in = msg->ioctl.buffer_in; switch(cmd) { case FFS_IOCTL_CREATEDIR: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; char dirname[MAX_FILENAME_SIZE]; preappend_nand_dev_name((const char *) buffer_in, dirname); ret = FAT_MakeDir(dirname); break; } case FFS_IOCTL_CREATEFILE: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; char filename[MAX_FILENAME_SIZE]; preappend_nand_dev_name((const char *) buffer_in, filename); ret = FAT_MakeFile(filename); break; } case FFS_IOCTL_DELETE: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; char filename[MAX_FILENAME_SIZE]; preappend_nand_dev_name((const char *) buffer_in, filename); ret = FAT_Delete(filename); break; } case FFS_IOCTL_RENAME: { u8 *names = (u8 *) buffer_in; if (emulationType == FFS_EMU_NONE) goto originalIoctl; char newname[MAX_FILENAME_SIZE]; char oldname[MAX_FILENAME_SIZE]; struct stat filestat; preappend_nand_dev_name((const char *)names, oldname); preappend_nand_dev_name((const char *) names+OFFSET_NEW_NAME, newname); // Check if newname exists if (FAT_Stat(newname, &filestat) >=0) { if (S_ISDIR(filestat.st_mode)) FAT_DeleteDir(newname); else FAT_Delete(newname); } ret = FAT_Rename(oldname, newname); break; } case FFS_IOCTL_GETSTATS: { char drive[MAX_FILENAME_SIZE]; struct statvfs vfsstat; if (emulationType == FFS_EMU_NONE) goto originalIoctl; preappend_nand_dev_name("/", drive); ret = FAT_VFSStats(drive, &vfsstat); if (ret >= 0) { fsstats *s = (fsstats *) buffer_io; Memset(buffer_io, 0, length_io); s->block_size = vfsstat.f_bsize; s->free_blocks = vfsstat.f_bfree; s->free_inodes = vfsstat.f_ffree; s->used_blocks = vfsstat.f_blocks - vfsstat.f_bfree; os_sync_after_write(buffer_io, length_io); } break; } case FFS_IOCTL_GETFILESTATS: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; if (msg->fd > FFS_FD_MAGIC) goto originalIoctl; struct fstats *s = (struct fstats *) buffer_io; ret = FAT_FileStats(msg->fd, s); break; } case FFS_IOCTL_GETATTR: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; char name[MAX_FILENAME_SIZE]; preappend_nand_dev_name((const char *)buffer_in, name); ret = FAT_Stat(name, NULL); // if it exists, return the same permissions always if (ret >= 0) { fsattr *attributes = (fsattr *) buffer_io; attributes->owner_id = 0; attributes->group_id = 0; attributes->attributes = 0; attributes->ownerperm = ISFS_OPEN_RW; attributes->groupperm = ISFS_OPEN_RW; attributes->otherperm = ISFS_OPEN_RW; os_sync_after_write(buffer_io, length_io); ret = 0; } break; } case FFS_IOCTL_SETATTR: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; char name[MAX_FILENAME_SIZE]; preappend_nand_dev_name((const char *)buffer_in, name); ret = FAT_Stat(name, NULL); // Ignore permission, success if the file exists break; } case FFS_IOCTL_FORMAT: { if (emulationType == FFS_EMU_NONE) goto originalIoctl; ret = 0; break; } case FFS_IOCTL_SETNANDEMULATION: { char tmpdir[MAX_FILENAME_SIZE]; u32 state = buffer_in[0]; if (state) { preappend_nand_dev_name("/tmp", tmpdir); if (FAT_Init() >= 0) { FAT_DeleteDir(tmpdir); } } emulationType = state; break; } originalIoctl: default: ret = FFS_HandleIoctl(msg); break; } return ret; }
s32 FS_Ioctl(ipcmessage *message, u32 *flag) { u32 *inbuf = message->ioctl.buffer_in; u32 *iobuf = message->ioctl.buffer_io; u32 iolen = message->ioctl.length_io; u32 cmd = message->ioctl.command; s32 ret; /* Set flag */ *flag = config.mode; /* Parse comamnd */ switch (cmd) { /** Create directory **/ case IOCTL_ISFS_CREATEDIR: { fsattr *attr = (fsattr *)inbuf; FS_printf("FS_CreateDir(): %s\n", attr->filepath); /* Check path */ ret = FS_CheckPath(attr->filepath); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char fatpath[FAT_MAXPATH]; /* Generate path */ FS_GeneratePath(attr->filepath, fatpath); /* Create directory */ return FAT_CreateDir(fatpath); } break; } /** Create file **/ case IOCTL_ISFS_CREATEFILE: { fsattr *attr = (fsattr *)inbuf; FS_printf("FS_CreateFile(): %s\n", attr->filepath); /* Check path */ ret = FS_CheckPath(attr->filepath); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char fatpath[FAT_MAXPATH]; /* Generate path */ FS_GeneratePath(attr->filepath, fatpath); /* Create file */ return FAT_CreateFile(fatpath); } break; } /** Delete object **/ case IOCTL_ISFS_DELETE: { char *filepath = (char *)inbuf; FS_printf("FS_Delete(): %s\n", filepath); /* Check path */ ret = FS_CheckPath(filepath); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char fatpath[FAT_MAXPATH]; /* Generate path */ FS_GeneratePath(filepath, fatpath); /* Delete */ return FAT_Delete(fatpath); } break; } /** Rename object **/ case IOCTL_ISFS_RENAME: { fsrename *rename = (fsrename *)inbuf; FS_printf("FS_Rename(): %s -> %s\n", rename->filepathOld, rename->filepathNew); /* Check paths */ ret = FS_CheckPath(rename->filepathOld); ret |= FS_CheckPath(rename->filepathNew); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char oldpath[FAT_MAXPATH]; char newpath[FAT_MAXPATH]; struct stats stats; /* Generate paths */ FS_GeneratePath(rename->filepathOld, oldpath); FS_GeneratePath(rename->filepathNew, newpath); /* Compare paths */ if (strcmp(oldpath, newpath)) { /* Check new path */ ret = FAT_GetStats(newpath, &stats); /* New path exists */ if (ret >= 0) { /* Delete directory */ if (stats.attrib & AM_DIR) FAT_DeleteDir(newpath); /* Delete */ FAT_Delete(newpath); } /* Rename */ return FAT_Rename(oldpath, newpath); } /* Check path exists */ return FAT_GetStats(oldpath, NULL); } break; } /** Get device stats **/ case IOCTL_ISFS_GETSTATS: { FS_printf("FS_GetStats():\n"); /* FAT mode */ if (config.mode) { fsstats *stats = (fsstats *)iobuf; /* Clear buffer */ memset(iobuf, 0, iolen); /* Fill stats */ stats->block_size = 0x4000; stats->free_blocks = 0x5DEC; stats->used_blocks = 0x1DD4; stats->unk3 = 0x10; stats->unk4 = 0x02F0; stats->free_inodes = 0x146B; stats->unk5 = 0x0394; /* Flush cache */ os_sync_after_write(iobuf, iolen); return 0; } break; } /** Get file stats **/ case IOCTL_ISFS_GETFILESTATS: { s32 fd = message->fd; FS_printf("FS_GetFileStats(): %d\n", fd); /* Disable flag */ *flag = 0; break; } /** Get attributes **/ case IOCTL_ISFS_GETATTR: { char *path = (char *)inbuf; FS_printf("FS_GetAttributes(): %s\n", path); /* Check path */ ret = FS_CheckPath(path); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { fsattr *attr = (fsattr *)iobuf; char fatpath[FAT_MAXPATH]; /* Generate path */ FS_GeneratePath(path, fatpath); /* Check path */ ret = FAT_GetStats(fatpath, NULL); if (ret < 0) return ret; /* Fake attributes */ attr->owner_id = FS_GetUID(); attr->group_id = FS_GetGID(); attr->ownerperm = ISFS_OPEN_RW; attr->groupperm = ISFS_OPEN_RW; attr->otherperm = ISFS_OPEN_RW; attr->attributes = 0; /* Copy filepath */ memcpy(attr->filepath, path, ISFS_MAXPATH); /* Flush cache */ os_sync_after_write(iobuf, iolen); return 0; } break; } /** Set attributes **/ case IOCTL_ISFS_SETATTR: { fsattr *attr = (fsattr *)inbuf; FS_printf("FS_SetAttributes(): %s\n", attr->filepath); /* Check path */ ret = FS_CheckPath(attr->filepath); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char fatpath[FAT_MAXPATH]; /* Generate path */ FS_GeneratePath(attr->filepath, fatpath); /* Check path */ return FAT_GetStats(fatpath, NULL); } break; } /** Format **/ case IOCTL_ISFS_FORMAT: { FS_printf("FS_Format():\n"); /* FAT mode */ if (config.mode) return 0; break; } /** Set FS mode **/ case IOCTL_ISFS_SETMODE: { u32 val = inbuf[0]; FS_printf("FS_SetMode(): %d\n", val); /* Set flag */ *flag = 1; /* Set path */ strcpy(config.path, ""); /* FAT mode enabled */ if (val) { char fatpath[FAT_MAXPATH]; /* Initialize FAT */ ret = FAT_Init(); if (ret < 0) return ret; /* Generate path */ FS_GeneratePath("/tmp", fatpath); /* Delete "/tmp" */ FAT_DeleteDir(fatpath); } /* Set FS mode */ config.mode = val; return 0; } default: break; } /* Call handler */ return -6; }
s32 FS_Ioctlv(ipcmessage *message, u32 *flag) { ioctlv *vector = message->ioctlv.vector; u32 inlen = message->ioctlv.num_in; u32 iolen = message->ioctlv.num_io; u32 cmd = message->ioctlv.command; s32 ret; /* Set flag */ *flag = config.mode; /* Parse comamnd */ switch (cmd) { /** Read directory **/ case IOCTL_ISFS_READDIR: { char *dirpath = (char *)vector[0].data; FS_printf("FS_Readdir(): %s\n", dirpath); /* Check path */ ret = FS_CheckPath(dirpath); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char *outbuf = NULL; u32 *outlen = NULL; u32 buflen = 0; char fatpath[FAT_MAXPATH]; u32 entries; /* Set pointers/values */ if (iolen > 1) { entries = *(u32 *)vector[1].data; outbuf = (char *)vector[2].data; outlen = (u32 *)vector[3].data; buflen = vector[2].len; } else outlen = (u32 *)vector[1].data; /* Generate path */ FS_GeneratePath(dirpath, fatpath); /* Read directory */ ret = FAT_ReadDir(fatpath, outbuf, &entries); if (ret >= 0) { *outlen = entries; os_sync_after_write(outlen, sizeof(u32)); } /* Flush cache */ if (outbuf) os_sync_after_write(outbuf, buflen); return ret; } break; } /** Get device usage **/ case IOCTL_ISFS_GETUSAGE: { char *dirpath = (char *)vector[0].data; FS_printf("FS_GetUsage(): %s\n", dirpath); /* Check path */ ret = FS_CheckPath(dirpath); if (ret) { *flag = 0; break; } /* FAT mode */ if (config.mode) { char fatpath[FAT_MAXPATH]; u32 *blocks = (u32 *)vector[1].data; u32 *inodes = (u32 *)vector[2].data; /* Generate path */ FS_GeneratePath(dirpath, fatpath); /* Get usage */ ret = FAT_GetUsage(fatpath, blocks, inodes); /* Flush cache */ os_sync_after_write(blocks, sizeof(u32)); os_sync_after_write(inodes, sizeof(u32)); return ret; } break; } /** Set FS mode **/ case IOCTL_ISFS_SETMODE: { u32 val = *(u32 *)vector[0].data; char *path = ""; /* Get path */ if (inlen > 1) path = (char *)vector[1].data; /* Set flag */ *flag = 1; /* Set path */ strcpy(config.path, path); /* FAT mode enabled */ if (val) { char fatpath[FAT_MAXPATH]; /* Initialize FAT */ ret = FAT_Init(); if (ret < 0) return ret; /* Generate path */ FS_GeneratePath("/tmp", fatpath); /* Delete "/tmp" */ FAT_DeleteDir(fatpath); } /* Set FS mode */ config.mode = val; return 0; } default: break; } /* Call handler */ return -6; }
int main(void) { UART2_Init(); // Initialize USART2 (for printf) TIMER_Init(SYSTICK_FREQ); // Initialize timer int8_t timerID = TIMER_AddSoftTimer(1000,softTimerCallback); TIMER_StartSoftTimer(timerID); LED_TypeDef led; led.nr = LED0; led.gpio = GPIOD; led.pin = 12; led.clk = RCC_AHB1Periph_GPIOD; LED_Add(&led); // Add an LED printf("Starting program\r\n"); // Print a string to UART2 // SD_Init(); // // uint8_t buf[1024]; // // SD_ReadSectors(buf, 0, 1); // // TIMER_Delay(1000); // hexdump(buf, 512); // // printf("After hexdump\r\n"); FAT_Init(SD_Init, SD_ReadSectors, SD_WriteSectors); // FATFS FatFs; // FIL file; // FRESULT result; // // char buf[256]; // // printf("Mounting volume\r\n"); // result = f_mount(&FatFs, "", 1); // Mount SD card // // if (result) { // printf("Error mounting volume!\r\n"); // while(1); // } // // printf("Opening file: \"hello.txt\"\r\n"); // result = f_open(&file, "hello.txt", FA_READ); // // if (result) { // printf("Error opening file!\r\n"); // while(1); // } // // f_gets((char*)buf, 256, &file); // // printf("The file contains the following text:\r\n\"%s\"\r\n", buf); // // f_close(&file); // Close file // f_mount(NULL, "", 1); // Unmount SD Card while (1){ TIMER_SoftTimersUpdate(); } }