From owner-freebsd-hackers Mon Mar 20 8: 6:14 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from duke.cs.duke.edu (duke.cs.duke.edu [152.3.140.1]) by hub.freebsd.org (Postfix) with ESMTP id 3778437B819 for ; Mon, 20 Mar 2000 08:06:07 -0800 (PST) (envelope-from anderson@cs.duke.edu) Received: from cold.cs.duke.edu (cold.cs.duke.edu [152.3.140.78]) by duke.cs.duke.edu (8.9.3/8.9.3) with ESMTP id LAA22873; Mon, 20 Mar 2000 11:06:05 -0500 (EST) From: Darrell Anderson Received: (anderson@localhost) by cold.cs.duke.edu (8.8.5/8.6.9) id LAA12793; Mon, 20 Mar 2000 11:06:05 -0500 (EST) Message-Id: <200003201606.LAA12793@cold.cs.duke.edu> Subject: Re: How to read a file from a device driver? To: garycor@home.com Date: Mon, 20 Mar 2000 11:06:05 -0500 (EST) Cc: freebsd-hackers@FreeBSD.ORG X-Mailer: ELM [version 2.5 PL2] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Fri, 17 Mar 2000, Gary T. Corcoran wrote: > > I'm trying to initialize a network device, and I'm trying to download > code *into* my device from some binary system files. There is no "user > space" or user process, for that matter, to deal with at this point. I > just want to (at this step) open a file(s) directly from my device > driver, read the file(s), and download the relevant parts to my device. sysctl might also do the trick. to answer your question, here's some code: struct vnode * vp_open(char *fname) { struct vnode *vp; struct nameidata nd; int error; NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, curproc); if ((error = vn_open(&nd, FREAD|FWRITE|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != 0) { printf("vp_open: failed for %s with error %d\n", fname, error); return NULL; } vp = nd.ni_vp; NDFREE(&nd, NDF_ONLY_PNBUF); VOP_UNLOCK(vp, 0, curproc); if (vp->v_type != VREG) { printf("vp_open: %s not a regular file\n", fname); vn_close(vp, FREAD|FWRITE, curproc->p_ucred, curproc); return NULL; } return vp; } int vp_close(struct vnode *vp) { int error; if (vp == NULL) { printf("vp_close: caller supplied NULL vp\n"); return ENOENT; } if ((error = vn_close(vp, FREAD|FWRITE, curproc->p_ucred, curproc)) { printf("vp_close: vn_close error %d\n", error); } return error; } int vp_read(struct vnode *vp, off_t offset, int len, caddr_t data) { struct iovec iov; struct uio uio; int error; iov.iov_base = data; iov.iov_len = len; uio.uio_iov = &iov; uio.uio_iovcnt = 1; uio.uio_offset = offset; uio.uio_resid = iov.iov_len; uio.uio_segflg = UIO_SYSSPACE; uio.uio_rw = UIO_READ; uio.uio_procp = NULL; if ((error = vn_lock(vp, LK_SHARED|LK_RETRY, curproc)) == 0) { error = VOP_READ(vp, &uio, IO_SYNC, curproc->p_ucred); VOP_UNLOCK(vp, 0, curproc); if (uio.uio_resid) { bzero(data + (len - uio.uio_resid), uio.uio_resid); } } return error; } -Darrell -- Department of Computer Science, Duke University, Durham, NC 27708-0129 Darrell Anderson, anderson@cs.duke.edu, http://www.cs.duke.edu/~anderson To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message