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

📄 hdisk.c

📁 format命令的源代码
💻 C
字号:
/*
// Program:  Format
// Version:  0.90
// Written By:  Brian E. Reifsnyder
// Copyright:  2002 under the terms of the GNU GPL, Version 2
// Module Name:  hdisk.c
// Module Description:  Hard Drive Specific Functions
*/

#define HDISK


#include <stdlib.h>

#include "floppy.h"
#include "format.h"
#include "createfs.h"
#include "btstrct.h"
#include "hdisk.h"


void Get_Device_Parameters()
{
  unsigned long error_code=0;

  if(debug_prog==TRUE)
    {
    printf("[DEBUG]  Enter Get_Device_Parameters() function\n");
    }

  /* Get the device parameters for the logical drive */

  regs.h.ah=0x44;                     /* IOCTL Block Device Request          */
  regs.h.al=0x0d;
  regs.h.bl=param.drive_number + 1;
  regs.h.ch=0x08;                     /* Always 0x08...unless fs is FAT32                         */
  regs.h.cl=0x60;                     /* Get device parameters               */
  regs.x.dx=FP_OFF(&parameter_block);
  sregs.ds =FP_SEG(&parameter_block);
  parameter_block.use_current_bpb = 0;
  parameter_block.use_track_layout_fields = 0;
  parameter_block.all_sectors_same_size = 1;
  parameter_block.reserved = 0;

  intdosx(&regs, &regs, &sregs);

  error_code = regs.h.al;

  if (regs.x.cflag)
    {
    /* Add error trapping here */
    printf("\nCall to int 0x21, 0x440d 0x60, failed error %02x.\n", error_code);
    exit(1);
    }

  if(parameter_block.sectors_per_fat == 0)
    {
    /* Ok, this is assumed to be a FAT32 partition and it will be       */
    /* formatted as such.                                               */


    if(debug_prog==TRUE)
      {
      printf("[DEBUG]  FAT32 detected.\n");
      }

    param.fat_type = FAT32;

    /* Get the device parameters for the logical drive */

    regs.h.ah=0x44;                     /* IOCTL Block Device Request          */
    regs.h.al=0x0d;
    regs.h.bl=param.drive_number + 1;
    regs.h.ch=0x48;                     /* Always 0x48 for FAT32                         */
    regs.h.cl=0x60;                     /* Get device parameters               */
    regs.x.dx=FP_OFF(&parameter_block);
    sregs.ds =FP_SEG(&parameter_block);
    parameter_block.use_current_bpb = 0;
    parameter_block.use_track_layout_fields = 0;
    parameter_block.all_sectors_same_size = 1;
    parameter_block.reserved = 0;

    intdosx(&regs, &regs, &sregs);

    error_code = regs.h.al;

    if (regs.x.cflag)
      {
      /* Add error trapping here */
      printf("\nCall to int 0x21, 0x440d 0x60, failed error %02x.\n", error_code);
      exit(1);
      }

    }

  if(debug_prog==TRUE)
    {
    printf("[DEBUG]  Exit Get_Device_Parameters() function\n");
    }

}


void Get_DPB()
{
  if(param.fat_type!=FAT32)
    {
    regs.h.ah = 0x32;

    regs.h.dl = param.drive_number + 1;
    intdosx(&regs, &regs, &sregs);

/*
    if (regs.x.cflag )
	{
	    printf("INT 0x21/0x32/drive=%c failed\n", param.drive_number+'A');
	    exit(1);
	}
*/
    dpb = MK_FP(sregs.ds,regs.x.bx);

    segread(&sregs);                    /* restore defaults */
    }
  else
    {
    unsigned int drive_number = param.drive_number +1;

    unsigned int ext_param_block_seg = FP_SEG(&ext_parameter_block);
    unsigned int ext_param_block_off = FP_OFF(&ext_parameter_block);

    asm{
      mov ah,0x73
      mov al,0x02

      mov cx,0x003f

      mov dl,BYTE PTR drive_number

      mov es,WORD PTR ext_param_block_seg
      mov di,WORD PTR ext_param_block_off

      mov si,0xf1a6

      int 0x21

      }
/*
    regs.h.ah = 0x73;
    regs.h.al = 0x02;

    regs.h.ch = 0x00;
    regs.h.cl = 0x3f;

    regs.h.es = 0;
    regs.h.di = 0;

    regs.h.si = 0xf1a6;

    regs.h.dl = param.drive_number + 1;
    intdosx(&regs, &regs, &sregs);

/*
    if (regs.x.cflag )
	{
	    printf("INT 0x21/0x32/drive=%c failed\n", param.drive_number+'A');
	    exit(1);
	}
*/
//    dpb = MK_FP(sregs.ds,regs.x.bx);

//    segread(&sregs);                    /* restore defaults */
    }
}

