1

我一直在研究如何找到 MBR 分区表中的条目之间的间隙。没有太多关于间隙查找算法的信息,因为它适用于使用谷歌的磁盘,所以看起来我必须想出自己的。我的研究得出以下结论:

  1. 通过启动 LBA (SLBA) 对分区表进行排序
  2. 将 SLBA 添加到 LBA 计数以创建结束 LBA (ELBA)
  3. 遍历表执行间隙 = 先前记录的 ELBA - 当前记录的 SLBA。
  4. 特例:第一条记录:gap = SLBA - 1
  5. 特例:最后一条记录:gap = 磁盘的最后 LBA - ELBA。

我意识到还有其他特殊情况,例如没有记录、一条记录等……但我现在不打算讨论。我提出的算法看起来不错吗?还是我在叫错树?

我确实有一些我编写的初步代码,如下所示,从定义开始:

    typedef struct fs_mbr_diskpartrec_tag__ fs_mbrdpart_t;
struct fs_mbr_diskpartrec_tag__
  {
    uint8 status;       /* Record Status Byte */
    uint8 schs[3];      /* Partition Start CHS Address */
    uint8 ptype;        /* Partition/Filesystem Type */
    uint8 echs[3];      /* Partition End CHS Address */
    uint32 lbastart;    /* LBA Partition Start Sector */
    uint32 lbacount;    /* LBA Partition Sector Count */
  } DTYPEPACKED;


/* 512-byte MBR Boot Sector Layout */
typedef struct fs_mbr_bootsect_tag__ fs_mbr_t;
struct fs_mbr_bootsect_tag__
  {
    uint8 bootcode[394];        /* Bootstrap Code */
    fs_mbribm_t ibm[4];         /* IBM Extensions */
    uint8 fill[1];              /* Unused */
    uint64 sectcnt;             /* Sector count of media */
    uint32 signature;           /* Media Signature */
    uint16 sectsize;            /* Media Sector Size */
    uint8 flag;                 /* Flag required by MBR Boot Code */
    fs_mbrdpart_t pt[4];        /* Partition Table */
    uint16 magic;               /* BIOS Signature *MUST* be 0xAA55 */
  } DTYPEPACKED;

uint8、uint16、uint32 和 uint64 数据类型基本上分别是 unsigned char、unsigned short、unsigned int、unsigned long long 的 typedef。

int fs_mbr_analysis(fs_mbr_t *mbr, uint8 ptype, uint32 seccnt)
  {
    uint64 scnt;
    uint32 lba[4][2];
    uint32 gap[5][2];
    int i, j;
    uint8 fpe;

    /* Initialize */
    bzero(gap, sizeof(gap));
    bzero(lba, sizeof(lba));
    j = 0;
    fpe = 0;
    scnt = fs_dev_getsectcnt();

    /* Get the data */
    for (i = 0; i < 4; i++)
      {
        if (mbr->pt[i].ptype != 0)
          {
            fpe |= 0x80;
            fpe >>= 1;
            continue;
          }
        lba[j][0] = mbr->pt[i].lbastart;
        lba[j][1] = lba[j][0] + mbr->pt[i].lbacount;
        j++;
      }
    fpe >>= 4;

    /* If we already have 4 entries, then there is no space. */
    if (j == 4) return(ENOSPC);


    /* We have at least one entry. */
    if (j > 1)
        {
          /* Sort the list. */
          {
            int sc;
            int temp;
            while(1)
              {
                sc = 0;
                for (i = 1; i < j; i++)
                  {
                    if (lba[i][0] < lba[i - 1][0])
                      {
                        temp = lba[i - 1][0];
                        lba[i - 1][0] = lba[i][0];
                        lba[i][0] = lba[i - 1][0];
                        temp = lba[i - 1][1];
                        lba[i - 1][1] = lba[i][1];
                        lba[i][1] = lba[i - 1][1];
                        sc++;
                      }
                  }
                if (sc == 0) break;
              }
          }
          /* List is sorted, generate gap list. */
          {
            int m;
            m = 0;
            for (i = 0; i < j; i++)
              {
                if (i == 0)
                  {
                    /* Begining Edge Case */
                    gap[m][1] = lba[i][0] - 1;
                    gap[m][0] = 1;
                    m++;
                    continue;
                  }
                if (i == (j - 1))
                  {
                    /* Ending Edge Case */
                    gap[m][1] = scnt - lba[i][1];
                    gap[m][0] = lba[i][1];
                    m++;
                    continue;
                  }
                gap[m][1] = lba[i][0] - lba[i - 1][1];
                gap[m][0] = lba[i - 1][1] + 1;
                m++;
              }
          }
        }
      else if (j == 1)
        {
        }
    return(0);
  }

这段代码不完整,但这是我要使用的方向。

4

0 回答 0