Example #1
0
int fat_make_fs(fat_dev_t * pdev){
    //1. write boot sector
    fat_boot_t boot;
    fat_boot_init(&boot);
    int ret = write_boot_sector(pdev, &boot);
    if(ret != 0) {
        ret = -1;
        goto final_boot;
    }
    
    //2. write the fat
    fat_fat_t fat;
    ret = fat_fat_init(&fat, SECTOR_OF_BOOT* two_bytes_to_short(boot.sector_size) , boot.fats, boot.fat_length, two_bytes_to_short(boot.sector_size));
    if(ret != 0){ 
        ret = -1;
        goto final_boot;
    }
    ret = write_fat(pdev, &fat);
    if(ret != 0){
        ret = -1;
        goto final_fat;
    }

    //3. write the root_entries
    fat_root_entries_t root_entries;
    int start_offset = fat.start_offset + fat.number_of_fat * fat.sectors_per_fat * fat.bytes_per_sector;
    ret = fat_root_entries_init(&root_entries, start_offset, two_bytes_to_short(boot.dir_entries));
    if(ret != 0){
        ret = -1;
        goto final_root_entries;
    }
    ret = write_root_entries(pdev, &root_entries);
    if(ret != 0){
        ret = -1;
        goto final_root_entries;
    }

    //4. write the left data block
    fat_data_t data;
    start_offset = root_entries.start_offset + root_entries.number_entries_in_root * sizeof(fat_dentry_t);
    ret = fat_data_init(&data, start_offset, 2880-33, fat.bytes_per_sector); //TODO: 
    if(ret != 0){
        ret = -1;
        goto final_data;
    }
    ret = write_data_blocks(pdev, &data);
    if(ret != 0){
        ret = -1;
        goto final_data;
    }
    return 0;

final_data:
    fat_data_deinit(&data);
final_root_entries:
    fat_root_entries_deinit(&root_entries);
final_fat: 
    fat_fat_deinit(&fat);
final_boot:
    fat_boot_deinit(&boot);
    return ret;
}
Example #2
0
boolean fips_partition::split (fips_harddrive hd)
{
	if (read_boot_sector ())
		error ("Error reading boot sector");

	if (global.debug_mode)
	{
		fprintf
		(
			global.debugfile,
			"\nBoot sector drive %02Xh, partition %u:\n\n",
			hd.number,
			number + 1
		);

		hexwrite
		(
			boot_sector->data,
			512,
			global.debugfile
		);
	}

	get_bpb ();

	printx ("\nBoot sector:\n\n");
	print_bpb ();

	get_info ();
	if (global.debug_mode)
		write_info_debugfile ();

	check ();

	fat16 fat1 (this,1);
	fat16 fat2 (this,2);

	fat1.check_against (&fat2);

	dword new_part_min_cylinder =
		min_cylinder (fat2, hd.geometry);

	if (ask_if_save()) save_root_and_boot(&hd,this);

	dword new_start_cylinder =
		ask_for_new_start_cylinder
		(
			partition_info->start_cylinder,
			new_part_min_cylinder,
			partition_info->end_cylinder,
			hd.geometry.heads * hd.geometry.sectors
		);

	fat2.check_empty
	(
		new_start_cylinder
			* hd.geometry.heads
			* hd.geometry.sectors
		- partition_info->start_sector_abs
	);

	hd.calculate_new_root (new_start_cylinder, this);

	hd.put_partition_table();
	hd.get_partition_table();

	printx ("\nNew partition table:\n\n");
	hd.print_partition_table ();

	hd.check (FINAL_CHECK);

	if (ask_if_continue () == false)
	{
		return (false);
	}

	calculate_new_boot ();

	put_bpb ();
	get_bpb ();

	printx ("\nNew boot sector:\n\n");
	print_bpb ();

	get_info ();
	if (global.debug_mode)
		write_info_debugfile ();

	check();

	if (!global.test_mode)
	{
		ask_for_write_permission ();

		if (hd.write_root_sector ())
			error ("Error writing root sector");

		if (write_boot_sector ())
			error ("Error writing boot sector");

		printx ("Repartitioning complete\n");
	}

	return (true);
}