Tuesday, November 17, 2009

Simplicity

Here is a simple kernel module, demonstrating how easy it is to write and build one.


kmod.c

#include <kernel.h>
#include <errors.h>
#include <timers.h>
#include <usrProcess.h>





Err kmod_main(UInt32 reason, void* data){

UInt64 time = timersGetTicks64();

if (reason == K_MOD_LOADED){

charsPrintF("Module loaded at 0x%08lx\n", time);
}
else if(reason == K_MOD_UNLOADED){

charsPrintF("Module unloaded at 0x%08lx\n", time);
}
else{

charsPrintF("Module called with unknown command (0x%x,0x%08x)\n", reason, data);
}

return errNone;
}



Makefile

LIBNAME = kmod.kmod
CC = arm-linux-elf-gcc
FLAGS = -O4 -fpic -fshort-wchar -mthumb-interwork
SLIBS =
DLIBS = ../KernelLib/KernelLib.lib
CCFLAGS = $(FLAGS) -I../Src/public -I../Src
LDFLAGS = $(FLAGS) -nostartfiles -nodefaultlibs -nostdlib $(DLIBS) $(SLIBS) \
-Wl,-shared -Wl,-soname,$(LIBNAME)




kmod.kmod: kmod.o
$(CC) kmod.o -o kmod.kmod $(LDFLAGS) -e kmod_main

kmod.o: kmod.c
$(CC) $(CCFLAGS) -o kmod.o -c kmod.c

clean:
rm -rf *.o kmod.kmod

No comments: