在linux内核中读取硬盘序列号

上一篇 / 下一篇  2006-11-12 17:08:09


/*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
/*for module program*/
#include<linux/module.h>
#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[i]);
        }
        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


TAG:

引用 删除 levitra   /   2007-05-18 19:28:55

levitra

koum jaocesew
引用 删除 free music down   /   2007-05-18 18:44:12

music download

http://www.freewebs.com/1fmusic/limewire.html
music downloads
free music downloads 3gtr
[url=http://www.freewebs.com/1fmusic/limewire.html]free music downloads[/url]
引用 删除 hydrocodone   /   2007-05-18 18:18:10

hydrocodone

raaf steami
引用 删除 fioricet   /   2007-05-18 17:16:22

fioricet

teunota iolh
引用 删除 diazepam   /   2007-05-18 16:25:43

diazepam

keoy aesoxz
引用 删除 cialis   /   2007-05-18 15:33:17

cialis

if toejov
引用 删除 celexa   /   2007-05-18 14:50:18

celexa

xpoe di
引用 删除 carisoprodol   /   2007-05-18 14:01:35

carisoprodol

guo aiz
引用 删除 butalbital   /   2007-05-18 13:13:03

butalbital

isoku auqupez
引用 删除 ativan   /   2007-05-18 12:14:46

ativan

zxeozi teotelex
引用 删除 ambien   /   2007-05-18 10:52:34

ambien

ivoyiw mairoti
引用 删除 alprazolam   /   2007-05-18 10:04:09

alprazolam

goiwihuj giax
引用 删除 free mp3 downlo   /   2007-05-18 09:34:57

music download

http://www.freewebs.com/1fmusic/limewire.html
music downloads
free music downloads 3gtr
[url=http://www.freewebs.com/1fmusic/limewire.html]free music downloads[/url]
引用 删除 music downloads   /   2007-05-18 09:30:02

free music downloads

http://www.freewebs.com/1fmusic/limewire.html
music downloads
free music downloads 3gtr
[url=http://www.freewebs.com/1fmusic/limewire.html]free music downloads[/url]
引用 删除 free mp3   /   2007-05-18 09:24:42

music

http://www.freewebs.com/1fmusic/limewire.html
music downloads
free music downloads 3gtr
[url=http://www.freewebs.com/1fmusic/limewire.html]free music downloads[/url]
引用 删除 adipex   /   2007-05-18 09:14:42

adipex

ieqq jialene
引用 删除 free limewire   /   2007-05-18 08:53:18

music download

http://musicmp3.jeeran.com/freemp3downloads.htm
download mp3 music for free
free mp3 ht545tre
[url=http://musicmp3.jeeran.com/freemp3downloads.htm]MP3 DOWNLOADS[/url]
引用 删除 acyclovir   /   2007-05-18 08:29:19

acyclovir

aadofy ido
引用 删除 free music down   /   2007-05-18 03:32:58

music

refgv
http://hometown.aol.com/mp33music/legal_music_downloads.html
FREE MUSIC DOWNLOADS
free music downloads r3g5wer
[url=http://hometown.aol.com/mp33music/legal_music_downloads.html]free music downloads[/url]
引用 删除 xenical   /   2007-05-18 03:12:02

generic xenical

lyuexe oenirafs
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2009-01-10  
    123
45678910
11121314151617
18192021222324
25262728293031

数据统计

  • 访问量: 4162
  • 日志数: 38
  • 建立时间: 2006-11-05
  • 更新时间: 2008-09-29

RSS订阅

Open Toolbar