打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
格式化扩展分区
int CDiskFormat::DiskFormatPartOneExtendPart(U32 disk_type, U32 disk_id, U32 extend_part_id)
{
int ret;
U32 rec_logic_part_cnt;
U64 extent_rec_sector_cnt;
U64 extend_part_sector_num;
U32 sectors_per_cylinder;

    /// @brief 每柱面扇区数
sectors_per_cylinder = m_pDiskBase->GetDiskSetorsPerCylinder(disk_type, disk_id);
if ((int)sectors_per_cylinder == -1)
return -1;

    /// @brief 每分区扇区数
extend_part_sector_num = m_pDiskBase->GetDiskSectorNumBySpecificPart(disk_type, disk_id, extend_part_id);
if ((long long)extend_part_sector_num == -1)
return -1;

if (disk_type == DISK_TYPE_HD)
extent_rec_sector_cnt = extend_part_sector_num - ADV_PART_SECTOR_COUNT ;
else
extent_rec_sector_cnt = extend_part_sector_num; /* 扩展分区的所有扇区数, /dev/sda1 */

rec_logic_part_cnt = DiskFomatGetRecordPartNum(disk_type, extent_rec_sector_cnt);
//LOG_DEBUG_MSG_ERROR("", "part_total_sector_cnt=%llu  rec_logic_part_cnt =%u\n",
// extend_part_sector_num, rec_logic_part_cnt);

//逻辑分区的扇区总数, 以sectors_per_cylinder扇区数对齐, sd卡目前是16(8192 Bytes对齐)
U64 one_logic_part_total_sector_cnt_include_mbr = extent_rec_sector_cnt / rec_logic_part_cnt ;

    /// @brief 逻辑分区总扇区数,整理成每柱面扇区数的整数倍。
one_logic_part_total_sector_cnt_include_mbr = one_logic_part_total_sector_cnt_include_mbr -
one_logic_part_total_sector_cnt_include_mbr % sectors_per_cylinder;

//扩展分区的起始扇区号
U64 abs_ext_part_start_sector_idx = m_pDiskBase->GetDiskPartStartSectorID(disk_type, disk_id, extend_part_id);
if ((long long)abs_ext_part_start_sector_idx == -1)
return -1;

//逻辑分区MBR起始扇区号
U64 abs_current_logic_mbr_start = abs_ext_part_start_sector_idx ;

//获取MBR的头部扇区的大小
U64 curr_mbr_head_sector_offset = 0 ;
U64 next_mbr_head_sector_offset = 0 ;

//写扩展分区的逻辑分区
int i ;
tMBR_ITEM *ptMbr ;
int part_num ;
U8 szMbrBuf[SECTOR_SIZE];
if (disk_type == DISK_TYPE_HD )
part_num = rec_logic_part_cnt + 1 ;
else
part_num = rec_logic_part_cnt ;

    /// @brief SD 卡 一般为一个逻辑分区
for(i = 0; i < part_num; i++)
{
memset( szMbrBuf, 0 , sizeof( szMbrBuf ) ) ;
ptMbr = (tMBR_ITEM *)( szMbrBuf + MBR_TABLE_START_OFFSET ) ;
ptMbr->active_id = 00 ;
ptMbr->start_head = 0xFE ;
ptMbr->start_sector = 0xFF ;
ptMbr->start_cylinder = 0xFF ;
#if (SUPPORT_NTFS_FILE_SYSTEM == 1)
ptMbr->part_type = NTFS_VOLUME_SET_86 ; //NTFS标志
#elif (SUPPORT_FAT32_FILE_SYSTEM == 1)
        ptMbr->part_type = WIN95_FAT32 ;        //FAT32标志
#endif
ptMbr->end_head = 0xFE ;
ptMbr->end_sector = 0xFF ;
ptMbr->end_cylinder = 0xFF ;

//重要参数: 1 起始扇区号, 2 分区的总扇区数
ptMbr->part_start_sector_offset = sectors_per_cylinder;
if ((disk_type == DISK_TYPE_HD ) && (i == (part_num - 1)))
ptMbr->part_total_sector_cnt = ADV_PART_SECTOR_COUNT -
curr_mbr_head_sector_offset - sectors_per_cylinder;
else
/// @brief 分区总扇区数
ptMbr->part_total_sector_cnt = one_logic_part_total_sector_cnt_include_mbr -
curr_mbr_head_sector_offset - sectors_per_cylinder;

if (i == (part_num - 1))
{
ptMbr++ ;

ptMbr->active_id = 00 ;
ptMbr->start_head = 0x00 ;
ptMbr->start_sector = 0x00 ;
ptMbr->start_cylinder = 0xFF ;
ptMbr->part_type = 0x00 ;
ptMbr->end_head = 0x00 ;
ptMbr->end_sector = 0x00 ;
ptMbr->end_cylinder = 0x00 ;
ptMbr->part_start_sector_offset = 0 ;
ptMbr->part_total_sector_cnt = 0 ;
}
else
{
//查找下一逻辑分区MBR的位置
U64 search_sector_num ;
if ((disk_type == DISK_TYPE_HD) && (i == (part_num - 2)))
search_sector_num = ADV_PART_SECTOR_COUNT ;
else
search_sector_num = one_logic_part_total_sector_cnt_include_mbr ;

ptMbr++ ;

ptMbr->active_id = 00 ;
ptMbr->start_head = 0xFE ;
ptMbr->start_sector = 0xFF ;
ptMbr->start_cylinder = 0xFF ;
ptMbr->part_type = WIN95_EXTENDED ;
ptMbr->end_head = 0xFE ;
ptMbr->end_sector = 0xFF ;
ptMbr->end_cylinder = 0xFF ;
ptMbr->part_start_sector_offset = one_logic_part_total_sector_cnt_include_mbr * ( i + 1 ) ;
ptMbr->part_total_sector_cnt = search_sector_num - sectors_per_cylinder ;
}

szMbrBuf[510] = 0x55 ;
szMbrBuf[511] = 0xAA ;

//写入逻辑分区的MBR扇区
ret = m_pDiskBase->WriteDiskSectorByType(disk_type, disk_id, abs_current_logic_mbr_start, 1, szMbrBuf);
if (ret != 0)
return ret;

//清除MBR后面的保留扇区和DBR扇区的数据
U32 j ;
memset( szMbrBuf, 0 , sizeof( szMbrBuf ) ) ;
for( j = 0 ; j < sectors_per_cylinder ; j++ )
{
ret = m_pDiskBase->WriteDiskSectorByType(disk_type, disk_id, (abs_current_logic_mbr_start + 1 + j) , 1, szMbrBuf ) ;
if (ret != 0)
return ret;
}

//设置分区在内存的参数
U32 current_disk_total_part_num = m_pDiskBase->GetDiskTotalPartNum(disk_type, disk_id);
if ((int)current_disk_total_part_num == -1)
return -1;

        /* MBR起始扇区号 */
m_pDiskBase->SetDiskPartMbrSectorID(disk_type, disk_id, current_disk_total_part_num,
                      abs_ext_part_start_sector_idx +
  one_logic_part_total_sector_cnt_include_mbr * i
  + curr_mbr_head_sector_offset);

        /* 分区起始扇区号 */
m_pDiskBase->SetDiskPartStartSectorID(disk_type, disk_id, current_disk_total_part_num,
  abs_ext_part_start_sector_idx +
  one_logic_part_total_sector_cnt_include_mbr * i
  + curr_mbr_head_sector_offset + sectors_per_cylinder);

        /* 分区总扇区数 */
if ((disk_type == DISK_TYPE_HD) && (i == (part_num - 1)))
m_pDiskBase->SetDiskPartTotalSectorCnt(disk_type, disk_id, current_disk_total_part_num,
                         ADV_PART_SECTOR_COUNT -
     curr_mbr_head_sector_offset - sectors_per_cylinder);
else
m_pDiskBase->SetDiskPartTotalSectorCnt(disk_type, disk_id, current_disk_total_part_num,
                         one_logic_part_total_sector_cnt_include_mbr -
     curr_mbr_head_sector_offset - sectors_per_cylinder);

U32 disk_logic_part_index_id = m_pDiskBase->GetDiskPartLogicPartIndexID(disk_type, disk_id);
if ((int)disk_logic_part_index_id == -1)
return -1;
        /* 逻辑分区从5开始 */
m_pDiskBase->SetDiskPartID(disk_type, disk_id, current_disk_total_part_num, disk_logic_part_index_id);

        /* 暂存逻辑ID号 */
disk_logic_part_index_id++;
m_pDiskBase->SetDiskPartLogicPartIndexID(disk_type, disk_id, disk_logic_part_index_id);

        /* 逻辑分区名字 /dev/sda5 */
m_pDiskBase->SetDiskPartName(disk_type, disk_id, current_disk_total_part_num);

        /* 逻辑分区文件系统类型 */
#if (SUPPORT_NTFS_FILE_SYSTEM == 1)
m_pDiskBase->SetDiskPartType(disk_type, disk_id, current_disk_total_part_num, NTFS_VOLUME_SET_86);
#elif (SUPPORT_FAT32_FILE_SYSTEM == 1)
        m_pDiskBase->SetDiskPartType(disk_type, disk_id, current_disk_total_part_num, WIN95_FAT32);
#endif

        /* 逻辑分区用途 */
if ((disk_type == DISK_TYPE_HD) && (i == (part_num - 1)))
m_pDiskBase->SetDiskPartUseType(disk_type, disk_id, current_disk_total_part_num, PART_USE_TYPE_ADV);
else
m_pDiskBase->SetDiskPartUseType(disk_type, disk_id, current_disk_total_part_num, PART_USE_TYPE_REC);

        /* 逻辑分区的扩展分区索引, 表明该逻辑分区属于哪一个扩展分区 */
m_pDiskBase->SetDiskPartExtendPartID(disk_type, disk_id, current_disk_total_part_num, extend_part_id);

        /* 分区状态 */
m_pDiskBase->SetDiskPartStatus(disk_type, disk_id, current_disk_total_part_num, PART_STATUS_DBR_INVLAID_WARRING);

U32 disk_logic_part_num = m_pDiskBase->GetDiskLogicPartNum(disk_type, disk_id);
if ((int)disk_logic_part_num == -1)
return -1;

        /* 逻辑分区号, 表示第几个逻辑分区, 从0开始 */
m_pDiskBase->SetDiskLogicPartIndexID(disk_type, disk_id, current_disk_total_part_num, disk_logic_part_num);

        /* 逻辑分区数目 */
disk_logic_part_num++;
m_pDiskBase->SetDiskLogicPartNum(disk_type, disk_id, disk_logic_part_num);

        /* 总的磁盘分区数目 */
current_disk_total_part_num++;
m_pDiskBase->SetDiskTotalPartNum(disk_type, disk_id, current_disk_total_part_num);

abs_current_logic_mbr_start = abs_current_logic_mbr_start + one_logic_part_total_sector_cnt_include_mbr + next_mbr_head_sector_offset ;
curr_mbr_head_sector_offset = next_mbr_head_sector_offset ;
}

    /* 扩展分区的状态, /dev/sda1--->PART_STATUS_NOT_INIT */
m_pDiskBase->SetDiskPartStatus(disk_type, disk_id, extend_part_id, PART_STATUS_NOT_INIT);
return 0 ;
}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Grubinst使用介绍 - Grub4Dos Wiki
Save And Restore Boot Sector or any Sector of Any Disk (Even Removeable)
硬盘物理坏道屏蔽两例
系统引导过程及硬盘分区结构论述 | LinuxSir.Org
SSD硬盘咋分区?专家告诉你最佳分区实践
硬盘分区与故障排解手册(上)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服