|
楼主 |
发表于 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 |
|