# How can I use kernel functions?



## freebsdan (Dec 9, 2014)

Hi, I'm very new to the FreeBSD world. I need to write some BIOS information to a file, but as I figured the function I need, (bios_oem_strings(9)), can be called only from kernel mode.

I do the following: 
	
	



```
#include <sys/param.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <machine/param.h>
#include <machine/pmap.h>
#include <machine/pc/bios.h>

struct bios_oem tmp;

int main()
{
  unsigned char buf[128];
  bzero(buf, 128);
  bios_oem_strings(&tmp, buf, 127);
  return 0;
}
```
 and of course compilation failed with CC (clang). What can I do to call bios_oem_strings(9) and write the result to a file?


----------



## zirias@ (Dec 9, 2014)

Well, this is not exactly FreeBSD specific. It's the general design principle of having a _privileged_ kernel managing the system (how much of it depends on monolithic vs microkernel architecture) that cannot just be "mapped" into userspace. For details on this for e.g. x86, you could read up on protection rings.

Now, what can you do? You need a kernel <-> userspace interface, that could be for example:

a syscall
a device node, optionally supporting ioctls
something more "special" like sysctl, procfs, ...
You could try to provide e.g. a device node for reading BIOS properties.

OTOH... for this particular problem, maybe some /dev/mem based tool like dmidecode(8) or biosdecode(8), completely circumventing the kernel interface you found, would fit the bill?


----------



## freebsdan (Dec 9, 2014)

I must use exactly the bios_oem_strings function (I'm copying some existing functionality). Does that mean I should write a kernel module or compile my code right into kernel? My FreeBSD is x86 (i386).


----------



## zirias@ (Dec 9, 2014)

freebsdan said:


> I must use exactly the bios_oem_strings function (I'm copying some existing functionality).


Just maybe, someone could come up with better ideas when you outline the exact requirements 


freebsdan said:


> Does that mean I should write a kernel module or compile my code right into kernel?


That's the same thing in this context -- yes, if you must use the kernel's internal API, your code has to run inside the kernel, at least some glue presenting a userspace interface for it. I wouldn't do that as long as there aren't compelling reasons to do so. And as your initial question comes down to how to use this from userspace, you probably don't want it, but as I don't know what you're trying to accomplish, this is just a guess


----------



## freebsdan (Dec 11, 2014)

Thanks, Zirias.


----------

