SlideShare a Scribd company logo
1 of 22
© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Character Drivers
2© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
After this session, you would know
W's of Character Drivers
Major & Minor Numbers
Registering & Unregistering Character Driver
File Operations of a Character Driver
Writing a Character Driver
Linux Device Model
udev & automatic device creation
3© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Character Drivers
What does “Character” stand for?
Look at entries starting with 'c' after
ls -l /dev
Device File Name
User Space specific
Used by Applications
Device File Number
Kernel Space specific
Used by Kernel Internals as easy for Computation
4© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Major & Minor Number
ls -l /dev
Major is to Category; Minor is to Device
Data Structures described in Kernel C in object
oriented fashion
Type Header: <linux/types.h>
Type: dev_t – 12 bits for major & 20 bits for minor
Macro Header: <linux/kdev_t.h>
MAJOR(dev_t dev)
MINOR(dev_t dev)
MKDEV(int major, int minor)
5© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
3 Entities in 3 Spaces
Device
Driver
/dev/io
Device
Kernel Space
User Space
Hardware
Space
VFS
Device File
Application
open()
6© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Registering & Unregistering
Registering the Device Driver
int register_chrdev_region(dev_t first, unsigned int count, char *name);
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned
int cnt, char *name);
Unregistering the Device Driver
void unregister_chrdev_region(dev_t first, unsigned int count);
Header: <linux/fs.h>
Kernel Window: /proc/devices
7© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The file operations
struct file_operations
struct module owner = THIS_MODULE; /* <linux/module.h> */
int (*open)(struct inode *, struct file *);
int (*release)(struct inode *, struct file *);
ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
loff_t (*llseek)(struct file *, loff_t, int);
int (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
Header: <linux/fs.h>
8© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Initialization for Registration
1st
way initialization
struct cdev *my_cdev = cdev_alloc();
my_cdev->owner = THIS_MODULE;
my_cdev->ops = &my_fops;
2nd
way initialization
struct cdev my_cdev;
cdev_init(&my_cdev, &my_fops);
Header: <linux/cdev.h>
9© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Registering the file operations
The Registration
int cdev_add(struct cdev *cdev, dev_t num, unsigned int count);
The Unregistration
void cdev_del(struct cdev *cdev);
Header: <linux/cdev.h>
10© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The file & inode structures
Important fields of struct file
mode_t f_mode
loff_t f_pos
unsigned int f_flags
struct file_operations *f_op
void *private_data
Important fields of struct inode
unsigned int iminor(struct inode *);
unsigned int imajor(struct inode *);
11© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Register/Unregister: Old Way
Registering the Device Driver
int register_chrdev(unsigned int major, const char *name, struct
file_operations *fops);
Unregistering the Device Driver
int unregister_chrdev(unsigned int major, const char *name);
12© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The /dev/null read & write
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
{
...
return read_cnt;
}
ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off)
{
...
return wrote_cnt;
}
13© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The read flow
struct file
-------------------------
f_count
f_flags
f_mode
-------------------------
f_pos
-------------------------
...
...
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
Buffer
(in the driver)
Buffer
(in the
application
or libc)
Kernel Space (Non-swappable) User Space (Swappable)
copy_to_user
14© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The write flow
struct file
-------------------------
f_count
f_flags
f_mode
-------------------------
f_pos
-------------------------
...
...
ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off)
Buffer
(in the driver)
Buffer
(in the
application
or libc)
Kernel Space (Non-swappable) User Space (Swappable)
copy_from_user
15© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The mem device read
#include <asm/uaccess.h>
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
{
...
if (copy_to_user(buf, from, cnt) != 0)
{
return -EFAULT;
}
...
return read_cnt;
}
16© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The mem device write
#include <asm/uaccess.h>
ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off)
{
...
if (copy_from_user(to, buf, cnt) != 0)
{
return -EFAULT;
}
...
return wrote_cnt;
}
17© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The I/O Control API
API
int (*unlocked_ioctl)(struct file *, unsigned int cmd,
unsigned long arg)
Command
Macros
_IO, _IOW, _IOR, _IOWR
Parameters
type (character) [15:8]
number (index) [7:0]
size (param type) [29:16]
Header: <linux/ioctl.h> →...→ <asm-generic/ioctl.h>
size [29:16] num[7:0]type[15:8]
dir[31:30]
18© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Device Model (LDM)
struct kobject - <linux/kobject.h>
kref object
Pointer to kset, the parent object
kobj_type, type describing the kobject
kobject instantiation → sysfs representation
Parent object guides the entries under /sys/
bus – the physical buses
class – the device categories
device – the actual devices
19© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
udev & LDM
Daemon: udevd
Configuration: /etc/udev/udev.conf
Rules: /etc/udev/rules.d/
Utility: udevinfo [-a] [-p <device_path>]
Receives uevent on a change in /sys
Accordingly, updates /dev &/or
Performs the appropriate action for
Hotplug
Microcode / Firmware Download
Module Autoload
20© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Device Model & Classes
Latest way to create dynamic devices
Create or Get the appropriate device category
Create the desired device under that category
Class Operations
struct class *class_create(struct module *owner, char
*name);
void class_destroy(struct class *cl);
Device into & out of Class
struct class_device *device_create(struct class *cl, NULL,
dev_t devnum, NULL, const char *fmt, ...);
void device_destroy(struct class *cl, dev_t devnum);
21© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of Character Drivers
Major & Minor Numbers
Registering & Unregistering Character
Driver
File Operations of a Character Driver
Writing a Character Driver
Linux Device Model
udev & automatic device creation
22© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot

