flat assembler
Message board for the users of flat assembler.

Index > Linux > OSS ioctl parameters

Author
Thread Post new topic Reply to topic
keantoken



Joined: 19 Mar 2008
Posts: 69
keantoken 09 Mar 2013, 15:28
Hello.

I just found the Linux 64 examples on the site index and have been working on trying to make some functional programs. I have only begun learning programming, and only know assembly at this point. Higher level languages are far too confusing and indirect for me.

I am trying to figure out how to correctly pass the ioctl parameters from OSS defined in soundcard.h. I have successfully pulled off other syscalls but I can't figure out what format I need to use to enter the parameters. For instance, what does this line look like in bytes?

Code:
   999 #define SNDCTL_DSP_SETFMT                __SIOWR('P',5, int)     /* Selects ONE fmt */    


I'm guessing it is 2 bytes, first a P and then 5 in binary. But no matter what I do I get Einval. Here is what I'm doing:

Code:
        mov     rdx,[bitmask_afmt]             ; Hopefully this is correct?
        mov     rsi,[SNDCTL_DSP_SETFMT]
        mov     rdi,[dev_fd]
        mov     rax,sys_ioctl
        syscall
        or      rax,rax
        js      ERRFMT                                                                         

segment readable writeable  

bitmask_afmt    dq      0x00001000
zts_ossfmt      dq      "P",5 
    


If I can do this I might convert a portion of soundcard.h to use with with FASM.

Thanks
Post 09 Mar 2013, 15:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 11 Mar 2013, 00:49
There should not be any need to guess. It should be possible to determine exactly what the __SIOWR macro does by reading the definition of it.
Post 11 Mar 2013, 00:49
View user's profile Send private message Visit poster's website Reply with quote
keantoken



Joined: 19 Mar 2008
Posts: 69
keantoken 11 Mar 2013, 04:17
OH.

Code:
/* #define      SIOCTYPE                (0xff<<8) */
    74 #define  SIOCPARM_MASK   0x1fff  /* parameters must be < 8192 bytes */
    75 #define  SIOC_VOID       0x00000000      /* no parameters */
    76 #define  SIOC_OUT        0x20000000      /* copy out parameters */
    77 #define  SIOC_IN         0x40000000      /* copy in parameters */
    78 #define  SIOC_INOUT      (SIOC_IN|SIOC_OUT)
    79 
    80 #define  __SIO(x,y)      ((int)(SIOC_VOID|(x<<8)|y))
    81 #define  __SIOR(x,y,t)   ((int)(SIOC_OUT|((sizeof(t)&SIOCPARM_MASK)<<16)|(x<<8)|y))
    82 #define  __SIOW(x,y,t)   ((int)(SIOC_IN|((sizeof(t)&SIOCPARM_MASK)<<16)|(x<<8)|y))
    83 #define  __SIOWR(x,y,t)  ((int)(SIOC_INOUT|((sizeof(t)&SIOCPARM_MASK)<<16)|(x<<8)|y))
    84 #define __SIOC_SIZE(x)   ((x>>16)&SIOCPARM_MASK)
    85 #define __SIOC_DIR(x)    (x & 0xf0000000)
    86 #define __SIOC_NONE      SIOC_VOID
    87 #define __SIOC_READ      SIOC_OUT
    88 #define __SIOC_WRITE     SIOC_IN
    89 #  endif /* _IOWR */
    90 #endif /* !__SIOWR */    


So __SIOWR seems to be equivalent to:

Code:
ioctl_mask              dq      (0x20000000 or 0x40000000 or ((0x1fff and 4) shl 16)) or ('P' shl 8) or 5     


So now I've changed it to

Code:
        ; Set bit format
        lea     rdx,[bitmask_afmt]
        mov     rsi,[ioctl_mask]
        mov     rdi,[dev_fd]
        mov     rax,sys_ioctl
        syscall
        or      rax,rax
        js      ERRFMT                


segment readable writeable      ; data 

bitmask_afmt            dq      0x00001000
ioctl_mask              dq      (0x20000000 or 0x40000000 or ((0x1fff and 4) shl 16)) or ('P' shl 8) or 5
                                                                                                                       


Now it's telling me that the 3rd argument points to an invalid memory address. It's possible something in ioctl_mask is still wrong, since changing things in it does not lead to an EINVAL.
Post 11 Mar 2013, 04:17
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
bitRAKE 11 Mar 2013, 07:51
Maybe, this is a dumb* question: Is __SIOWR a memory address or a value to be used by the syscall? Maybe, I'm understanding incorrectly, but aren't all the arguments unsigned ints? Why would the syscall be needed if you were reading directly from the hardware?

*I don't have a clue about Linux.
Post 11 Mar 2013, 07:51
View user's profile Send private message Visit poster's website Reply with quote
keantoken



Joined: 19 Mar 2008
Posts: 69
keantoken 11 Mar 2013, 18:19
For OSS you use SYS_OPEN to open /dev/dsp. You use SYS_IOCTL to set parameters such as format and samplerate. You write to the file to play, read from the file to record. OSS handles everything else.

I'm not sure how to know whether a parameter is a memory address or the actual parameter. I'm guessing that's what it means when it says the parameter is a "pointer". But a pointer in Assembly is its own thing, so a pointer in this sense would in fact be a pointer to a pointer!
Post 11 Mar 2013, 18:19
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
bitRAKE 21 Mar 2013, 04:06
A pointer in assembly is just a number/address. It's how one uses that number that matters.

mov eax,[0x80000000]

...same as...

mov eax,0x80000000 ; just a number
mov eax,[eax]

So, I assume the brackets are not needed in your code?
Post 21 Mar 2013, 04:06
View user's profile Send private message Visit poster's website Reply with quote
keantoken



Joined: 19 Mar 2008
Posts: 69
keantoken 21 Mar 2013, 08:24
I really have tried everything I know of and the best error I can get is Invalid Argument. This happens when I use either the lea instruction or remove the brackets. So it seems the arguments must be addresses (pointers), rather than actual input. Assuming this is right, then my input must be wrong. But i've replicated the soundcard.h code to generate the input in assembly. The errno numbers just aren't enough to know what I'm doing wrong.
Post 21 Mar 2013, 08:24
View user's profile Send private message Reply with quote
keantoken



Joined: 19 Mar 2008
Posts: 69
keantoken 21 Mar 2013, 09:16
I managed to compile this C program:

http://manuals.opensound.com/developer/singen.c.html

I am trying to work with the debuggers I have but I haven't been able to figure out how to display the syscall parameters in any of them. BUT, if and when I do, maybe it will tell me what I'm doing wrong.

Note: this program plays a sine wave at full blast!


Description:
Download
Filename: singen.zip
Filesize: 3.29 KB
Downloaded: 567 Time(s)

Post 21 Mar 2013, 09:16
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.