磁盘不包含有效的分区表

时间:2016-06-01 10:04:24

标签: c linux-kernel linux-device-driver

我为PCI卡写了一个PCI驱动程序。我想在尝试进行分区之前挂载文件系统。但是,当我执行fdisk -l时,它会显示我的设备:

  

磁盘/dev/dor不包含有效的分区表

我的分区代码是:

#include <asm/io.h>
#include <linux/string.h>
/*
* This is critical part of our code, here we are doing set-up so that user can implement any type of file system.
* Here we ary logically treating our device as a Disk having some Heads,Cylinders,and Secotrs
* By default each sector size is 512 here we have taken 512 as default if you wish you can modify as per your 
* requirement you just need to use some system API.
* For any Storage device first sector i.e sector0 called as boot sector ,this boot sector consist of boot information
* and partion table.We are not interested in boot information ,we are intereste in partion table.
*/
#include "partition.h"

#define SECTOR_SIZE 512
#define MBR_SIZE SECTOR_SIZE
#define MBR_DISK_SIGNATURE_OFFSET 440
#define MBR_DISK_SIGNATURE_SIZE 4
#define PARTITION_TABLE_OFFSET 446
#define PARTITION_ENTRY_SIZE 8 // sizeof(PartEntry)
#define PARTITION_TABLE_SIZE 32 // sizeof(PartTable)
#define MBR_SIGNATURE_OFFSET 510
#define MBR_SIGNATURE_SIZE 2
#define MBR_SIGNATURE 0xAA55
#define BR_SIZE SECTOR_SIZE
#define BR_SIGNATURE_OFFSET 510
#define BR_SIGNATURE_SIZE 2
#define BR_SIGNATURE 0xAA55
/*
* Total Partion table start from 446 bytes location system support 64 bytes for partion table in that we can have four 
* actual partion table haing 16 bytes each size.
* we will explain this 16 bytes as follows
*1st byte-Boot_type....0x00 for inactive and 0x80 for active 
*in our case it is 0x00 because we already tlod you we are not  interested in booting OS from our device.
*2nd byte-Start of head  you can have maximum 256 head on your disk
*3rd byte-out of this 8 bit only 6 bits are for sector so you can have 64 maximum sector
*4th byte-this 8 bit + 2 most significant bits from 3rd byte==10 bits are for cylinder so you can have 1024 maximum cylinder
*5th byte-this byte is for patition type as we are interested in single partion of 32MB we are noot extending our partion
*if you wish you can there will we different vale for this particular type
*6th bytes-end of head
*7th bytes-out of 8 bit 6 bit= end of sectors
*8th btes 8+2 bits end of cylinder
*4 bytes for start of sector no of this partion table
*4 bytes for total no of sector which are in this partition 
*According to above we have made one structure pf PartEntry type which has all these above mention member of appropriate size
*/
typedef struct {
    unsigned char boot_type; // 0x00 - Inactive; 0x80 - Active (Bootable)
    unsigned char start_head;
    unsigned char start_sec:6;
    unsigned char start_cyl_hi:2;
    unsigned char start_cyl;
    unsigned char part_type;
    unsigned char end_head;
    unsigned char end_sec:6;
    unsigned char end_cyl_hi:2;
    unsigned char end_cyl;
    unsigned int abs_start_sec;
    unsigned int sec_in_part;
} PartEntry;

typedef PartEntry PartTable[4];

static PartTable def_part_table = {
    {
        boot_type: 0x00,
        start_head: 0x00,
        start_sec: 0x2,             //first starting sector
        start_cyl: 0x00,            //first starting cylinder
        start_cyl_hi:0x00,
        part_type: 0x83,            //type of partition
        end_head: 0x00,     
        end_sec: 0x3F,              //end of sector
        end_cyl: 0xFE,              //end of cylinder
        end_cyl_hi:0x03,
        abs_start_sec: 0x00000001,
        sec_in_part: 0x0000FB90
    },
    {}
};
/* In this copy_mbr method we are actually storing our partion table information on boot sector i.e sector0 */
//Bellow one is set up for partition information
static void copy_mbr(void __iomem *disk) {
    printk("inside partition table setup\n");
    memset_io(disk, 0x0, MBR_SIZE);

    //*(unsigned long *)(disk + MBR_DISK_SIGNATURE_OFFSET) = 0x36E5756D;
    unsigned char sign[]={0x6D,0x75,0xE5,0x36};
    memcpy_toio(disk+MBR_DISK_SIGNATURE_OFFSET,sign,4);

    memcpy_toio(disk + PARTITION_TABLE_OFFSET, &def_part_table, PARTITION_TABLE_SIZE);

    //*(unsigned short *)(disk + MBR_SIGNATURE_OFFSET) = MBR_SIGNATURE;
    iowrite16(MBR_SIGNATURE,disk+MBR_SIGNATURE_OFFSET);
}
void partition_setup(void __iomem *disk) {
    copy_mbr(disk);
}

0 个答案:

没有答案
相关问题