commit 50addb6fd22cd5dc45b80be2a1214ab81c8811ae Author: ramenu Date: Thu Mar 28 18:43:18 2024 -0400 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57cda7e --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +/kernel +/contrib +/build.txt +/kolibri.img +/nvm.img +/programs +/skins +/_tools +/data +/.nvim +/.tup +/.svn +/tup.config.template +/drivers/ethernet +/drivers/examples +/drivers/gpio +/drivers/include +/drivers/audio +/drivers/ddk +/drivers/devman +/drivers/mouse +/drivers/old +/drivers/sdhci +/drivers/sensors +/drivers/unfinished +/drivers/usb +/drivers/video +/drivers/disk +/drivers/*.lua +/drivers/*.inc +/drivers/*.asm +/drivers/**/*.sys diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c81cbf3 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +QEMU=qemu-system-i386 +ASM=./drivers/nvme/nvme.asm +NVME_SYS=./drivers/nvme/nvme.sys + +all: MOUNT +.PHONY: all + +run: MOUNT + $(QEMU) -m 128 -fda ./kolibri.img -boot a -drive file=nvm.img,if=none,id=nvm -device nvme,serial=deadbeef,drive=nvm +.PHONY: run + +MOUNT: $(NVME_SYS) + @mcopy -moi kolibri.img $(NVME_SYS) ::DRIVERS/NVME.SYS +.PHONY: MOUNT + +$(NVME_SYS): $(ASM) + @cd ./drivers/nvme && $(MAKE) + diff --git a/drivers/nvme/Makefile b/drivers/nvme/Makefile new file mode 100644 index 0000000..8680eb0 --- /dev/null +++ b/drivers/nvme/Makefile @@ -0,0 +1,9 @@ +FASM = fasm +KPACK = kpack + +all: + $(FASM) nvme.asm + $(KPACK) nvme.sys + +clean: + rm *.sys diff --git a/drivers/nvme/nvme.asm b/drivers/nvme/nvme.asm new file mode 100644 index 0000000..a9ff157 --- /dev/null +++ b/drivers/nvme/nvme.asm @@ -0,0 +1,158 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; +;; Distributed under terms of the GNU General Public License ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;driver sceletone + +format PE DLL native +entry START + +DEBUG equ 1 + +API_VERSION equ 0 ;debug + +STRIDE equ 4 ;size of row in devices table + +SRV_GETVERSION equ 0 + +section ".flat" code readable writable executable +include "../proc32.inc" +include "../struct.inc" +include "../macros.inc" +include "../peimport.inc" +include "../pci.inc" +include "../../programs/debug.inc" + +proc START c, reason:dword + cmp [reason], DRV_ENTRY + jne .exit +.entry: + push esi + if DEBUG + mov esi, msgInit + invoke SysMsgBoardStr + end if + call detect + pop esi + test eax, eax + jz .exit + + invoke RegService, my_service, service_proc + ret +.exit: + xor eax, eax + ret +endp + +proc service_proc stdcall, ioctl:dword + + mov ebx, [ioctl] + mov eax, [ebx+IOCTL.io_code] + cmp eax, SRV_GETVERSION + jne @F + + mov eax, [ebx+IOCTL.output] + cmp [ebx+IOCTL.out_size], 4 + jne .fail + mov dword [eax], API_VERSION + xor eax, eax + ret +@@: +.fail: + or eax, -1 + ret +endp + +proc detect + push ebx + invoke GetPCIList + mov edx, eax +.check_dev: + mov ecx, [eax+PCIDEV.class] + and ecx, 0x00ffff00 ; retrieve class/subclass code only + ;if DEBUG + ; mov esi, msgPciClass + ; invoke SysMsgBoardStr + ; debug_print_hex ecx + ; mov esi, newLine + ; invoke SysMsgBoardStr + ;end if + cmp ecx, 0x00010800 ; Mass Storage Controller - Non-Volatile Memory Controller + je .found_nvme +.next_dev: + mov eax, [eax + PCIDEV.fd] + cmp eax, edx + jne .check_dev + + ; no more PCI devices to enumerate? + xor eax, eax + pop ebx + ret +.found_nvme: + mov esi, msgFound + invoke SysMsgBoardStr +.check_cap: + push eax + movzx ebx, [eax + PCIDEV.bus] + mov [bus], ebx + movzx ebx, [eax + PCIDEV.devfn] + mov [devfn], ebx + invoke PciRead16, [bus], [devfn], PCI_header00.status + test ax, 0x10 ; check capabilities list bit + jnz .got_cap + pop eax +.got_cap: + if DEBUG + mov esi, msgHasCap + invoke SysMsgBoardStr + end if + invoke PciRead8, [bus], [devfn], PCI_header00.cap_ptr + and eax, 11111100b + mov edi, eax +@@: + invoke PciRead32, [bus], [devfn], edi + mov ecx, eax + and ecx, 0xff + if DEBUG + mov esi, msgCap + invoke SysMsgBoardStr + debug_print_hex ecx + mov esi, newLine + invoke SysMsgBoardStr + end if + mov eax, ecx + movzx edi, ah + test edi, edi + jnz @b + + ; return successfully + pop eax + xor eax, eax + inc eax + pop ebx + ret +endp + +; uninitialized data +bus dd ? +devfn dd ? + +;all initialized data place here + +align 4 +my_service db "NVME Service",0 ;max 16 chars include zero + +msgInit db "detecting NVME hardware...",13,10,0 +msgFound db "found NVME device",13,10,0 +msgFail db "NVME device not found",13,10,0 +msgHasCap db "NVME device has capabilities",13,10,0 +msgCap db "Capability: ",0 +msgPciClass db "PCI class: ",0 +newLine db 13,10,0 + +align 4 +data fixups +end data