Recent

Author Topic: Windows 10 x64 Kernel Driver (FPC trunk)  (Read 4011 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 653
  • Internal Error Hunter
Windows 10 x64 Kernel Driver (FPC trunk)
« on: December 28, 2024, 01:19:44 pm »
Anyone using FPC for drivers development?

1. Remove all initialization code from the RTL, everything that might call any user mode API calls, or create your own minimal RTL, or find one.
2. Declare SysDriverObject and SysRegistryPath pointers somewhere in the interface of system.pp, we need them to save the parameters that the entrypoint was called with for later use in PASCALMAIN.

Code: Pascal  [Select][+][-]
  1. var
  2.   SysDriverObject: Pointer;
  3.   SysRegistryPath: Pointer;

3. Use entrypoint for DLL, just change its parameteres to take 2 params instead of 3. You see, drivers are basically DLLs that are loaded into the process "System".

Code: Pascal  [Select][+][-]
  1. function _FPC_DLLMainCRTStartup(DriverObject: Pointer; RegistryPath: Pointer): LongInt; stdcall; public name '_DLLMainCRTStartup';
  2. begin
  3.   SysDriverObject := DriverObject;
  4.   SysRegistryPath := RegistryPath;
  5.   PASCALMAIN;
  6.   result := ExitCode;
  7. end;

4. Create a file named "driver.lpr":

Code: Pascal  [Select][+][-]
  1. library driver;
  2.  
  3. function DbgPrint(aFormat: PAnsiChar): Integer; cdecl; varargs; external 'ntoskrnl.exe';
  4.  
  5. type
  6.   _DRIVER_OBJECT = packed record
  7.     // cut
  8.   end;
  9.   DRIVER_OBJECT = _DRIVER_OBJECT;
  10.   PDRIVER_OBJECT = ^_DRIVER_OBJECT;
  11.  
  12. procedure DriverUnload(DriverObject: PDRIVER_OBJECT); stdcall;
  13. begin
  14. end;
  15.  
  16. begin
  17.   DbgPrint(LineEnding);
  18.   DbgPrint('Hello World! Windows Kernel Mode Driver in FPC trunk' + LineEnding);
  19.   DbgPrint('Build time  = ' + {$I %DATE%} + ' ' + {$I %TIME%} + LineEnding);
  20.   DbgPrint('FPC version = ' + {$I %FPCVERSION%} + LineEnding);
  21.   DbgPrint('FPC target  = ' + {$I %FPCTARGET%} + LineEnding);
  22.   DbgPrint(LineEnding);
  23.  
  24.   PDRIVER_OBJECT(SysDriverObject)^.DriverUnload := @DriverUnload;
  25.  
  26.   ExitCode := 0; // STATUS_SUCCESS
  27. end.

I removed _DRIVER_OBJECT struct to leave it as an excercise for you :D The struct in the FPC sources is correct only for x86. Actually the whole NativeNT source is full of bugs and is not suitable for use in x64.

5. Open driver.lpr in Lazarus and compile :D

6. Create a service for the driver, I named mine "fpcd" and put the compiled DLL file in C:\fpcd\fpcd.sys

Code: Pascal  [Select][+][-]
  1. sc create fpcd type= kernel start= demand binPath= "C:\fpcd\fpcd.sys"

7. Start the service

Code: Pascal  [Select][+][-]
  1. sc start fpcd

In Windows 32 bit it works fine.

For Windows 64 bit either enable TestSigning, or there are few ways to bypass the signature validation. A great literature:
https://www.geoffchappell.com/notes/windows/license/customkernelsigners.htm

Perhaps I will create a github repo with all the information needed? Any interest in this?
« Last Edit: December 28, 2024, 01:41:14 pm by Fibonacci »

mav

  • Jr. Member
  • **
  • Posts: 83
Re: Windows 10 x64 Kernel Driver (FPC trunk)
« Reply #1 on: December 28, 2024, 05:11:40 pm »
Yes! Waiting for github repo.
Thanks :) :)

Fibonacci

  • Hero Member
  • *****
  • Posts: 653
  • Internal Error Hunter
Re: Windows 10 x64 Kernel Driver (FPC trunk)
« Reply #2 on: December 30, 2024, 12:32:51 pm »
I see little interest :-\ But I created a repo anyway

https://github.com/fibodevy/fpc-driver

mav

  • Jr. Member
  • **
  • Posts: 83
Re: Windows 10 x64 Kernel Driver (FPC trunk)
« Reply #3 on: December 30, 2024, 01:44:53 pm »
Great! :D

VisualLab

  • Hero Member
  • *****
  • Posts: 639
Re: Windows 10 x64 Kernel Driver (FPC trunk)
« Reply #4 on: December 30, 2024, 03:30:16 pm »
I see little interest :-\ But I created a repo anyway

It's not that bad. It's only been 2 days since your message was published. I think they're interested. For now, mav and me. Maybe the other interested people are just reading the forum.



P.S. It's a pity that Linux isn't fully modular* (it's only pseudo-modular). Of course, I mean writing such drivers just for yourself, for experiments. Torvalds and his colleagues would probably have a heart attack if they found out about such "computer blasphemy" as writing Linux drivers in Pascal :D

*) Although on the other hand, when there is no Linux API on the hardware and driver side, it wouldn't help. Because after a few kernel versions (changes), such drivers would stop working.

DragoRosso

  • Guest
Re: Windows 10 x64 Kernel Driver (FPC trunk)
« Reply #5 on: December 30, 2024, 05:42:28 pm »
I am also interested, I recently purchased an EV certificate and I am finishing the procedures for the release (maybe tomorrow they will release the certificates to me).
I need in the near future to write some drivers for Windows x64 specifically for Intel processors and your work is really excellent.
I don't know yet when I will "throw myself" into this undertaking but your starting point is very important.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8319
Re: Windows 10 x64 Kernel Driver (FPC trunk)
« Reply #6 on: December 30, 2024, 09:42:47 pm »
I feel that this is definitely something worth doing, the lower levels of Windows are- IMO- better thought out than unix and- in particular- Linux which by now is more a "necessary evil" than a paragon of good design.

P.S. It's a pity that Linux isn't fully modular* (it's only pseudo-modular). Of course, I mean writing such drivers just for yourself, for experiments. Torvalds and his colleagues would probably have a heart attack if they found out about such "computer blasphemy" as writing Linux drivers in Pascal :D

*) Although on the other hand, when there is no Linux API on the hardware and driver side, it wouldn't help. Because after a few kernel versions (changes), such drivers would stop working.

xref to recent thread https://forum.lazarus.freepascal.org/index.php/topic,69507.msg539943.html#msg539943

While I deeply admire the technical achievement of writing a Linux kernel module in Pascal, the defensiveness of Linux system programmers when presented with something that isn't written in C and compliant with their (unwritten) doctrine amounts to rabid hostility. As such, even if we can do it I don't think we should talk about it...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018