Recent

Author Topic: [Help] Creating Drivers in Pascal  (Read 4641 times)

shonay

  • Full Member
  • ***
  • Posts: 169
[Help] Creating Drivers in Pascal
« on: December 06, 2016, 08:56:58 pm »
Hi

Is it possible to Develop kernel mode Drivers .sys in Lazarus Pascal
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: [Help] Creating Drivers in Pascal
« Reply #1 on: December 07, 2016, 12:23:45 pm »
Theoretically it is possible, but not recommendable.

Drivers must be efficient, specially those that will work in kernel mode, and also they must communicate with hardware directly.  Both can be done by Pascal, really, but Pascal is a high-level language so you'll need too much work to accomplish that, specially if you need perfect timing synchronization and perfect memory usage as most drivers need.  That's why low-level languages are used, like C and Assembler.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Help] Creating Drivers in Pascal
« Reply #2 on: December 07, 2016, 01:23:57 pm »
Theoretically it is possible, but not recommendable.

Drivers must be efficient, specially those that will work in kernel mode, and also they must communicate with hardware directly.  Both can be done by Pascal, really, but Pascal is a high-level language so you'll need too much work to accomplish that, specially if you need perfect timing synchronization and perfect memory usage as most drivers need.  That's why low-level languages are used, like C and Assembler.
Low levelness is not a problem, as Pascal is equally capable with C of doing low level stuff required by a driver, along with inline assembler whenever needed, as how it's done in C. The problem lies more on provided OS headers and libraries to help writing the driver. Unless you want to write everything by yourself, you need to wrap the headers. M$ provides Windows DDK, while Linux has everything in kernel headers package. It's harder with Windows as DDK is not so simple.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [Help] Creating Drivers in Pascal
« Reply #3 on: December 07, 2016, 03:03:34 pm »
Yes it is possible. There is the "slight" problem that windows 64 bit drivers only work when properly signed by a microsoft provided certificate. And that is NOT free.
There's also the slight problem that for 16 and 32 bit drivers you need an external MS linker to obtain a working driver.
And YES, you are limited to the DDK provided framework, although that is not really an issue because there are Pascal translations out there.
You will also need to be able to strip system.pas into a very low-weight one, because as-is, you can't use system.pas in a driver.
That takes a whole lot of knowledge about FPC internals anyway.

It is definitely not for beginners, not even for intermediate programmers, not even for GOOD Object Pascal programmers.
It is also a bit pointless, because most people that CAN write a driver in Object Pascal will probably do it in C anyway.
Unless you are nuts, like me ;) If it CAN be done, we give it a try ;)
« Last Edit: December 07, 2016, 03:06:43 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: [Help] Creating Drivers in Pascal
« Reply #4 on: December 07, 2016, 04:55:17 pm »
I'm pretty sure I saw somebody post an example Linux kernel driver some years ago in the FPC mailing list. As far as I know there is no technical reason for not being able to develop drivers with Free Pascal.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Creating Drivers in Pascal
« Reply #5 on: December 07, 2016, 07:53:32 pm »
@Thaddy you suggest using C?

i even tried following this Example

Code: Pascal  [Select][+][-]
  1. library helloworld; // this is important - do not try a "program" here
  2.  
  3. // tell FPC that we want to compile a kernel mode application
  4. // (NEEDS a RTL that was compiled with KMODE)
  5. {$apptype native}
  6.  
  7. uses
  8.   // for entry point types and debug output
  9.   DDK;
  10.  
  11. // this method is called once our driver is unloaded
  12. procedure DriverUnload(aObject: PDriverObject); stdcall;
  13. begin
  14.   DbgPrint('Unloading driver');
  15. end;
  16.  
  17. // during the entry point the variables DriverObject and
  18. // RegistryPath are valid
  19. begin
  20.   DbgPrint('Hello World!');
  21.  
  22.   // we need to setup the unload routine or the driver will
  23.   // only be unloaded on shutdown!
  24.   DriverObject^.DriverUnload := @DriverUnload;
  25. end.
  26.  

From here http://wiki.lazarus.freepascal.org/Target_NativeNT

I started to think i could do this as this is a good starting point.

Do i have to start looking for DDK.pas
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

 

TinyPortal © 2005-2018