Linux kernel & VS Code

If you are using VSCode for Linux kernel development, following setup will be useful:

  1. Clone https://github.com/aakbar5/handy-kernel_modules. It contains the
    • Makefile: to build kernel modules
    • .devcontainer: to setup vscode for remote development inside the Docker container
    • .vscode: From tasks build and run Qemu.
  2. Start vscode
  3. Open the folder having linux kernel development project
  4. Select: Remote-Containers: Open Folder in Container… from the popup menu.
  5. Select the folder selected in #2
  6. Keep an eye on the vscode status bar while it is showing Opening Remote…
  7. Once vscode is setup, your project will be ready for development in container environment.
  8. Press CTRL+P for Tasks: Run Task
  9. Select kmod – build task to build a kernel module setup in Makefile.
  10. Press CTRL+P for Tasks: Run Task
  11. Select Generate compile_commands.json from the list of tasks.
  12. Press CTRL+P for Tasks: Run Task
  13. Select Run QEMU and wait for QEMU to be ready for use
  14. Press CTRL+P for Tasks: Run Task and select kmod – load

    • This command will copy built kernel module to QEMU.
  15. Get back to QEMU shell in vscode terminal and use kernel module commands to test your new kernel module.
  16. Once you are done with development, use Dev Container: Close Remote Connection option.

kernel_in_vscode

dup2 usage

// A simple example of how dup2 can be used
const char* msg = "Text written by standard file descriptor\n";
int fd = open("dup2_test.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
write(fd, msg, strlen(msg));
/* Map stdout to our file */
dup2(fd, STDOUT_FILENO);
printf("Text written by dup @ stdout file descriptor\n");
close(fd);

view raw
dup2_usage.c
hosted with ❤ by GitHub

Linux Device Tree Compiler (DTC)

It has been a common practice to keep a device tree of a platform scattered across multiple DTS files. Seeing all of these DTS files as a single device tree is a nightmare.

Currently everyone is using DTC compiler to convert DTS file(s) into DTB file and then use it again to convert DTB into DTS file. The problem with this approach is that DTC compiler will convert alias/device name (string) into integer (phandle). This transformation makes final DTS file less readable.

To solve this problem, I have added a new option is added in forked DTC compiler. This new option (-m or –merge) allows to generate DTS very similar to DTB but keeps device
names/aliases in their original format.

There is an another option (-g or –ignore-dead) which will be useful alongwith –merge option. This option allows DTC compiler to ignore dead aliases otherwise it will generate an error on finding dead alias.

Here is the comparison of the DTB converted into DTS using mainline and forked DTC.

dtc1

dtc2

 

 

Handy commands to convert DTB into DTS using mainline and forked DTC.

# To convert dts file into dtb
<kernel-source>/scripts/dtc/dtc -I dts -O dtb <dts_file> -o <dtb_file>
# To convert dtb to dts
<kernel-source>/scripts/dtc/dtc -I dtb -O dts <dtb_file> -o <dts_file>
# Both of above commands are using dtc built with linux kernel source.
# However you can also use dtc binary comes with ubuntu packages. For this you
# need to install following package:
sudo apt-get install -y device-tree-compiler
# Ubuntu device-tree-compiler package comes with another utility known as fdtdump.
# fdtdump can also be used to convert dtb file into dts.
fdtdump <dtb_file> > <dts_dump>

view raw
dts <> dtb
hosted with ❤ by GitHub

# !/bin/bash
#
# This script uses forked dtc to generate flat device tree.
#
# Change these path as per your machine
COMPILER='arm-linux-gnueabihf-gcc'
KERNEL_FOLDER='/your/linux/source/code'
DTS_FILE_NAME='dra7-evm.dts'
FORKED_DTC='/forked/dtc/executable'
# Setup useful env
DTS_FOLDER=$KERNEL_FOLDER/arch/arm/boot/dts
DTS_INCLUDE=$DTS_FOLDER/include
KERNEL_INCLUDE=$KERNEL_FOLDER/include
DTS_FILE=$KERNEL_FOLDER/arch/arm/boot/dts/$DTS_FILE_NAME
echo "Generate flat dts file…(an intermediate file)"
$COMPILER -E -nostdinc -undef -D__DTS__ -x assembler-with-cpp -I$DTS_FOLDER -I$DTS_INCLUDE -o $DTS_FILE_NAME.flat $DTS_FILE
echo "Generate merge dts file…($DTS_FILE_NAME.merge)"
$FORKED_DTC/dtc -i$DTS_FOLDER -i$DTS_INCLUDE -I dts -O dts -m -g $DTS_FILE_NAME.flat -o $DTS_FILE_NAME.merge

view raw
dts <> dtb (forked)
hosted with ❤ by GitHub