Esempio n. 1
0
static int fat_probe(struct device_d *dev)
{
	struct fs_device_d *fsdev = dev_to_fs_device(dev);
	struct fat_priv *priv = xzalloc(sizeof(struct fat_priv));
	char *backingstore = fsdev->backingstore;
	int ret;

	dev->priv = priv;

	if (!strncmp(backingstore , "/dev/", 5))
		backingstore += 5;

	priv->cdev = cdev_open(backingstore, O_RDWR);
	if (!priv->cdev) {
		ret = -ENOENT;
		goto err_open;
	}

	priv->fat.userdata = priv;
	ret = f_mount(&priv->fat);
	if (ret)
		goto err_mount;

	return 0;

err_mount:
	cdev_close(priv->cdev);
err_open:
	free(priv);

	return ret;
}
Esempio n. 2
0
static void *read_mtd_barebox(const char *partition)
{
	int ret;
	int size;
	void *to, *header;
	struct cdev *cdev;

	header = read_image_head(partition);
	if (header == NULL)
		return NULL;

	size = get_image_size(header);
	if (!size) {
		printf("failed to get image size\n");
		return NULL;
	}

	to = xmalloc(size);

	cdev = cdev_open(partition, O_RDONLY);
	if (!cdev) {
		printf("failed to open partition\n");
		return NULL;
	}

	ret = cdev_read(cdev, to, size, 0, 0);
	if (ret != size) {
		printf("failed to read from partition\n");
		return NULL;
	}

	return to;
}
Esempio n. 3
0
int mxs_ocotp_read(void *buf, int count, int offset)
{
	struct cdev *cdev;
	int ret;

	cdev = cdev_open(DRIVERNAME, O_RDONLY);
	if (!cdev)
		return -ENODEV;

	ret = cdev_read(cdev, buf, count, offset, 0);

	cdev_close(cdev);

	return ret;
}
Esempio n. 4
0
static void *read_image_head(const char *name)
{
	void *header = xmalloc(ARM_HEAD_SIZE);
	struct cdev *cdev;
	int ret;

	cdev = cdev_open(name, O_RDONLY);
	if (!cdev) {
		printf("failed to open %s\n", name);
		return NULL;
	}

	ret = cdev_read(cdev, header, ARM_HEAD_SIZE, 0, 0);
	cdev_close(cdev);

	if (ret != ARM_HEAD_SIZE) {
		printf("failed to read from %s\n", name);
		return NULL;
	}

	return header;
}
Esempio n. 5
0
/*
 * Internal open routine.  This really should be inside com.c
 * But I'm putting it here until we have a generic internal open
 * mechanism.
 */
