consolidate all repos to one for archive
This commit is contained in:
27
semester_4/namenska_programska_oprema/Naloga_7/Makefile
Normal file
27
semester_4/namenska_programska_oprema/Naloga_7/Makefile
Normal file
@@ -0,0 +1,27 @@
|
||||
obj-m += xpo_xor_module.o
|
||||
|
||||
all: build
|
||||
|
||||
build:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
|
||||
clean:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
||||
|
||||
load:
|
||||
sudo insmod xpo_xor_module.ko
|
||||
|
||||
unload:
|
||||
sudo rmmod xpo_xor_module
|
||||
|
||||
read:
|
||||
sudo cat /dev/xpo_xor_module
|
||||
|
||||
write1:
|
||||
sudo echo "NikolaJeVelik" > /dev/xpo_xor_module
|
||||
|
||||
write2:
|
||||
sudo echo "1234567890NikolaJeVelokMozKiHodiNaFaksInJeVMariboruInSedajDelaNalogoSajmujedolgcajtinbirajepocelkajdrugega" > /dev/xpo_xor_module
|
||||
|
||||
zip:
|
||||
zip naloga.zip Makefile xpo_xor_module.c xpo_xor_module.h
|
170
semester_4/namenska_programska_oprema/Naloga_7/xpo_xor_module.c
Normal file
170
semester_4/namenska_programska_oprema/Naloga_7/xpo_xor_module.c
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* xpo_xor_module.c: Creates a read-only char device that says how many times
|
||||
* you have read from the dev file
|
||||
*/
|
||||
#include "xpo_xor_module.h"
|
||||
|
||||
/* Prototypes - this would normally go in a .h file */
|
||||
static int device_open(struct inode *, struct file *);
|
||||
static int device_release(struct inode *, struct file *);
|
||||
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
|
||||
static ssize_t device_write(struct file *, const char __user *, size_t, loff_t *);
|
||||
|
||||
#define SUCCESS 0
|
||||
#define DEVICE_NAME "xpo_xor_module" /* Dev name as it appears in /proc/devices */
|
||||
#define BUF_LEN 80 /* Max length of the message from the device */
|
||||
|
||||
/* Global variables are declared as static, so are global within the file. */
|
||||
static int major; /* major number assigned to our device driver */
|
||||
|
||||
enum {
|
||||
CDEV_NOT_USED = 0,
|
||||
CDEV_EXCLUSIVE_OPEN = 1,
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* Is device open? Used to prevent multiple access to device */
|
||||
static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
|
||||
|
||||
static char message[BUF_LEN + 1]; /* The message the device will give when asked */
|
||||
static struct class *cls;
|
||||
|
||||
static int __init xpo_xor_module_init(void)
|
||||
{
|
||||
major = register_chrdev(0, DEVICE_NAME, &xpo_xor_module_fops);
|
||||
if (major < 0) {
|
||||
pr_alert("Registering char device failed with %d\n", major);
|
||||
return major;
|
||||
}
|
||||
|
||||
pr_info("I was assigned major number %d.\n", major);
|
||||
|
||||
cls = class_create(THIS_MODULE, DEVICE_NAME);
|
||||
|
||||
device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
|
||||
|
||||
sprintf(message, "I already told you %d times Hello world!\n", 1);
|
||||
|
||||
pr_info("Device created on /dev/%s\n", DEVICE_NAME);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void __exit xpo_xor_module_exit(void)
|
||||
{
|
||||
device_destroy(cls, MKDEV(major, 0));
|
||||
|
||||
class_destroy(cls);
|
||||
|
||||
/* Unregister the device */
|
||||
unregister_chrdev(major, DEVICE_NAME);
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
/* Called when a process tries to open the device file, like
|
||||
* "sudo cat /dev/xpo_xor_module"
|
||||
*/
|
||||
|
||||
static int device_open(struct inode *inode, struct file *file)
|
||||
{
|
||||
|
||||
if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
|
||||
return -EBUSY;
|
||||
|
||||
|
||||
|
||||
try_module_get(THIS_MODULE);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Called when a process closes the device file. */
|
||||
static int device_release(struct inode *inode, struct file *file)
|
||||
{
|
||||
/* We're now ready for our next caller */
|
||||
atomic_set(&already_open, CDEV_NOT_USED);
|
||||
/* Decrement the usage count, or else once you opened the file, you will
|
||||
* never get rid of the module.
|
||||
*/
|
||||
|
||||
module_put(THIS_MODULE);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Called when a process, which already opened the dev file, attempts to
|
||||
* read from it.
|
||||
*/
|
||||
|
||||
static ssize_t device_read(struct file *filp,
|
||||
char __user *buffer, /* buffer to fill with data */
|
||||
size_t length, /* length of the buffer */
|
||||
loff_t *offset)
|
||||
|
||||
{
|
||||
/* Number of bytes actually written to the buffer */
|
||||
int bytes_read = 0;
|
||||
const char *message_ptr = message;
|
||||
|
||||
if (!*(message_ptr + *offset)) { /* we are at the end of message */
|
||||
*offset = 0; /* reset the offset */
|
||||
return 0; /* signify end of file */
|
||||
}
|
||||
|
||||
message_ptr += *offset;
|
||||
/* Actually put the data into the buffer */
|
||||
|
||||
while (length && *message_ptr) {
|
||||
/* The buffer is in the user data segment, not the kernel
|
||||
* segment so "*" assignment won't work. We have to use
|
||||
* put_user which copies data from the kernel data segment to
|
||||
* the user data segment.
|
||||
*/
|
||||
put_user(*(message_ptr++), buffer++);
|
||||
|
||||
length--;
|
||||
|
||||
bytes_read++;
|
||||
}
|
||||
*offset += bytes_read;
|
||||
/* Most read functions return the number of bytes put into the buffer. */
|
||||
return bytes_read;
|
||||
}
|
||||
static int xpo_msg_location = 0;
|
||||
|
||||
static uint8_t xpo_key[5] = {2,6,1,3,2};
|
||||
static uint8_t xpo_key_location = 0;
|
||||
static ssize_t device_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
|
||||
{
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
get_user(message[xpo_msg_location], buffer + i);
|
||||
|
||||
message[xpo_msg_location] ^= xpo_key[xpo_key_location];
|
||||
|
||||
xpo_key_location++;
|
||||
if(xpo_key_location > 5) xpo_key_location = 0;
|
||||
|
||||
xpo_msg_location++;
|
||||
if(xpo_msg_location > BUF_LEN - 1) xpo_msg_location = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
module_init(xpo_xor_module_init);
|
||||
module_exit(xpo_xor_module_exit);
|
||||
MODULE_LICENSE("GPL");
|
@@ -0,0 +1,30 @@
|
||||
|
||||
#ifndef _MY_DEVICE_H
|
||||
#define _MY_DEVICE_H
|
||||
|
||||
#include <linux/atomic.h>
|
||||
#include <linux/cdev.h>
|
||||
#include <linux/delay.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/kernel.h> /* for sprintf() */
|
||||
#include <linux/module.h>
|
||||
#include <linux/printk.h>
|
||||
#include <linux/types.h>
|
||||
#include <linux/uaccess.h> /* for get_user and put_user */
|
||||
#include <asm/errno.h>
|
||||
|
||||
static int device_open(struct inode *, struct file *);
|
||||
static int device_release(struct inode *, struct file *);
|
||||
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
|
||||
static ssize_t device_write(struct file *, const char __user *, size_t, loff_t *);
|
||||
|
||||
static struct file_operations xpo_xor_module_fops = {
|
||||
.read = device_read,
|
||||
.write = device_write,
|
||||
.open = device_open,
|
||||
.release = device_release,
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user