From aa0a75e7156ac7b72701d37a9f7a8c6b550561ac Mon Sep 17 00:00:00 2001 From: Abdur-Rahman Mansoor Date: Fri, 9 Aug 2024 13:17:39 -0400 Subject: [PATCH] fix: running LOADDRV on NVMe driver more than once (#2) Credits to @Burer for finding this bug, and @Doczom for providing a solution. --- drivers/nvme/nvme.asm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/nvme/nvme.asm b/drivers/nvme/nvme.asm index 357d029..6f0b691 100644 --- a/drivers/nvme/nvme.asm +++ b/drivers/nvme/nvme.asm @@ -526,6 +526,16 @@ proc detect_nvme ret .found_dev: + ; skip PCIDEV.owner check if the PCI device pointer has already been + ; allocated (without this check, more than 1 NVMe device cannot be + ; registered) + mov eax, dword [p_nvme_devices] + test eax, eax + jnz @f + cmp dword [esi + PCIDEV.owner], 0 + jnz .err + +@@: PDEBUGF DBG_INFO, "PCI(%u.%u.%u): Detected NVMe device...\n", [esi + PCIDEV.bus], [esi + PCIDEV.devfn] cmp dword [pcidevs_len], TOTAL_PCIDEVS jne @f @@ -538,8 +548,9 @@ proc detect_nvme jnz @f ; was the pointer already allocated? invoke KernelAlloc, sizeof.pcidev * TOTAL_PCIDEVS test eax, eax - jz .err_no_mem + jz .err mov dword [p_nvme_devices], eax + mov dword [esi + PCIDEV.owner], eax @@: mov ecx, dword [pcidevs_len] @@ -557,7 +568,7 @@ proc detect_nvme jmp .next_dev -.err_no_mem: +.err: xor eax, eax ret @@ -1337,7 +1348,7 @@ endp align 4 p_nvme_devices dd 0 pcidevs_len dd 0 - my_service db "NVMe",0 ;max 16 chars include zero + my_service db "nvme",0 ;max 16 chars include zero disk_functions: dd disk_functions.end - disk_functions dd 0 ; no close function