/// read super block and write to image head extern void initial_image_hdr(char* device, image_head* image_hdr) { unsigned long long total_sector = 0; unsigned long long bused = 0; log_mesg(2, 0, 0, fs_opt.debug, "%s: initial_image start\n", __FILE__); fs_open(device); get_fat_type(); total_sector = get_total_sector(); bused = get_used_block();//so I need calculate by myself. strncpy(image_hdr->magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE); strncpy(image_hdr->fs, fat_type, FS_MAGIC_SIZE); image_hdr->block_size = (int)fat_sb.sector_size; image_hdr->totalblock = (unsigned long long)total_sector; image_hdr->device_size = (unsigned long long)(total_sector * image_hdr->block_size); image_hdr->usedblocks = (unsigned long long)bused; log_mesg(2, 0, 0, fs_opt.debug, "%s: Block Size:%i\n", __FILE__, image_hdr->block_size); log_mesg(2, 0, 0, fs_opt.debug, "%s: Total Blocks:%llu\n", __FILE__, image_hdr->totalblock); log_mesg(2, 0, 0, fs_opt.debug, "%s: Used Blocks:%llu\n", __FILE__, image_hdr->usedblocks); log_mesg(2, 0, 0, fs_opt.debug, "%s: Device Size:%llu\n", __FILE__, image_hdr->device_size); fs_close(); log_mesg(2, 0, 0, fs_opt.debug, "%s: initial_image down\n", __FILE__); }
/** Creates a new instance of a FS @param image_path Path to the FAT12/FAT16 image location @return Pointer to a new instance of a FS struct **/ FS_Instance *fs_create_instance(char *image_path) { FS_Instance *fs = (FS_Instance *)malloc(sizeof(FS_Instance)); fs->file_name = image_path; fs->image = fopen(image_path, "rb"); // Boot Sector fseek(fs->image, 0, SEEK_SET); fread(&fs->BS_jmpBoot, 3, 1, fs->image); fseek(fs->image, 3, SEEK_SET); fread(&fs->BS_OEMName, 8, 1, fs->image); fseek(fs->image, 11, SEEK_SET); fread(&fs->BPB_BytsPerSec, 11, 1, fs->image); fseek(fs->image, 13, SEEK_SET); fread(&fs->BPB_SecPerClus, 13, 1, fs->image); fseek(fs->image, 14, SEEK_SET); fread(&fs->BPB_RsvdSecCnt, 14, 1, fs->image); fseek(fs->image, 16, SEEK_SET); fread(&fs->BPB_NumFATs, 16, 1, fs->image); fseek(fs->image, 17, SEEK_SET); fread(&fs->BPB_RootEntCnt, 17, 1, fs->image); fseek(fs->image, 19, SEEK_SET); fread(&fs->BPB_TotSec16, 19, 1, fs->image); fseek(fs->image, 21, SEEK_SET); fread(&fs->BPB_Media, 21, 1, fs->image); fseek(fs->image, 22, SEEK_SET); fread(&fs->BPB_FATSz16, 22, 1, fs->image); fseek(fs->image, 24, SEEK_SET); fread(&fs->BPB_SecPerTrk, 24, 1, fs->image); fseek(fs->image, 26, SEEK_SET); fread(&fs->BPB_NumHeads, 26, 1, fs->image); fseek(fs->image, 28, SEEK_SET); fread(&fs->BPB_HiddSec, 28, 1, fs->image); fseek(fs->image, 32, SEEK_SET); fread(&fs->BPB_TotSec32, 32, 1, fs->image); // FAT12 and FAT16 fseek(fs->image, 36, SEEK_SET); fread(&fs->BPB_DrvNum, 36, 1, fs->image); fseek(fs->image, 37, SEEK_SET); fread(&fs->BPB_Reserved1, 37, 1, fs->image); fseek(fs->image, 38, SEEK_SET); fread(&fs->BPB_BootSig, 38, 1, fs->image); fseek(fs->image, 39, SEEK_SET); fread(&fs->BPB_VolID, 39, 1, fs->image); fseek(fs->image, 43, SEEK_SET); fread(&fs->BS_VolLab, 43, 1, fs->image); fseek(fs->image, 54, SEEK_SET); fread(&fs->BS_FilSysType, 54, 1, fs->image); fs->num_sectors = get_total_sectors(fs); fs->root_sectors = ((fs->BPB_RootEntCnt * 32) + (fs->BPB_BytsPerSec - 1)) / fs->BPB_BytsPerSec; fs->data_sectors = (fs->BPB_RsvdSecCnt + (fs->BPB_NumFATs * fs->BPB_FATSz16) + fs->root_sectors); fs->cluster_count = (fs->num_sectors - fs->data_sectors)/fs->BPB_SecPerClus; fs->fat_type = get_fat_type(fs); fs->current_cluster = 0x00000001; fs->current_directory_position = fs->BPB_RsvdSecCnt + (fs->BPB_NumFATs * fs->BPB_FATSz16); return fs; }
/* Creates the PedFileSystem struct for the new resized file system, and sticks it in a FatOpContext. At the end of the process, the original (ctx->old_fs) is destroyed, and replaced with the new one (ctx->new_fs). */ static FatOpContext* create_resize_context (PedFileSystem* fs, const PedGeometry* new_geom) { FatSpecific* fs_info = FAT_SPECIFIC (fs); FatSpecific* new_fs_info; PedFileSystem* new_fs; PedSector new_cluster_sectors; FatCluster new_cluster_count; PedSector new_fat_sectors; FatType new_fat_type; PedSector root_dir_sector_count; FatOpContext* context; /* hypothetical number of root dir sectors, if we end up using * FAT16 */ if (fs_info->root_dir_sector_count) root_dir_sector_count = fs_info->root_dir_sector_count; else root_dir_sector_count = FAT_ROOT_DIR_ENTRY_COUNT * sizeof (FatDirEntry) / 512; if (!get_fat_type (fs, new_geom, &new_fat_type)) return 0; fat_calc_resize_sizes (new_geom, fs_info->cluster_sectors, new_fat_type, root_dir_sector_count, fs_info->cluster_sectors, &new_cluster_sectors, &new_cluster_count, &new_fat_sectors); if (!fat_check_resize_geometry (fs, new_geom, new_cluster_sectors, new_cluster_count)) goto error; new_fs = fat_alloc (new_geom); if (!new_fs) goto error; new_fs_info = FAT_SPECIFIC (new_fs); if (!new_fs_info) goto error_free_new_fs; /* preserve boot code, etc. */ memcpy (&new_fs_info->boot_sector, &fs_info->boot_sector, sizeof (FatBootSector)); memcpy (&new_fs_info->info_sector, &fs_info->info_sector, sizeof (FatInfoSector)); new_fs_info->logical_sector_size = fs_info->logical_sector_size; new_fs_info->sector_count = new_geom->length; new_fs_info->sectors_per_track = fs_info->sectors_per_track; new_fs_info->heads = fs_info->heads; new_fs_info->cluster_size = new_cluster_sectors * 512; new_fs_info->cluster_sectors = new_cluster_sectors; new_fs_info->cluster_count = new_cluster_count; new_fs_info->dir_entries_per_cluster = fs_info->dir_entries_per_cluster; new_fs_info->fat_type = new_fat_type; new_fs_info->fat_table_count = 2; new_fs_info->fat_sectors = new_fat_sectors; /* what about copying? */ new_fs_info->serial_number = fs_info->serial_number; if (new_fs_info->fat_type == FAT_TYPE_FAT32) { new_fs_info->info_sector_offset = 1; new_fs_info->boot_sector_backup_offset = 6; new_fs_info->root_dir_offset = 0; new_fs_info->root_dir_entry_count = 0; new_fs_info->root_dir_sector_count = 0; /* we add calc_align_sectors to push the cluster_offset forward, to keep the clusters aligned between the new and old file systems */ new_fs_info->fat_offset = fat_min_reserved_sector_count (FAT_TYPE_FAT32) + fat_calc_align_sectors (new_fs, fs); new_fs_info->cluster_offset = new_fs_info->fat_offset + 2 * new_fs_info->fat_sectors; } else { new_fs_info->root_dir_sector_count = root_dir_sector_count; new_fs_info->root_dir_entry_count = root_dir_sector_count * 512 / sizeof (FatDirEntry); new_fs_info->fat_offset = fat_min_reserved_sector_count (FAT_TYPE_FAT16) + fat_calc_align_sectors (new_fs, fs); new_fs_info->root_dir_offset = new_fs_info->fat_offset + 2 * new_fs_info->fat_sectors; new_fs_info->cluster_offset = new_fs_info->root_dir_offset + new_fs_info->root_dir_sector_count; } new_fs_info->total_dir_clusters = fs_info->total_dir_clusters; context = fat_op_context_new (new_fs, fs); if (!context) goto error_free_new_fs_info; if (!fat_op_context_create_initial_fat (context)) goto error_free_context; if (!fat_alloc_buffers (new_fs)) goto error_free_fat; return context; error_free_fat: fat_table_destroy (new_fs_info->fat); error_free_context: free (context); error_free_new_fs_info: free (new_fs_info); error_free_new_fs: free (new_fs); error: return NULL; }