> For the complete documentation index, see [llms.txt](https://aptivi.gitbook.io/aptivi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aptivi.gitbook.io/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers.md).

# Kernel Drivers

How the kernel drivers work and their role in the kernel

The kernel drivers allows your kernel to provide interfaces for different purposes, from console to filesystem. This is to present the kernel your implementation of different driver types. It's not a device driver, though!

The kernel drivers can be called using either the properties that point to the current driver of each driver type or the `GetDriver<TResult>()` function found in the `DriverHandler` class under the `KS.Drivers` namespace. These two will return an appropriate driver interface that actually allows you to call their functions that each driver of the same type implements.

***

## <mark style="color:$primary;">Types of kernel drivers</mark>

The types of kernel drivers are listed below:

<table><thead><tr><th data-type="content-ref">Driver</th><th>Interface</th><th>Base</th><th>Description</th></tr></thead><tbody><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/rng-drivers.md">RNG Drivers</a></td><td><code>IRandomDriver</code></td><td><code>BaseRandomDriver</code></td><td>Random number generator drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/filesystem-drivers.md">Filesystem Drivers</a></td><td><code>IFilesystemDriver</code></td><td><code>BaseFilesystemDriver</code></td><td>Filesystem drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/encryption-drivers.md">Encryption Drivers</a></td><td><code>IEncryptionDriver</code></td><td><code>BaseEncryptionDriver</code></td><td>Encryption drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/regular-expression-drivers.md">Regular Expression Drivers</a></td><td><code>IRegexpDriver</code></td><td><code>BaseRegexpDriver</code></td><td>Regular expression drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/debug-logger-drivers.md">Debug Logger Drivers</a></td><td><code>IDebugLoggerDriver</code></td><td><code>BaseDebugLoggerDriver</code></td><td>Debug logger drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/encoding-drivers.md">Encoding Drivers</a></td><td><code>IEncodingDriver</code></td><td><code>BaseEncodingDriver</code></td><td>Encoding drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/hardware-prober-drivers.md">Hardware Prober Drivers</a></td><td><code>IHardwareProberDriver</code></td><td><code>BaseHardwareProberDriver</code></td><td>Hardware probing drivers</td></tr><tr><td><a href="/aptivi/nitrocid-ks-v0.2.1-rolling-manual/advanced-and-power-users/inner-workings/inner-essentials/kernel-drivers/sorting-drivers.md">Sorting Drivers</a></td><td><code>ISortingDriver</code></td><td><code>BaseSortingDriver</code></td><td>Integer sorting drivers</td></tr></tbody></table>

***

## <mark style="color:$primary;">Mods and Drivers</mark>

Each driver contains its own interface containing its own method definitions and their signatures. These interfaces are the ones that you must implement with your mod. The most basic interface for the kernel drivers is `IDriver` found in the same namespace, which is implemented by the driver-specific interfaces.

{% hint style="info" %}
`IsBuiltin()` only returns true if the driver is found in the built-in driver list. `IsRegistered()`, however, returns true if the driver is either built-in or found in the custom drivers list.
{% endhint %}

{% hint style="warning" %}
Your kernel modifications **should** reference local versions of driver properties, like `CurrentRegexpDriverLocal`, unless it's not possible.
{% endhint %}

Here are some of the useful tools.

<details>

<summary>Driver class implementation</summary>

The final driver class implementation must implement both the base driver-specific base class and its interface, such as:

```csharp
internal class MyRegexpDriver : BaseRegexpDriver, IRegexpDriver
```

</details>

<details>

<summary>Registering a kernel driver</summary>

If you have a kernel driver that you wish to register, you'll have to register the kernel driver, passing it the `IDriver` interface for your driver and the appropriate type.

You can use the `RegisterDriver()` function to perform this operation, but you should set the current driver to your driver for the target wrappers, like the methods found in the `KS.Files` namespace that call the current filesystem driver, using the `SetDriverSafe<T>()` function.

{% hint style="warning" %}
The `SetDriver<T>()` function can be used, but doesn't prevent you from using internal drivers. We advice you to use the `SetDriverSafe<T>()` function instead.
{% endhint %}

{% hint style="danger" %}
Be sure to unregister your driver with the `UnregisterDriver()` function, or your driver will not get updated in the list of kernel drivers!
{% endhint %}

</details>

<details>

<summary>Local drivers</summary>

If you want to temporarily to use a driver without affecting the entire kernel and its configuration, you can use this feature to temporarily switch drivers of supported types.

Assuming that your mod uses local drivers to do their actions, like `CurrentConsoleDriverLocal`, you must use the following functions **in order**:

<table><thead><tr><th width="245">Function</th><th>Description</th></tr></thead><tbody><tr><td><code>BeginLocalDriverSafe&#x3C;T>()</code></td><td>This notifies the kernel that the mod is going to use the provided driver temporarily.</td></tr><tr><td><code>EndLocalDriver&#x3C;T>()</code></td><td>This notifies the kernel that the mod is done with the temporary driver.</td></tr></tbody></table>

Where `T` is the interface type of the driver found in the above table at the start of this page.

{% hint style="warning" %}
The `BeginLocalDriver<T>()` function can be used, but doesn't prevent you from using internal drivers. We advice you to use the `BeginLocalDriverSafe<T>()` function instead.
{% endhint %}

{% hint style="danger" %}
Be sure to execute the two above functions in the order specified above. Forgetting to call `EndLocalDriver<T>()` after you're done with the driver can cause the kernel to think that the mod is going to use your local driver forever.
{% endhint %}

</details>

<details>

<summary>Fallback drivers</summary>

Additionally, the driver management API provides you an easy way to get the default driver that the kernel declares.

You can use the below functions:

<table><thead><tr><th width="405.666748046875">Function</th><th>Description</th></tr></thead><tbody><tr><td><code>GetFallbackDriver&#x3C;TDriverInterfaceType>()</code></td><td>This returns an instance of the default driver that the kernel initialized before drivers get set by the config reader.</td></tr><tr><td><code>GetFallbackDriverName&#x3C;TDriverInterfaceType>()</code></td><td>This returns the actual driver name grabbed from the dictionary.</td></tr></tbody></table>

{% hint style="warning" %}
Don't confuse the name given by the `GetDriverName()` and its siblings with the name from the driver instance.
{% endhint %}

</details>

<details>

<summary>Current drivers</summary>

If you have a driver interface type or the driver type from the `DriverTypes` enumeration, you can request for a current driver either from the available properties or from the `GetCurrentDriver()` and `GetCurrentDriverLocal()` functions.

```csharp
var currentDriver = DriverHandler.GetCurrentDriver(driverType);
```

<table><thead><tr><th width="226.333251953125">Function</th><th>Description</th></tr></thead><tbody><tr><td><code>GetCurrentDriver()</code></td><td>Returns the current driver that's used by the kernel.</td></tr><tr><td><code>GetCurrentDriverLocal()</code></td><td>Returns the current local driver set by <code>BeginLocalDriver&#x3C;T>()</code> or its safe sibling function.</td></tr></tbody></table>

{% hint style="info" %}
For all the driver management functions, you can also use the non-generic versions where they ask for a `DriverTypes` value in their first argument.
{% endhint %}

</details>

***

## <mark style="color:$primary;">Driver Configuration</mark>

<figure><img src="https://content.gitbook.com/content/wBz0xp5LOdy4QJ8roqPV/blobs/OdMD8bTobrBlr3sP0ZFL/image.png" alt=""><figcaption></figcaption></figure>

The `settings -driver` application now lets you set your system-wide kernel drivers up in a completely new section called `Kernel driver settings`. It lets you configure current drivers for each supported driver type.

In order to configure your driver, select one of the driver types from the settings section shown above by pressing `ENTER`, and select your driver.

{% hint style="info" %}
You can also use `driverman` to change the kernel driver settings for scripting scenarios.

Like all other settings, you must save your kernel settings to apply your changes across reboots.
{% endhint %}
