Skip to content

Instantly share code, notes, and snippets.

@farhangamary
Last active February 25, 2024 19:37
Show Gist options
  • Select an option

  • Save farhangamary/5318a83146be79a6955471d925a31313 to your computer and use it in GitHub Desktop.

Select an option

Save farhangamary/5318a83146be79a6955471d925a31313 to your computer and use it in GitHub Desktop.
A simple Kernel module in C - It reads the remaining Battery charge and logs it :)
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/power_supply.h>
static int __init hellow_init(void)
{
pr_info("Hello and welcome!");
struct power_supply *pws;
union power_supply_propval bat_info;
int ret;
pws = power_supply_get_by_name("BAT0");
if (!pws){
pr_err("Faild to get Battery - BAT0");
return -ENODEV;
}
ret = power_supply_get_property(pws, POWER_SUPPLY_PROP_CAPACITY, &bat_info);
if (ret < 0){
pr_err("Faild to get Battery info - charge remaining.");
return ret;
}
pr_info("Battery charge remaining: %d%% \n", bat_info.intval);
return 0;
}
static void __exit hellow_exit(void)
{
pr_info("Exiting Battary info Module - goodbye!");
}
module_init(hellow_init);
module_exit(hellow_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ME");
MODULE_DESCRIPTION("A hello world kernel module - written in C");
MODULE_VERSION("0.1");
#
#Instructions on how to make, load, check and unload.
#
#
############## check prerequisites ##########################
$ uname -r
6.5.0-18-generic
$ gcc --version
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
$ make --version
GNU Make 4.3
# You may need to install or reinstall the kernel headers too #####
$ sudo apt install linux-headers-6.5.0-18-generic
# You may even need to make config and prepare the source
:/usr/src/linux-hwe-6.5-headers-6.5.0-18$ sudo make oldconfig && sudo make prepare
############### Make the module ################################
$ sudo make
make -C "/lib/modules/6.5.0-18-generic/build" "M=/***/" modules
make[1]: Entering directory '/usr/src/linux-headers-6.5.0-18-generic'
CC [M] /***/hellow.o
MODPOST /***/Module.symvers
CC [M] /***/hellow.mod.o
LD [M] /***/hellow.ko
BTF [M] /***/hellow.ko
Skipping BTF generation for /***/hellow.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.5.0-18-generic'
#### The hellow.ko is the kernel module.########
################ load the module###############
$sudo insmod hellow.ko
################ check if it is loaded ########
$ sudo lsmod | grep hello
hellow 12288 0
############### unload the module ############
$ sudo rmmod hellow
################ check the kernel log #########
$ sudo dmesg | tail
***
***
***
[ 3698.854620] Hello and welcome!
[ 3698.866501] Battery charge remaining: 89%
################ clean the module ############
$ sudo make clean
make -C "/lib/modules/6.5.0-18-generic/build" "M=/***" clean
make[1]: Entering directory '/usr/src/linux-headers-6.5.0-18-generic'
CLEAN /***/Module.symvers
make[1]: Leaving directory '/usr/src/linux-headers-6.5.0-18-generic'
############################################################
# You may also need to disable the module signing temporarily if not signing it,
# for this you need to modify the grub file - located at:
# /etc/default/grub
# and
# add "module.sig_enforce=0" to GRUB_CMDLINE_LINUX_DEFAULT
# and then update it:
$sudo update-grub
# and then reboot the system.
# Do not forget to revert it again.
obj-m += hellow.o
all:
$(MAKE) -C "/lib/modules/6.5.0-18-generic/build" "M=/path_to_where_hellow.c_is_located/" modules
clean:
$(MAKE) -C "/lib/modules/6.5.0-18-generic/build" "M=/path_to_where_hellow.c_is_located/" clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment