Subject: Re: Kernel API question 2
To: None <tech-kern@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 03/23/2006 11:45:46
> Is it possible to access files from kernel space?

Yes.  Indeed, it *must* be, because the kernel mediates all file
access.

> I need to upload a firmware into a FPGA and I don't now if it is
> allowed to directly access files in NetBSD.

Others have pointed towards firmload(9) in -current.  But it's possible
to access files directly in older kernels too; you just have to do it
"by hand".  Here, for example, is a routine I once wrote (for a much
older OS version) to dump a debugging buffer to a file.  I can't
imagine it would be much harder to do something similar for read.  (My
use of IO_SYNC|IO_DSYNC was because this was known to be happening just
before a panic, and I wanted to have at least a fighting chance of the
data being there on reboot.)

static void save_buf_to_file(void)
{
 struct nameidata nd;
 int e;
 struct vnode *vp;
 struct uio uio;
 struct iovec iov;
 struct vattr vattr;

 NDINIT(&nd,LOOKUP,NOFOLLOW,UIO_SYSSPACE,"/heap.debug",initproc);
 e = vn_open(&nd,FREAD|FWRITE,0600);
 if (e)
  { printf("can't save buf: vn_open error %d\n",e);
    return;
  }
 vp = nd.ni_vp;
 iov.iov_base = &dbgbuf[0];
 iov.iov_len = dbgptr;
 uio.uio_iov = &iov;
 uio.uio_offset = 0;
 uio.uio_segflg = UIO_SYSSPACE;
 uio.uio_rw = UIO_WRITE;
 uio.uio_resid = dbgptr;
 uio.uio_iovcnt = 1;
 uio.uio_procp = 0;
 e = VOP_WRITE(vp,&uio,IO_SYNC|IO_DSYNC,initproc->p_ucred);
 if (e)
  { printf("can't save buf: VOP_WRITE error %d\n",e);
  }
 VATTR_NULL(&vattr);
 vattr.va_size = dbgptr;
 e = VOP_SETATTR(vp,&vattr,initproc->p_ucred,initproc);
 if (e)
  { printf("setting size: VOP_SETATTR error %d\n",e);
  }
 VOP_UNLOCK(vp,0);
 vn_close(vp,FREAD|FWRITE,initproc->p_ucred,initproc);
}

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B