⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unformat.cpp

📁 unformat格式化后的硬盘的数据恢复
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		{
		physical_drive=possible_physical_hard_disk;
		partition_number_of_logical_drive=pointer;
		increment_heads=TRUE;
		logical_drive_found=TRUE;
		}
	      }
	    pointer++;
	    }while(pointer<=maximum_partition_assigned[possible_physical_hard_disk-128]);
	  }
	possible_physical_hard_disk++;
	}while(possible_physical_hard_disk<136);
      }

    /* Get a full partition table listing for that drive */

    if(logical_drive_found==TRUE)
      {
      Get_Physical_Hard_Drive_Parameters();
      Read_Partition_Table();
      }
    else
      {
      printf("\nDrive letter entered is out of range...operation terminated.\n");
      exit(4);
      }

    /* Load physical partition information */

    partition_starting_cylinder=starting_cylinder[partition_number_of_logical_drive];
    partition_starting_head=starting_head[partition_number_of_logical_drive];
    partition_starting_sector=starting_sector[partition_number_of_logical_drive];

    partition_ending_cylinder=ending_cylinder[partition_number_of_logical_drive];
    partition_ending_head=ending_head[partition_number_of_logical_drive];
    partition_ending_sector=ending_sector[partition_number_of_logical_drive];

    drive_maximum_cylinders=ending_cylinder[partition_number_of_logical_drive];
    drive_maximum_heads=total_heads;
    drive_maximum_sectors=total_sectors;

    total_logical_sectors=((partition_ending_cylinder-partition_starting_cylinder)+1)*(total_heads+1)*total_sectors;
    total_physical_sectors=total_logical_sectors;

    logical_sector_offset=((partition_starting_cylinder)*(total_heads+1)*(total_sectors));
    if(logical_sector_offset==0) logical_sector_offset=(partition_starting_head)*(total_sectors);
    if(increment_heads==TRUE) logical_sector_offset=logical_sector_offset+total_sectors;

    /* Compute cluster size */
    unsigned long size=(total_logical_sectors*512)/1000000;

    /* Sectors per cluster conversion table */
    if (size<=32767) number_of_sectors_per_cluster=64;
    if (size<=16383) number_of_sectors_per_cluster=32;
    if (size<=8191) number_of_sectors_per_cluster=16;

    /* May be illegal cluster sizes
    if (size<=256) number_of_sectors_per_cluster=8;
    if (size<=128) number_of_sectors_per_cluster=4;
    */
    }
}

/* Get Physical Floppy Drive Parameters */
void Get_Physical_Floppy_Drive_Parameters()
{
  asm{
    mov ah, 0x08
    mov dl, BYTE PTR physical_drive
    int 0x013

    mov BYTE PTR total_sectors, cl
    mov BYTE PTR total_cylinders, ch
    mov BYTE PTR total_heads, dh
    mov BYTE PTR floppy_drive_type, bl
    }
}

/* Get Physical Hard Drive Parameters */
void Get_Physical_Hard_Drive_Parameters()
{
  asm{
    mov ah, 0x08
    mov dl, BYTE PTR physical_drive
    int 0x13

    mov bl,cl
    and bl,00111111B

    mov BYTE PTR total_sectors, bl

    mov bl,cl
    mov cl,ch
    shr bl,1
    shr bl,1
    shr bl,1
    shr bl,1
    shr bl,1
    shr bl,1

    mov ch,bl

    mov WORD PTR total_cylinders, cx
    mov BYTE PTR total_heads, dh
    }
}

/* List File & Directory Names //////// */
void List_Names()
{
  int loop=0;
  int sub_loop=0;

  char file_or_directory [12];

  file_or_directory [8] = 46;

  do
    {
    sub_loop = 0;
    do
      {
      file_or_directory[sub_loop]=sector_buffer[(loop+sub_loop)];
      sub_loop++;
      } while(sub_loop <=7);

    sub_loop++;

    do
      {
      file_or_directory[sub_loop]=sector_buffer[(loop+sub_loop-1)];
      sub_loop++;
      } while(sub_loop <=11);

    if( (file_or_directory[0]>=48) && (file_or_directory[0]<=122) )
      {
      printf("\n%.12s",file_or_directory);
      }

    loop = loop + 32;
    } while(loop <=480);
}

