Skip to content

Instantly share code, notes, and snippets.

@smalinux
Last active January 18, 2021 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smalinux/71c7af7802691739d200fb90c1211f67 to your computer and use it in GitHub Desktop.
Save smalinux/71c7af7802691739d200fb90c1211f67 to your computer and use it in GitHub Desktop.
Embox hacking - 101 tutorial
From 39991fa0f75e7e6302f17e4882ff2d2a18f36cae Mon Sep 17 00:00:00 2001
From: Sohaib Mohammed <sohaib.amhmd@gmail.com>
Date: Sat, 16 Jan 2021 06:48:31 +0200
Subject: [PATCH 1/1] mychar - hello world character device
---
src/kernel/Mybuild | 7 +++++++
src/kernel/mychar.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
create mode 100644 src/kernel/mychar.c
diff --git a/src/kernel/Mybuild b/src/kernel/Mybuild
index 70683c113..567b7b4f2 100644
--- a/src/kernel/Mybuild
+++ b/src/kernel/Mybuild
@@ -18,3 +18,10 @@ module Kernel {
depends embox.compat.libc.assert
depends embox.init.system_start
}
+
+module mychar {
+ source "mychar.c"
+
+ depends embox.fs.driver.devfs
+ depends embox.driver.char_dev
+}
diff --git a/src/kernel/mychar.c b/src/kernel/mychar.c
new file mode 100644
index 000000000..5aaaf6b4c
--- /dev/null
+++ b/src/kernel/mychar.c
@@ -0,0 +1,31 @@
+/**
+ * @file
+ * brief hello world char dev
+ *
+ * @author Sohaib Mohammed
+ * @date
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/uio.h>
+#include <sys/stat.h>
+
+#include <drivers/char_dev.h>
+
+#include <util/err.h>
+#include <kernel/printk.h>
+
+
+
+static struct idesc *mychar_open(struct dev_module *cdev, void *priv) {
+ //struct dev_module *cdev;
+
+ printk("\n===================================\n");
+ printk("mychar: hi!");
+ printk("\n===================================\n");
+ return char_dev_idesc_create(cdev);
+}
+
+CHAR_DEV_DEF("mychar", mychar_open, NULL, NULL, NULL);
--
2.29.2

Embox hacking - quick start tutorial

Embox/Demo/website
touch src/demo/website/test_favicon.ico
# endit src/demo/website/Mybuild && add test_favicon.ico to it
# compile & run
Add new cmd (ex: cpuinfo)

I made mycpuinfo command acts like cpuinfo

# touch src/cmds/mycpuinfo.c
# cp src/cmds/cpuinfo.my src/cmds/Mycpuinfo.my
&& endit the file: `mdoule mycpuinfo {`
# add `include embox.cmd.mycpuinfo` to your conf/mods.conf
# compile & run

fortunately MyBuild could detect any nested dir automatically, So I could made mycpuinfo command in /src/cmds/1/2/3 dir.

Invoke snake game

Add this line to conf/mods.conf

include embox.cmd.games.snake

that’s it. compile & run.

what is exactely make confload-x86/qemu

cp templates/x86/qemu/mods.conf conf/mods.conf

+ All your configuration is here: embox/conf/

How to hack libc

Example: make your own printf function

# touch src/compat/libc/stdio/myprintf.c
# add it to src/compat/libc/stdio/MyBuild
# and don't forget to declare your function in stdio.h ))
How to hack embox kernel

edit file: src/kernel/init and this two lines

+ #include <kernel/printk.h>

static void kernel_init(void) {
+	printk("printk: Hello, from kernel_init\n");

Now your are kernel hacker Congratulations! : ))

Use GDB to catch ls command

After the following isntractions https://github.com/embox/embox#debugging then:

(GDB) b ls.c:main
(gdb) win
(gdb) c
let make simple char device

Apply this patch. yes. to make every changes simple and clean.

$ git am --signoff 0001-mychar-hello-world-character-driver.patch
# add this "@Runlevel(2) include embox.kernel.mychar" to conf/mods.conf

Compile & run: ls /dev

If you want to read more char samples: $ git grep "CHAR_DEV_DEF"

How Embox organized

$ find . -depth -name "include":

./src/arch/x86/include
./src/arch/xen/include
./src/compat/cxx/include
./src/compat/libc/include
./src/compat/linux/include
./src/compat/posix/include
./src/include

$ find . -depth -name "fs":

./src/cmds/fs
./src/compat/posix/fs
./src/fs
./src/include/fs
./src/tests/fs
Unit testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment