QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2095|回复: 2

linux下如何取得ide硬盘的物理序列号?

[复制链接]
发表于 2003-10-22 16:44:41 | 显示全部楼层 |阅读模式
有谁知道在linux内核中如何取得ide硬盘的序列号(假设
硬盘有物理序列号)?
 楼主| 发表于 2003-10-23 15:21:47 | 显示全部楼层
我在内核中写了这样的函数,没有系统,还没有试,请大家评一下:
/*hardid.c*/
#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_fs_sb.h>
#include <linux/nfs_mount.h>
#include <linux/genhd.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/mc146818rtc.h>
#include <linux/smp_lock.h>

#include "ide.h"

#ifdef MODULE
#include <linux/module.h>        /*for module program*/
#endif

#include <asm/processor.h>
#include <asm/page.h>
#include <asm/smp.h>
#include <asm/param.h>
#include <asm/fcntl.h>
#include <asm/uaccess.h>

/*HDIO_GET_IDENTITY*/
ide_drive_t *topsec_get_info_ptr(kdev_t i_rdev)
{
        int                major = MAJOR(i_rdev);
        unsigned int        h;

        for (h = 0; h < MAX_HWIFS; ++h)
        {
                ide_hwif_t  *hwif = &ide_hwifs[h];
                if (hwif->present && major == hwif->major)
                {
                        unsigned unit = DEVICE_NR(i_rdev);
                        if (unit < MAX_DRIVES)
                        {
                                ide_drive_t *drive = &hwif->drives[unit];
                                if (drive->present)
                                {
                                        return drive;
                                }
                        }
                        break;
                }
        }
        return NULL;
}

#define                HARDDISK        "/dev/hda"

int topsec_get_hardisk_id(char* buffer)
{
        int len=0;
        struct file *filp=NULL;
        struct inode *inode;
        ide_drive_t *drive;

        filp= filp_open((char*)HARDDISK, 0, 0);
        if(filp)
        {
                inode=filp->f_dentry->d_inode;
                if (!inode || !(inode->i_rdev))
                {
                        return -EINVAL;
                }
                if ((drive = topsec_get_info_ptr(inode->i_rdev)) == NULL)
                {
                        return -ENODEV;
                }
                memcpy(buffer+len,(char *)drive->id->serial_no,20);
                len+=20;
/*                memcpy(buffer+len,(char *)drive->id,sizeof(*drive->id));
                len+=sizeof(*drive->id);*/
                filp_close(filp, NULL);

                return len;
        }

        return 0;
}

#ifdef MODULE

int init_module(void)
{
        char*        buffer=NULL;
        int        i=0,len=0;

        lock_kernel();
        buffer=(char*)kmalloc(512);
        memset(buffer,0,512);
                len=topsec_get_hardisk_id(buffer);
                if(len>0)
                {
                        printk("harddisk serial no is:\n");
                        for(;i<len;i++)
                        {
                                 printk("%x",buffer);
                         }
                         printk("\n");
                }
        kfree(buffer);
        unlock_kernel();

        return 0;/*for module,it must return 0*/
}

void cleanup_module(void)
{

}
#endif


make文件如下:
DFLAGS= -D __KERNEL__ -D MODULE -D __DEBUG__
CFLAGS= -O2 -g -Wall -Wstrict-prototypes -pipe -I/usr/src/linux/drivers/block -I/usr/include/linux/

hardid.o : hardid.c
        gcc -c hardid.c $(DFLAGS) $(CFLAGS) -o hardid.o

clean:
        rm -f *.o

然后:
insmod hardid.o
即可

清除可:
rmmod hardid
make clean
回复

使用道具 举报

发表于 2003-10-23 21:12:36 | 显示全部楼层
no sure if that number is serial number or model number. u can check the kernel code to confirm it.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-9-28 04:17 , Processed in 0.064679 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表