汇编读取PCI配置空间

学习PCI:http://blog.sina.com.cn/s/articlelist_1685243084_3_1.html

  1 ;------------------------------------------------
  2 ;
  3 ;程序功能: 读取PCI 配置信息,存入文件zpci_config.txt
  4 ;         通过端口CF8h / CFCh 来读取
  5 ;运行环境: DOS + MASM5
  6 ;时间: 2015/08/25
  7 ;
  8 ;----------------------------自定义宏结构-------------------
  9 ;功    能:在文件中换行
 10 nextrow macro
 11     mov buffer  ,0dh
 12     mov buffer+1,0ah
 13     mov dx,offset buffer
 14     mov cx,2
 15     mov ah,40h
 16     int 21h
 17     endm
 18 ;功能:把ascii 表示的字符写入文件
 19 tofile macro ascii
 20     mov buffer,ascii
 21     mov dx,offset buffer
 22     mov cx,1
 23     mov ah,40h
 24     int 21h
 25     endm
 26 ;------------------------------------------------
 27     .386P
 28 ;-------------------------------------------------
 29 dseg segment use16
 30     busnum dw 0000h  ;总线号0 - 00FFh
 31     devnum dw 001fh  ;设备号0 - 001Fh
 32     funnum dw 0007h  ;功能号0 - 0007h
 33     regnum dw 00ffh  ;寄存器0 - 00FFh
 34     ;
 35     config_addr dd 00000000h      ;用来暂存eax中的地址
 36     buff_num db ‘bus:device:function:‘
 37     ;
 38     config_data dd 00000000h      ;用来存放eax中的pci数据
 39     fname db ‘\zpci_config.txt‘,0 ;文件名
 40     buffer db 2
 41 dseg ends
 42 ;----------------------------------------------------
 43 ;----------------------------------------------------
 44 cseg segment use16
 45 assume cs:cseg, ds:dseg
 46 start:
 47     mov ax,dseg
 48     mov ds,ax
 49     ;
 50     mov dx,offset fname
 51     mov cx,0      ;common file
 52     mov ah,3ch    ;creat file
 53     int 21h
 54     ;
 55     mov bx,ax     ;save file handle
 56     ;
 57     mov busnum,0000h
 58     mov devnum,0000h
 59     mov funnum,0000h
 60     mov regnum,0000h
 61 ;-----------------------------------------
 62     call print_num    ;打印busnum:devnum:funnum = 00:00:00
 63     nextrow           ;换行
 64 nextreg:
 65     call pci_read     ;读取pci 配置空间
 66     cmp regnum,00h
 67     jnz  continue     ;判断不是第一个寄存器
 68     cmp ax,0ffffh     ;判断设备是否存在
 69     jz nextfun        ;不存在,跳到下一个fun
 70 continue:
 71     call writefile
 72     add regnum,4      ;只能每次读4个寄存器
 73     cmp regnum,00ffh  ;判断
 74     ja nextfun        ;256B 已读完,跳到下一个function
 75     jmp nextreg       ;否则,读下一个reg
 76 nextfun:
 77     nextrow
 78     ;
 79     mov regnum,0000h
 80     inc funnum
 81     cmp funnum,0007h
 82     ja nextdev        ;funnum 大于 7,跳到下一个dev
 83     call print_num
 84     nextrow
 85     jmp nextreg
 86 nextdev:
 87     mov regnum,0000h
 88     mov funnum,0000h
 89     inc devnum
 90     cmp devnum,001fh
 91     ja nextbus      ;devnum 大于 1fh,跳到下一个bus
 92     call print_num
 93     nextrow
 94     jmp nextreg
 95 nextbus:
 96     mov regnum,0000h
 97     mov funnum,0000h
 98     mov devnum,0000h
 99     inc busnum