/* Help Routine  ////////////////////// */
void Display_Help_Screen()
{
  printf("\nUnformat Version 0.8               Restores a disk that has been formatted.\n\n");
  printf("Syntax:\n\n");
  printf("UNFORMAT drive: [/J]\n");
  printf("UNFORMAT drive: [/U] [/L] [/TEST] [/P]\n");
  printf("UNFORMAT /PARTN [/L]\n");
  printf("UNFORMAT /?\n\n");
  printf("  drive:    Drive letter to unformat.\n");
  printf("  /J        Verifies that the drive mirror files is synchronized with the disk.\n");
  printf("  /U      * Unformats without using MIRROR files.\n");
  printf("  /L        Lists all files and directories found, or, when used with the\n");
  printf("              /PART switch, displays current partition tables.\n");
  printf("  /TEST   * Displays information but does not write changes to disk.\n");
  printf("  /P      * Sends output messages to LPT1.\n");
  printf("  /PARTN    Restores disk partition tables.\n\n");
  printf("  /?        Displays this help screen.\n");
  printf("This program is copyright 1998 by Brian E. Reifsnyder under the terms of\n");
  printf("GNU General Public License and is without warranty of any kind.\n");
  printf("\n* Indicates functions not yet available.\n");
}

/* Read Partition Table */
void Read_Partition_Table()
{
  long index=0x1be;

  int exiting_primary=TRUE;
  int extended=FALSE;
  int partition_designation=PRIMARY;
  int pointer=0;
  int record_extended_info_flag=FALSE;

  int done_flag=FALSE;

  unsigned long extended_cylinder;
  unsigned long extended_head;
  unsigned long extended_sector;

  Read_Physical_Sector(physical_drive,0,0,1);

  do{
    if(pointer==4) partition_designation=EXTENDED;

    if((pointer>=4) && (extended==TRUE))
      {
      Read_Physical_Sector(physical_drive,extended_head,extended_cylinder,extended_sector);
      extended=FALSE;
      index=0x1be;

      if(exiting_primary==FALSE)
	{
	pointer--;
	}
      else
	{
	exiting_primary=FALSE;
	}
      }

    /* Determine Partition Type */
    numeric_partition_type[pointer]=sector_buffer[index+4];

    if(sector_buffer[index+4]==0x00)
      {
      if(partition_designation==EXTENDED)
	{
	number_of_partitions=pointer;
	done_flag=TRUE;
	}
      }
    if(sector_buffer[index+4]==0x05)
      {
      extended=TRUE;
      record_extended_info_flag=TRUE;
      }

    starting_head[pointer] = sector_buffer[index+1];
    ending_head[pointer] = sector_buffer[index+5];

    partition_size[pointer]=Decimal_Number(sector_buffer[index+12],sector_buffer[index+13],sector_buffer[index+14],sector_buffer[index+15])/2000;

    Extract_Cylinder_and_Sector(sector_buffer[index+2],sector_buffer[index+3]);

    starting_cylinder[pointer]=g_cylinder;
    starting_sector[pointer]=g_sector;

    if((extended==TRUE) && (record_extended_info_flag==TRUE))
      {
      extended_cylinder=starting_cylinder[pointer];
      extended_head=starting_head[pointer];
      extended_sector=starting_sector[pointer];
      record_extended_info_flag=FALSE;
      }

    Extract_Cylinder_and_Sector(sector_buffer[index+6],sector_buffer[index+7]);

    ending_cylinder[pointer]=g_cylinder;
    ending_sector[pointer]=g_sector;

    partition_size_1[pointer]=sector_buffer[index+12];
    partition_size_2[pointer]=sector_buffer[index+13];
    partition_size_3[pointer]=sector_buffer[index+14];
    partition_size_4[pointer]=sector_buffer[index+15];

    relative_sectors_1[pointer]=sector_buffer[index+8];
    relative_sectors_2[pointer]=sector_buffer[index+9];
    relative_sectors_3[pointer]=sector_buffer[index+10];
    relative_sectors_4[pointer]=sector_buffer[index+11];

    pointer++;
    number_of_partitions=pointer-1;

    if((extended==FALSE) && (pointer==4))
      {
      number_of_partitions=4;
      done_flag=TRUE;
      }

    index=index + 16;
    } while(done_flag==FALSE);
}

