<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://francois141.github.io/feed.xml" rel="self" type="application/atom+xml"/><link href="https://francois141.github.io/" rel="alternate" type="text/html" hreflang="en"/><updated>2026-10-07T04:01:30+00:00</updated><id>https://francois141.github.io/feed.xml</id><title type="html">blank</title><subtitle>PhD student at Stanford University working on kernel generation, compilers, and verification. </subtitle><entry><title type="html">Add a Custom Linux Driver on the VisionFive2 Board</title><link href="https://francois141.github.io/visionfive2-board-custom-kernel/" rel="alternate" type="text/html" title="Add a Custom Linux Driver on the VisionFive2 Board"/><published>2012-08-14T00:00:00+00:00</published><updated>2012-08-14T00:00:00+00:00</updated><id>https://francois141.github.io/driver-visionfive2</id><content type="html" xml:base="https://francois141.github.io/visionfive2-board-custom-kernel/"><![CDATA[<p>In this article, we will see how to introduce a custom driver into the Linux kernel of the VisionFive2 board.</p> <h2 id="step-1-set-up-debian-on-the-visionfive2-board">Step 1: Set Up Debian on the VisionFive2 Board</h2> <p>Take your microSD card, download the image and Balena Etcher, and flash it. This step is straightforward. Then you can connect using <code class="language-plaintext highlighter-rouge">user</code> as the username and <code class="language-plaintext highlighter-rouge">starfive</code> as the password. Personally I connect either with UART or through SSH.</p> <h2 id="step-2-download-a-specific-linux-kernel">Step 2: Download a Specific Linux Kernel</h2> <p>By default, dynamic modules are disabled with the Debian distribution, and we can’t compile kernel modules because the kernel sources don’t have a build folder. To address this issue, we download another release that supports dynamic modules. For this tutorial, we use version <code class="language-plaintext highlighter-rouge">v3.6.1</code> and place the new kernel in the boot folder.</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>wget https://github.com/starfive-tech/VisionFive2/releases/download/VF2_v3.6.1/Image.gz
<span class="nb">sudo cp </span>Image.gz /boot/new_kernel
</code></pre></div></div> <h2 id="step-3-add-the-new-kernel-to-the-boot-menu">Step 3: Add the New Kernel to the Boot Menu</h2> <p>Now that we have the new kernel downloaded on the board and located in the boot folder, the next step consists of placing a new entry in the bootloader configuration.</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>label l2
    menu label The new kernel
    linux /new_kernel
    initrd /initrd.img-5.15.0-starfive

    fdtdir /dtbs
    append <span class="nv">root</span><span class="o">=</span>/dev/mmcblk1p4 rw <span class="nv">console</span><span class="o">=</span>tty0 <span class="nv">console</span><span class="o">=</span>ttyS0,115200 earlycon rootwait <span class="nv">stmmaceth</span><span class="o">=</span>chain_mode:1 <span class="nv">selinux</span><span class="o">=</span>0
</code></pre></div></div> <h2 id="step-4-reboot-the-board">Step 4: Reboot the Board</h2> <p>Now you can reboot the board. In the bootloader menu, press <code class="language-plaintext highlighter-rouge">3</code>. If the kernel doesn’t boot manually, make sure the path is correct. Also, ensure the kernel is found in the bootloader; otherwise, the bootloader will boot the default kernel.</p> <h2 id="step-5-generate-the-module-files">Step 5: Generate the Module Files</h2> <p>When the step 4 workd, the next step consists of installing the kernel sources to generate the new driver (this will take around two hours). It is important to <strong>use the exact same commit as the kernel we downloaded</strong>.</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/starfive-tech/linux/ <span class="nt">--branch</span> visionfive <span class="nt">--single-branch</span> <span class="nt">--depth</span> 1
<span class="nb">cd </span>linux
git fetch <span class="nt">--depth</span> 1 origin 05533e9c31d6f0da20efc2d436a3b0f6d516ed4b
git checkout 05533e9c31d6f0da20efc2d436a3b0f6d516ed4b

<span class="nb">sudo cp</span> /boot/config-5.15.0-starfive .config
make <span class="nt">-j4</span> all
<span class="nb">sudo </span>make modules_install
</code></pre></div></div> <h2 id="step-6-compile-and-insert-the-module">Step 6: Compile and Insert the Module</h2> <p>The last step consists of writing the example kernel module we use in this tutorial and compiling it. The name of the file should be <code class="language-plaintext highlighter-rouge">driver.c</code> As a final step, we need to add it to the kernel.</p> <p>The driver:</p> <div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;linux/init.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;linux/module.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;linux/fs.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;linux/uaccess.h&gt;</span><span class="cp">
</span>
<span class="n">MODULE_LICENSE</span><span class="p">(</span><span class="s">"GPL"</span><span class="p">);</span>

<span class="k">static</span> <span class="kt">int</span> <span class="n">__init</span> <span class="nf">driver_init</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="cm">/* Constructor */</span>
<span class="p">{</span>
    <span class="n">printk</span><span class="p">(</span><span class="n">KERN_INFO</span> <span class="s">"Loading driver...</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">static</span> <span class="kt">void</span> <span class="n">__exit</span> <span class="nf">driver_exit</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="cm">/* Destructor */</span>
<span class="p">{</span> 
    <span class="n">printk</span><span class="p">(</span><span class="n">KERN_INFO</span> <span class="s">"Goodbye from driver!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="p">}</span>

<span class="n">module_init</span><span class="p">(</span><span class="n">driver_init</span><span class="p">);</span>
<span class="n">module_exit</span><span class="p">(</span><span class="n">driver_exit</span><span class="p">);</span>
</code></pre></div></div> <p>The makefile:</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>obj-m +<span class="o">=</span> driver.o

all:
	make <span class="nt">-C</span> /lib/modules/5.15.0/build <span class="nv">M</span><span class="o">=</span><span class="si">$(</span>PWD<span class="si">)</span> modules

clean:
	make <span class="nt">-C</span> ../linux <span class="nv">M</span><span class="o">=</span><span class="si">$(</span>PWD<span class="si">)</span> clean
</code></pre></div></div> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>make all
<span class="nb">sudo </span>insmod module.ko
<span class="nb">sudo </span>dmesg | <span class="nb">tail</span> <span class="nt">-n</span> 1
</code></pre></div></div> <p>If you see the output, congratulations! You have successfully added a custom (minimal) driver to the VisionFive2 board!</p>]]></content><author><name></name></author><category term="VisionFive2 board"/><category term="Linux kernel"/><summary type="html"><![CDATA[How to introduce a custom driver into the Linux kernel of the VisionFive2 board.]]></summary></entry></feed>