int
sunkbdiopen(device_t dev, int flags)
{
	struct kbd_sun_softc *k = device_private(dev);
	struct tty *tp = k->k_priv;
	struct lwp *l = curlwp ? curlwp : &lwp0;
	struct termios t;
	int error;

	/* Open the lower device */
	if ((error = cdev_open(tp->t_dev, O_NONBLOCK|flags,
				     0/* ignored? */, l)) != 0)
		return (error);

	/* Now configure it for the console. */
	tp->t_ospeed = 0;
	t.c_ispeed = sunkbd_bps;
	t.c_ospeed = sunkbd_bps;
	t.c_cflag =  CLOCAL|CS8;
	(*tp->t_param)(tp, &t);

	return (0);
}
Esempio n. 6
0
void *omap_xload_boot_nand(int offset, int size)
{
	int ret;
	void *to = xmalloc(size);
	struct cdev *cdev;

	devfs_add_partition("nand0", offset, size, PARTITION_FIXED, "x");
	dev_add_bb_dev("x", "bbx");

	cdev = cdev_open("bbx", O_RDONLY);
	if (!cdev) {
		printf("failed to open nand\n");
		return NULL;
	}

	ret = cdev_read(cdev, to, size, 0, 0);
	if (ret != size) {
		printf("failed to read from nand\n");
		return NULL;
	}

	return to;
}
Esempio n. 7
0
static void *omap_xload_boot_nand(int offset, int part_size)
{
	int ret;
	int size;
	void *to, *header;
	struct cdev *cdev;

	devfs_add_partition("nand0", offset, part_size,
					DEVFS_PARTITION_FIXED, "x");
	dev_add_bb_dev("x", "bbx");

	header = read_image_head("bbx");
	if (header == NULL)
		return NULL;

	size = get_image_size(header);
	if (!size) {
		printf("failed to get image size\n");
		return NULL;
	}

	to = xmalloc(size);

	cdev = cdev_open("bbx", O_RDONLY);
	if (!cdev) {
		printf("failed to open nand\n");
		return NULL;
	}

	ret = cdev_read(cdev, to, size, 0, 0);
	if (ret != size) {
		printf("failed to read from nand\n");
		return NULL;
	}

	return to;
}
Esempio n. 8
0
static int do_imx_nand_bbm(int argc, char *argv[])
{
	int opt, ret;
	struct cdev *cdev;
	struct mtd_info *mtd;
	int yes = 0;
	void *bbt;

	while ((opt = getopt(argc, argv, "y")) > 0) {
		switch (opt) {
		case 'y':
			yes = 1;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	cdev = cdev_open("nand0", O_RDWR);
	if (!cdev)
		return -ENOENT;

	mtd = cdev->mtd;
	if (!mtd)
		return -EINVAL;

	if (strcmp(mtd->name, "imx_nand")) {
		printf("This is not an i.MX nand but a %s\n", mtd->name);
		ret = -EINVAL;
		goto out;
	}

	switch (mtd->writesize) {
	case 512:
		printf("writesize is 512. This command is not needed\n");
		ret = 1;
		goto out;
	case 2048:
		break;
	default:
		printf("not implemented for writesize %d\n", mtd->writesize);
		ret = 1;
		goto out;
	}

	bbt = create_bbt(mtd);
	if (IS_ERR(bbt)) {
		ret = 1;
		goto out;
	}

	if (!yes) {
		int c;

		printf("create flash bbt (y/n)?");
		c = getc();
		if (c == 'y')
			yes = 1;
		printf("\n");
	}

	if (!yes) {
		free(bbt);
		ret = 1;

		goto out;
	}

	ret = attach_bbt(mtd, bbt);
	if (!ret)
		printf("bbt successfully added\n");
	else
		free(bbt);

out:
	cdev_close(cdev);

	return ret;
}
/*===========================================================================*
 *				common_open				     *
 *===========================================================================*/
int common_open(char path[PATH_MAX], int oflags, mode_t omode, int for_exec)
{
/* Common code from do_creat and do_open. */
  int b, r, exist = TRUE;
  devmajor_t major_dev;
  dev_t dev;
  mode_t bits;
  struct filp *filp, *filp2;
  struct vnode *vp;
  struct vmnt *vmp;
  struct dmap *dp;
  struct lookup resolve;
  int fd, start = 0;

  /* Remap the bottom two bits of oflags. */
  bits = (mode_t) mode_map[oflags & O_ACCMODE];
  if (!bits) return(EINVAL);

  /* See if file descriptor and filp slots are available. */
  if ((r = get_fd(fp, start, bits, &fd, &filp)) != OK)
	return(r);

  lookup_init(&resolve, path, PATH_NOFLAGS, &vmp, &vp);

  /* If O_CREATE is set, try to make the file. */
  if (oflags & O_CREAT) {
        omode = I_REGULAR | (omode & ALLPERMS & fp->fp_umask);
	vp = new_node(&resolve, oflags, omode);
	r = err_code;
	if (r == OK) exist = FALSE;	/* We just created the file */
	else if (r != EEXIST) {		/* other error */
		if (vp) unlock_vnode(vp);
		unlock_filp(filp);
		return(r);
	}
	else exist = !(oflags & O_EXCL);/* file exists, if the O_EXCL
					   flag is set this is an error */
  } else {
	/* Scan path name */
	resolve.l_vmnt_lock = VMNT_READ;
	resolve.l_vnode_lock = VNODE_OPCL;
	if ((vp = eat_path(&resolve, fp)) == NULL) {
		unlock_filp(filp);
		return(err_code);
	}

	if (vmp != NULL) unlock_vmnt(vmp);
  }

  /* Claim the file descriptor and filp slot and fill them in. */
  fp->fp_filp[fd] = filp;
  filp->filp_count = 1;
  filp->filp_vno = vp;
  filp->filp_flags = oflags;
  if (oflags & O_CLOEXEC)
	FD_SET(fd, &fp->fp_cloexec_set);

  /* Only do the normal open code if we didn't just create the file. */
  if (exist) {
	/* Check permissions based on the given open flags, except when we are
	 * opening an executable for the purpose of passing a file descriptor
	 * to its interpreter for execution, in which case we check the X bit.
	 */
	if ((r = forbidden(fp, vp, for_exec ? X_BIT : bits)) == OK) {
		/* Opening reg. files, directories, and special files differ */
		switch (vp->v_mode & S_IFMT) {
		   case S_IFREG:
			/* Truncate regular file if O_TRUNC. */
			if (oflags & O_TRUNC) {
				if ((r = forbidden(fp, vp, W_BIT)) != OK)
					break;
				upgrade_vnode_lock(vp);
				truncate_vnode(vp, 0);
			}
			break;
		   case S_IFDIR:
			/* Directories may be read but not written. */
			r = (bits & W_BIT ? EISDIR : OK);
			break;
		   case S_IFCHR:
			/* Invoke the driver for special processing. */
			dev = vp->v_sdev;
			/* TTY needs to know about the O_NOCTTY flag. */
			r = cdev_open(fd, dev, bits | (oflags & O_NOCTTY));
			vp = filp->filp_vno;	/* Might be updated by
						 * cdev_open after cloning */
			break;
		   case S_IFBLK:

			lock_bsf();

			/* Invoke the driver for special processing. */
			dev = vp->v_sdev;
			r = bdev_open(dev, bits);
			if (r != OK) {
				unlock_bsf();
				break;
			}

			major_dev = major(vp->v_sdev);
			dp = &dmap[major_dev];
			if (dp->dmap_driver == NONE) {
				printf("VFS: block driver disappeared!\n");
				unlock_bsf();
				r = ENXIO;
				break;
			}

			/* Check whether the device is mounted or not. If so,
			 * then that FS is responsible for this device.
			 * Otherwise we default to ROOT_FS.
			 */
			vp->v_bfs_e = ROOT_FS_E; /* By default */
			for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp)
				if (vmp->m_dev == vp->v_sdev &&
				    !(vmp->m_flags & VMNT_FORCEROOTBSF)) {
					vp->v_bfs_e = vmp->m_fs_e;
				}

			/* Send the driver label to the file system that will
			 * handle the block I/O requests (even when its label
			 * and endpoint are known already), but only when it is
			 * the root file system. Other file systems will
			 * already have it anyway.
			 */
			if (vp->v_bfs_e != ROOT_FS_E) {
				unlock_bsf();
				break;
			}

			if (req_newdriver(vp->v_bfs_e, vp->v_sdev,
					dp->dmap_label) != OK) {
				printf("VFS: error sending driver label\n");
				bdev_close(dev);
				r = ENXIO;
			}
			unlock_bsf();
			break;

		   case S_IFIFO:
			/* Create a mapped inode on PFS which handles reads
			   and writes to this named pipe. */
			upgrade_vnode_lock(vp);
			r = map_vnode(vp, PFS_PROC_NR);
			if (r == OK) {
				if (vp->v_ref_count == 1) {
					if (vp->v_size != 0)
						r = truncate_vnode(vp, 0);
				}
				oflags |= O_APPEND;	/* force append mode */
				filp->filp_flags = oflags;
			}
			if (r == OK) {
				r = pipe_open(fd, vp, bits, oflags);
			}
			if (r != ENXIO) {
				/* See if someone else is doing a rd or wt on
				 * the FIFO.  If so, use its filp entry so the
				 * file position will be automatically shared.
				 */
				b = (bits & R_BIT ? R_BIT : W_BIT);
				filp->filp_count = 0; /* don't find self */
				if ((filp2 = find_filp(vp, b)) != NULL) {
				    /* Co-reader or writer found. Use it.*/
				    fp->fp_filp[fd] = filp2;
				    filp2->filp_count++;
				    filp2->filp_vno = vp;
				    filp2->filp_flags = oflags;

				    /* v_count was incremented after the vnode
				     * has been found. i_count was incremented
				     * incorrectly in FS, not knowing that we
				     * were going to use an existing filp
				     * entry.  Correct this error.
				     */
				    unlock_vnode(vp);
				    put_vnode(vp);
				} else {
				    /* Nobody else found. Restore filp. */
				    filp->filp_count = 1;
				}
			}
			break;
		   case S_IFSOCK:
			r = EOPNOTSUPP;
			break;
		   default:
			printf("VFS: attempt to open file <%llu,%llu> of "
			    "type 0%o\n", vp->v_dev, vp->v_inode_nr,
			    vp->v_mode & S_IFMT);
			r = EIO;
		}
	}
  }

  unlock_filp(filp);

  /* If error, release inode. */
  if (r != OK) {
	if (r != SUSPEND) {
		fp->fp_filp[fd] = NULL;
		filp->filp_count = 0;
		filp->filp_vno = NULL;
		put_vnode(vp);
	}
  } else {
	r = fd;
  }

  return(r);
}