linux device driver
linux device driverlinux device driver
linux device driverRahul Batra
 
Virtualization Support in ARMv8+
Virtualization Support in ARMv8+Virtualization Support in ARMv8+
Virtualization Support in ARMv8+Aananth C N
 
Linux PCI device driver
Linux PCI device driverLinux PCI device driver
Linux PCI device driver艾鍗科技
 

What's hot (20)

USB Drivers
USB DriversUSB Drivers
USB Drivers
 
Embedded Android : System Development - Part IV
Embedded Android : System Development - Part IVEmbedded Android : System Development - Part IV
Embedded Android : System Development - Part IV
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
Linux I2C
Linux I2CLinux I2C
Linux I2C
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Embedded Android : System Development - Part I
Embedded Android : System Development - Part IEmbedded Android : System Development - Part I
Embedded Android : System Development - Part I
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Virtualization Support in ARMv8+
Virtualization Support in ARMv8+Virtualization Support in ARMv8+
Virtualization Support in ARMv8+
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Toolchain
ToolchainToolchain
Toolchain
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Linux PCI device driver
Linux PCI device driverLinux PCI device driver
Linux PCI device driver
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 

Viewers also liked (18)

Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Interrupts
InterruptsInterrupts
Interrupts
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
Embedded C
Embedded CEmbedded C
Embedded C
 
References
ReferencesReferences
References
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
File Systems
File SystemsFile Systems
File Systems
 

Similar to Character Driver Fundamentals

리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2Sangho Park
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded SystemsAnil Kumar Pugalia
 
How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)Dimitrios Platis
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android晓东 杜
 
Cognitive data capture with Elis - Rossum's technical webinar
Cognitive data capture with Elis - Rossum's technical webinarCognitive data capture with Elis - Rossum's technical webinar
Cognitive data capture with Elis - Rossum's technical webinarPetr Baudis
 
1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2Stanley Ho
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Vincenzo Iozzo
 
Attack your Trusted Core
Attack your Trusted CoreAttack your Trusted Core
Attack your Trusted CoreDi Shen
 

Similar to Character Driver Fundamentals (20)

Character drivers
Character driversCharacter drivers
Character drivers
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
Embedded Applications
Embedded ApplicationsEmbedded Applications
Embedded Applications
 
How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android
 
LSA2 - 02 Namespaces
LSA2 - 02  NamespacesLSA2 - 02  Namespaces
LSA2 - 02 Namespaces
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Cognitive data capture with Elis - Rossum's technical webinar
Cognitive data capture with Elis - Rossum's technical webinarCognitive data capture with Elis - Rossum's technical webinar
Cognitive data capture with Elis - Rossum's technical webinar
 
1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2
 
Processes
ProcessesProcesses
Processes
 
Linux IO
Linux IOLinux IO
Linux IO
 
Activity 5
Activity 5Activity 5
Activity 5
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
Attack your Trusted Core
Attack your Trusted CoreAttack your Trusted Core
Attack your Trusted Core
 

More from Anil Kumar Pugalia (19)

System Calls
System CallsSystem Calls
System Calls
 
Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
System Calls
System CallsSystem Calls
System Calls
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Processes
ProcessesProcesses
Processes
 
Signals
SignalsSignals
Signals
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 

