Stuart
Stuart

Reputation: 1773

How to read a kernel module (/dev) file from user space C

In my kernel module the read function is as follows:

ssize_t aes_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { 

  unsigned long aes_reg[4];
  aes_reg[0] = leon_load_reg(output_mem_loc);
  aes_reg[1] = leon_load_reg(output_mem_loc+4);
  aes_reg[2] = leon_load_reg(output_mem_loc+8);
  aes_reg[3] = leon_load_reg(output_mem_loc+12);
  copy_to_user(buf, (char *)aes_reg, 16);
  ....

And it appears in the kernel module that buf is being set properly. On the user space side I have written this:

int main(int argc, char* argv[]){
    FILE *fpaes;
    char *str;
    int buf[4];

    fpaes = fopen("/dev/aes", "r");
    fread(str, 16, 1, fpaes);
    p_long = (unsigned long *)str;
    ....

But str is not being updated with the values I expect. Am I allowed to do an fread in this way or am I way off?

Upvotes: 0

Views: 783

Answers (1)

ouah
ouah

Reputation: 145899

str is not initialized in your program. Accessing *str is undefined behavior.

Upvotes: 3

Related Questions