block_off_t parity_allocated_size(struct snapraid_state* state) { block_off_t parity_block; tommy_node* i; /* compute the size of the parity file */ parity_block = 0; for (i = state->disklist; i != 0; i = i->next) { struct snapraid_disk* disk = i->data; /* start from the declared size */ block_off_t block = disk_size(disk); /* decrease the block until an allocated one, but part of a file */ /* we don't stop at deleted blocks, because we want to have them cleared */ /* if they are at the end of the parity */ while (block > parity_block && !block_has_file(fs_par2block_get(disk, block - 1))) --block; /* get the highest value */ if (block > parity_block) parity_block = block; } return parity_block; }
bool USBMSD::connect() { //disk initialization if (disk_status() & NO_INIT) { if (disk_initialize()) { return false; } } // get number of blocks BlockCount = disk_sectors(); // get memory size MemorySize = disk_size(); if (BlockCount > 0) { BlockSize = MemorySize / BlockCount; if (BlockSize != 0) { page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t)); if (page == NULL) return false; } } else { return false; } //connect the device USBDevice::connect(); return true; }
int main(int argc, char **argv) { int i,disk; struct DEVICEPARAMS dp; if (argc == 2) disk = atoi(argv[1]); else disk = get_drive(); printf("\nDrive %c:\n",disk+'A'); if ((i = disk_getparams(disk,&dp)) != DISK_OK) { lib_error("get",i); } else { #ifdef _WIN32 #if defined __WATCOMC__ && __WATCOMC__ >= 1100 if (dp.total_sectors > MAX_FAT16_SECS) printf("drive size %Lu\n",disk_size32(&dp)); /* Microsoft %? */ else #endif #endif printf("drive size %lu\n",disk_size(&dp)); display(&dp); } return 0; }
void swap_init(void) { swap_disk = disk_get(1,1); swap_bitmap = bitmap_create(disk_size(swap_disk)/SECTOR_PER_PAGE); bitmap_set_all(swap_bitmap, 0); }
void swap_init (void) { swap_disk = disk_get (1, 1); swap_slot = bitmap_create ((size_t)disk_size (swap_disk)); lock_init (&swap_lock); lock_init (&swap_bitmap_lock); }
/* Initializes the free map. */ void free_map_init (void) { free_map = bitmap_create (disk_size (filesys_disk)); if (free_map == NULL) PANIC ("bitmap creation failed--disk is too large"); bitmap_mark (free_map, FREE_MAP_SECTOR); bitmap_mark (free_map, ROOT_DIR_SECTOR); }
int highest_cluster() { int disk_bytes = disk_size(); int disk_sectors = disk_bytes / bytes_sector(); int data_sectors = disk_sectors - start_of_data(); int data_clusters = data_sectors / sectors_cluster(); int total_clusters = FAT_CLUSTER_OFFSET + data_clusters; return total_clusters; }
/* Swap Table access methods. */ void swap_init () { struct disk *swap = disk_get (1,1); // swap 1:1 channel:device no if (!swap) PANIC ("No swap disk found\n"); disk_sector_t size = disk_size (swap); swap_table = bitmap_create (size); if (!swap_table) PANIC ("Cannot create bitmap for the swap table\n"); lock_init (&swap_table_lock); }
block_off_t parity_used_size(struct snapraid_state* state) { block_off_t parity_block; tommy_node* i; /* compute the size of the parity file */ parity_block = 0; for (i = state->disklist; i != 0; i = i->next) { struct snapraid_disk* disk = i->data; /* start from the declared size */ block_off_t block = disk_size(disk); /* decrease the block until an used one */ while (block > parity_block && !block_has_file_and_valid_parity(fs_par2block_get(disk, block - 1))) --block; /* get the highest value */ if (block > parity_block) parity_block = block; } return parity_block; }
// Generate a hybrid MBR based on the current GPT. Stores the result in the // new_mbr_parts[] array. static VOID generate_hybrid_mbr(VOID) { UINTN i, k, iter, count_active; UINT64 first_used_lba; new_mbr_part_count = 1; first_used_lba = (UINT64) MAX_MBR_LBA + (UINT64) 1; // Copy partitions in three passes.... // First, do FAT and NTFS partitions.... i = 0; do { if ((gpt_parts[i].start_lba > 0) && (gpt_parts[i].end_lba > 0) && (gpt_parts[i].end_lba <= MAX_MBR_LBA) && /* Within MBR limits */ (gpt_parts[i].gpt_parttype->kind == GPT_KIND_BASIC_DATA) && /* MS Basic Data GPT type code */ (gpt_parts[i].mbr_type != 0x83)) { /* Not containing Linux filesystem */ copy_gpt_to_new_mbr(i, new_mbr_part_count); if (new_mbr_parts[new_mbr_part_count].start_lba < first_used_lba) first_used_lba = new_mbr_parts[new_mbr_part_count].start_lba; new_mbr_part_count++; } i++; } while (i < gpt_part_count && new_mbr_part_count <= 3); // Second, do Linux partitions. Note that we start from the END of the // partition list, so as to maximize the space covered by the 0xEE // partition if there are several Linux partitions before other hybridized // partitions. i = gpt_part_count - 1; // Note that gpt_part_count can't be 0; filtered by check_gpt() while (i < gpt_part_count && new_mbr_part_count <= 3) { // if too few GPT partitions, i loops around to a huge value if ((gpt_parts[i].start_lba > 0) && (gpt_parts[i].end_lba > 0) && (gpt_parts[i].end_lba <= MAX_MBR_LBA) && ((gpt_parts[i].gpt_parttype->kind == GPT_KIND_DATA) || (gpt_parts[i].gpt_parttype->kind == GPT_KIND_BASIC_DATA)) && (gpt_parts[i].mbr_type == 0x83)) { copy_gpt_to_new_mbr(i, new_mbr_part_count); if (new_mbr_parts[new_mbr_part_count].start_lba < first_used_lba) first_used_lba = new_mbr_parts[new_mbr_part_count].start_lba; new_mbr_part_count++; } i--; } // while // Third, do anything that's left to cover uncovered spaces; but this requires // first creating the EFI protective entry, since we don't want to bother with // anything already covered by this entry.... new_mbr_parts[0].index = 0; new_mbr_parts[0].start_lba = 1; new_mbr_parts[0].end_lba = (disk_size() > first_used_lba) ? (first_used_lba - 1) : disk_size() - 1; if (new_mbr_parts[0].end_lba > MAX_MBR_LBA) new_mbr_parts[0].end_lba = MAX_MBR_LBA; new_mbr_parts[0].mbr_type = 0xEE; i = 0; while (i < gpt_part_count && new_mbr_part_count <= 3) { if ((gpt_parts[i].start_lba > new_mbr_parts[0].end_lba) && (gpt_parts[i].end_lba > 0) && (gpt_parts[i].end_lba <= MAX_MBR_LBA) && (gpt_parts[i].gpt_parttype->kind != GPT_KIND_BASIC_DATA) && (gpt_parts[i].mbr_type != 0x83)) { copy_gpt_to_new_mbr(i, new_mbr_part_count); new_mbr_part_count++; } i++; } // while // find matching partitions in the old MBR table, copy undetected details.... for (i = 1; i < new_mbr_part_count; i++) { for (k = 0; k < mbr_part_count; k++) { if (mbr_parts[k].start_lba == new_mbr_parts[i].start_lba) { // keep type if not detected if (new_mbr_parts[i].mbr_type == 0) new_mbr_parts[i].mbr_type = mbr_parts[k].mbr_type; // keep active flag new_mbr_parts[i].active = mbr_parts[k].active; break; } // if } // for (k...) if (new_mbr_parts[i].mbr_type == 0) { // final fallback: set to a (hopefully) unused type new_mbr_parts[i].mbr_type = 0xc0; } // if } // for (i...) sort_mbr(new_mbr_parts); // make sure there's exactly one active partition for (iter = 0; iter < 3; iter++) { // check count_active = 0; for (i = 0; i < new_mbr_part_count; i++) if (new_mbr_parts[i].active) count_active++; if (count_active == 1) break; // set active on the first matching partition if (count_active == 0) { for (i = 0; i < new_mbr_part_count; i++) { if (((new_mbr_parts[i].mbr_type == 0x07 || // NTFS new_mbr_parts[i].mbr_type == 0x0b || // FAT32 new_mbr_parts[i].mbr_type == 0x0c)) || // FAT32 (LBA) (iter >= 1 && (new_mbr_parts[i].mbr_type == 0x83)) || // Linux (iter >= 2 && i > 0)) { new_mbr_parts[i].active = TRUE; break; } } } else if (count_active > 1 && iter == 0) { // too many active partitions, try deactivating the ESP / EFI Protective entry if ((new_mbr_parts[0].mbr_type == 0xee || new_mbr_parts[0].mbr_type == 0xef) && new_mbr_parts[0].active) { new_mbr_parts[0].active = FALSE; } } else if (count_active > 1 && iter > 0) { // too many active partitions, deactivate all but the first one count_active = 0; for (i = 0; i < new_mbr_part_count; i++) if (new_mbr_parts[i].active) { if (count_active > 0) new_mbr_parts[i].active = FALSE; count_active++; } } } // for } // VOID generate_hybrid_mbr()
static void init_super_block() { mb.magic = FS_MAGIC; mb.nfatblocks = ceil((double)disk_size() / (double)N_ADDRESSES_PER_BLOCK); mb.nblocks = disk_size() - mb.nfatblocks - 2; }
int main( int argc, char *argv[] ) { char line[1024]; char cmd[1024]; char arg1[1024]; char arg2[1024]; int result, args; if(argc!=3) { printf("use: %s <diskfile> <nblocks>\n",argv[0]); return 1; } if(!disk_init(argv[1],atoi(argv[2]))) { printf("couldn't initialize %s: %s\n",argv[1],strerror(errno)); return 1; } printf("opened emulated disk image %s with %d blocks\n",argv[1],disk_size()); while(1) { printf(" prompt> "); fflush(stdout); if(!fgets(line,sizeof(line),stdin)) break; if(line[0]=='\n') continue; line[strlen(line)-1] = 0; args = sscanf(line,"%s %s %s",cmd,arg1,arg2); if(args==0) continue; if(!strcmp(cmd,"format")) { if(args==1) { if(!fs_format()) { printf("disk formatted.\n"); } else { printf("format failed!\n"); } } else { printf("use: format\n"); } } else if(!strcmp(cmd,"mount")) { if(args==1) { if(!fs_mount()) { printf("disk mounted.\n"); } else { printf("mount failed!\n"); } } else { printf("use: mount\n"); } } else if(!strcmp(cmd,"debug")) { if(args==1) { fs_debug(); } else { printf("use: debug\n"); } } else if(!strcmp(cmd,"getsize")) { if(args==2) { result = fs_getsize(arg1); if(result>=0) { printf("file %s has size %d\n",arg1,result); } else { printf("getsize failed!\n"); } } else { printf("use: getsize <filename>\n"); } } else if(!strcmp(cmd,"create")) { if(args==2) { result = fs_create(arg1); if(result==0) { printf("created file %s\n",arg1); } else { printf("create failed!\n"); } } else { printf("use: create <filename>\n"); } } else if(!strcmp(cmd,"delete")) { if(args==2) { if(!fs_delete(arg1)) { printf("file %s deleted.\n",arg1); } else { printf("delete failed!\n"); } } else { printf("use: delete <filename>\n"); } } else if(!strcmp(cmd,"cat")) { if(args==2) { if(!do_copyout(arg1,"/dev/stdout")) { printf("cat failed!\n"); } } else { printf("use: cat <name>\n"); } } else if(!strcmp(cmd,"copyin")) { if(args==3) { if(do_copyin(arg1,arg2)) { printf("copied file %s to %s\n",arg1,arg2); } else { printf("copy failed!\n"); } } else { printf("use: copyin <filename in host system> <filename in fs-miei-02>\n"); } } else if(!strcmp(cmd,"copyout")) { if(args==3) { if(do_copyout(arg1,arg2)) { printf("copied myfs_file %s to file %s\n", arg1,arg2); } else { printf("copy failed!\n"); } } else { printf("use: copyout <inumber> <filename>\n"); } } else if(!strcmp(cmd,"help")) { printf("Commands are:\n"); printf(" format\n"); printf(" mount\n"); printf(" debug\n"); printf(" create\n"); printf(" delete <filename>\n"); printf(" cat <filename>\n"); printf(" getsize <filename>\n"); printf(" copyin <file name in host system> <miei02-filename>\n"); printf(" copyout <miei02-filename> <file name in host system>\n"); printf(" dump <number_of_block_with_text_contents>\n"); printf(" help\n"); printf(" quit\n"); printf(" exit\n"); } else if(!strcmp(cmd,"quit")) { break; } else if(!strcmp(cmd,"exit")) { break; } else if (!strcmp(cmd, "dump")){ if(args==2) { int blNo = atoi(arg1); printf("Dumping disk block %d\n", blNo); char b[4096]; disk_read( blNo, b); printf("------------------------------\n"); printf("%s", b); printf("\n------------------------------\n"); } else { printf("use: dump <block_number>\n"); } } else { printf("unknown command: %s\n",cmd); printf("type 'help' for a list of commands.\n"); result = 1; } } printf("closing emulated disk.\n"); disk_close(); return 0; }
int main( int argc, char *argv[] ) { char line[1024]; char cmd[1024]; char arg1[1024]; char arg2[1024]; int inumber, result, args; if(argc!=3) { printf("use: %s <diskfile> <nblocks>\n",argv[0]); return 1; } if(!disk_init(argv[1],atoi(argv[2]))) { printf("couldn't initialize %s: %s\n",argv[1],strerror(errno)); return 1; } printf("opened emulated disk image %s with %d blocks\n",argv[1],disk_size()); while(1) { printf(" prompt> "); fflush(stdout); if(!fgets(line,sizeof(line),stdin)) break; if(line[0]=='\n') continue; line[strlen(line)-1] = 0; args = sscanf(line,"%s %s %s",cmd,arg1,arg2); if(args==0) continue; if(!strcmp(cmd,"format")) { if(args==1) { if(fs_format()) { printf("disk formatted.\n"); } else { printf("format failed!\n"); } } else { printf("use: format\n"); } } else if(!strcmp(cmd,"mount")) { if(args==1) { if(fs_mount()) { printf("disk mounted.\n"); } else { printf("mount failed!\n"); } } else { printf("use: mount\n"); } } else if(!strcmp(cmd,"umount")) { if(args==1) { if(fs_umount()) { printf("disk umounted.\n"); } else { printf("umount failed!\n"); } } else { printf("use: mount\n"); } } else if(!strcmp(cmd,"debug")) { if(args==1) { fs_debug(); } else { printf("use: debug\n"); } } else if(!strcmp(cmd,"getsize")) { if(args==2) { inumber = atoi(arg1); result = fs_getsize(inumber); if(result>=0) { printf("inode %d has size %d\n",inumber,result); } else { printf("getsize failed!\n"); } } else { printf("use: getsize <inumber>\n"); } } else if(!strcmp(cmd,"create")) { if(args==1) { inumber = fs_create(); if(inumber>=0) { printf("created inode %d\n",inumber); } else { printf("create failed!\n"); } } else { printf("use: create\n"); } } else if(!strcmp(cmd,"delete")) { if(args==2) { inumber = atoi(arg1); if(fs_delete(inumber)) { printf("inode %d deleted.\n",inumber); } else { printf("delete failed!\n"); } } else { printf("use: delete <inumber>\n"); } } else if(!strcmp(cmd,"cat")) { if(args==2) { inumber = atoi(arg1); if(!do_copyout(inumber,"/dev/stdout")) { printf("cat failed!\n"); } } else { printf("use: cat <inumber>\n"); } } else if(!strcmp(cmd,"copyin")) { if(args==3) { inumber = atoi(arg2); if(do_copyin(arg1,inumber)) { printf("copied file %s to inode %d\n",arg1,inumber); } else { printf("copy failed!\n"); } } else { printf("use: copyin <filename> <inumber>\n"); } } else if(!strcmp(cmd,"copyout")) { if(args==3) { inumber = atoi(arg1); if(do_copyout(inumber,arg2)) { printf("copied inode %d to file %s\n",inumber,arg2); } else { printf("copy failed!\n"); } } else { printf("use: copyout <inumber> <filename>\n"); } } else if(!strcmp(cmd,"help")) { printf("Commands are:\n"); printf(" format\n"); printf(" mount\n"); printf(" umount\n"); printf(" debug\n"); printf(" create\n"); printf(" delete <inode>\n"); printf(" getsize <inode>\n"); printf(" cat <inode>\n"); printf(" copyin <file> <inode>\n"); printf(" copyout <inode> <file>\n"); printf(" help\n"); printf(" quit\n"); printf(" exit\n"); } else if(!strcmp(cmd,"quit")) { break; } else if(!strcmp(cmd,"exit")) { break; } else { printf("unknown command: %s\n",cmd); printf("type 'help' for a list of commands.\n"); result = 1; } } printf("closing emulated disk.\n"); disk_close(); return 0; }