Character Driver Fundamentals

  • 1. © 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Character Drivers
  • 2. 2© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? After this session, you would know W's of Character Drivers Major & Minor Numbers Registering & Unregistering Character Driver File Operations of a Character Driver Writing a Character Driver Linux Device Model udev & automatic device creation
  • 3. 3© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. W's of Character Drivers What does “Character” stand for? Look at entries starting with 'c' after ls -l /dev Device File Name User Space specific Used by Applications Device File Number Kernel Space specific Used by Kernel Internals as easy for Computation
  • 4. 4© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Major & Minor Number ls -l /dev Major is to Category; Minor is to Device Data Structures described in Kernel C in object oriented fashion Type Header: <linux/types.h> Type: dev_t – 12 bits for major & 20 bits for minor Macro Header: <linux/kdev_t.h> MAJOR(dev_t dev) MINOR(dev_t dev) MKDEV(int major, int minor)
  • 5. 5© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. 3 Entities in 3 Spaces Device Driver /dev/io Device Kernel Space User Space Hardware Space VFS Device File Application open()
  • 6. 6© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Registering & Unregistering Registering the Device Driver int register_chrdev_region(dev_t first, unsigned int count, char *name); int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int cnt, char *name); Unregistering the Device Driver void unregister_chrdev_region(dev_t first, unsigned int count); Header: <linux/fs.h> Kernel Window: /proc/devices
  • 7. 7© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The file operations struct file_operations struct module owner = THIS_MODULE; /* <linux/module.h> */ int (*open)(struct inode *, struct file *); int (*release)(struct inode *, struct file *); ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); loff_t (*llseek)(struct file *, loff_t, int); int (*unlocked_ioctl)(struct file *, unsigned int, unsigned long); Header: <linux/fs.h>
  • 8. 8© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Initialization for Registration 1st way initialization struct cdev *my_cdev = cdev_alloc(); my_cdev->owner = THIS_MODULE; my_cdev->ops = &my_fops; 2nd way initialization struct cdev my_cdev; cdev_init(&my_cdev, &my_fops); Header: <linux/cdev.h>
  • 9. 9© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Registering the file operations The Registration int cdev_add(struct cdev *cdev, dev_t num, unsigned int count); The Unregistration void cdev_del(struct cdev *cdev); Header: <linux/cdev.h>
  • 10. 10© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The file & inode structures Important fields of struct file mode_t f_mode loff_t f_pos unsigned int f_flags struct file_operations *f_op void *private_data Important fields of struct inode unsigned int iminor(struct inode *); unsigned int imajor(struct inode *);
  • 11. 11© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Register/Unregister: Old Way Registering the Device Driver int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); Unregistering the Device Driver int unregister_chrdev(unsigned int major, const char *name);
  • 12. 12© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The /dev/null read & write ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... return read_cnt; } ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... return wrote_cnt; }
  • 13. 13© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The read flow struct file ------------------------- f_count f_flags f_mode ------------------------- f_pos ------------------------- ... ... ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) Buffer (in the driver) Buffer (in the application or libc) Kernel Space (Non-swappable) User Space (Swappable) copy_to_user
  • 14. 14© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The write flow struct file ------------------------- f_count f_flags f_mode ------------------------- f_pos ------------------------- ... ... ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off) Buffer (in the driver) Buffer (in the application or libc) Kernel Space (Non-swappable) User Space (Swappable) copy_from_user
  • 15. 15© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The mem device read #include <asm/uaccess.h> ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... if (copy_to_user(buf, from, cnt) != 0) { return -EFAULT; } ... return read_cnt; }
  • 16. 16© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The mem device write #include <asm/uaccess.h> ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off) { ... if (copy_from_user(to, buf, cnt) != 0) { return -EFAULT; } ... return wrote_cnt; }
  • 17. 17© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The I/O Control API API int (*unlocked_ioctl)(struct file *, unsigned int cmd, unsigned long arg) Command Macros _IO, _IOW, _IOR, _IOWR Parameters type (character) [15:8] number (index) [7:0] size (param type) [29:16] Header: <linux/ioctl.h> →...→ <asm-generic/ioctl.h> size [29:16] num[7:0]type[15:8] dir[31:30]
  • 18. 18© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Device Model (LDM) struct kobject - <linux/kobject.h> kref object Pointer to kset, the parent object kobj_type, type describing the kobject kobject instantiation → sysfs representation Parent object guides the entries under /sys/ bus – the physical buses class – the device categories device – the actual devices
  • 19. 19© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. udev & LDM Daemon: udevd Configuration: /etc/udev/udev.conf Rules: /etc/udev/rules.d/ Utility: udevinfo [-a] [-p <device_path>] Receives uevent on a change in /sys Accordingly, updates /dev &/or Performs the appropriate action for Hotplug Microcode / Firmware Download Module Autoload
  • 20. 20© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Device Model & Classes Latest way to create dynamic devices Create or Get the appropriate device category Create the desired device under that category Class Operations struct class *class_create(struct module *owner, char *name); void class_destroy(struct class *cl); Device into & out of Class struct class_device *device_create(struct class *cl, NULL, dev_t devnum, NULL, const char *fmt, ...); void device_destroy(struct class *cl, dev_t devnum);
  • 21. 21© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? W's of Character Drivers Major & Minor Numbers Registering & Unregistering Character Driver File Operations of a Character Driver Writing a Character Driver Linux Device Model udev & automatic device creation
  • 22. 22© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?