Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- svaka skripta pocinje sa #!/bin/bash
- pokretanje: 1) bash myscript
- 2) chmod +x myscript
- ./myscript
- $LOGNAME - ime korisnika
- $(hostname) - ime sistema
- davanje vrednosti varijablama bez razmaka x=y
- citanje varijable $x
- neke od dostupnih varijabli BASH_VERSION,HOSTNAME,PPID,PWD,UID,LINES,PATH...
- pazi na razmake kad postavljas varijable
- --------petlje---------
- -----------------------
- if [[]]
- something
- -----------
- if COMMANDS
- then OTHER COMMANDS
- fi
- if COMMANDS
- then
- OTHER COMMANDS
- fi
- if COMMANDS; then
- OTHER COMMANDS
- fi
- ------------------------
- while true
- do
- done
- ------------------------
- continue samo prebaci u sledecu iteraciju
- break iskace iz petlje
- ------------------------
- case condition
- :radi nesto:
- esac
- ------------------------
- >$0 stdin
- >$1 stdout
- >$2 stderr
- Upis u fajl:
- echo "Hello" > file
- ako hocemo da ispisemo nesto na stdout cat < file
- >> dodaje na kraj fajla a > prepisuje fajl
- -------------------------------
- GREP
- -------------------------
- -r uzima i nazive direktorijuma
- grep -r string home/
- grep "$HOSTNAME" /etc/*
- **************************************************************
- ------------
- VEZBA 4.1
- ------------
- klonira se kernel sa gita
- raspakuje se a zatim se raspakuje RFS
- $sudo tar xvjf linux-kernel-labs/modules/rootfs.tar.bz2 -C linux-kernel-labs/modules/nfsroot
- postave se vlasnici
- $sudo chown -R rtrk.rtrk linux-kernel-labs/modules/nfsroot/root
- pristup u-boot i terminalu raspberry-a
- $picocom -b 115200 /dev/ttyUSB0
- postave se varijable u u-boot
- $setenv ipaddr <IP rpi>
- $setenv serverip <IP pc>
- $saveenv
- kopiraju se zImage i bcm2709-rpi-2-b.dtb u /var/lib/tftpboot
- (ovde imamo vec buildovan image kernela)
- $sudo cp ~/linux-kernel-labs/bootloader/rpi-2-b/zImage /var/lib/tftpboot
- $sudo cp ~/linux-kernel-labs/bootloader/rpi-2-b/bcm2709-rpi-2-b.dtb /var/lib/tftpboot
- treba i restartovati tftpboot(samo prvi put po instalaciji???)
- $sudo service tftpd-hpa restart
- ------Podesavanje NFS servera--------
- izmeniti /etc/exports/
- $sudo nano /etc/exports/
- i dodati
- /home/rtrk/linux-kernel-labs/modules/nfsroot 10.85.32.182(rw,no_root_squash,no_subtree_check)
- restartovati NFS
- $sudo /etc/init.d/nfs-kernel-server restart
- u u-boot promeniti bootargs
- $setenv bootargs "root=/dev/nfs rw ip=10.85.32.182 console=ttyAMA0,115200 nfsroot=10.85.32.75:/home/rtrk/linux-kernel-labs/modules/nfsroot"
- $saveenv
- za automatsko pokretanje bootovanja
- $setenv bootcmd "tftp 0x01000000 zImage; tftp 0x02000000 bcm2709-rpi-2-b.dtb; bootz 0x01000000 - 0x02000000"
- $saveenv
- *****************************************************
- Prevodjenje kernela i zakrpe
- pozicionira se u ~/linux-kernel-labs/src
- klonira se sa gita linux folder
- patchuje se kernel sa
- $sudo apt-get install patch
- i pozove dok smo u linux folderu (VALJDA OVAKO???)
- $patch -p1 ~/linux-kernel-labs/modules/data/Linuks.patch
- da bi se menjala konfiguracija kernela prvo se postavi
- $export ARCH=arm
- $export CROSS_COMPILE=arm-linux-gnueabihf-
- u patchovanom kernel direktorijumu primeni se default konfiguracija
- $make bcm2709_rtrk_linux_kurs_defconfig
- zatim
- $make xconfig
- i onda vrsimo izmene
- alternativa je make menuconfig
- kad smo zavrsili opicimo make
- kopiraju se zImage i bcm2709-rpi-2-b.dtb u /var/lib/tftpboot/
- posto u kernel nije ukljucena podrska za nfs treba podesiti da se RFS preuzima sa kartice
- $setenv bootargs "root=/dev/mmcblk0p2 rootwait console=tty1 console=ttyAMA0,115200"
- $boot
- ********************************************************
- Konfigurisanje i prevodjenje kernela
- koriscenje date konfiguracije... dok smo u ~/linux-kernel-labs/src/linux
- $sudo cp ~/linux-kernel-labs/modules/data/Linux.config .config
- $make xconfig (make menuconfig)
- $make
- pazi na putanje u /etc/exports
- restartuj nfs-kernel-server
- pogledaj bootargs
- kopiraj zImage i ono drugo govno
- pokreni kernel
- Dodavanje uredjaja
- idi u ~/linux-kernel-labs/modules/nfsroot
- $sudo mknod /dev/ttyAMA0 c MINOR MAJOR
- dodaj jos ako treba nesto
- bootuj
- ********************************************************
- Pravljenje i pokretanje ugradjenog sistema(busybox raspali)
- nalazim se u src folderu
- $wget link do busybox arhive
- raspakuj arhivu
- $sudo tar -xvjf busybox.tar.bz2 <dest dir>
- sad ovde u linux folderu uradim
- $sudo cp ~/linux-kernel-labs/modules/data/tinyLinux.config .config
- $make
- a u busybox folderu
- $sudo cp ~/linux-kernel-labs/modules/data/tinyBusybox.config .config
- ukljuciti opcije i dodati putanju preko
- $sudo make menuconfig
- $make
- $make install
- za rfs koristim dir gde je busybox
- tj podesim sve putanje u /etc/exports i u bootargs
- restartujem nfs
- mozda treba i kopirati zImage i dtb???
- -----------------
- Dodavanje proc i sys sistema datoteka
- U nfsroot folderu napraviti foldere proc/ sys/ etc/init.d
- U etc/ folderu napraviti fajl inittab i popuniti ga sa
- ::sysinit:/etc/init.d/rcS
- ::askfirst:/bin/sh
- da bi se izbegla greska "/bin/sh: can't access tty; job control turned off"
- treba pisati ttyAMA0::askfirst:/bin/sh, ali tada mora da postoji /dev
- sa console i ttyAMA0, kao sto mora i za slucaj da se radi sa initramfs
- mkdev dev/console c 5 1
- mkdev dev/ttyAMA0 c 204 64
- U etc/init.d/ folderu napraviti fajl rcS i popuniti ga sa
- #!/bin/sh
- mount -t proc none /proc
- mount -t sysfs none /sys
- za initramfs, u rfs treba preimenovati linuxrc u init i
- CONFIG_INITRAMFS_SOURCE postaviti na putanju do napravljenog rfs u
- bootargs treba postaviti samo parametre rw i console(=ttyAMA0)
- ----------------
- Implementacija web sprege
- ------------
- Kopirati direktorijum ~linux-kernel-labs/modules/data/www u nfsroot
- pokrenuti posluzioc u pi-u
- $/usr/sbin/httpd -h /www/ &
- u linuxu otvoriti
- $http://<ip rpi>
- popraviti pozive ka proc unutar web sprege???????
- VIDI NA VEZBAMA
- ---------------------
- Staticke i dinamicke biblioteke
- ---------------------
- Za staticke napravi helloworld.c
- napravi Makefile
- sve u nfsroot folderu
- pokreni na rpi
- ZA DINAMICKE VIDI NA VEZBAMA A I ZA STATICKE KAKO SE PRAVI Makefile
- ---------------------
- Generisanje INITRAMFS
- -------------------
- Iskoristiti generisani rfs kao i izvorne datoteke za generisanje initramfs-a
- (CONFIG_INITRAMFS_SOURCE) i ponovo prevedi kernel
- preimenuj linuxrc u init u nfsroot
- nemoj zaboraviti kopirati image i dtb kao i /etc/exports/ ako treba?????
- izmeni bootargs da ostane samo rw i console
- pokreni initramfs na RPI
- po pokretanju dodaj sve sto je potrebno da za funkcionalnost kao kad je rfs bio na mrezi
- ???????????????????????????????????????????????
- POGLEDAJ NA VEZBAMA
- ************************************************
- Pisanje i prevodjenje modula
- ----------------------
- exportuj ARCH i CROSS_COMPILE
- $make bcm2709_defconfig
- nalazis se u linux folderu
- $make
- kopiraj image i dtb
- promeni /etc/exports i bootargs
- restartuj ono sranje
- bootuj RPI
- u nfsroot/root/hello
- izmeni hello_version.c
- POGLEDAJ NA VEZBAMA KAKO SI URADIO
- podesi Makefile
- exportuj ARCH i CROSS_COMPILE
- prevedi ($make)
- vidi na RPI jel radi
- insmod naziv_modula
- rmmod naziv_modula
- lsmod
- ako treba dodaj dodaj u dev/ potrebne nodove
- Za znakovni rukovalac(chardev) kopirati iz sppurva onu vezbu
- namestiti Makefile i prevesti
- POGLEDAJ KAKO SE RADI NA VEZBAMA I ZAPISI KOMANDE
- ZAVRSI VEZBU I ZAPISI KOMANDE :D
- *************************************************
- /dts-v1/;
- #include "bcm2709.dtsi"
- / {
- compatible = "brcm,bcm2709";
- model = "Raspberry Pi 2 Model B";
- };
- &gpio {
- i2c1_pins: i2c1 {
- brcm,pins = <2 3>;
- brcm,function = <4>;
- };
- };
- &uart0 {
- status = "okay";
- };
- &i2c1 {
- pinctrl-names = "default";
- pinctrl-0 = <&i2c1_pins>;
- clock-frequency = <100000>;
- status = "okay";
- nunchuk: nunchuk@52 {
- compatible = "nintendo,nunchuk";
- reg = <0x52>;
- };
- };
- / {
- __overrides__ {
- uart0 = <&uart0>,"status";
- uart0_clkrate = <&clk_uart0>,"clock-frequency:0";
- i2c1 = <&i2c1>,"status";
- i2c1_baudrate = <&i2c1>,"clock-frequency:0";
- core_freq = <&clk_core>,"clock-frequency:0";
- };
- };
- ************************************************************
- /* Download it from http://j.mp/19nnhkq */
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/i2c.h>
- #include <linux/delay.h>
- static int nunchuk_read_registers(struct i2c_client *client,
- u8 *recv)
- {
- u8 buf[1];
- int ret;
- /* Ask the device to get ready for a read */
- mdelay(10);
- buf[0] = 0x00;
- ret = i2c_master_send(client, buf, 1);
- if (ret != 1) {
- dev_err(&client->dev, "i2c send failed (%d)\n", ret);
- return ret < 0 ? ret : -EIO;
- }
- mdelay(10);
- /* Now read registers */
- ret = i2c_master_recv(client, recv, 6);
- if (ret != 6) {
- dev_err(&client->dev, "i2c recv failed (%d)\n", ret);
- return ret < 0 ? ret : -EIO;
- }
- return 0;
- }
- static int nunchuk_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- u8 recv[6], buf[2];
- int ret, zpressed, cpressed;
- pr_info("Called nunchuk_probe\n");
- /* Initialize device */
- buf[0] = 0xf0;
- buf[1] = 0x55;
- ret = i2c_master_send(client, buf, 2);
- if (ret != 2) {
- dev_err(&client->dev, "i2c send failed (%d)\n", ret);
- return ret < 0 ? ret : -EIO;
- }
- udelay(1000);
- buf[0] = 0xfb;
- buf[1] = 0x00;
- ret = i2c_master_send(client, buf, 2);
- if (ret != 2) {
- dev_err(&client->dev, "i2c send failed (%d)\n", ret);
- return ret < 0 ? ret : -EIO;
- }
- /* Make a dummy read from the device
- The device seems to update the state of its internal
- registers only once they have been read */
- if ((ret = nunchuk_read_registers(client, recv)) < 0)
- return ret;
- /* Now get the real state of the device */
- if ((ret = nunchuk_read_registers(client, recv)) < 0)
- return ret;
- zpressed = (recv[5] & BIT(0)) ? 0 : 1;
- if (zpressed)
- dev_info(&client->dev, "Z button pressed\n");
- cpressed = (recv[5] & BIT(1)) ? 0 : 1;
- if (cpressed)
- dev_info(&client->dev, "C button pressed\n");
- return 0;
- }
- static int nunchuk_remove(struct i2c_client *client)
- {
- pr_info("Called nunchuk_remove\n");
- return 0;
- }
- static const struct i2c_device_id nunchuk_id[] = {
- { "nunchuk", 0 },
- { },
- };
- MODULE_DEVICE_TABLE(i2c, nunchuk_id);
- #ifdef CONFIG_OF
- static struct of_device_id nunchuk_dt_match[] = {
- { .compatible = "nintendo,nunchuk" },
- { },
- };
- #endif
- static struct i2c_driver nunchuk_driver = {
- .driver = {
- .name = "nunchuk",
- .owner = THIS_MODULE,
- .of_match_table = of_match_ptr(nunchuk_dt_match),
- },
- .probe = nunchuk_probe,
- .remove = nunchuk_remove,
- .id_table = nunchuk_id,
- };
- module_i2c_driver(nunchuk_driver);
- MODULE_LICENSE("GPL");
- ************************************************************
- #Kernel moduli se prevode upotrebom Linux Kernel Build (Kbuild) okruzenja. Nakon uspesnog
- #prevodjenja u korenom (root) direktorijumu projekta treba da se pojavi .ko datoteka koja
- #predstavlja prevedeni kernel modul u binarnom obliku.
- -------------------------------------------------------------------------------------------
- #nothing.ko
- #Prijavljivanje (registrovanje) modula u Linux kernel - insmod komanda
- insmod ./nothing.ko
- #Komandom lsmod omoguceno je izlistavanje svih modula koji se trenutno nalaze u kernelu.
- lsmod
- #Nakon izlistavanja u spisku svih modula, ukoliko je modul uspesno preveden i registrovan,
- #moze se naci nothing modul. (Pomoc: lsmod | grep "nothing")
- #Brisanje modula iz kernela - rmmod komanda
- rmmod nothing
- #Ukoliko ponovo izlistate sve module videcete da modul nothing ne postoji u listi modula.
- -------------------------------------------------------------------------------------------
- #hello1.ko
- #Registrovanje modula.
- insmod ./hello1.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Kako bi se omogucilo pracenje log poruka koje su definisane unutar kernel modula
- #(printk funkcija) potrebno je uraditi jedno od sledeceg:
- # 1) Upotrebiti dmesg komandu
- # 2) Pristupiti (sudo) posebnom uredjaju /dev/kmsg unutar OS Linux koji omogucuje
- # pracenje log poruka.
- #
- #dmesg: Ova komanda omogucuje izlistavanje svih log poruka koje su se desile unutar Linux
- # kernela. Nakon zavrsetka dmesg komande korisnicki terminal postaje ponovo dostupan
- # korisniku za poziv novih komandi - dmesg komanda nije blokirajuca. Brisanje svih
- # log poruka se vrsi pozivom "dmesg -c".
- #
- #/dev/kmsg: Da bi se pristupilo log porukama pomocu uredjaja /dev/kmsg potrebno je pozvati
- # "sudo cat /dev/kmsg" komandu. Na ovaj nacin se iscitavaju samo nove poruke, tj.
- # ne izlistavaju se sve poruke koje su bile. Za razliku od upotrebe dmesg komande
- # ovaj nacin iscitavanja je blokirajuci. Da bi se oslobodio korisnicki terminal
- # potrebno je pozvati "Ctrl + C".
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Hello world 1
- #Brisanje modula.
- rmmod hello1
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Goodbye world 1
- -------------------------------------------------------------------------------------------
- #hello2.ko
- #Registrovanje modula.
- insmod ./hello2.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Hello world 2
- #Brisanje modula.
- rmmod hello2
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Goodbye world 2
- -------------------------------------------------------------------------------------------
- #hello3.ko
- #Registrovanje modula.
- insmod ./hello3.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Hello world 3
- #Brisanje modula.
- rmmod hello3
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Goodbye world 3
- -------------------------------------------------------------------------------------------
- #hello4.ko
- #Registrovanje modula.
- insmod ./hello4.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Hello world 4
- #Izlistavanje dokumentacije modula - modinfo komanda.
- modinfo ./hello4.ko
- #Izlaz.
- filename: ./hello4.ko
- description: A simple driver
- author: RT-RK
- license: GPL
- srcversion: 6EF93F994B70D0795902EC7
- depends:
- vermagic: 3.8.0-30-generic SMP mod_unload modversions 68
- #Brisanje modula.
- rmmod hello4
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Goodbye world 4
- -------------------------------------------------------------------------------------------
- #hello5.ko
- #Registrovanje modula.
- insmod ./hello5.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Izlistavanje kernel log-a.
- dmesg
- #Izlaz.
- Hello world 5
- myShort is a short integer: 1
- myInt is an integer: 100
- myLong is a long integer: 1000
- myString is a string: initString...
- myIntArray[0] = -1
- myIntArray[1] = -1
- Got 0 arguments for myIntArray.
- #Izlistavanje dokumentacije modula - modinfo komanda.
- modinfo ./hello5.ko
- #Izlaz.
- filename: ./hello5.ko
- description: Demonstrates command line argument passing to a kernel module
- author: RT-RK
- license: GPL
- srcversion: C4A2BE7854E02AA2DAD4224
- depends:
- vermagic: 3.8.0-30-generic SMP mod_unload modversions 686
- parm: myShort:A short integer. (short)
- parm: myInt:An integer. (int)
- parm: myLong:A long integer. (long)
- parm: myString:A character string. (charp)
- parm: myIntArray:An array of integers. (array of int)
- #Brisanje modula.
- rmmod hello5
- #Izlistavanje kernel log-a.
- dmesg
- #Poslednja linija u terminalu.
- Goodbye world 5
- #Registrovanje modula sa parametrima.
- insmod hello5.ko myShort=234 myInt=12345 myLong=9123456 myString="test" myIntArray=7,8
- #Izlistavanje kernel log-a.
- dmesg
- #Izlaz (primetite vrednosti parametara).
- Hello world 5
- myShort is a short integer: 234
- myInt is an integer: 12345
- myLong is a long integer: 9123456
- myString is a string: test
- myIntArray[0] = 7
- myIntArray[1] = 8
- Got 2 arguments for myIntArray.
- #Brisanje modula.
- rmmod hello5
- -------------------------------------------------------------------------------------------
- #chardev.ko
- #Registrovanje modula.
- insmod ./chardev.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Izlistavanje kernel log-a.
- dmesg
- #Izlaz:
- I was assigned major number 250. To talk to
- the driver, create a dev file with
- 'mknod /dev/chardev c 250 0'.
- Try various minor numbers. Try to cat and echo to
- the device file.
- Remove the device file and module when done.
- #Definisanje cvora (node) novog uredjaja u OS Linux preko kog se omogucuje pristup
- #uredjaju. Pravnjenje novog cvora se vrsi upotrebom mknod komande.
- mknod /dev/chardev c 250 0
- #Citanje uredjaja upotrebom cat comande.
- cat /dev/chardev
- #Izlaz.
- I already told you 0 times Hello world!
- #Citanje uredjaja upotrebom cat comande.
- cat /dev/chardev
- #Izlaz.
- I already told you 1 times Hello world!
- #Pisanje u uredjaj upotrebom echo komande.
- echo "test" >> /dev/chardev
- #Izlistavanje kernel log-a.
- dmesg
- #Izlaz (poslednja linija).
- Sorry, this operation isn't supported.
- #Brisanje modula.
- rmmod chardev
- #Brisanje cvora uredjaja.
- rm /dev/chardev
- -------------------------------------------------------------------------------------------
- #memory.ko
- #Registrovanje modula.
- insmod ./memory.ko
- #Proveravamo da li je modul uspesno registrovan.
- lsmod
- #Izlistavanje kernel log-a.
- dmesg
- #Izlaz:
- Inserting memory module
- #Definisanje cvora novog uredjaja.
- mknod /dev/memory c 60 0
- #Pisanje u uredjaj upotrebom echo komande.
- echo "test" >> /dev/memory
- #Citanje uredjaja upotrebom cat comande.
- cat /dev/memory
- #Izlaz (korisnicki terminal).
- test
- #Brisanje modula.
- rmmod memory
- #Izlistavanje kernel log-a.
- dmesg
- #Izlaz:
- Removing memory module
- #Brisanje cvora uredjaja.
- rm /dev/memory
- -------------------------------------------------------------------------------------------
- #memory_test_app
- #Pozivanje testnog programa(potrebno je pozicionirati se u memory_test_app/bin/Release).
- ./memory_test_app
- #Izlaz.
- '/dev/memory' device is successfully opened!
- Written message: '/dev/memory' test message!!!
- ret_val: 30
- Read message: '/dev/memory' test message!!!
- ret_val: 30
- -------------------------------------------------------------------------------------------
- ************************************************************************
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/types.h>
- #include <linux/fcntl.h>
- #include <linux/proc_fs.h>
- #include <asm/uaccess.h>
- #include <linux/string.h>
- #include <linux/slab.h>
- MODULE_LICENSE("Dual BSD/GPL");
- #define BUF_LEN 80
- /* Declaration of memory.c functions */
- void memory_exit(void);
- int memory_init(void);
- static int memory_open(struct inode *, struct file *);
- static int memory_release(struct inode *, struct file *);
- static ssize_t memory_read(struct file *, char *buf, size_t , loff_t *);
- static ssize_t memory_write(struct file *, const char *buf, size_t , loff_t *);
- /* Structure that declares the usual file access functions. */
- struct file_operations memory_fops =
- {
- read : memory_read,
- write : memory_write,
- open : memory_open,
- release : memory_release
- };
- /* Declaration of the init and exit functions. */
- module_init(memory_init);
- module_exit(memory_exit);
- /* Global variables of the driver */
- /* Major number. */
- int memory_major = 60;
- /* Buffer to store data. */
- char *memory_buffer;
- /*
- * Initialization:
- * 1. Register device driver
- * 2. Allocate buffer
- * 3. Initialize buffer
- */
- int memory_init(void)
- {
- int result;
- /* Registering device. */
- result = register_chrdev(memory_major, "memory", &memory_fops);
- if (result < 0)
- {
- printk(KERN_INFO "memory: cannot obtain major number %d\n", memory_major);
- return result;
- }
- /* Allocating memory for the buffer. */
- memory_buffer = kmalloc(BUF_LEN, GFP_KERNEL);
- if (!memory_buffer)
- {
- result = -ENOMEM;
- goto fail;
- }
- memset(memory_buffer, 0, BUF_LEN);
- printk(KERN_INFO "Inserting memory module\n");
- return 0;
- fail:
- memory_exit();
- return result;
- }
- /*
- * Cleanup:
- * 1. Unregister device driver
- * 2. Free buffer
- */
- void memory_exit(void)
- {
- /* Freeing the major number. */
- unregister_chrdev(memory_major, "memory");
- /* Freeing buffer memory. */
- if (memory_buffer)
- {
- kfree(memory_buffer);
- }
- printk(KERN_INFO "Removing memory module\n");
- }
- /* File open function. */
- static int memory_open(struct inode *inode, struct file *filp)
- {
- /* Initialize driver variables here. */
- /* Reset the device here. */
- /* Success. */
- return 0;
- }
- /* File close function. */
- static int memory_release(struct inode *inode, struct file *filp)
- {
- /* Free memory allocated in memory_open here. */
- /* Success. */
- return 0;
- }
- /*
- * File read function
- * Parameters:
- * filp - a type file structure;
- * buf - a buffer, from which the user space function (fread) will read;
- * len - a counter with the number of bytes to transfer, which has the same
- * value as the usual counter in the user space function (fread);
- * f_pos - a position of where to start reading the file;
- * Operation:
- * The memory_read function transfers data from the driver buffer (memory_buffer)
- * to user space with the function copy_to_user.
- */
- static ssize_t memory_read(struct file *filp, char *buf, size_t len, loff_t *f_pos)
- {
- /* Size of valid data in memory - data to send in user space. */
- int data_size = 0;
- if (*f_pos == 0)
- {
- /* Get size of valid data. */
- data_size = strlen(memory_buffer);
- /* Send data to user space. */
- if (copy_to_user(buf, memory_buffer, data_size) != 0)
- {
- return -EFAULT;
- }
- else
- {
- (*f_pos) += data_size;
- return data_size;
- }
- }
- else
- {
- return 0;
- }
- }
- /*
- * File write function
- * Parameters:
- * filp - a type file structure;
- * buf - a buffer in which the user space function (fwrite) will write;
- * len - a counter with the number of bytes to transfer, which has the same
- * values as the usual counter in the user space function (fwrite);
- * f_pos - a position of where to start writing in the file;
- * Operation:
- * The function copy_from_user transfers the data from user space to kernel space.
- */
- static ssize_t memory_write(struct file *filp, const char *buf, size_t len, loff_t *f_pos)
- {
- /* Reset memory. */
- memset(memory_buffer, 0, BUF_LEN);
- /* Get data from user space.*/
- if (copy_from_user(memory_buffer, buf, len) != 0)
- {
- return -EFAULT;
- }
- else
- {
- return len;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement