如何通过pci能力id识别设备

发布于 2025-01-17 11:44:26 字数 850 浏览 2 评论 0原文

如何通过PCI功能ID识别设备? 这是我的代码:

我尝试访问34h并检查第一个循环中的功能ID是否存在 如果存在,它指向下一个指针,但是在获取指针并放置地址的步骤中似乎存在一些问题。

'''

    push eax
    push edi
    push esi

    mov cx,100
    
    ;mov edi,[esi]      
    add edi,52     ;access 34h
    
lopreg:     
    mov eax,edi    ;read
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in eax,dx
   
    cmp cx,100    ;first time 
    je first
    
    cmp ah,10
    jne nextreg
    jmp ispcie
    
first:
    cmp ah,0
    je  ending
    sub edi,52
    movzx bx,ah
    add di,bx

    loop lopreg
    jmp ending
    
ispcie:
    call set_cur        
    mov ah,09h
    lea dx,regmem        ;print pcie
    int 21h
    jmp ending
    
nextreg:
    cmp al,0
    je ending
    movzx bx,al ;
    add di,bx
    loop lopreg
ending: 
    pop esi
    pop edi
    pop eax
    ret

'''

How to identify the device by pci capability id?
this is my code:

I try to access 34h and check if the capability id exists on the first loop
If it exists, it points to the next pointer, But there seems to be some problems in the steps of getting the pointer and putting the address.

'''

    push eax
    push edi
    push esi

    mov cx,100
    
    ;mov edi,[esi]      
    add edi,52     ;access 34h
    
lopreg:     
    mov eax,edi    ;read
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in eax,dx
   
    cmp cx,100    ;first time 
    je first
    
    cmp ah,10
    jne nextreg
    jmp ispcie
    
first:
    cmp ah,0
    je  ending
    sub edi,52
    movzx bx,ah
    add di,bx

    loop lopreg
    jmp ending
    
ispcie:
    call set_cur        
    mov ah,09h
    lea dx,regmem        ;print pcie
    int 21h
    jmp ending
    
nextreg:
    cmp al,0
    je ending
    movzx bx,al ;
    add di,bx
    loop lopreg
ending: 
    pop esi
    pop edi
    pop eax
    ret

'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2025-01-24 11:44:26

这个答案是在假设该代码正在寻找 PCI Express 功能的情况下编写的。

这段代码有几个问题。

  1. first标签处,应该使用al而不是ah来确定第一个能力的偏移量。
  2. PCI Express 功能的功能 ID 是 10h,而不是 10。
  3. 读取每个功能标头后,al 包含功能 id,ah 包含下一个指针。所以 cmp ah, 10 应该是 cmp al, 10h
  4. nextreg 标签处,应该使用 ah 而不是 al 来确定下一个功能的偏移量。
  5. 在每次迭代中,它都会将 bx 添加到 di 中,而不删除先前的偏移量。
  6. 目前尚不清楚 edi 被初始化为什么,但如果它没有设置位 31,那么此代码根本不会读取 PCI 配置空间。

这应该可行:

    mov cx,100
    
    ;mov edi,[esi]      
    add edi,52     ;access 34h
    
lopreg:     
    mov eax,edi    ;read
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in eax,dx
   
    cmp cx,100    ;first time 
    je first
    
    cmp al,10h
    jne nextreg
    jmp ispcie
    
first:
    cmp al,0
    je  ending
    sub edi,52
    movzx bx,al
    add di,bx

    loop lopreg
    jmp ending
    
ispcie:
    call set_cur        
    mov ah,09h
    lea dx,regmem        ;print pcie
    int 21h
    jmp ending
    
nextreg:
    cmp ah,0
    je ending
    sub di, bx
    movzx bx,ah
    add di,bx
    loop lopreg

ending:
  1. 更好的方法是将原始地址保留在 edi 中而不更改它,并为每个新偏移量使用 lea eax, [edi+ebx] .

  2. 没有必要使用ecx作为循环计数器,而且逻辑有点复杂。可以将其拉直一点,以便更清晰地流动。

这是我的编写方式:(

    lea eax,[edi+34h]     
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in al,dx              ; read offset of first capability
    cmp al,0
    je ending
    movzx ebx,al

lopreg:     
    lea eax,[edi+ebx]     ; offset of next capability is in ebx
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in eax,dx             ; read capability header

    cmp al,10h            ; check if it is the PCI Express capability
    je ispcie

nextreg:
    cmp ah,0              ; check if it is the end of the capability list
    je ending
    movzx ebx, ah         ; set up ebx with the offset of the next capability
    jmp lopreg

ispcie:
    ; base of device PCI config space is in edi
    ; offset of PCI Express Capability is in ebx
    ...

ending:

我不知道 set_curregmem 是什么,所以我没有尝试编写这部分代码。)

This answer is written with the assumption that this code is looking for the PCI Express Capability.

There are several problems in this code.

  1. At the first label, it should use al instead of ah to determine the offset of the first capability.
  2. The Capability ID of the PCI Express capability is 10h, not 10.
  3. After reading each capability header, al contains the capability id and ah contains the next pointer. So cmp ah, 10 should be cmp al, 10h .
  4. At the nextreg label, it should use ah instead of al to determine the offset of the next capability.
  5. On each iteration, it adds bx to di without removing the previous offset.
  6. It's not clear what edi is initialized to, but if it does not have bit 31 set, then this code won't be reading PCI config space at all.

This should work:

    mov cx,100
    
    ;mov edi,[esi]      
    add edi,52     ;access 34h
    
lopreg:     
    mov eax,edi    ;read
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in eax,dx
   
    cmp cx,100    ;first time 
    je first
    
    cmp al,10h
    jne nextreg
    jmp ispcie
    
first:
    cmp al,0
    je  ending
    sub edi,52
    movzx bx,al
    add di,bx

    loop lopreg
    jmp ending
    
ispcie:
    call set_cur        
    mov ah,09h
    lea dx,regmem        ;print pcie
    int 21h
    jmp ending
    
nextreg:
    cmp ah,0
    je ending
    sub di, bx
    movzx bx,ah
    add di,bx
    loop lopreg

ending:
  1. A better approach is to keep the original address in edi without changing it, and use lea eax, [edi+ebx] for each new offset.

  2. There's no need to use ecx as a loop counter and the logic is a bit convoluted. It can be straightened out a bit to flow more clearly.

Here's how I would write it:

    lea eax,[edi+34h]     
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in al,dx              ; read offset of first capability
    cmp al,0
    je ending
    movzx ebx,al

lopreg:     
    lea eax,[edi+ebx]     ; offset of next capability is in ebx
    mov dx,0cf8h
    out dx,eax
    mov dx,0cfch
    in eax,dx             ; read capability header

    cmp al,10h            ; check if it is the PCI Express capability
    je ispcie

nextreg:
    cmp ah,0              ; check if it is the end of the capability list
    je ending
    movzx ebx, ah         ; set up ebx with the offset of the next capability
    jmp lopreg

ispcie:
    ; base of device PCI config space is in edi
    ; offset of PCI Express Capability is in ebx
    ...

ending:

(I don't know what set_cur and regmem are so I didn't attempt to write that part of the code.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文