Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.88 KB | None | 0 0
  1. svaka skripta pocinje sa #!/bin/bash
  2. pokretanje: 1) bash myscript
  3. 2) chmod +x myscript
  4. ./myscript
  5. $LOGNAME - ime korisnika
  6. $(hostname) - ime sistema
  7.  
  8. davanje vrednosti varijablama bez razmaka x=y
  9. citanje varijable $x
  10. neke od dostupnih varijabli BASH_VERSION,HOSTNAME,PPID,PWD,UID,LINES,PATH...
  11. pazi na razmake kad postavljas varijable
  12.  
  13. --------petlje---------
  14. -----------------------
  15. if [[]]
  16. something
  17. -----------
  18. if COMMANDS
  19. then OTHER COMMANDS
  20. fi
  21.  
  22. if COMMANDS
  23. then
  24. OTHER COMMANDS
  25. fi
  26.  
  27. if COMMANDS; then
  28. OTHER COMMANDS
  29. fi
  30. ------------------------
  31. while true
  32. do
  33. done
  34. ------------------------
  35. continue samo prebaci u sledecu iteraciju
  36. break iskace iz petlje
  37. ------------------------
  38. case condition
  39. :radi nesto:
  40. esac
  41. ------------------------
  42.  
  43. >$0 stdin
  44. >$1 stdout
  45. >$2 stderr
  46.  
  47. Upis u fajl:
  48. echo "Hello" > file
  49.  
  50. ako hocemo da ispisemo nesto na stdout cat < file
  51. >> dodaje na kraj fajla a > prepisuje fajl
  52. -------------------------------
  53. GREP
  54. -------------------------
  55. -r uzima i nazive direktorijuma
  56. grep -r string home/
  57.  
  58. grep "$HOSTNAME" /etc/*
  59.  
  60. **************************************************************
  61.  
  62. ------------
  63. VEZBA 4.1
  64. ------------
  65. klonira se kernel sa gita
  66. raspakuje se a zatim se raspakuje RFS
  67.  
  68. $sudo tar xvjf linux-kernel-labs/modules/rootfs.tar.bz2 -C linux-kernel-labs/modules/nfsroot
  69.  
  70. postave se vlasnici
  71. $sudo chown -R rtrk.rtrk linux-kernel-labs/modules/nfsroot/root
  72.  
  73. pristup u-boot i terminalu raspberry-a
  74. $picocom -b 115200 /dev/ttyUSB0
  75.  
  76. postave se varijable u u-boot
  77. $setenv ipaddr <IP rpi>
  78. $setenv serverip <IP pc>
  79. $saveenv
  80.  
  81. kopiraju se zImage i bcm2709-rpi-2-b.dtb u /var/lib/tftpboot
  82.  
  83. (ovde imamo vec buildovan image kernela)
  84. $sudo cp ~/linux-kernel-labs/bootloader/rpi-2-b/zImage /var/lib/tftpboot
  85. $sudo cp ~/linux-kernel-labs/bootloader/rpi-2-b/bcm2709-rpi-2-b.dtb /var/lib/tftpboot
  86.  
  87. treba i restartovati tftpboot(samo prvi put po instalaciji???)
  88. $sudo service tftpd-hpa restart
  89.  
  90. ------Podesavanje NFS servera--------
  91. izmeniti /etc/exports/
  92. $sudo nano /etc/exports/
  93. i dodati
  94. /home/rtrk/linux-kernel-labs/modules/nfsroot 10.85.32.182(rw,no_root_squash,no_subtree_check)
  95.  
  96. restartovati NFS
  97. $sudo /etc/init.d/nfs-kernel-server restart
  98.  
  99. u u-boot promeniti bootargs
  100. $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"
  101. $saveenv
  102. za automatsko pokretanje bootovanja
  103. $setenv bootcmd "tftp 0x01000000 zImage; tftp 0x02000000 bcm2709-rpi-2-b.dtb; bootz 0x01000000 - 0x02000000"
  104. $saveenv
  105.  
  106. *****************************************************
  107.  
  108. Prevodjenje kernela i zakrpe
  109.  
  110. pozicionira se u ~/linux-kernel-labs/src
  111. klonira se sa gita linux folder
  112. patchuje se kernel sa
  113. $sudo apt-get install patch
  114. i pozove dok smo u linux folderu (VALJDA OVAKO???)
  115. $patch -p1 ~/linux-kernel-labs/modules/data/Linuks.patch
  116.  
  117. da bi se menjala konfiguracija kernela prvo se postavi
  118. $export ARCH=arm
  119. $export CROSS_COMPILE=arm-linux-gnueabihf-
  120. u patchovanom kernel direktorijumu primeni se default konfiguracija
  121. $make bcm2709_rtrk_linux_kurs_defconfig
  122. zatim
  123. $make xconfig
  124. i onda vrsimo izmene
  125. alternativa je make menuconfig
  126. kad smo zavrsili opicimo make
  127.  
  128. kopiraju se zImage i bcm2709-rpi-2-b.dtb u /var/lib/tftpboot/
  129. posto u kernel nije ukljucena podrska za nfs treba podesiti da se RFS preuzima sa kartice
  130. $setenv bootargs "root=/dev/mmcblk0p2 rootwait console=tty1 console=ttyAMA0,115200"
  131. $boot
  132.  
  133. ********************************************************
  134.  
  135. Konfigurisanje i prevodjenje kernela
  136.  
  137. koriscenje date konfiguracije... dok smo u ~/linux-kernel-labs/src/linux
  138. $sudo cp ~/linux-kernel-labs/modules/data/Linux.config .config
  139. $make xconfig (make menuconfig)
  140. $make
  141. pazi na putanje u /etc/exports
  142. restartuj nfs-kernel-server
  143. pogledaj bootargs
  144. kopiraj zImage i ono drugo govno
  145. pokreni kernel
  146.  
  147. Dodavanje uredjaja
  148. idi u ~/linux-kernel-labs/modules/nfsroot
  149. $sudo mknod /dev/ttyAMA0 c MINOR MAJOR
  150. dodaj jos ako treba nesto
  151. bootuj
  152.  
  153. ********************************************************
  154.  
  155. Pravljenje i pokretanje ugradjenog sistema(busybox raspali)
  156.  
  157. nalazim se u src folderu
  158. $wget link do busybox arhive
  159. raspakuj arhivu
  160. $sudo tar -xvjf busybox.tar.bz2 <dest dir>
  161. sad ovde u linux folderu uradim
  162. $sudo cp ~/linux-kernel-labs/modules/data/tinyLinux.config .config
  163. $make
  164. a u busybox folderu
  165. $sudo cp ~/linux-kernel-labs/modules/data/tinyBusybox.config .config
  166. ukljuciti opcije i dodati putanju preko
  167. $sudo make menuconfig
  168. $make
  169. $make install
  170. za rfs koristim dir gde je busybox
  171. tj podesim sve putanje u /etc/exports i u bootargs
  172. restartujem nfs
  173. mozda treba i kopirati zImage i dtb???
  174.  
  175. -----------------
  176. Dodavanje proc i sys sistema datoteka
  177.  
  178. U nfsroot folderu napraviti foldere proc/ sys/ etc/init.d
  179. U etc/ folderu napraviti fajl inittab i popuniti ga sa
  180. ::sysinit:/etc/init.d/rcS
  181. ::askfirst:/bin/sh
  182.  
  183. da bi se izbegla greska "/bin/sh: can't access tty; job control turned off"
  184. treba pisati ttyAMA0::askfirst:/bin/sh, ali tada mora da postoji /dev
  185. sa console i ttyAMA0, kao sto mora i za slucaj da se radi sa initramfs
  186. mkdev dev/console c 5 1
  187. mkdev dev/ttyAMA0 c 204 64
  188.  
  189. U etc/init.d/ folderu napraviti fajl rcS i popuniti ga sa
  190. #!/bin/sh
  191. mount -t proc none /proc
  192. mount -t sysfs none /sys
  193.  
  194. za initramfs, u rfs treba preimenovati linuxrc u init i
  195. CONFIG_INITRAMFS_SOURCE postaviti na putanju do napravljenog rfs u
  196. bootargs treba postaviti samo parametre rw i console(=ttyAMA0)
  197.  
  198. ----------------
  199. Implementacija web sprege
  200. ------------
  201. Kopirati direktorijum ~linux-kernel-labs/modules/data/www u nfsroot
  202. pokrenuti posluzioc u pi-u
  203. $/usr/sbin/httpd -h /www/ &
  204. u linuxu otvoriti
  205. $http://<ip rpi>
  206. popraviti pozive ka proc unutar web sprege???????
  207. VIDI NA VEZBAMA
  208. ---------------------
  209. Staticke i dinamicke biblioteke
  210. ---------------------
  211. Za staticke napravi helloworld.c
  212. napravi Makefile
  213. sve u nfsroot folderu
  214. pokreni na rpi
  215. ZA DINAMICKE VIDI NA VEZBAMA A I ZA STATICKE KAKO SE PRAVI Makefile
  216. ---------------------
  217. Generisanje INITRAMFS
  218. -------------------
  219. Iskoristiti generisani rfs kao i izvorne datoteke za generisanje initramfs-a
  220. (CONFIG_INITRAMFS_SOURCE) i ponovo prevedi kernel
  221. preimenuj linuxrc u init u nfsroot
  222. nemoj zaboraviti kopirati image i dtb kao i /etc/exports/ ako treba?????
  223. izmeni bootargs da ostane samo rw i console
  224. pokreni initramfs na RPI
  225. po pokretanju dodaj sve sto je potrebno da za funkcionalnost kao kad je rfs bio na mrezi
  226. ???????????????????????????????????????????????
  227. POGLEDAJ NA VEZBAMA
  228.  
  229. ************************************************
  230.  
  231. Pisanje i prevodjenje modula
  232. ----------------------
  233. exportuj ARCH i CROSS_COMPILE
  234. $make bcm2709_defconfig
  235. nalazis se u linux folderu
  236. $make
  237. kopiraj image i dtb
  238. promeni /etc/exports i bootargs
  239. restartuj ono sranje
  240. bootuj RPI
  241.  
  242. u nfsroot/root/hello
  243. izmeni hello_version.c
  244. POGLEDAJ NA VEZBAMA KAKO SI URADIO
  245. podesi Makefile
  246. exportuj ARCH i CROSS_COMPILE
  247. prevedi ($make)
  248. vidi na RPI jel radi
  249. insmod naziv_modula
  250. rmmod naziv_modula
  251. lsmod
  252.  
  253. ako treba dodaj dodaj u dev/ potrebne nodove
  254.  
  255. Za znakovni rukovalac(chardev) kopirati iz sppurva onu vezbu
  256. namestiti Makefile i prevesti
  257. POGLEDAJ KAKO SE RADI NA VEZBAMA I ZAPISI KOMANDE
  258. ZAVRSI VEZBU I ZAPISI KOMANDE :D
  259.  
  260. *************************************************
  261.  
  262. /dts-v1/;
  263.  
  264. #include "bcm2709.dtsi"
  265.  
  266. / {
  267. compatible = "brcm,bcm2709";
  268. model = "Raspberry Pi 2 Model B";
  269. };
  270.  
  271. &gpio {
  272. i2c1_pins: i2c1 {
  273. brcm,pins = <2 3>;
  274. brcm,function = <4>;
  275. };
  276. };
  277.  
  278. &uart0 {
  279. status = "okay";
  280. };
  281.  
  282. &i2c1 {
  283. pinctrl-names = "default";
  284. pinctrl-0 = <&i2c1_pins>;
  285. clock-frequency = <100000>;
  286. status = "okay";
  287.  
  288. nunchuk: nunchuk@52 {
  289. compatible = "nintendo,nunchuk";
  290. reg = <0x52>;
  291. };
  292. };
  293.  
  294. / {
  295. __overrides__ {
  296. uart0 = <&uart0>,"status";
  297. uart0_clkrate = <&clk_uart0>,"clock-frequency:0";
  298. i2c1 = <&i2c1>,"status";
  299. i2c1_baudrate = <&i2c1>,"clock-frequency:0";
  300. core_freq = <&clk_core>,"clock-frequency:0";
  301. };
  302. };
  303.  
  304.  
  305. ************************************************************
  306.  
  307. /* Download it from http://j.mp/19nnhkq */
  308.  
  309. #include <linux/init.h>
  310. #include <linux/module.h>
  311. #include <linux/i2c.h>
  312. #include <linux/delay.h>
  313.  
  314. static int nunchuk_read_registers(struct i2c_client *client,
  315. u8 *recv)
  316. {
  317. u8 buf[1];
  318. int ret;
  319.  
  320. /* Ask the device to get ready for a read */
  321.  
  322. mdelay(10);
  323.  
  324. buf[0] = 0x00;
  325. ret = i2c_master_send(client, buf, 1);
  326. if (ret != 1) {
  327. dev_err(&client->dev, "i2c send failed (%d)\n", ret);
  328. return ret < 0 ? ret : -EIO;
  329. }
  330.  
  331. mdelay(10);
  332.  
  333. /* Now read registers */
  334.  
  335. ret = i2c_master_recv(client, recv, 6);
  336. if (ret != 6) {
  337. dev_err(&client->dev, "i2c recv failed (%d)\n", ret);
  338. return ret < 0 ? ret : -EIO;
  339. }
  340.  
  341. return 0;
  342. }
  343.  
  344. static int nunchuk_probe(struct i2c_client *client,
  345. const struct i2c_device_id *id)
  346. {
  347. u8 recv[6], buf[2];
  348. int ret, zpressed, cpressed;
  349.  
  350. pr_info("Called nunchuk_probe\n");
  351.  
  352. /* Initialize device */
  353.  
  354. buf[0] = 0xf0;
  355. buf[1] = 0x55;
  356.  
  357. ret = i2c_master_send(client, buf, 2);
  358. if (ret != 2) {
  359. dev_err(&client->dev, "i2c send failed (%d)\n", ret);
  360. return ret < 0 ? ret : -EIO;
  361. }
  362.  
  363. udelay(1000);
  364.  
  365. buf[0] = 0xfb;
  366. buf[1] = 0x00;
  367.  
  368. ret = i2c_master_send(client, buf, 2);
  369. if (ret != 2) {
  370. dev_err(&client->dev, "i2c send failed (%d)\n", ret);
  371. return ret < 0 ? ret : -EIO;
  372. }
  373.  
  374. /* Make a dummy read from the device
  375. The device seems to update the state of its internal
  376. registers only once they have been read */
  377.  
  378. if ((ret = nunchuk_read_registers(client, recv)) < 0)
  379. return ret;
  380.  
  381. /* Now get the real state of the device */
  382.  
  383. if ((ret = nunchuk_read_registers(client, recv)) < 0)
  384. return ret;
  385.  
  386. zpressed = (recv[5] & BIT(0)) ? 0 : 1;
  387.  
  388. if (zpressed)
  389. dev_info(&client->dev, "Z button pressed\n");
  390.  
  391. cpressed = (recv[5] & BIT(1)) ? 0 : 1;
  392.  
  393. if (cpressed)
  394. dev_info(&client->dev, "C button pressed\n");
  395.  
  396. return 0;
  397. }
  398.  
  399. static int nunchuk_remove(struct i2c_client *client)
  400. {
  401. pr_info("Called nunchuk_remove\n");
  402. return 0;
  403. }
  404.  
  405. static const struct i2c_device_id nunchuk_id[] = {
  406. { "nunchuk", 0 },
  407. { },
  408. };
  409. MODULE_DEVICE_TABLE(i2c, nunchuk_id);
  410.  
  411. #ifdef CONFIG_OF
  412. static struct of_device_id nunchuk_dt_match[] = {
  413. { .compatible = "nintendo,nunchuk" },
  414. { },
  415. };
  416. #endif
  417.  
  418. static struct i2c_driver nunchuk_driver = {
  419. .driver = {
  420. .name = "nunchuk",
  421. .owner = THIS_MODULE,
  422. .of_match_table = of_match_ptr(nunchuk_dt_match),
  423. },
  424. .probe = nunchuk_probe,
  425. .remove = nunchuk_remove,
  426. .id_table = nunchuk_id,
  427. };
  428.  
  429. module_i2c_driver(nunchuk_driver);
  430.  
  431. MODULE_LICENSE("GPL");
  432.  
  433. ************************************************************
  434.  
  435. #Kernel moduli se prevode upotrebom Linux Kernel Build (Kbuild) okruzenja. Nakon uspesnog
  436. #prevodjenja u korenom (root) direktorijumu projekta treba da se pojavi .ko datoteka koja
  437. #predstavlja prevedeni kernel modul u binarnom obliku.
  438.  
  439. -------------------------------------------------------------------------------------------
  440. #nothing.ko
  441. #Prijavljivanje (registrovanje) modula u Linux kernel - insmod komanda
  442. insmod ./nothing.ko
  443.  
  444. #Komandom lsmod omoguceno je izlistavanje svih modula koji se trenutno nalaze u kernelu.
  445. lsmod
  446.  
  447. #Nakon izlistavanja u spisku svih modula, ukoliko je modul uspesno preveden i registrovan,
  448. #moze se naci nothing modul. (Pomoc: lsmod | grep "nothing")
  449.  
  450. #Brisanje modula iz kernela - rmmod komanda
  451. rmmod nothing
  452.  
  453. #Ukoliko ponovo izlistate sve module videcete da modul nothing ne postoji u listi modula.
  454.  
  455. -------------------------------------------------------------------------------------------
  456. #hello1.ko
  457.  
  458. #Registrovanje modula.
  459. insmod ./hello1.ko
  460.  
  461. #Proveravamo da li je modul uspesno registrovan.
  462. lsmod
  463.  
  464. #Kako bi se omogucilo pracenje log poruka koje su definisane unutar kernel modula
  465. #(printk funkcija) potrebno je uraditi jedno od sledeceg:
  466. # 1) Upotrebiti dmesg komandu
  467. # 2) Pristupiti (sudo) posebnom uredjaju /dev/kmsg unutar OS Linux koji omogucuje
  468. # pracenje log poruka.
  469. #
  470. #dmesg: Ova komanda omogucuje izlistavanje svih log poruka koje su se desile unutar Linux
  471. # kernela. Nakon zavrsetka dmesg komande korisnicki terminal postaje ponovo dostupan
  472. # korisniku za poziv novih komandi - dmesg komanda nije blokirajuca. Brisanje svih
  473. # log poruka se vrsi pozivom "dmesg -c".
  474. #
  475. #/dev/kmsg: Da bi se pristupilo log porukama pomocu uredjaja /dev/kmsg potrebno je pozvati
  476. # "sudo cat /dev/kmsg" komandu. Na ovaj nacin se iscitavaju samo nove poruke, tj.
  477. # ne izlistavaju se sve poruke koje su bile. Za razliku od upotrebe dmesg komande
  478. # ovaj nacin iscitavanja je blokirajuci. Da bi se oslobodio korisnicki terminal
  479. # potrebno je pozvati "Ctrl + C".
  480.  
  481. #Izlistavanje kernel log-a.
  482. dmesg
  483.  
  484. #Poslednja linija u terminalu.
  485. Hello world 1
  486.  
  487. #Brisanje modula.
  488. rmmod hello1
  489.  
  490. #Izlistavanje kernel log-a.
  491. dmesg
  492.  
  493. #Poslednja linija u terminalu.
  494. Goodbye world 1
  495.  
  496. -------------------------------------------------------------------------------------------
  497. #hello2.ko
  498.  
  499. #Registrovanje modula.
  500. insmod ./hello2.ko
  501.  
  502. #Proveravamo da li je modul uspesno registrovan.
  503. lsmod
  504.  
  505. #Izlistavanje kernel log-a.
  506. dmesg
  507.  
  508. #Poslednja linija u terminalu.
  509. Hello world 2
  510.  
  511. #Brisanje modula.
  512. rmmod hello2
  513.  
  514. #Izlistavanje kernel log-a.
  515. dmesg
  516.  
  517. #Poslednja linija u terminalu.
  518. Goodbye world 2
  519.  
  520. -------------------------------------------------------------------------------------------
  521. #hello3.ko
  522.  
  523. #Registrovanje modula.
  524. insmod ./hello3.ko
  525.  
  526. #Proveravamo da li je modul uspesno registrovan.
  527. lsmod
  528.  
  529. #Izlistavanje kernel log-a.
  530. dmesg
  531.  
  532. #Poslednja linija u terminalu.
  533. Hello world 3
  534.  
  535. #Brisanje modula.
  536. rmmod hello3
  537.  
  538. #Izlistavanje kernel log-a.
  539. dmesg
  540.  
  541. #Poslednja linija u terminalu.
  542. Goodbye world 3
  543.  
  544. -------------------------------------------------------------------------------------------
  545. #hello4.ko
  546.  
  547. #Registrovanje modula.
  548. insmod ./hello4.ko
  549.  
  550. #Proveravamo da li je modul uspesno registrovan.
  551. lsmod
  552.  
  553. #Izlistavanje kernel log-a.
  554. dmesg
  555.  
  556. #Poslednja linija u terminalu.
  557. Hello world 4
  558.  
  559. #Izlistavanje dokumentacije modula - modinfo komanda.
  560. modinfo ./hello4.ko
  561.  
  562. #Izlaz.
  563. filename: ./hello4.ko
  564. description: A simple driver
  565. author: RT-RK
  566. license: GPL
  567. srcversion: 6EF93F994B70D0795902EC7
  568. depends:
  569. vermagic: 3.8.0-30-generic SMP mod_unload modversions 68
  570.  
  571. #Brisanje modula.
  572. rmmod hello4
  573.  
  574. #Izlistavanje kernel log-a.
  575. dmesg
  576.  
  577. #Poslednja linija u terminalu.
  578. Goodbye world 4
  579.  
  580. -------------------------------------------------------------------------------------------
  581. #hello5.ko
  582.  
  583. #Registrovanje modula.
  584. insmod ./hello5.ko
  585.  
  586. #Proveravamo da li je modul uspesno registrovan.
  587. lsmod
  588.  
  589. #Izlistavanje kernel log-a.
  590. dmesg
  591.  
  592. #Izlaz.
  593. Hello world 5
  594. myShort is a short integer: 1
  595. myInt is an integer: 100
  596. myLong is a long integer: 1000
  597. myString is a string: initString...
  598. myIntArray[0] = -1
  599. myIntArray[1] = -1
  600. Got 0 arguments for myIntArray.
  601.  
  602.  
  603. #Izlistavanje dokumentacije modula - modinfo komanda.
  604. modinfo ./hello5.ko
  605.  
  606. #Izlaz.
  607. filename: ./hello5.ko
  608. description: Demonstrates command line argument passing to a kernel module
  609. author: RT-RK
  610. license: GPL
  611. srcversion: C4A2BE7854E02AA2DAD4224
  612. depends:
  613. vermagic: 3.8.0-30-generic SMP mod_unload modversions 686
  614. parm: myShort:A short integer. (short)
  615. parm: myInt:An integer. (int)
  616. parm: myLong:A long integer. (long)
  617. parm: myString:A character string. (charp)
  618. parm: myIntArray:An array of integers. (array of int)
  619.  
  620. #Brisanje modula.
  621. rmmod hello5
  622.  
  623. #Izlistavanje kernel log-a.
  624. dmesg
  625.  
  626. #Poslednja linija u terminalu.
  627. Goodbye world 5
  628.  
  629. #Registrovanje modula sa parametrima.
  630. insmod hello5.ko myShort=234 myInt=12345 myLong=9123456 myString="test" myIntArray=7,8
  631.  
  632. #Izlistavanje kernel log-a.
  633. dmesg
  634.  
  635. #Izlaz (primetite vrednosti parametara).
  636. Hello world 5
  637. myShort is a short integer: 234
  638. myInt is an integer: 12345
  639. myLong is a long integer: 9123456
  640. myString is a string: test
  641. myIntArray[0] = 7
  642. myIntArray[1] = 8
  643. Got 2 arguments for myIntArray.
  644.  
  645. #Brisanje modula.
  646. rmmod hello5
  647.  
  648. -------------------------------------------------------------------------------------------
  649. #chardev.ko
  650.  
  651. #Registrovanje modula.
  652. insmod ./chardev.ko
  653.  
  654. #Proveravamo da li je modul uspesno registrovan.
  655. lsmod
  656.  
  657. #Izlistavanje kernel log-a.
  658. dmesg
  659.  
  660. #Izlaz:
  661. I was assigned major number 250. To talk to
  662. the driver, create a dev file with
  663. 'mknod /dev/chardev c 250 0'.
  664. Try various minor numbers. Try to cat and echo to
  665. the device file.
  666. Remove the device file and module when done.
  667.  
  668. #Definisanje cvora (node) novog uredjaja u OS Linux preko kog se omogucuje pristup
  669. #uredjaju. Pravnjenje novog cvora se vrsi upotrebom mknod komande.
  670. mknod /dev/chardev c 250 0
  671.  
  672. #Citanje uredjaja upotrebom cat comande.
  673. cat /dev/chardev
  674.  
  675. #Izlaz.
  676. I already told you 0 times Hello world!
  677.  
  678. #Citanje uredjaja upotrebom cat comande.
  679. cat /dev/chardev
  680.  
  681. #Izlaz.
  682. I already told you 1 times Hello world!
  683.  
  684. #Pisanje u uredjaj upotrebom echo komande.
  685. echo "test" >> /dev/chardev
  686.  
  687. #Izlistavanje kernel log-a.
  688. dmesg
  689.  
  690. #Izlaz (poslednja linija).
  691. Sorry, this operation isn't supported.
  692.  
  693. #Brisanje modula.
  694. rmmod chardev
  695.  
  696. #Brisanje cvora uredjaja.
  697. rm /dev/chardev
  698.  
  699. -------------------------------------------------------------------------------------------
  700. #memory.ko
  701.  
  702. #Registrovanje modula.
  703. insmod ./memory.ko
  704.  
  705. #Proveravamo da li je modul uspesno registrovan.
  706. lsmod
  707.  
  708. #Izlistavanje kernel log-a.
  709. dmesg
  710.  
  711. #Izlaz:
  712. Inserting memory module
  713.  
  714. #Definisanje cvora novog uredjaja.
  715. mknod /dev/memory c 60 0
  716.  
  717. #Pisanje u uredjaj upotrebom echo komande.
  718. echo "test" >> /dev/memory
  719.  
  720. #Citanje uredjaja upotrebom cat comande.
  721. cat /dev/memory
  722.  
  723. #Izlaz (korisnicki terminal).
  724. test
  725.  
  726. #Brisanje modula.
  727. rmmod memory
  728.  
  729. #Izlistavanje kernel log-a.
  730. dmesg
  731.  
  732. #Izlaz:
  733. Removing memory module
  734.  
  735. #Brisanje cvora uredjaja.
  736. rm /dev/memory
  737.  
  738. -------------------------------------------------------------------------------------------
  739. #memory_test_app
  740.  
  741. #Pozivanje testnog programa(potrebno je pozicionirati se u memory_test_app/bin/Release).
  742. ./memory_test_app
  743.  
  744. #Izlaz.
  745. '/dev/memory' device is successfully opened!
  746. Written message: '/dev/memory' test message!!!
  747. ret_val: 30
  748. Read message: '/dev/memory' test message!!!
  749. ret_val: 30
  750.  
  751. -------------------------------------------------------------------------------------------
  752.  
  753. ************************************************************************
  754.  
  755. #include <linux/init.h>
  756. #include <linux/module.h>
  757. #include <linux/kernel.h>
  758. #include <linux/fs.h>
  759. #include <linux/errno.h>
  760. #include <linux/types.h>
  761. #include <linux/fcntl.h>
  762. #include <linux/proc_fs.h>
  763. #include <asm/uaccess.h>
  764. #include <linux/string.h>
  765. #include <linux/slab.h>
  766.  
  767. MODULE_LICENSE("Dual BSD/GPL");
  768.  
  769. #define BUF_LEN 80
  770.  
  771. /* Declaration of memory.c functions */
  772. void memory_exit(void);
  773. int memory_init(void);
  774. static int memory_open(struct inode *, struct file *);
  775. static int memory_release(struct inode *, struct file *);
  776. static ssize_t memory_read(struct file *, char *buf, size_t , loff_t *);
  777. static ssize_t memory_write(struct file *, const char *buf, size_t , loff_t *);
  778.  
  779. /* Structure that declares the usual file access functions. */
  780. struct file_operations memory_fops =
  781. {
  782. read : memory_read,
  783. write : memory_write,
  784. open : memory_open,
  785. release : memory_release
  786. };
  787.  
  788. /* Declaration of the init and exit functions. */
  789. module_init(memory_init);
  790. module_exit(memory_exit);
  791.  
  792.  
  793. /* Global variables of the driver */
  794.  
  795. /* Major number. */
  796. int memory_major = 60;
  797.  
  798. /* Buffer to store data. */
  799. char *memory_buffer;
  800.  
  801. /*
  802. * Initialization:
  803. * 1. Register device driver
  804. * 2. Allocate buffer
  805. * 3. Initialize buffer
  806. */
  807. int memory_init(void)
  808. {
  809. int result;
  810.  
  811. /* Registering device. */
  812. result = register_chrdev(memory_major, "memory", &memory_fops);
  813. if (result < 0)
  814. {
  815. printk(KERN_INFO "memory: cannot obtain major number %d\n", memory_major);
  816. return result;
  817. }
  818.  
  819. /* Allocating memory for the buffer. */
  820. memory_buffer = kmalloc(BUF_LEN, GFP_KERNEL);
  821. if (!memory_buffer)
  822. {
  823. result = -ENOMEM;
  824. goto fail;
  825. }
  826.  
  827. memset(memory_buffer, 0, BUF_LEN);
  828. printk(KERN_INFO "Inserting memory module\n");
  829. return 0;
  830.  
  831. fail:
  832. memory_exit();
  833. return result;
  834. }
  835.  
  836. /*
  837. * Cleanup:
  838. * 1. Unregister device driver
  839. * 2. Free buffer
  840. */
  841. void memory_exit(void)
  842. {
  843. /* Freeing the major number. */
  844. unregister_chrdev(memory_major, "memory");
  845.  
  846. /* Freeing buffer memory. */
  847. if (memory_buffer)
  848. {
  849. kfree(memory_buffer);
  850. }
  851.  
  852. printk(KERN_INFO "Removing memory module\n");
  853. }
  854.  
  855. /* File open function. */
  856. static int memory_open(struct inode *inode, struct file *filp)
  857. {
  858. /* Initialize driver variables here. */
  859.  
  860. /* Reset the device here. */
  861.  
  862. /* Success. */
  863. return 0;
  864. }
  865.  
  866. /* File close function. */
  867. static int memory_release(struct inode *inode, struct file *filp)
  868. {
  869. /* Free memory allocated in memory_open here. */
  870.  
  871. /* Success. */
  872. return 0;
  873. }
  874.  
  875. /*
  876. * File read function
  877. * Parameters:
  878. * filp - a type file structure;
  879. * buf - a buffer, from which the user space function (fread) will read;
  880. * len - a counter with the number of bytes to transfer, which has the same
  881. * value as the usual counter in the user space function (fread);
  882. * f_pos - a position of where to start reading the file;
  883. * Operation:
  884. * The memory_read function transfers data from the driver buffer (memory_buffer)
  885. * to user space with the function copy_to_user.
  886. */
  887. static ssize_t memory_read(struct file *filp, char *buf, size_t len, loff_t *f_pos)
  888. {
  889. /* Size of valid data in memory - data to send in user space. */
  890. int data_size = 0;
  891.  
  892. if (*f_pos == 0)
  893. {
  894. /* Get size of valid data. */
  895. data_size = strlen(memory_buffer);
  896.  
  897. /* Send data to user space. */
  898. if (copy_to_user(buf, memory_buffer, data_size) != 0)
  899. {
  900. return -EFAULT;
  901. }
  902. else
  903. {
  904. (*f_pos) += data_size;
  905.  
  906. return data_size;
  907. }
  908. }
  909. else
  910. {
  911. return 0;
  912. }
  913. }
  914.  
  915. /*
  916. * File write function
  917. * Parameters:
  918. * filp - a type file structure;
  919. * buf - a buffer in which the user space function (fwrite) will write;
  920. * len - a counter with the number of bytes to transfer, which has the same
  921. * values as the usual counter in the user space function (fwrite);
  922. * f_pos - a position of where to start writing in the file;
  923. * Operation:
  924. * The function copy_from_user transfers the data from user space to kernel space.
  925. */
  926. static ssize_t memory_write(struct file *filp, const char *buf, size_t len, loff_t *f_pos)
  927. {
  928. /* Reset memory. */
  929. memset(memory_buffer, 0, BUF_LEN);
  930.  
  931. /* Get data from user space.*/
  932. if (copy_from_user(memory_buffer, buf, len) != 0)
  933. {
  934. return -EFAULT;
  935. }
  936. else
  937. {
  938. return len;
  939. }
  940. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement