Posts

Showing posts with the label linux

Notification Chains in Linux Kernel

Notification chains in Linux kernel is a mechanism for the subsystems being informed by the events generated by the other subsystems. In other words, if you are developing a new driver in Linux and you want to listen some events from other driver and handle these events in your own driver, you need to use the Linux's notification chains mechanism. As an example, if you develop a driver and this driver needs to be informed about the net device events such as net device UP, DOWN events, the driver needs to register itself to the net device notifications with the  register_netdevice_notifier function. Similarly, it unregisters itself with the unregister_netdevice_notifier function. These functions are provided by the net device subsystem. The APIs and the data structures of notification chains are defined in the include/linux/notifier.h and  kernel/notifier.c files under linux source code. In this post, I am going to explain how this notification chains mechanism wo...

List Handling in Linux Kernel Development

If you want to get a brief information about how to use Linux's linked list data structure, please see the below sample code. You can check-out the code from the github location, build and run on your own Linux development environment. /* * my_object structure. */ struct my_object { int data; struct list_head list; }; /* * list_init() */ static int list_init ( void ) { struct my_object *tmp; /* Temporary object created for each list member */ struct my_object obj_list; /* List constructed with the tmp objects */ struct list_head *pos, *n; /* Position pointers in the list */ int i; printk(KERN_INFO "list init \n " ); /* * Initialize the list object. This object is used only as the * head of the list. It doesn't have any data in it. */ INIT_LIST_HEAD(&obj_list.li...

How to Use container_of in Linux Kernel Development

In this post, I will explain the usage of linux's container_of macro with a sample code. If you know address of a data structure's field, you can get the address of the instance of that data structure by using the container_of macro. This is the simplest explanation of this macro. Let's say you have a function called with a pointer parameter and you know that the pointer is pointing an address of a data structure field. struct my_object { int a; int b; } obj; function(&obj->b); If you want to access the other field "a" in the function, you need to use container_of macro. Let's see the below sample code and it's output to understand it easily. /* * my_object structure. */ struct my_object { int a; int b; int c; }; /* * container_test() * Test function for usage of container_of macro. * * This function is passed a pointer which points a field of * an allocated my_obj...

Build and Flash OpenWrt for Raspberry Pi

Image
OpenWrt is an open source project for embedded operating system based on Linux, primarily used on embedded devices to route network traffic. [ 1 ] In this post, I will write about how to get the OpenWrt source, compile it for your Raspberry Pi board type and flash the SD card with the generated image. First of all, you need a Linux build environment installed with the requirements needed to build the OpenWrt. I use Ubuntu 14.04. In the market, there are 4 types of Raspberry Pi boards. Type 1, 2, 3 and Zero. OpenWrt's latest trunk branch supports all of these types. I will go through trunk. Check out the source: git clone git://github.com/openwrt/openwrt.git This will create a directory called "openwrt" in your current directory. Go inside this directory and run make package/symlinks command. msezgin@ubuntu:~/openwrt/make package/symlinks Now, we need to do the configuration before starting the build. msezgin@ubuntu:~/openwrt/make menuconfig ...

Linux Dynamic Debugging

Image
In Linux programming, it is very important to debug your code by adding some debug messages to your code. By using the dynamic debug feature of Linux, you can enable these debug messages per file, per function and even per line.  This post will show you how to do this with a simple kernel loadable module implementation. First, you need compile your kernel with the CONFIG_DYNAMIC_DEBUG option. It is under " Kernel hacking " menu and prompt name is " E nable dynamic printk() support " as you see on the below screenshot. Sample Code: TBD Enable/Disable dynamic debug in the sample code: TBD

Linux Debugfs implementation as a userspace interface

debugfs is a Linux file system. It is a user space interface to and from the Linux kernel. It is mounted at /sys/kernel/debug.  The kernel must be compiled with the   CONFIG_DEBUG_FS option to use this file system. It exists in the Linux kernel since version 2.6.10-rc3 [1] In the below example, you will see an example to a debugfs. A 32-bit flag is created under the /sys/kernel/debug/debugfs_ex directory called enable for read and write. debugfs_ex.c #include <linux/version.h> #include <linux/module.h> #include <linux/debugfs.h> static struct dentry * dn; /* Dentry object for debugfs directory */ static int enable; /* Enable flag for debugfs_ex module */ /* * debugfs_ex_init() */ static int __init debugfs_ex_init ( void ) { printk(KERN_INFO "debugfs_ex init \n " ); dn = debugfs_create_dir( "debugfs_ex" , NULL ); if ( ! dn) { printk(KERN_ERR "Failed to create debugfs_ex directory in debugfs \n " ); ...

TFTP server for u-boot on Ubuntu

Install the required packages. $ sudo apt-get install xinetd tftpd-hpa tftp Create the /etc/xinetd.d/tftp file with the following content. service tftp { protocol = udp socket_type = dgram wait = yes user = root server = /usr/sbin/in.tftpd server_args = -s /tftpboot -c disable = no } Create /tftpboot directory. $ sudo mkdir /tftpboot $ sudo chmod -R 777 /tftpboot $ sudo chown -R nobody /tftpboot Modify the /etc/default/tftpd-hpa file as below. # /etc/default/tftpd-hpa TFTP_USERNAME="nobody" TFTP_DIRECTORY="/tftpboot" TFTP_ADDRESS="0.0.0.0:69" TFTP_OPTIONS="--secure" Start tftp with xinetd $ sudo /etc/init.d/xinetd start Configure your server's IP address with the serverip environment variable in your u-boot configuratio...

Increase yank buffer size of your vi editor

If you think that your vi editor's yank buffer size is not enough, you can increase it with the following vi command. :set  v iminf o='1000,h This command will increase the buffer size to 1000 lines. .