forked from PacktPublishing/Linux-Kernel-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genlkm
executable file
·121 lines (104 loc) · 2.6 KB
/
genlkm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# genlkm
# **********************************************************************
# This program is part of the source code released for the book
# "Linux Kernel Programming"
# (c) Author: Kaiwan N Billimoria
# Publisher: Packt
# GitHub repository:
# https://github.com/PacktPublishing/Linux-Kernel-Programming
# **********************************************************************
# Brief Description:
# Generate a simple Linux LKM (Loadable Kernel Module) 'template'.
# For details, please refer the book, Ch 4.
name=$(basename "$0")
usage()
{
echo "${name} is a simple Linux kernel module 'generator' script.
(It internally invokes the xcc_lkm.sh script as well).
Usage: ${name} mykmod
; where \"mykmod\" is a filename (without any extension)
This helper script will create a directory under the current one named \"mykmod\"
and will then generate two files within it:
(i) the \"mykmod\".c kernel module 'template' file, and
(ii) the Makefile for it.
Enjoy!"
}
[ $# -lt 1 ] && {
usage
exit 1
}
# Check all params for a "."
for param in "$@"
do
if [[ "${param}" = *"."* ]]; then
echo "*** Error: do Not use any extension or \".\", thank you! ***"
usage
exit 1
fi
done
#------------- File Sections ------------------------
HDR_1="/*
* $1.c
***************************************************************
* This program is <insert your comments here ... >
****************************************************************
* Brief Description:
*
*/
"
INC_1="
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
"
MAC_1="
#define OURMODNAME \"$1\"
"
MOD_STUFF="
MODULE_AUTHOR(\"<insert your name here>\");
MODULE_DESCRIPTION(\"<insert your module desc here>\");
MODULE_LICENSE(\"Dual MIT/GPL\");
MODULE_VERSION(\"0.1\");
"
CODE_1="
static int __init $1_init(void)
{
pr_debug(\"%s: inserted\n\", OURMODNAME);
return 0; /* success */
}
static void __exit $1_exit(void)
{
pr_debug(\"%s: removed\n\", OURMODNAME);
}
module_init($1_init);
module_exit($1_exit);"
#---
if [ ! -d "$1" ] ; then
mkdir -p "$1"
else
echo "${name}: the directory $1/ already exists, aborting..."
exit 1
fi
# Within a sub-shell ...
(
cd "$1" || exit 1
[ -f "$1".c ] && cp -f "$1".c "$1".c.bkp
cat > "$1".c << EOF
${HDR_1}
${INC_1}
${MAC_1}
${MOD_STUFF}
${CODE_1}
EOF
echo "[+] LKM $1/$1.c generated"
ls -l "$1".c
# If our build /cross-compile script is available, lets use it
which xcc_lkm.sh >/dev/null 2>&1 || {
echo "[!] xcc_lkm.sh script not found (not in PATH?), unable to generate Makefile"
exit 1
}
echo "[+] Generating the $1/Makefile for $1.c (via xcc_lkm.sh)..."
xcc_lkm.sh "$1"
)
exit 0