100     cmp busnum,0005h
101     ja endd           ;busnum 大于5,跳到结束
102     call print_num
103     nextrow
104     jmp nextreg
105 ;-----------------------结束------------------------
106 endd:
107     mov ah,3eh   ;close file
108     int 21h
109     ;
110     mov ah,4ch   ;return DOS
111     int 21h
112 ;---------------------------------------------------
113 ;--------------------------------------------------
114 ;函数功能:打印busnum:devnum:funnum
115 print_num proc
116     mov config_addr,eax   ;保护eax中的地址
117     ;------------------------------------
118     mov dx,offset buff_num
119     mov cx,20
120     mov ah,40h
121     int 21h
122     ;----------busnum------------
123     mov ax,busnum
124     push ax
125     shr al,4
126     call toascii
127     tofile al
128     pop ax
129     call toascii
130     tofile al
131     tofile 2Dh
132     ;----------devnum----------
133     mov ax,devnum
134     push ax
135     shr al,4
136     call toascii
137     tofile al
138     pop ax
139     call toascii
140     tofile al
141     tofile 2Dh
142     ;-----------funnum---------
143     mov ax,funnum
144     push ax
145     shr al,4
146     call toascii
147     tofile al
148     pop ax
149     call toascii
150     tofile al
151     ;-----------
152     mov eax,config_addr    ;恢复eax
153     ret
154 print_num endp
155 ;------------------------------------------------------
156 ;---------------------- writefile ----------------------------
157 ;函数功能: 把eax 中的值写入文件
158 ;入口参数: eax
159 ;出口参数: 无
160 ;所用寄存器和存储单元:ebx,ecx,edx
161 writefile proc
162     mov config_data,eax   ;用config_data暂存eax中的pci数据
163     ;--------第一个字节-----
164     push eax
165     shr al,4
166     call toascii
167     tofile al
168     pop eax
169     call toascii
170     tofile al
171     tofile 20h
172     ;--------第二个字节------
173     mov eax,config_data
174     shr eax,8
175     ;
176     push eax
177     shr al,4
178     call toascii
179     tofile al
180     pop eax
181     call toascii
182     tofile al
183     tofile 20h
184     ;--------第三个字节-------
185     mov eax,config_data
186     shr eax,16
187     ;
188     push eax
189     shr al,4
190     call toascii
191     tofile al
192     pop eax
193     call toascii
194     tofile al
195     tofile 20h
196     ;--------第四个字节---------
197     mov eax,config_data
198     shr eax,24
199     ;
200     push eax
201     shr al,4
202     call toascii
203     tofile al
204     pop eax
205     call toascii
206     tofile al
207     tofile 20h
208     ret
209 writefile endp
210 ;---------------------------------------------------
211 ;-----------------------toascii---------------------------
212 ;子程序名: toascii
213 ;功            能: 把al的低4位的值转成ascii码,存入al
214 ;入口参数: al
215 ;出口参数: al
216 toascii proc
217     and al,0fh
218     add al,90h
219     daa
220     adc al,40h
221     daa
222     ret
223 toascii endp
224 ;----------------------------------------------------
225 ;----------------------pci_read---------------------------
226 ;子程序名: pci_read
227 ;功            能: 根据eax中的地址读取pci的配置空间,并存入eax
228 ;入口参数: busnum、devnum、funnum、regnum
229 ;出口参数: eax
230 ;
231 pci_read proc
232     ;protect register
233     push ebx
234     push dx
235     ;clear
236     xor eax,eax
237     xor ebx,ebx
238     ;enable
239     add eax,1h
240     shl eax,31
241     ;bus number
242     mov ebx,ds:[00]
243     and ebx,0ffh
244     shl ebx,16
245     add eax,ebx
246     ;device number
247     xor ebx,ebx
248     mov ebx,ds:[02]
249     and ebx,0ffh
250     shl ebx,11
251     add eax,ebx
252     ;function number
253     xor ebx,ebx
254     mov ebx,ds:[04]
255     and ebx,0ffh
256     shl ebx,8
257     add eax,ebx
258     ;register
259     xor ebx,ebx
260     mov ebx,ds:[06]
261     and ebx,0ffh
262     add eax,ebx
263     ;read IO
264     mov dx,0cf8h
265     out dx,eax
266     mov dx,0cfch
267     in eax,dx
268     ;resume register
269     pop dx
270     pop ebx
271     ret
272 pci_read endp
273 ;--------------------------------------------
274 ;----------------------------------------------
275 cseg ends
276     end start
时间: 2024-08-14 01:53:30

汇编读取PCI配置空间的相关文章

wdmWin10下读取PCI配置信息

WDM下HalGetBusData不能用了.加上感觉png方式太麻烦.自己修改了驱动开发技术详解上的代码直接在驱动下获取信息 #include "Driver.h" NTSTATUS DriverEntry( IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath) { NTSTATUS status; //判断CPU类型 CPUType(); //枚举 EnumeratePCI(); KdPrint((&qu

wdmWin10下遍历PCI配置空间

图右边是引用驱动开发技术详解书中的代码:3环只增加了个死循环 驱动没变 #include <windows.h> #include <stdio.h> //使用CTL_CODE必须加入winioctl.h #include <winioctl.h> #include "Ioctls.h" DWORD In_32(HANDLE hDevice, USHORT port) { DWORD dwOutput; DWORD inputBuffer[2] =

PCI、PCIE配置空间的訪问(MCFG,Bus,Device,Funtion)

一般来说,在x86平台上,有两大类方式能够訪问这一区间的寄存器, 1,配置机制1#或者配置机制2# 訪问时借助in/out指令.请注意,这样的方式有别于一般的in/out指令訪问PCI的IO空间,它引入了地址port和数据port. 配置机制2#仅仅在某些特定的主板上被使用. 新的设计应使用配置机制1#来产生配置空间的物理操作.这样的机制使用了两个特定的32位I/O空间,即CF8h和CFCh.这两个空间相应于PCI桥路的两个寄存器,当桥路看到CPU在局部总线对这两个I/O空间进行双字操作时,就将

PCI、PCIE配置空间的访问(MCFG,Bus,Device,Funtion)

一般来说,在x86平台上,有两大类方式可以访问这一区间的寄存器, 1,配置机制1#或者配置机制2# 访问时借助in/out指令.请注意,这种方式有别于一般的in/out指令访问PCI的IO空间,它引入了地址端口和数据端口. 配置机制2#只在某些特定的主板上被使用. 新的设计应使用配置机制1#来产生配置空间的物理操作.这种机制使用了两个特定的32位I/O空间,即CF8h和CFCh.这两个空间对应于PCI桥路的两个寄存器,当桥路看到CPU在局部总线对这两个I/O空间进行双字操作时,就将该I/O操作转

PCIe 基础(一)操作配置空间

PCI配置空间 PCI有三种地址空间:I/O空间,内存地址空间,PCI配置空间.在启动时bootloader 或者内核会遍历PCI总线并分配资源,如中断和内存,设备驱动程序通过PCI配置空间 找到资源分配.大小为256字节. 配置空间图: (1) Device ID和Vendor ID寄存器 这两个寄存器的值由PCISIG分配,只读.其中Vendor ID代表PCI设备的生产厂商, 而Device ID代表这个厂商所生产的具体设备.如Intel公司的基于82571EB芯片的 系列网卡,其Vend

怎样訪问pcie整个4k的配置空间

眼下用于訪问PCIe配置空间寄存器的方法须要追溯到原始的PCI规范. 为了发起PCI总线配置周期,Intel实现的PCI规范使用IO空间的CF8h和CFCh来分别作为索引和数据寄存器,这样的方法能够訪问全部PCI设备的255 bytes配置寄存器.Intel Chipsets眼下仍然支持这样的訪问PCI配置空间的方法. PCIe规范在PCI规范的基础上,将配置空间扩展到4K bytes,至于为什么扩展到4K,详细能够參考PCIe规范,这些功能都须要配置空间.原来的CF8/CFC方法仍然能够訪问全

如何访问pcie整个4k的配置空间

目前用于访问PCIe配置空间寄存器的方法需要追溯到原始的PCI规范.为了发起PCI总线配置周期,Intel实现的PCI规范使用IO空间的CF8h和CFCh来分别作为索引和数据寄存器,这种方法可以访问所有PCI设备的255 bytes配置寄存器.Intel Chipsets目前仍然支持这种访问PCI配置空间的方法. PCIe规范在PCI规范的基础上,将配置空间扩展到4K bytes,至于为什么扩展到4K,具体可以参考PCIe规范,这些功能都需要配置空间.原来的CF8/CFC方法仍然可以访问所有PC

【PCIE-2】---PCIE配置空间及访问方式简介

对新手来说,第一步了解PCIE的相关基本概念,第二步了解PCIE配置空间,第三步深入研究PCIE设备枚举方式.本章主要总结第二步的PCIE配置空间 按照国际惯例,先提问题: 1. 什么是PCIE的配置空间? 2. PCIE设备的配置空间有多大?     PCI和PCIE的配置空间有何区别与联系? 3. 如何访问PCIE设备的配置空间? 4. 有几种类型,都包含什么内容? 带着上述问题,来进行该部分的总结: 什么是PCIE的配置空间? 每个PCIE设备都有自己的独立的一段配置空间,该部分空间是这个

golang 读取 ini配置信息

package main //BY: [email protected]//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ;主机//golang 读取 ini配置信息//http://www.widuu.com/archives/02/961.htmlimport (  "fmt"  "github.com/widuu/goini"  //"runtime"  //&