Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	struct block_device *bldev;
	FatVol vol;
	FatFile file;
	char *rootpath = argc > 2 ? argv[2] : "/";

	bldev = block_device_file_new(argc > 1 ? argv[1] : "fat32.img", "r+");
	assert(bldev != NULL);

	assert(fat_vol_init(bldev, &vol) == 0);
	fprintf(stderr, "Fat type is FAT%d\n", vol.type);

	fat_mkdir(&vol, "Directory1");
	fat_mkdir(&vol, "Directory2");
	fat_mkdir(&vol, "Directory3");
	assert(fat_chdir(&vol, "Directory1") == 0);
	fat_mkdir(&vol, "Directory1");
	fat_mkdir(&vol, "Directory2");
	fat_mkdir(&vol, "Directory3");
	if(fat_create(&vol, "Message file with a long name.txt", O_WRONLY, &file) == 0) {
		for(int i = 0; i < 100; i++) {
			char message[80];
			sprintf(message, "Here is a message %d\n", i);
			assert(fat_write(&file, message, strlen(message)) == (int)strlen(message));
		}
	}
	assert(fat_chdir(&vol, "..") == 0);
	assert(fat_open(&vol, ".", O_RDONLY, &file) == 0);
	print_tree(&vol, &file, rootpath[0] == '/' ? rootpath + 1 : rootpath);

	block_device_file_destroy(bldev);
}
Ejemplo n.º 2
0
int main(void)
{
	struct mmc_port spi2;
	struct block_mbr_partition part;
	struct fat_vol_handle vol;
	struct fat_file_handle file;

	stm32_setup();

	mmc_init(SPI2, GPIOA, GPIO3, &spi2);
	mbr_partition_init(&part, (struct block_device *)&spi2, 0);

	assert(fat_vol_init((struct block_device *)&part, &vol) == 0);
	printf("Fat type is FAT%d\n", vol.type);

	time_counter = 0;
	char dirname[20];
	char filename[20];
	char buffer[2000];
	for(int i = 0; i < 100; i++) {
		sprintf(dirname, "Dir%d", i);
		fat_mkdir(&vol, dirname);
		assert(fat_chdir(&vol, dirname) == 0);
		for(int j = 0; j < 100; j++) {
			sprintf(filename, "File%d", j);
			assert(fat_create(&vol, filename, O_WRONLY, &file) == 0);
			assert(fat_write(&file, buffer, sizeof(buffer)) == sizeof(buffer));
		}
		assert(fat_chdir(&vol, "..") == 0);
	}
	asm("bkpt");

	assert(fat_open(&vol, ".", 0, &file) == 0);
	print_tree(&vol, &file, 0);

	while (1) {
	}

	return 0;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[]) {
    int p = 0;
    int rerrno = 0;
    int result;
    int parts;
    uint8_t temp[512];
    struct partition *part_list;

    if(argc < 2) {
        printf("Please specify a disk image to work on.\n");
        exit(-2);
    }

    block_pc_set_image_name(argv[1]);
//   int v;
    printf("Running FAT tests...\n\n");
    printf("[%4d] start block device emulation...", p++);
    printf("   %d\n", block_init());

    printf("[%4d] mount filesystem, FAT32", p++);

    result = fat_mount(0, block_get_volume_size(), PART_TYPE_FAT32);

    printf("   %d\n", result);

    if(result != 0) {
        // mounting failed.
        // try listing the partitions
        block_read(0, temp);
        parts = read_partition_table(temp, block_get_volume_size(), &part_list);

        printf("Found %d valid partitions.\n", parts);

        if(parts > 0) {
            result = fat_mount(part_list[0].start, part_list[0].length, part_list[0].type);
        }
        if(result != 0) {
            printf("Mount failed\n");
            exit(-2);
        }

    }

    printf("Part type = %02X\n", fatfs.type);
    //   p = test_open(p);

    int fd;
    int i;
    char block_o_data[1024];
    uint32_t temp_uint = 0xDEADBEEF;
    memset(block_o_data, 0x42, 1024);
//   printf("Open\n");
//   fd = fat_open("/newfile.txt", O_WRONLY | O_CREAT, 0777, &rerrno);
//   printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno));
//   if(fd > -1) {
//     printf("Write\n");
//     fat_write(fd, "Hello World\n", 12, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//     printf("Close\n");
//     fat_close(fd, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//   }

//   printf("Open\n");
//   fd = fat_open("/newfile.png", O_WRONLY | O_CREAT, 0777, &rerrno);
//   printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno));
//   if(fd > -1) {
//     fp = fopen("gowrong_draft1.png", "rb");
//     fseek(fp, 0, SEEK_END);
//     len = ftell(fp);
//     d = malloc(len);
//     fseek(fp, 0, SEEK_SET);
//     fread(d, 1, len, fp);
//     fclose(fp);
//     printf("Write PNG\n");
//     fat_write(fd, d, len, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//     printf("Close\n");
//     fat_close(fd, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//   }

    printf("errno = (%d) %s\n", rerrno, strerror(rerrno));
    result = fat_mkdir("/foo", 0777, &rerrno);
    printf("mkdir /foo: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    result = fat_mkdir("/foo/bar", 0777, &rerrno);
    printf("mkdir /foo/bar: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    result = fat_mkdir("/web", 0777, &rerrno);
    printf("mkdir /web: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    if((fd = fat_open("/foo/bar/file.html", O_WRONLY | O_CREAT, 0777, &rerrno)) == -1) {
        printf("Couldn't open file (%d) %s\n", rerrno, strerror(rerrno));
        exit(-1);
    }

    for(i=0; i<20; i++) {
//     printf("fd.cluster = %d\n", file_num[fd].full_first_cluster);
        if(fat_write(fd, block_o_data, 1024, &rerrno) == -1) {
            printf("Error writing to new file (%d) %s\n", rerrno, strerror(rerrno));
        }
    }

    if(fat_close(fd, &rerrno)) {
        printf("Error closing file (%d) %s\n", rerrno, strerror(rerrno));
    }

    printf("Open directory\n");
    if((fd = fat_open("/foo/bar", O_RDONLY, 0777, &rerrno)) < 0) {
        printf("Failed to open directory (%d) %s\n", rerrno, strerror(rerrno));
        exit(-1);
    }
    struct dirent de;

    while(!fat_get_next_dirent(fd, &de, &rerrno)) {
        printf("%s\n", de.d_name);
    }
    printf("Directory read failed. (%d) %s\n", rerrno, strerror(rerrno));

    if(fat_close(fd, &rerrno)) {
        printf("Error closing directory, (%d) %s\n", rerrno, strerror(rerrno));
    }

    if(fat_open("/web/version.txt", O_RDONLY, 0777, &rerrno) < 0) {
        printf("Error opening missing file (%d) %s\n", rerrno, strerror(rerrno));
    } else {
        printf("success! opened non existent file for reading.\n");
    }

    printf("Trying to write a big file.\n");

    if((fd = fat_open("big_file.bin", O_WRONLY | O_CREAT, 0777, &rerrno))) {
        printf("Error opening a file for writing.\n");
    }

    for(i=0; i<1024 * 1024 * 10; i++) {
        if((i & 0xfff) == 0)
            printf("Written %d bytes\n", i * 4);
        fat_write(fd, &temp_uint, 4, &rerrno);
    }
    fat_close(fd, &rerrno);

//   result = fat_rmdir("/foo/bar", &rerrno);
//   printf("rmdir /foo/bar: %d (%d) %s\n", result, rerrno, strerror(rerrno));
//
//   result = fat_rmdir("/foo", &rerrno);
//   printf("rmdir /foo: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    block_pc_snapshot_all("writenfs.img");
    exit(0);
}
Ejemplo n.º 4
0
int fat_request(DWORD Function, void *Params)
{
  int Res;
  switch (Function)
  {
    case FD32_READ:
    {
      fd32_read_t *X = (fd32_read_t *) Params;
      tFile       *F;
      if (X->Size < sizeof(fd32_read_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_read(F, X->Buffer, X->BufferBytes);
    }
    case FD32_WRITE:
    {
      #ifdef FATWRITE
      fd32_write_t *X = (fd32_write_t *) Params;
      tFile        *F;
      if (X->Size < sizeof(fd32_write_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_write(F, X->Buffer, X->BufferBytes);
      #else
      return EROFS;
      #endif
    }
    case FD32_LSEEK:
    {
      fd32_lseek_t *X = (fd32_lseek_t *) Params;
      if (X->Size < sizeof(fd32_lseek_t)) return EINVAL;
      if (((tFile *) X->DeviceId)->FilSig != FAT_FILSIG) return EBADF;
      return fat_lseek((tFile *) X->DeviceId, &X->Offset, X->Origin);
    }
    case FD32_OPENFILE:
    {
      fd32_openfile_t *X = (fd32_openfile_t *) Params;
      tVolume         *V;
      if (X->Size < sizeof(fd32_openfile_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_open(V, X->FileName, X->Mode, X->Attr, X->AliasHint, (tFile **) &X->FileId);
    }
    case FD32_CLOSE:
    {
      fd32_close_t *X = (fd32_close_t *) Params;
      tFile        *F;
      if (X->Size < sizeof(fd32_close_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_close(F);
    }
    case FD32_READDIR:
    {
      fd32_readdir_t *X = (fd32_readdir_t *) Params;
      tFile          *F;
      if (X->Size < sizeof(fd32_readdir_t)) return EINVAL;
      F = (tFile *) X->DirId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_readdir(F, (fd32_fs_lfnfind_t *) X->Entry);
    }
    case FD32_FFLUSH:
    {
      #ifdef FATWRITE
      fd32_fflush_t *X = (fd32_fflush_t *) Params;
      tFile         *F;
      if (X->Size < sizeof(fd32_fflush_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_fflush(F);
      #else
      return EROFS;
      #endif
    }
    case FD32_OPEN:
    {
      fd32_open_t *X = (fd32_open_t *) Params;
      if (X->Size < sizeof(fd32_open_t)) return EINVAL;
      if (((tFile *) X->DeviceId)->FilSig != FAT_FILSIG) return EBADF;
      return ++((tFile *) X->DeviceId)->References;
    }
    case FD32_GETATTR:
    {
      fd32_getattr_t *X = (fd32_getattr_t *) Params;
      if (X->Size < sizeof(fd32_getattr_t)) return EINVAL;
      if (((tFile *) X->FileId)->FilSig != FAT_FILSIG) return EBADF;
      return fat_get_attr((tFile *) X->FileId, (fd32_fs_attr_t *) X->Attr);
    }
    case FD32_SETATTR:
    {
      #ifdef FATWRITE
      fd32_setattr_t *X = (fd32_setattr_t *) Params;
      tFile          *F;
      if (X->Size < sizeof(fd32_setattr_t)) return EINVAL;
      F = (tFile *) X->FileId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_set_attr(F, (fd32_fs_attr_t *) X->Attr);
      #else
      return EROFS;
      #endif
    }
    case FD32_REOPENDIR:
    {
      fd32_reopendir_t *X = (fd32_reopendir_t *) Params;
      tVolume          *V;
      if (X->Size < sizeof(fd32_reopendir_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_reopendir(V, (tFindRes *) X->DtaReserved, (tFile **) &X->DirId);
    }
    case FD32_UNLINK:
    {
      #ifdef FATWRITE
      fd32_unlink_t *X = (fd32_unlink_t *) Params;
      tVolume       *V;
      if (X->Size < sizeof(fd32_unlink_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_unlink(V, X->FileName, X->Flags);
      #else
      return EROFS;
      #endif
    }
    case FD32_RENAME:
    {
      #ifdef FATWRITE
      fd32_rename_t *X = (fd32_rename_t *) Params;
      tVolume       *V;
      if (X->Size < sizeof(fd32_rename_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_rename(V, X->OldName, X->NewName);
      #else
      return EROFS;
      #endif
    }
    case FD32_MKDIR:
    {
      #ifdef FATWRITE
      fd32_mkdir_t *X = (fd32_mkdir_t *) Params;
      tVolume      *V;
      if (X->Size < sizeof(fd32_mkdir_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_mkdir(V, X->DirName);
      #else
      return FD32_EROFS;
      #endif
    }
    case FD32_RMDIR:
    {
      #ifdef FATWRITE
      fd32_rmdir_t *X = (fd32_rmdir_t *) Params;
      tVolume      *V;
      if (X->Size < sizeof(fd32_rmdir_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_rmdir(V, X->DirName);
      #else
      return FD32_EROFS;
      #endif
    }
    case FD32_MOUNT:
    {
      fd32_mount_t *X = (fd32_mount_t *) Params;
      if (X->Size < sizeof(fd32_mount_t)) return EINVAL;
      return fat_mount(X->hDev, (tVolume **) &X->FsDev);
    }
    case FD32_UNMOUNT:
    {
      fd32_unmount_t *X = (fd32_unmount_t *) Params;
      tVolume        *V;
      if (X->Size < sizeof(fd32_unmount_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_unmount(V);
    }
    case FD32_PARTCHECK:
    {
      fd32_partcheck_t *X = (fd32_partcheck_t *) Params;
      if (X->Size < sizeof(fd32_partcheck_t)) return EINVAL;
      return fat_partcheck(X->PartId);
    }
    case FD32_GETFSINFO:
    {
      fd32_getfsinfo_t *X = (fd32_getfsinfo_t *) Params;
      if (X->Size < sizeof(fd32_getfsinfo_t)) return EINVAL;
      return fat_get_fsinfo((fd32_fs_info_t *) X->FSInfo);
    }
    case FD32_GETFSFREE:
      return fat_get_fsfree((fd32_getfsfree_t *) Params);
  }
  return EINVAL;
}