TEE_Result tee_rpmb_fs_rename(const char *old_name, const char *new_name) { TEE_Result res = TEE_ERROR_GENERIC; struct file_handle *fh_old = NULL; struct file_handle *fh_new = NULL; uint32_t old_len; uint32_t new_len; if (old_name == NULL || new_name == NULL) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } old_len = strlen(old_name); new_len = strlen(new_name); if ((old_len >= FILENAME_LENGTH - 1) || (new_len >= FILENAME_LENGTH - 1) || (new_len == 0)) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } fh_old = alloc_file_handle(old_name); if (fh_old == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } fh_new = alloc_file_handle(new_name); if (fh_new == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } res = read_fat(fh_old, NULL); if (res != TEE_SUCCESS) goto out; res = read_fat(fh_new, NULL); if (res == TEE_SUCCESS) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } memset(fh_old->fat_entry.filename, 0, FILENAME_LENGTH); memcpy(fh_old->fat_entry.filename, new_name, new_len); res = write_fat_entry(fh_old, false); out: free(fh_old); free(fh_new); return res; }
static int next_cluster(struct fat32_fs *fs, unsigned int current_cluster) { unsigned int offset, sector, ent_offset, n; kprintf("finding next for %d\n", current_cluster); offset = current_cluster * 4; sector = fs->bs->reserved_sector_count + (offset / FAT32_CLUSTER_SIZE); ent_offset = offset % FAT32_CLUSTER_SIZE; /* Re-read the table */ fs->table = read_fat(fs, sector); /* ignore the high 4 bits */ n = *(unsigned int *)&fs->table[ent_offset] & 0x0FFFFFFF; if (n == 0x00000000) { /* unused cluster */ return 0; } else if (n == 0x0FFFFFF7) { /* bad cluster */ return -2; } else if (n >= 0x0FFFFFF8) { /* end of cluster */ return -1; } else { /* pointer to next cluster */ return n; } }
TEE_Result tee_rpmb_fs_stat(const char *filename, struct tee_rpmb_fs_stat *stat) { TEE_Result res = TEE_ERROR_GENERIC; struct file_handle *fh = NULL; if (stat == NULL || filename == NULL) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } fh = alloc_file_handle(filename); if (fh == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } res = read_fat(fh, NULL); if (res != TEE_SUCCESS) goto out; stat->size = fh->fat_entry.data_size; stat->reserved = 0; out: free(fh); return res; }
TEE_Result tee_rpmb_fs_rm(const char *filename) { TEE_Result res = TEE_ERROR_GENERIC; struct file_handle *fh = NULL; if (filename == NULL || strlen(filename) >= FILENAME_LENGTH - 1) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } fh = alloc_file_handle(filename); if (fh == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } res = read_fat(fh, NULL); if (res != TEE_SUCCESS) goto out; /* Clear this file entry. */ memset(&fh->fat_entry, 0, sizeof(struct rpmb_fat_entry)); res = write_fat_entry(fh, false); out: free(fh); return res; }
void fs_debug() { super_block sb; if(mb.magic == FS_MAGIC) { sb = mb; } else { // disk not mounted disk_read(0, (char*)&sb); if(sb.magic != FS_MAGIC) { printf("disk not formatted\n"); return; } read_dir(); read_fat(sb.nfatblocks); } list_super_block(&sb); list_files(); }
int fs_mount() { if(mb.magic == FS_MAGIC){ printf("disk already mounted!\n"); return -1; } disk_read(0, (char*)&mb); if(mb.magic != FS_MAGIC) { printf("Invalid magic number\n"); return -1; } read_dir(); read_fat(mb.nfatblocks); return 0; }
struct fat32_fs *mount_fat32(struct drive *d) { struct fat_boot_sector *bs = kmalloc(512); if (!read_ata(d, 0, bs, 512)) { kprintf("Failed to read sector 0\n"); kfree(bs); return NULL; } /* Treat bootjmp as magic */ if (bs->bootjmp[0] != 0xEB && bs->bootjmp[0] != 0xE9) { kprintf("There is no FAT32 file system here\n"); kfree(bs); return NULL; } /* null-terminate all of the string fields */ bs->oem_name[7] = '\0'; bs->fat32.volume_label[10] = '\0'; bs->fat32.fat_type_label[5] = '\0'; /* Verify this is FAT32 */ if (strcmp((char *)bs->fat32.fat_type_label, "FAT32") != 0) { kprintf("There is a FAT file system here, but it's not FAT32: `%s`\n", bs->fat32.fat_type_label); return NULL; } /* Populate the file system struct */ struct fat32_fs *fs = kmalloc(sizeof(struct fat32_fs)); fs->d = d; fs->bs = bs; fs->first_data_sector = bs->reserved_sector_count + (bs->table_count * bs->table_size_16); fs->table = read_fat(fs, fs->first_data_sector); kprintf("Mounted FAT32 file system with label `%s`\n", bs->fat32.volume_label); return fs; }
int main(int argc, char** argv){ fat_object obj; internal_file* file; char* filename = (char*)malloc((strlen("/test.txt")+1)*sizeof(char)); strcpy(filename,"/test.txt"); create_fat("test.img",FAT32,20*1024*1024); read_fat(&obj,"test.img"); file = open_file_fat(&obj,filename); close_file_fat(&obj,file); free(filename); filename = (char*)malloc((strlen("/fat.h")+1)*sizeof(char)); strcpy(filename,"/fat.h"); copy_file_to_fat(&obj,"fat.h",filename); copy_file_from_fat(&obj,filename,"test.txt"); close_fat(&obj); return 0; }
int tee_rpmb_fs_read(const char *filename, uint8_t *buf, size_t size) { TEE_Result res = TEE_ERROR_GENERIC; struct file_handle *fh = NULL; int read_size = -1; if (filename == NULL || buf == NULL || strlen(filename) >= FILENAME_LENGTH - 1) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } fh = alloc_file_handle(filename); if (fh == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } res = read_fat(fh, NULL); if (res != TEE_SUCCESS) goto out; if (size < fh->fat_entry.data_size) { res = TEE_ERROR_SHORT_BUFFER; goto out; } res = tee_rpmb_read(DEV_ID, fh->fat_entry.start_address, buf, fh->fat_entry.data_size); out: if (res == TEE_SUCCESS) read_size = fh->fat_entry.data_size; free(fh); return read_size; }
NTSTATUS WINAPI VfatChkdsk(IN PUNICODE_STRING DriveRoot, IN BOOLEAN FixErrors, IN BOOLEAN Verbose, IN BOOLEAN CheckOnlyIfDirty, IN BOOLEAN ScanDrive, IN PFMIFSCALLBACK Callback) { #if 0 BOOLEAN verify; BOOLEAN salvage_files; #endif //ULONG free_clusters; //DOS_FS fs; /* Store callback pointer */ ChkdskCallback = Callback; FsCheckMemQueue = NULL; /* Set parameters */ FsCheckFlags = 0; if (Verbose) FsCheckFlags |= FSCHECK_VERBOSE; FsCheckTotalFiles = 0; #if 0 verify = TRUE; salvage_files = TRUE; /* Open filesystem */ fs_open(DriveRoot,FixErrors); if (CheckOnlyIfDirty && !fs_isdirty()) { /* No need to check FS */ return fs_close(FALSE); } read_boot(&fs); if (verify) VfatPrint("Starting check/repair pass.\n"); while (read_fat(&fs), scan_root(&fs)) qfree(&FsCheckMemQueue); if (ScanDrive) fix_bad(&fs); if (salvage_files) reclaim_file(&fs); else reclaim_free(&fs); free_clusters = update_free(&fs); file_unused(); qfree(&FsCheckMemQueue); if (verify) { VfatPrint("Starting verification pass.\n"); read_fat(&fs); scan_root(&fs); reclaim_free(&fs); qfree(&FsCheckMemQueue); } if (fs_changed()) { if (FixErrors) { if (FsCheckFlags & FSCHECK_INTERACTIVE) FixErrors = get_key("yn","Perform changes ? (y/n)") == 'y'; else VfatPrint("Performing changes.\n"); } else { VfatPrint("Leaving file system unchanged.\n"); } } VfatPrint("%wZ: %u files, %lu/%lu clusters\n", DriveRoot, FsCheckTotalFiles, fs.clusters - free_clusters, fs.clusters ); if (FixErrors) { /* Dismount the volume */ fs_dismount(); /* Unlock the volume */ fs_lock(FALSE); } /* Close the volume */ return fs_close(FixErrors) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL; #else return STATUS_SUCCESS; #endif }
int main(int argc, char **argv) { DOS_FS fs; int salvage_files, verify, c; uint32_t free_clusters = 0; memset(&fs, 0, sizeof(fs)); salvage_files = verify = 0; rw = interactive = 1; check_atari(); while ((c = getopt(argc, argv, "Aac:d:bflnprtu:vVwy")) != -1) switch (c) { case 'A': /* toggle Atari format */ atari_format = !atari_format; break; case 'a': case 'p': case 'y': rw = 1; interactive = 0; salvage_files = 1; break; case 'b': rw = 0; interactive = 0; boot_only = 1; break; case 'c': set_dos_codepage(atoi(optarg)); break; case 'd': file_add(optarg, fdt_drop); break; case 'f': salvage_files = 1; break; case 'l': list = 1; break; case 'n': rw = 0; interactive = 0; break; case 'r': rw = 1; interactive = 1; break; case 't': test = 1; break; case 'u': file_add(optarg, fdt_undelete); break; case 'v': verbose = 1; break; case 'V': verify = 1; break; case 'w': write_immed = 1; break; default: usage(argv[0]); } set_dos_codepage(-1); /* set default codepage if none was given in command line */ if ((test || write_immed) && !rw) { fprintf(stderr, "-t and -w can not be used in read only mode\n"); exit(2); } if (optind != argc - 1) usage(argv[0]); printf("fsck.fat " VERSION " (" VERSION_DATE ")\n"); fs_open(argv[optind], rw); read_boot(&fs); if (boot_only) goto exit; if (verify) printf("Starting check/repair pass.\n"); while (read_fat(&fs), scan_root(&fs)) qfree(&mem_queue); if (test) fix_bad(&fs); if (salvage_files) reclaim_file(&fs); else reclaim_free(&fs); free_clusters = update_free(&fs); file_unused(); qfree(&mem_queue); if (verify) { n_files = 0; printf("Starting verification pass.\n"); read_fat(&fs); scan_root(&fs); reclaim_free(&fs); qfree(&mem_queue); } exit: if (fs_changed()) { if (rw) { if (interactive) rw = get_key("yn", "Perform changes ? (y/n)") == 'y'; else printf("Performing changes.\n"); } else printf("Leaving filesystem unchanged.\n"); } if (!boot_only) printf("%s: %u files, %lu/%lu clusters\n", argv[optind], n_files, (unsigned long)fs.clusters - free_clusters, (unsigned long)fs.clusters); return fs_close(rw) ? 1 : 0; }
int tee_rpmb_fs_write(const char *filename, uint8_t *buf, size_t size) { TEE_Result res = TEE_ERROR_GENERIC; struct file_handle *fh = NULL; tee_mm_pool_t p; tee_mm_entry_t *mm = NULL; size_t length; uint32_t mm_flags; if (filename == NULL || buf == NULL) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } length = strlen(filename); if ((length >= FILENAME_LENGTH - 1) || (length == 0)) { res = TEE_ERROR_BAD_PARAMETERS; goto out; } /* Create a FAT entry for the file to write. */ fh = alloc_file_handle(filename); if (fh == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } /* Upper memory allocation must be used for RPMB_FS. */ mm_flags = TEE_MM_POOL_HI_ALLOC; if (!tee_mm_init (&p, RPMB_STORAGE_START_ADDRESS, RPMB_STORAGE_END_ADDRESS, RPMB_BLOCK_SIZE_SHIFT, mm_flags)) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } res = read_fat(fh, &p); if (res != TEE_SUCCESS) goto out; mm = tee_mm_alloc(&p, size); if (mm == NULL) { res = TEE_ERROR_OUT_OF_MEMORY; goto out; } if ((fh->fat_entry.flags & FILE_IS_LAST_ENTRY) != 0) { res = add_fat_entry(fh); if (res != TEE_SUCCESS) goto out; } memset(&fh->fat_entry, 0, sizeof(struct rpmb_fat_entry)); memcpy(fh->fat_entry.filename, filename, length); fh->fat_entry.data_size = size; fh->fat_entry.flags = FILE_IS_ACTIVE; fh->fat_entry.start_address = tee_mm_get_smem(mm); res = tee_rpmb_write(DEV_ID, fh->fat_entry.start_address, buf, size); if (res != TEE_SUCCESS) goto out; res = write_fat_entry(fh, true); out: free(fh); if (mm != NULL) tee_mm_final(&p); if (res == TEE_SUCCESS) return size; return -1; }
int main(int argc,char **argv) { DOS_FS fs; int rw,salvage_files,verify,c; unsigned n_files_check=0, n_files_verify=0; unsigned long free_clusters; rw = salvage_files = verify = 0; interactive = 1; check_atari(); while ((c = getopt(argc,argv,"Aad:flnprtu:vVwy")) != EOF) switch (c) { case 'A': /* toggle Atari format */ atari_format = !atari_format; break; case 'a': case 'p': case 'y': rw = 1; interactive = 0; salvage_files = 1; break; case 'd': file_add(optarg,fdt_drop); break; case 'f': salvage_files = 1; break; case 'l': list = 1; break; case 'n': rw = 0; interactive = 0; break; case 'r': rw = 1; interactive = 1; break; case 't': test = 1; break; case 'u': file_add(optarg,fdt_undelete); break; case 'v': verbose = 1; printf("dosfsck " VERSION " (" VERSION_DATE ")\n"); break; case 'V': verify = 1; break; case 'w': write_immed = 1; break; default: usage(argv[0]); } if ((test || write_immed) && !rw) { fprintf(stderr,"-t and -w require -a or -r\n"); exit(2); } if (optind != argc-1) usage(argv[0]); printf( "dosfsck " VERSION ", " VERSION_DATE ", FAT32, LFN\n" ); fs_open(argv[optind],rw); read_boot(&fs); if (verify) printf("Starting check/repair pass.\n"); while (read_fat(&fs), scan_root(&fs)) qfree(&mem_queue); if (test) fix_bad(&fs); if (salvage_files) reclaim_file(&fs); else reclaim_free(&fs); free_clusters = update_free(&fs); file_unused(); qfree(&mem_queue); n_files_check = n_files; if (verify) { n_files = 0; printf("Starting verification pass.\n"); read_fat(&fs); scan_root(&fs); reclaim_free(&fs); qfree(&mem_queue); n_files_verify = n_files; } if (fs_changed()) { if (rw) { if (interactive) rw = get_key("yn","Perform changes ? (y/n)") == 'y'; else printf("Performing changes.\n"); } else printf("Leaving file system unchanged.\n"); } printf( "%s: %u files, %lu/%lu clusters\n", argv[optind], n_files, fs.clusters - free_clusters, fs.clusters ); return fs_close(rw) ? 1 : 0; }
/*main*/ void HariMain(void){ fs_pid=get_pid(); while(identify_hd()==FALSE); while(identify_fs()==FALSE);//读取superBlock /*如果硬盘上不存在文件系统,则新建一个。注:约定tinyOS文件系统的superBlock前5字节为字符串“TINY”*/ if(strcmp(superBlock.sign,"TINY")==FALSE) { create_fs(); identify_fs();/*因为之前读取的可能无效,当文件系统不存在时*/ } read_hd_bmp (); read_fat(); while(1) { receive(&msg_recv,STATUS_RECV_ANY,0,fs_pid); u_int32 recv_type=msg_recv.type; u_int32 send_pid =msg_recv.send_pid; switch(recv_type) { case FILE_MSG_TYPE: { struct FILE_MSG *msg =&msg_recv.u.msg_file; u_int32 file_msg_type=msg->type ; int8*file_name =msg->file_name ; void*buf =msg->buf ; u_int32 buf_len =msg->buf_len ; u_int32 handle =msg->handle ; u_int32 result ; struct I_NODE*inode =msg->inode ; switch(file_msg_type) { case FILE_IDENTIFY: if(identify_file(handle,inode,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE ); break; case FILE_OPEN : handle=open_file(file_name,send_pid); awake(send_pid,handle); break; case FILE_READ : if((result=read_file(handle,buf_len,buf,send_pid))!=FALSE) awake(send_pid,result ); else awake(send_pid,FALSE ); break; case FILE_WRITE : if(write_file(handle,buf_len,buf,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE); break; case FILE_CREATE : if(create_file(file_name,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE); break; case FILE_DELETE : if(delete_file(handle,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE ); break; case POWER_OFF : write_back( ); awake(send_pid,TRUE ); break; default: awake(send_pid,FALSE ); break; } break; } default: break; } } }