Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ioctl Interface
#1
Hi
I am trying to implement IOCTL interface to driver
I have created a strcuture
class pass_cmd(Structure):
    _fields_ = [
        ('opcode',       c_uint8),                #__u8   
        ('addr',         POINTER(c_uint64)),      #__u64   
        ('data_len',     c_uint32),               #__u32   
        ('timeout_ms',   c_uint32),               #__u32
        ('result',       c_uint32),               #__u32
    ]


buf = (c_uint64 * 4096)()
and
Args = pass_cmd()
Args.opcode             = 0x12
Args.addr               = POINTER(buf)# id(buf)
#Args.addr              = ctypes.POINTER(ctypes.cast(ctypes.byref(buf),ctypes.c_void_p))
Args.data_len           = 4096
Args.timeout_ms         = 400
Args.restult            = 0

devicehandle = open('/dev/block', 'rw')

output_buffer = fcntl.ioctl(devicehandle, 0xc0484e43 /*Magic number IOCTL */,  (Args))
The structure transferred to the kernel, in the addr field (Buffer address) is correct,
But when the kernel tried to write to the buffer it fails,

My question is how can I transfer the buffer in the structure so the kernel can write to it
Regards
Reply
#2
(Jun-18-2018, 02:11 PM)mickib1 Wrote: But when the kernel tried to write to the buffer it fails,
Is there an error? How do you know it's failing?
Reply
#3
Get and error from that the command failed, and see in the kernel log the command was aborted
Reply
#4
To send and receive buffer larger then 1024 you should use the False
to send buffer as pointer
class pass_cmd(Structure):
    _fields_ = [
        ('opcode',       ctypes.c_uint8),                #__u8   
        ("addr",         ctypes.POINTER(ctypes.c_uint8)),  
        ('data_len',     ctypes.c_uint32),               #__u32   
        ('timeout_ms',   ctypes.c_uint32),               #__u32
        ('result',       ctypes.c_uint32),               #__u32
    ]

buf = (ctypes.c_uint8 * 4096)() 
rgs = pass_cmd()
Args.opcode             = 0x12
Args.addr               = POINTER(buf)# id(buf)
#Args.addr              = ctypes.cast(buf, ctypes.POINTER(ctypes.c_uint8))
Args.data_len           = 4096
Args.timeout_ms         = 400
Args.restult            = 0

devicehandle = open('/dev/block', 'rw')
output_buffer = fcntl.ioctl(devicehandle, 0xc0484e43 ,  (cmd), False)
This Works on my device

The command you should use in order to transfer the buffer to kernel is
cmd.addr = ctypes.cast(buf, ctypes.POINTER(ctypes.c_uint8))
And not
POINTER(buf)# id(buf)
In order to print the buffer after
for x in range(len(buf)):
    print buf[x],
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  os.system("netsh interface set interface 'Wi-Fi' enabled") aniyanetworks 12 10,146 Jan-18-2019, 04:07 AM
Last Post: aniyanetworks

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020