void Set_DPB_Access_Flag()
{

  if(param.fat_type!=FAT32)
    {
    dpb->dpb_access_flag=0xff;

    regs.h.ah = 0x32;
    regs.h.dl = param.drive_number + 1;
    intdosx(&regs, &regs, &sregs);

    segread(&sregs);                    /* restore defaults */
    }
  else
    {
    unsigned char drive_no;

    unsigned dpb_format_struct_high;
    unsigned dpb_format_struct_low;

    struct
      {
      unsigned int  size;
      unsigned int  structure_version;
      unsigned long function_number;

      unsigned long field_1;
      unsigned long field_2;
      unsigned long field_3;
      unsigned long field_4;
      }dpb_for_format_structure;

    dpb_for_format_structure.size              = 0x18;
    dpb_for_format_structure.structure_version = 0x00;
    dpb_for_format_structure.function_number   = 0x02;
    dpb_for_format_structure.field_1           = 0x0;
    dpb_for_format_structure.field_2           = 0x0;
    dpb_for_format_structure.field_3           = 0x0;
    dpb_for_format_structure.field_4           = 0x0;

    drive_no=param.drive_number + 1;

    dpb_format_struct_high = FP_SEG(&dpb_for_format_structure);
    dpb_format_struct_low  = FP_OFF(&dpb_for_format_structure);

    asm{
      mov ax,0x7304
      mov dl,BYTE PTR drive_no
      mov es,dpb_format_struct_high
      mov di,dpb_format_struct_low

      int 0x21

      }

    }
}

void Set_Hard_Drive_Media_Parameters()
{
  int index;
  int result;

  unsigned long number_of_sectors;

  param.media_type=HD;
  param.fat_type=FAT12;

  Get_Device_Parameters();
  Get_DPB();

  if(parameter_block.total_sectors!=0)
    {
    number_of_sectors = parameter_block.total_sectors;
    }
  else
    {
    number_of_sectors =  parameter_block.large_sector_count_high;
    number_of_sectors =  number_of_sectors<<16;
    number_of_sectors += parameter_block.large_sector_count_low;
    }

  drive_specs[HD].total_logical_sectors=number_of_sectors;

  /* Copy the returned BPB information into drive_specs. */
  memcpy(&drive_specs[HD].bytes_per_sector,&parameter_block.bytes_per_sector,25);

  /* Copy BPB into bpb_standard structure. */
  memcpy(&bpb_standard,&parameter_block.bytes_per_sector,25);

  /* If the file system is FAT32, copy the FAT32 BPB extension into     */
  /* bpb_fat32_ext.                                                     */
  if(param.fat_type==FAT32)
   memcpy(&bpb_fat32_ext,&parameter_block.sectors_per_fat_low,27);

  Get_FS_Info();
  if( (param.fat_type!=FAT32) && (file_sys_info.total_clusters>=4085) )
   param.fat_type=FAT16;

  param.size=number_of_sectors/2048;

  drive_statistics.bytes_total_disk_space
   =((unsigned long)drive_specs[param.media_type].bytes_per_sector*number_of_sectors)
   -((1+(2*(unsigned long)drive_specs[param.media_type].sectors_per_fat)
   +((unsigned long)drive_specs[param.media_type].root_directory_entries/16))
   *(unsigned long)drive_specs[param.media_type].bytes_per_sector);
  drive_statistics.bytes_available_on_disk
   =drive_statistics.bytes_total_disk_space;

  drive_statistics.bytes_in_each_allocation_unit
   =(unsigned long)drive_specs[param.media_type].sectors_per_cluster
   *(unsigned long)drive_specs[param.media_type].bytes_per_sector;
}

⌨️ 快捷键说明

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