/* Read Physical Sector */
void Read_Physical_Sector(int drive, int head, long cyl, int sector)
{
  int result;

  result=biosdisk(2, drive, head, cyl, sector, 1, sector_buffer);

    if (result!=0)
      {
      printf("\nRead error...operation terminated.\n");
      exit(4);
      }
}

/* Read Sector From Disk */
void Read_Sector(unsigned long sector)
{
  Convert_Logical_To_Physical(sector);

  printf("Cylinder:  %5i  ",translated_cylinder);
  printf("Head:  %2i \n",translated_head);
  /* printf("Sector: %i \n",translated_sector); */

  /* Re-position cursor back to the beginning of the line */
  asm{
    /* Get current video display mode */
    mov ah,0x0f
    int 0x10

    /* Get cursor position */
    mov ah,0x03
    int 0x10

    /* Set cursor position to beginning of line */
    mov ah,0x02
    sub dh,1
    int 0x10
    }

  Read_Physical_Sector(physical_drive,translated_head,translated_cylinder,translated_sector);
}

/* Read Mirror Map Into Buffer //////// */
void Read_Mirror_Map()
{
  int flag = 0;

  long loop;
  long mirror_map_pointer;
  long index;
  long subloop;

  long current_sector;
  long mirror_map_start_sector;

  long source_sector;
  long destination_sector;

  printf("\n\nSearching for drive mirror...\n");

  /*Find "pointer sector" at end of drive */

  current_sector=total_logical_sectors-200;

  if(current_sector<0)
    {
    current_sector=0;
    }

  /*If drive is a floppy drive, change the following: */
  if(drive_type==FLOPPY)
    {
    current_sector=total_logical_sectors-10;
    total_logical_sectors--;
    }

  do
    {
    Read_Sector(current_sector);
    if((65==sector_buffer[4])&&(77==sector_buffer[5])&&(83==sector_buffer[6])&&(69==sector_buffer[7]))
      {
      mirror_map_start_sector=Decimal_Number(sector_buffer[0],sector_buffer[1],sector_buffer[2],sector_buffer[3]);
      flag=FOUND;
      }
    current_sector++;
    } while((current_sector<=total_logical_sectors) || (flag!=FOUND));

  loop=0;

  if(FOUND==flag)
    {
    /*Read Mirror Map into buffer */
    printf("\n\nLoading drive mirror...\n");

    do
      {
      Read_Sector(mirror_map_start_sector+loop);

      subloop=0;
      do
	{
	mirror_map[subloop+(loop*512)]=sector_buffer[subloop];
	subloop++;
	}while(subloop<512);

      loop++;
      }while(loop<=10);
    }

  else
    {
    printf("\nThe sector that points to the drive mirror has not been found.\n");
    printf("Operation Terminated.\n");
    exit(1);
    }

}

/* Reset drive controller */
void Reset_Drive()
{
  asm{
    mov ah,0
    mov dl,BYTE PTR physical_drive
    int 0x13

    mov ah,0
    mov dl,BYTE PTR physical_drive
    int 0x13
    }
}

/* Restore Partition Tables /////////// */
void Restore_Partition_Tables(int list)
{
  if(list!=LIST)
    {
    /*  Re-create the partition tables on the hard disks from the
    /*  a:partnsav.fil. */


    /*
    Map of the PARTNSAV.FIL for FreeDOS:

    (I do not plan on using an MS-DOS compatible file.)

    offset:00
    FreeDOS partitio
    n mirror file

    offset:32-127 reserved for future expansion

    offset:128(drive#)      129(lb offset)    130(mb offset)    131(lb dest cyl)
	   132(mb dest cyl) 133(lb dest head) 134(mb dest head) 135(dest sect.)
    .
    .
    .
    offset:1024 stored sector
    offset:1536 stored sector
    offset:2048 stored sector

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -