Beispiel #1
0
int openconsole(const struct input_dev *idev, const struct output_dev *odev)
{
  close(0);
  if ( opendev(idev, odev, O_RDONLY) != 0 )
    return -1;
  close(1);
  if ( opendev(idev, odev, O_WRONLY) != 1 )
    return -1;
  close(2);
  if ( opendev(idev, odev, O_WRONLY) != 2 )
    return -1;

  return 0;
}
Beispiel #2
0
static PyObject *
S_get_toc(PyObject *self, PyObject *args)
{
    int fd;
    int newlist = 0;
    char *device_nodename;
    PyObject *tracks = NULL;
    if(!PyArg_ParseTuple(args, "s|O", &device_nodename, &tracks)) return NULL;
    fd = opendev(device_nodename, O_RDONLY | O_NONBLOCK, 0, &device_nodename);
    if(fd == -1)
    {
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, device_nodename);
        goto end;
    }
    if (tracks == NULL) {
        tracks = PyList_New(0);
        newlist = 1;
    }
    if(read_toc(fd, _get_toc_callback, (void*)tracks) == -1)
    {
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, device_nodename);
        goto error;
    }
    // catch exceptions from callbacks
    if(PyErr_Occurred() != NULL) goto error;
    end:
    close(fd);
    if (!newlist){ Py_INCREF(tracks); }
    return tracks;
    error:
    if (newlist){ Py_XDECREF(tracks); }
    tracks = NULL;
    goto end;
}
Beispiel #3
0
static int
cmdstop(int gargc __unused, char **gargv)
{
	int chip = 0, ctl = 0, err = 0, fd, running;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);

	err = is_ctrl_running(ctl, &running);
	if (err)
		return (EX_SOFTWARE);

	if (!running) {
		error(MSG_NOTRUNNING, ctl);
	} else {
		if (opendev(&fd) != EX_OK)
			return (EX_OSFILE);

		err = ioctl(fd, NANDSIM_STOP_CTRL, &ctl);
		close(fd);
		if (err) {
			error("Cannot stop controller#%d", ctl);
			err = EX_SOFTWARE;
		}
	}

	return (err);
}
Beispiel #4
0
int serial_open(const char *pathname, int flags, mode_t mode)
{
	char buf[1024];
	int speed = 9600;
	char *ptr = buf;

	strncpy(buf, pathname, 1000);

	if (!memcmp(ptr , "serial://", 9)) {
		ptr= ptr+9;
		// port
		char *spd = strchr(ptr, ':');
		if (spd == NULL) {
			printf("No speed defined.\n");
			return -1;
		}
		speed = atoi(spd+1);
		spd[0] = '\0';
		if (strlen(ptr)==0)
			return -1;
		
		serial_fd = opendev(ptr, speed);
		if (serial_fd>=0)
			printf("Serial connection to %s (speed=%d) done\n", ptr, speed);
		else	printf("Cannot open serial '%s'\n", ptr);
		serial_buf = (unsigned char *)malloc(1);
		serial_bufsz = 0;
		config_set("file.write", "true"); // ???
		buf[0]='\0';
	}
	return serial_fd;
}
Beispiel #5
0
static int
cmdstatus(int gargc, char **gargv)
{
	int chip = 0, ctl = 0, err = 0, fd, idx, idx2, start, stop;
	uint8_t verbose = 0;
	struct sim_ctrl ctrlconf;
	struct sim_chip chipconf;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err) {
		return (EX_USAGE);
	} else if (ctl == 0xff) {
		/* Every controller */
		start = 0;
		stop = MAX_SIM_DEV-1;
	} else {
		/* Specified controller only */
		start = ctl;
		stop = ctl;
	}

	if (opendev(&fd) != EX_OK)
		return (EX_OSFILE);

	for (idx = 0; idx < gargc; idx ++)
		if (strcmp(gargv[idx], "-v") == 0 ||
		    strcmp(gargv[idx], "--verbose") == 0)
			verbose = 1;

	for (idx = start; idx <= stop; idx++) {
		ctrlconf.num = idx;
		err = ioctl(fd, NANDSIM_STATUS_CTRL, &ctrlconf);
		if (err) {
			err = EX_SOFTWARE;
			error(MSG_STATUSACQCTRL, idx);
			continue;
		}

		printctrl(&ctrlconf);

		for (idx2 = 0; idx2 < MAX_CTRL_CS; idx2++) {
			chipconf.num = idx2;
			chipconf.ctrl_num = idx;

			err = ioctl(fd, NANDSIM_STATUS_CHIP, &chipconf);
			if (err) {
				err = EX_SOFTWARE;
				error(MSG_STATUSACQCTRL, idx);
				continue;
			}

			printchip(&chipconf, verbose);
		}
	}
	close(fd);
	return (err);
}
Beispiel #6
0
int
_rmtopendev(char *path, int oflags, int dflags, char **realpath)
{
#ifdef RMT
	if (host)
		return rmtopen(path, oflags);
#endif
	return opendev(path, oflags, dflags, realpath);
}
Beispiel #7
0
/*
 * Check a drive unit as the source for a move or exchange
 * operation. If the drive is not accessible, we attempt
 * to unmount the tape in it before moving to avoid
 * errors in "disconnected" type pickers where the drive
 * is on a separate target from the changer.
 */
static void
check_source_drive(int unit)
{
    struct mtop mtoffl =  { MTOFFL, 1 };
    struct changer_element_status_request cmd;
    struct changer_element_status *ces;
    struct changer_params data;
    size_t count = 0;
    int mtfd;
    char *tapedev;

    /*
     * Get params from changer.  Specifically, we need the element
     * counts.
     */
    bzero(&data, sizeof(data));
    if (ioctl(changer_fd, CHIOGPARAMS, &data))
        err(1, "%s: CHIOGPARAMS", changer_name);

    count = data.cp_ndrives;
    if (unit < 0 || unit >= count)
        err(1, "%s: invalid drive: drive %d", changer_name, unit);

    bzero(&cmd, sizeof(cmd));
    cmd.cesr_type = CHET_DT;
    /* Allocate storage for the status info. */
    cmd.cesr_data = calloc(count, sizeof(*cmd.cesr_data));
    if ((cmd.cesr_data) == NULL)
        errx(1, "can't allocate status storage");

    if (ioctl(changer_fd, CHIOGSTATUS, &cmd)) {
        free(cmd.cesr_data);
        err(1, "%s: CHIOGSTATUS", changer_name);
    }
    ces = &(cmd.cesr_data[unit]);

    if ((ces->ces_flags & CESTATUS_FULL) != CESTATUS_FULL)
        err(1, "%s: drive %d is empty!", changer_name, unit);

    if ((ces->ces_flags & CESTATUS_ACCESS) == CESTATUS_ACCESS)
        return; /* changer thinks all is well - trust it */

    /*
     * Otherwise, drive is FULL, but not accessible.
     * Try to make it accessible by doing an mt offline.
     */
    tapedev = parse_tapedev(_PATH_CH_CONF, changer_name, unit);
    mtfd = opendev(tapedev, O_RDONLY, 0, NULL);
    if (mtfd == -1)
        err(1, "%s drive %d (%s): open", changer_name, unit, tapedev);
    if (ioctl(mtfd, MTIOCTOP, &mtoffl) == -1)
        err(1, "%s drive %d (%s): rewoffl", changer_name, unit,
            tapedev);
    close(mtfd);
}
Beispiel #8
0
static int
cmdfreeze(int gargc __unused, char **gargv)
{
	int chip = 0, ctl = 0, err = 0, fd, i, start = 0, state, stop = 0;
	struct sim_ctrl_chip ctrlchip;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);

	if (ctl == 0xff) {
		error("You have to specify at least controller number");
		return (EX_USAGE);
	}

	if (ctl != 0xff && chip == 0xff) {
		start = 0;
		stop = MAX_CTRL_CS - 1;
	} else {
		start = chip;
		stop = chip;
	}

	ctrlchip.ctrl_num = ctl;

	err = is_ctrl_running(ctl, &state);
	if (err)
		return (EX_SOFTWARE);
	if (state == 0) {
		error(MSG_NOTRUNNING, ctl);
		return (EX_SOFTWARE);
	}

	if (opendev(&fd) != EX_OK)
		return (EX_OSFILE);

	for (i = start; i <= stop; i++) {
		err = is_chip_created(ctl, i, &state);
		if (err)
			return (EX_SOFTWARE);
		else if (state == 0) {
			continue;
		}

		ctrlchip.chip_num = i;
		err = ioctl(fd, NANDSIM_FREEZE, &ctrlchip);
		if (err) {
			error("Could not freeze ctrl#%d chip#%d", ctl, i);
			close(fd);
			return (EX_SOFTWARE);
		}
	}
	close(fd);
	return (EX_OK);
}
Beispiel #9
0
HRESULT
OPP_FPD_Initialize (FPD_Para Reg)
{
  printf ("FPD initializing...\n");
  if (hdl <= 0)
	hdl = opendev();
  if (ioctl (hdl, IO_OPP_FPD_Initialize, &Reg) < 0)
    {
      printf ("IO_OPP_FPD_Initialize error\n");
      return OPP_ERROR;
    }
  return OPP_OK;
}
Beispiel #10
0
static int
cmdlog(int gargc __unused, char **gargv)
{
	struct sim_log log;
	int chip = 0, ctl = 0, err = 0, fd, idx, start = 0, stop = 0;
	char *logbuf;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);

	logbuf = (char *)malloc(sizeof(char) * NANDSIM_RAM_LOG_SIZE);
	if (logbuf == NULL) {
		error("Not enough memory to create log buffer");
		return (EX_SOFTWARE);
	}

	memset(logbuf, 0, NANDSIM_RAM_LOG_SIZE);
	log.log = logbuf;
	log.len = NANDSIM_RAM_LOG_SIZE;

	if (ctl == 0xff) {
		start = 0;
		stop = MAX_SIM_DEV-1;
	} else {
		start = ctl;
		stop = ctl;
	}

	if (opendev(&fd) != EX_OK) {
		free(logbuf);
		return (EX_OSFILE);
	}

	/* Print logs for selected controller(s) */
	for (idx = start; idx <= stop; idx++) {
		log.ctrl_num = idx;

		err = ioctl(fd, NANDSIM_PRINT_LOG, &log);
		if (err) {
			error("Could not get log for controller %d!", idx);
			continue;
		}

		printf("Logs for controller#%d:\n%s\n", idx, logbuf);
	}

	free(logbuf);
	close(fd);
	return (EX_OK);
}
Beispiel #11
0
int
config(char *dev, char *file, int action, struct disklabel *dp, char *key,
    size_t keylen)
{
	struct vnd_ioctl vndio;
	char *rdev;
	int fd, rv = -1;

	if ((fd = opendev(dev, O_RDONLY, OPENDEV_PART, &rdev)) < 0) {
		err(4, "%s", rdev);
		goto out;
	}

	vndio.vnd_file = file;
	vndio.vnd_secsize = (dp && dp->d_secsize) ? dp->d_secsize : DEV_BSIZE;
	vndio.vnd_nsectors = (dp && dp->d_nsectors) ? dp->d_nsectors : 100;
	vndio.vnd_ntracks = (dp && dp->d_ntracks) ? dp->d_ntracks : 1;
	vndio.vnd_key = (u_char *)key;
	vndio.vnd_keylen = keylen;

	/*
	 * Clear (un-configure) the device
	 */
	if (action == VND_UNCONFIG) {
		rv = ioctl(fd, VNDIOCCLR, &vndio);
		if (rv)
			warn("VNDIOCCLR");
		else if (verbose)
			printf("%s: cleared\n", dev);
	}
	/*
	 * Configure the device
	 */
	if (action == VND_CONFIG) {
		rv = ioctl(fd, VNDIOCSET, &vndio);
		if (rv)
			warn("VNDIOCSET");
		else if (verbose)
			printf("%s: %llu bytes on %s\n", dev, vndio.vnd_size,
			    file);
	}

	close(fd);
	fflush(stdout);
 out:
	if (key)
		memset(key, 0, keylen);
	return (rv < 0);
}
Beispiel #12
0
int
DISK_open(char *disk, int mode)
{
	int fd;
	struct stat st;

	fd = opendev(disk, mode, OPENDEV_PART, NULL);
	if (fd == -1)
		err(1, "%s", disk);
	if (fstat(fd, &st) == -1)
		err(1, "%s", disk);
	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
		err(1, "%s is not a character device or a regular file", disk);
	return (fd);
}
Beispiel #13
0
static void
sr_install_bootblk(int devfd, int vol, int disk)
{
	struct bioc_disk bd;
	struct disklabel dl;
	struct partition *pp;
	uint32_t poffset;
	char *realdev;
	char part;
	int diskfd;
	int rv;

	/* Get device name for this disk/chunk. */
	memset(&bd, 0, sizeof(bd));
	bd.bd_volid = vol;
	bd.bd_diskid = disk;
	rv = ioctl(devfd, BIOCDISK, &bd);
	if (rv == -1)
		err(1, "BIOCDISK");

	/* Check disk status. */
	if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
		fprintf(stderr, "softraid chunk %u not online - skipping...\n",
		    disk);
		return;	
	}

	if (strlen(bd.bd_vendor) < 1)
		errx(1, "invalid disk name");
	part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
	if (part < 'a' || part >= 'a' + MAXPARTITIONS)
		errx(1, "invalid partition %c\n", part);
	bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';

	/* Open device. */
	if ((diskfd = opendev(bd.bd_vendor, (nowrite ? O_RDONLY : O_RDWR),
	    OPENDEV_PART, &realdev)) < 0)
		err(1, "open: %s", realdev);

	if (verbose)
		fprintf(stderr, "%s%c: installing boot blocks on %s\n",
		    bd.bd_vendor, part, realdev);

	/* Write boot blocks to device. */
	write_bootblk(diskfd);

	close(diskfd);
}
Beispiel #14
0
HRESULT
OSP04_Initialize (UINT8 brightness)
{
  printf ("OSP_Initialize start\n");
//  int hdl = getdev (1);
  if (hdl <= 0)
        hdl = opendev();
  unsigned int val = brightness;
  if (ioctl(hdl, IO_OSP_SetBrightness, &val) < 0)
    {
      printf ("IO_OPP_Initialize error\n");
      return OPP_ERROR;
    }
  printf("finished Initialize\n");
  return OPP_OK;
}
Beispiel #15
0
int
getinfo(const char *vname)
{
	int vd, print_all = 0;
	struct vnd_user vnu;

	if (vname == NULL) {
		vname = DEFAULT_VND;
		print_all = 1;
	}

	vd = opendev((char *)vname, O_RDONLY, OPENDEV_PART, NULL);
	if (vd < 0)
		err(1, "open: %s", vname);

	vnu.vnu_unit = -1;

query:
	if (ioctl(vd, VNDIOCGET, &vnu) == -1) {
		if (print_all && errno == ENXIO && vnu.vnu_unit > 0) {
			close(vd);
			return (0);
		} else {
			err(1, "ioctl: %s", vname);
		}
	}

	fprintf(stdout, "vnd%d: ", vnu.vnu_unit);

	if (!vnu.vnu_ino)
		fprintf(stdout, "not in use\n");
	else
		fprintf(stdout, "covering %s on %s, inode %llu\n",
		    vnu.vnu_file, devname(vnu.vnu_dev, S_IFBLK),
		    (unsigned long long)vnu.vnu_ino);

	if (print_all) {
		vnu.vnu_unit++;
		goto query;
	}

	close(vd);

	return (0);
}
Beispiel #16
0
int
bio_parse_devlist(char *lst, dev_t *dt)
{
    char			*s, *e;
    u_int32_t		sz = 0;
    int			no_dev = 0, i, x;
    struct stat		sb;
    char			dev[MAXPATHLEN];
    int			fd;

    if (!lst)
        errx(1, "invalid device list");

    s = e = lst;
    /* make sure we have a valid device list like /dev/sdNa,/dev/sdNNa */
    while (*e != '\0') {
        if (*e == ',')
            s = e + 1;
        else if (*(e + 1) == '\0' || *(e + 1) == ',') {
            /* got one */
            sz = e - s + 1;
            strlcpy(dev, s, sz + 1);
            fd = opendev(dev, O_RDONLY, OPENDEV_BLCK, NULL);
            if (fd == -1)
                err(1, "could not open %s", dev);
            if (fstat(fd, &sb) == -1) {
                close(fd);
                err(1, "could not stat %s", dev);
            }
            close(fd);
            dt[no_dev] = sb.st_rdev;
            no_dev++;
            if (no_dev > (int)(BIOC_CRMAXLEN / sizeof(dev_t)))
                errx(1, "too many devices on device list");
        }
        e++;
    }

    for (i = 0; i < no_dev; i++)
        for (x = 0; x < no_dev; x++)
            if (dt[i] == dt[x] && x != i)
                errx(1, "duplicate device in list");

    return (no_dev);
}
Beispiel #17
0
static int
cmderror(int gargc __unused, char **gargv)
{
	uint32_t page, column, len, pattern;
	int chip = 0, ctl = 0, err = 0, fd;
	struct sim_error sim_err;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);

	if (chip == 0xff) {
		error(MSG_CTRLCHIPNEEDED);
		return (EX_USAGE);
	}

	if (convert_arguint(gargv[3], &page) ||
	    convert_arguint(gargv[4], &column) ||
	    convert_arguint(gargv[5], &len) ||
	    convert_arguint(gargv[6], &pattern))
		return (EX_SOFTWARE);

	if (!assert_chip_connected(ctl, chip))
		return (EX_SOFTWARE);

	sim_err.page_num = page;
	sim_err.column = column;
	sim_err.len = len;
	sim_err.pattern = pattern;
	sim_err.ctrl_num = ctl;
	sim_err.chip_num = chip;

	if (opendev(&fd) != EX_OK)
		return (EX_OSFILE);

	err = ioctl(fd, NANDSIM_INJECT_ERROR, &sim_err);

	close(fd);
	if (err) {
		error("Could not inject error !");
		return (EX_SOFTWARE);
	}
	return (EX_OK);
}
Beispiel #18
0
void 
threadmain(int argc, char **argv)
{
	Dev *yd;
	char *devdir = nil;
	Ep *ep;
	int csps[] = {YurexCSP, 0};
	int i;

	if(finddevs(matchdevcsp, csps, &devdir, 1) < 1){
		fprint(2, "No yurex device\n");
		threadexitsall("yurex not found");
	}
	yd = opendev(devdir);
	if(yd == nil)
		sysfatal("opendev: %r");
	if(configdev(yd)<0)
		sysfatal("configdev: %r");

	for(i = 0; i < nelem(yd->usb->ep); i++){
		if((ep = yd->usb->ep[i]) == nil)
			break;
		if(ep->type == Eintr && ep->dir == Ein)
			if(ep->iface->csp == YurexCSP){
				yc.ep = openep(yd, ep->id);
				if(yc.ep == nil)
					sysfatal("YUREX: %s: openep %d: %r\n", yd->dir, ep->id);
				if(opendevdata(yc.ep, OREAD) < 0){
					fprint(2, "YUERX: %s: opendevdata: %r\n",  yc.ep->dir);
					closedev(yc.ep);
					yc.ep = nil;
					break;
				}

				fs.tree = alloctree(nil, nil, DMDIR|0777, destroyfile);
				createfile(fs.tree->root, "bbu", nil, 0444, nil);
				createfile(fs.tree->root, "mbbups", nil, 0444, nil);
				threadpostmountsrv(&fs, "yurex", nil, MREPL|MCREATE);
				proccreate(yurexwork, nil, Stack);
			}
	}

	threadexits(nil);
}
Beispiel #19
0
static int
ckprint(char *s)
{
	char buf[ERRMAX];
	int st;
	Dev d;

	squelch = 1;
	d.fd = -1;
	st = opendev(s, &d);
	squelch = 0;
	if(st == -1){
		rerrstr(buf, sizeof buf);
		if(strstr(buf, "ata command") != nil)
			return 0;
		return 0 /* -1 */;
	}
	close(d.fd);
	print("%s\t%llud; %ud\t%llux\n", s, d.nsect, d.secsize, d.wwn);
	return 1;
}
Beispiel #20
0
static int
openpartition(char *name, int flags, char **devicep)
{
    char		rawspec[MAXPATHLEN], *p;
    struct fstab	*fs;
    int		fd;

    fs = getfsfile(name);
    if (fs) {
        if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
            snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
                     (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
            name = rawspec;
        } else
            name = fs->fs_spec;
    }
    fd = opendev(name, flags, 0, devicep);
    if (fd == -1 && errno == ENOENT)
        devicep = &name;
    return (fd);
}
Beispiel #21
0
static PyObject *
S_rip_sectors(PyObject *self, PyObject *args)
{
    int start, end, fd;
    char *device_nodename;
    ripinfo state;
    if(!PyArg_ParseTuple(args, "siiOO", &device_nodename, &start, &end,
        &state.callback, &state.state)) return NULL;
    fd = opendev(device_nodename, O_RDONLY | O_NONBLOCK, 0, &device_nodename);
    if(fd == -1)
    {
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, device_nodename);
        return NULL;
    }
    if(ripsectors(fd, start, end, _ripsector_callback, (void*)&state) == -1)
    {
        close(fd);
        return NULL;
    }
    close(fd);
    Py_RETURN_NONE;
}
Beispiel #22
0
int
main(int argc, char *argv[])
{
	int c;
	int fd;
	int iflag = 0;
	int exitcode = 0;
	char *realpath;

	while ((c = getopt(argc, argv, "iqv")) != -1) {
		switch (c) {
		case 'i':
			iflag = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			usage();
		}
	}

	if (argc - optind != 1)
		usage();

	fd = opendev(argv[optind], iflag ? O_RDWR : O_RDONLY, OPENDEV_PART,
	    &realpath);
	if (fd < 0)
		err(1, "open(%s)", realpath);

	if (iflag)
		initialize(fd);
	else
		report(fd);

	close(fd);
	exit(exitcode);
}
Beispiel #23
0
/*
 * Find and open the configuration file
 */
__export int open_config(void)
{
    int fd, handle;
    struct file_info *fp;

    fd = opendev(&__file_dev, NULL, O_RDONLY);
    if (fd < 0)
	return -1;

    fp = &__file_info[fd];

    handle = this_fs->fs_ops->open_config(&fp->i.fd);
    if (handle < 0) {
	close(fd);
	errno = ENOENT;
	return -1;
    }

    fp->i.offset = 0;
    fp->i.nbytes = 0;

    return fd;
}
Beispiel #24
0
  int
main(int argc, char ** argv)
{
  if (sizeof(off_t) != 8) {
    printf("off_t: %zu\n", sizeof(off_t));
    exit(1);
  }

  if (argc < 6) {
    printf("usage: %s <dev> <nr_r> <nr_w> <shift> <dur> [<perc>]\n", argv[0]);
    exit(1);
  }

  const int fd = opendev(argv[1]);
  assert(fd >= 0);
  const int nr_r = atoi(argv[2]);
  const int nr_w = atoi(argv[3]);
  const int shift = atoi(argv[4]);
  const int dur = atoi(argv[5]);
  double perc = 1.0;
  if (argc == 7) {
    int iperc = 0;
    sscanf(argv[6], "%3d", &iperc);
    perc = ((double)iperc) / 100.0;
  }
  assert((shift < 32) && (shift >= 12));
  const size_t mask = (1u << shift) - 1u;
  struct sigaction sa;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART;
  sa.sa_handler = sig_handler;
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGTERM, &sa, NULL);
  threaded_rw(fd, mask, dur, nr_r, nr_w, perc);
  return 0;
}
Beispiel #25
0
static int
cmdstart(int gargc __unused, char **gargv)
{
	int chip = 0, ctl = 0, err = 0, fd, running, state;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);

	err = is_ctrl_created(ctl, &state);
	if (err) {
		return (EX_SOFTWARE);
	} else if (state == 0) {
		error(MSG_NOCTRL, ctl);
		return (EX_SOFTWARE);
	}

	err = is_ctrl_running(ctl, &running);
	if (err)
		return (EX_SOFTWARE);

	if (running) {
		warn(MSG_RUNNING, ctl);
	} else {
		if (opendev(&fd) != EX_OK)
			return (EX_OSFILE);

		err = ioctl(fd, NANDSIM_START_CTRL, &ctl);
		close(fd);
		if (err) {
			error("Cannot start controller#%d", ctl);
			err = EX_SOFTWARE;
		}
	}
	return (err);
}
Beispiel #26
0
int
main(int argc, char *argv[])
{
	int ch, f, error = 0;
	struct disklabel *lp;
	FILE *t;

	while ((ch = getopt(argc, argv, "ABEf:F:hRb:cdenp:tvw")) != -1)
		switch (ch) {
		case 'A':
			++aflag;
			break;
#if NUMBOOT > 0
		case 'B':
			++installboot;
			break;
		case 'b':
			xxboot = optarg;
			break;
#endif
		case 'R':
			if (op != UNSPEC)
				usage();
			op = RESTORE;
			break;
		case 'c':
			++cflag;
			break;
		case 'd':
			++dflag;
			break;
		case 'e':
			if (op != UNSPEC)
				usage();
			op = EDIT;
			break;
		case 'E':
			if (op != UNSPEC)
				usage();
			op = EDITOR;
			break;
		case 'f':
			fstabfile = optarg;
			uidflag = 0;
			break;
		case 'F':
			fstabfile = optarg;
			++uidflag;
			break;
		case 'h':
			print_unit = '*';
			break;
		case 't':
			++tflag;
			break;
		case 'w':
			if (op != UNSPEC)
				usage();
			op = WRITE;
			break;
		case 'p':
			if (strchr("bckmgtBCKMGT", optarg[0]) == NULL ||
			    optarg[1] != '\0') {
				fprintf(stderr, "Valid units are bckmgt\n");
				exit(1);
			}
			print_unit = tolower((unsigned char)optarg[0]);
			break;
		case 'n':
			donothing++;
			break;
		case 'v':
			verbose++;
			break;
		case '?':
		default:
			usage();
	}
	argc -= optind;
	argv += optind;

#if NUMBOOT > 0
	if (installboot) {
		if (op == UNSPEC)
			op = WRITEBOOT;
	} else {
		if (op == UNSPEC)
			op = READ;
	}
#else
	if (op == UNSPEC)
		op = READ;
#endif

	if (argc < 1 || (fstabfile && !(op == EDITOR || aflag)))
		usage();

	dkname = argv[0];
	f = opendev(dkname, (op == READ ? O_RDONLY : O_RDWR), OPENDEV_PART,
	    &specname);
	if (f < 0)
		err(4, "%s", specname);

	switch (op) {
	case EDIT:
		if (argc != 1)
			usage();
		readlabel(f);
		error = edit(&lab, f);
		break;
	case EDITOR:
		if (argc != 1)
			usage();
		readlabel(f);
		error = editor(f);
		break;
	case READ:
		if (argc != 1)
			usage();
		readlabel(f);
		if (tflag)
			makedisktab(stdout, &lab);
		else
			display(stdout, &lab, print_unit, 1);
		error = checklabel(&lab);
		break;
	case RESTORE:
		if (argc < 2 || argc > 3)
			usage();
		readlabel(f);
#if NUMBOOT > 0
		if (installboot && argc == 3)
			makelabel(argv[2], NULL, &lab);
#endif
		lp = makebootarea(bootarea, &lab);
		*lp = lab;
		if (!(t = fopen(argv[1], "r")))
			err(4, "%s", argv[1]);
		error = getasciilabel(t, lp);
		bzero(lp->d_uid, sizeof(lp->d_uid));
		if (error == 0)
			error = writelabel(f, bootarea, lp);
		fclose(t);
		break;
	case WRITE:
		if (dflag || aflag) {
			readlabel(f);
		} else if (argc < 2 || argc > 3)
			usage();
		else
			makelabel(argv[1], argc == 3 ? argv[2] : NULL, &lab);
		lp = makebootarea(bootarea, &lab);
		*lp = lab;
		error = checklabel(&lab);
		if (error == 0)
			error = writelabel(f, bootarea, lp);
		break;
#if NUMBOOT > 0
	case WRITEBOOT:
	{
		struct disklabel tlab;

		readlabel(f);
		tlab = lab;
		if (argc == 2)
			makelabel(argv[1], NULL, &lab);
		lp = makebootarea(bootarea, &lab);
		*lp = tlab;
		error = checklabel(&lab);
		if (error == 0)
			error = writelabel(f, bootarea, lp);
		break;
	}
#endif
	default:
		break;
	}
	exit(error);
}
Beispiel #27
0
static void
startdevproc(void *a)
{
	Sarg	*sa = a;
	Dev	*d;
	Devtab *dt;
	int	argc;
	char *args, *argse, **argv;
	char *fname;

	threadsetgrp(threadid());
	d = sa->pp->dev;
	dt = sa->dt;
	args = sa->args;
	argse = sa->args + sizeof sa->args;
	argv = sa->argv;
	fname = sa->fname;
	sa->pp->devmaskp = &dt->devmask;
	sa->pp->devnb = getdevnb(&dt->devmask);
	if(sa->pp->devnb < 0){
		sa->pp->devmaskp = nil;
		sa->pp->devnb = 0;
	}else
		args = seprint(args, argse, "-N %d", sa->pp->devnb);
	if(dt->args != nil)
		seprint(args, argse, " %s", dt->args);
	args = sa->args;
	dprint(2, "%s: start: %s %s\n", argv0, dt->name, args);
	argv[0] = dt->name;
	argc = 1;
	if(args[0] != 0)
		argc += tokenize(args, argv+1, nelem(sa->argv)-2);
	argv[argc] = nil;
	if(dt->init == nil){
		if(d->dfd > 0 ){
			close(d->dfd);
			d->dfd = -1;
		}
		rfork(RFCFDG);
		open("/dev/null", OREAD);
		open("/dev/cons", OWRITE);
		open("/dev/cons", OWRITE);

		xexec(sa->rc, argv[0], argv);
		snprint(fname, sizeof(sa->fname), "/bin/usb/%s", dt->name);
		xexec(sa->rc, fname, argv);
		snprint(fname, sizeof(sa->fname), "/boot/%s", dt->name);
		xexec(sa->rc, fname, argv);
		if(cputype == nil)
			cputype = getenv("cputype");
		if(cputype != nil){
			snprint(fname, sizeof(sa->fname), "/%s/bin/%s",
				cputype, dt->name);
			argv[0] = fname;
			xexec(sa->rc, fname, argv);
		}
		fprint(2, "%s: %s: not found. can't exec\n", argv0, dt->name);
		sendul(sa->rc, -1);
		threadexits("exec");
	}else{
		sa->pp->dev = opendev(d->dir);
		sendul(sa->rc, 0);
		if(dt->init(d, argc, argv) < 0)
			fprint(2, "%s: %s: %r\n", argv0, dt->name);
		closedev(d);
		free(sa);
	}
	threadexits(nil);
}
Beispiel #28
0
Dev*
openep(Dev *d, int id)
{
	char *mode;	/* How many modes? */
	Ep *ep;
	Altc *ac;
	Dev *epd;
	Usbdev *ud;
	char name[40];

	if(access("/dev/usb", AEXIST) < 0 && bind("#u", "/dev", MBEFORE) < 0)
		return nil;
	if(d->cfd < 0 || d->usb == nil){
		werrstr("device not configured");
		return nil;
	}
	ud = d->usb;
	if(id < 0 || id >= nelem(ud->ep) || ud->ep[id] == nil){
		werrstr("bad enpoint number");
		return nil;
	}
	ep = ud->ep[id];
	mode = "rw";
	if(ep->dir == Ein)
		mode = "r";
	if(ep->dir == Eout)
		mode = "w";
	snprint(name, sizeof(name), "/dev/usb/ep%d.%d", d->id, id);
	if(access(name, AEXIST) == 0){
		dprint(2, "%s: %s already exists; trying to open\n", argv0, name);
		epd = opendev(name);
		if(epd != nil)
			epd->maxpkt = ep->maxpkt;	/* guess */
		return epd;
	}
	if(devctl(d, "new %d %d %s", id, ep->type, mode) < 0){
		dprint(2, "%s: %s: new: %r\n", argv0, d->dir);
		return nil;
	}
	epd = opendev(name);
	if(epd == nil)
		return nil;
	epd->id = id;
	if(devctl(epd, "maxpkt %d", ep->maxpkt) < 0)
		fprint(2, "%s: %s: openep: maxpkt: %r\n", argv0, epd->dir);
	else
		dprint(2, "%s: %s: maxpkt %d\n", argv0, epd->dir, ep->maxpkt);
	epd->maxpkt = ep->maxpkt;
	ac = ep->iface->altc[0];
	if(ep->ntds > 1 && devctl(epd, "ntds %d", ep->ntds) < 0)
		fprint(2, "%s: %s: openep: ntds: %r\n", argv0, epd->dir);
	else
		dprint(2, "%s: %s: ntds %d\n", argv0, epd->dir, ep->ntds);

	/*
	 * For iso endpoints and high speed interrupt endpoints the pollival is
	 * actually 2ⁿ and not n.
	 * The kernel usb driver must take that into account.
	 * It's simpler this way.
	 */

	if(ac != nil && (ep->type == Eintr || ep->type == Eiso) && ac->interval != 0)
		if(devctl(epd, "pollival %d", ac->interval) < 0)
			fprint(2, "%s: %s: openep: pollival: %r\n", argv0, epd->dir);
	return epd;
}
Beispiel #29
0
static int
cmdrestore(int gargc __unused, char **gargv)
{
	struct sim_dump dump;
	struct sim_block_state bs;
	struct stat filestat;
	int chip = 0, ctl = 0, err = 0, fd, dumpfd = -1;
	uint32_t blkidx, blksz, fsize = 0, expfilesz;
	void *buf;
	struct chip_param_io cparams, dumpcparams;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);
	else if (ctl == 0xff) {
		error(MSG_CTRLCHIPNEEDED);
		return (EX_USAGE);
	}

	if (!assert_chip_connected(ctl, chip))
		return (EX_SOFTWARE);

	/* Get chip geometry */
	if (opencdev(&fd, ctl, chip) != EX_OK)
		return (EX_OSFILE);

	err = ioctl(fd, NAND_IO_GET_CHIP_PARAM, &cparams);
	if (err) {
		error("Cannot get parameters for chip %d:%d", ctl, chip);
		close(fd);
		return (err);
	}
	close(fd);

	/* Obtain dump file size */
	errno = 0;
	if (stat(gargv[3], &filestat) != 0) {
		error("Could not acquire file size! : %s",
		    strerror(errno));
		return (EX_IOERR);
	}

	fsize = filestat.st_size;
	blksz = cparams.pages_per_block * (cparams.page_size +
	    cparams.oob_size);

	/* Expected dump file size for chip */
	expfilesz = cparams.blocks * (blksz + sizeof(bs)) + sizeof(cparams);

	if (fsize != expfilesz) {
		error("File size does not match chip geometry (file size: %d"
		    ", dump size: %d)", fsize, expfilesz);
		return (EX_SOFTWARE);
	}

	dumpfd = open(gargv[3], O_RDONLY);
	if (dumpfd == -1) {
		error("Could not open dump file!");
		return (EX_IOERR);
	}

	/* Read chip params saved in dumpfile */
	read(dumpfd, &dumpcparams, sizeof(dumpcparams));

	/* XXX */
	if (bcmp(&dumpcparams, &cparams, sizeof(cparams)) != 0) {
		error("Supplied dump is created for a chip with different "
		    "chip configuration!");
		close(dumpfd);
		return (EX_SOFTWARE);
	}

	if (opendev(&fd) != EX_OK) {
		close(dumpfd);
		return (EX_OSFILE);
	}

	buf = malloc(blksz);
	if (buf == NULL) {
		error("Could not allocate memory for block buffer");
		close(dumpfd);
		close(fd);
		return (EX_SOFTWARE);
	}

	dump.ctrl_num = ctl;
	dump.chip_num = chip;
	dump.data = buf;
	/* Restore block states and wearouts */
	for (blkidx = 0; blkidx < cparams.blocks; blkidx++) {
		dump.block_num = blkidx;
		if (read(dumpfd, &bs, sizeof(bs)) != sizeof(bs)) {
			error("Error reading dumpfile");
			close(dumpfd);
			close(fd);
			free(buf);
			return (EX_SOFTWARE);
		}
		bs.ctrl_num = ctl;
		bs.chip_num = chip;
		debug("BLKIDX=%d BLOCKS=%d CTRL=%d CHIP=%d STATE=%d\n"
		    "WEAROUT=%d BS.CTRL_NUM=%d BS.CHIP_NUM=%d\n",
		    blkidx, cparams.blocks, dump.ctrl_num, dump.chip_num,
		    bs.state, bs.wearout, bs.ctrl_num, bs.chip_num);

		err = ioctl(fd, NANDSIM_SET_BLOCK_STATE, &bs);
		if (err) {
			error("Could not set bad block(%d) for "
			    "controller: %d, chip: %d!", blkidx, ctl, chip);
			close(dumpfd);
			close(fd);
			free(buf);
			return (EX_SOFTWARE);
		}
	}
	/* Restore data */
	for (blkidx = 0; blkidx < cparams.blocks; blkidx++) {
		errno = 0;
		dump.len = read(dumpfd, buf, blksz);
		if (errno) {
			error("Failed to read block#%d from dumpfile.", blkidx);
			err = EX_SOFTWARE;
			break;
		}
		dump.block_num = blkidx;
		err = ioctl(fd, NANDSIM_RESTORE, &dump);
		if (err) {
			error("Could not restore block#%d of ctrl#%d chip#%d"
			    ": %s", blkidx, ctl, chip, strerror(errno));
			err = EX_SOFTWARE;
			break;
		}
	}

	free(buf);
	close(dumpfd);
	close(fd);
	return (err);

}
Beispiel #30
0
static int
cmddump(int gargc __unused, char **gargv)
{
	struct sim_dump dump;
	struct sim_block_state bs;
	struct chip_param_io cparams;
	int chip = 0, ctl = 0, err = EX_OK, fd, dumpfd;
	uint32_t blkidx, bwritten = 0, totalwritten = 0;
	void *buf;

	err = parse_devstring(gargv[2], &ctl, &chip);
	if (err)
		return (EX_USAGE);

	if (chip == 0xff || ctl == 0xff) {
		error(MSG_CTRLCHIPNEEDED);
		return (EX_USAGE);
	}

	if (!assert_chip_connected(ctl, chip))
		return (EX_SOFTWARE);

	if (opencdev(&fd, ctl, chip) != EX_OK)
		return (EX_OSFILE);

	err = ioctl(fd, NAND_IO_GET_CHIP_PARAM, &cparams);
	if (err) {
		error("Cannot get parameters for chip %d:%d", ctl, chip);
		close(fd);
		return (EX_SOFTWARE);
	}
	close(fd);

	dump.ctrl_num = ctl;
	dump.chip_num = chip;

	dump.len = cparams.pages_per_block * (cparams.page_size +
	    cparams.oob_size);

	buf = malloc(dump.len);
	if (buf == NULL) {
		error("Could not allocate memory!");
		return (EX_SOFTWARE);
	}
	dump.data = buf;

	errno = 0;
	dumpfd = open(gargv[3], O_WRONLY | O_CREAT, 0666);
	if (dumpfd == -1) {
		error("Cannot create dump file.");
		free(buf);
		return (EX_SOFTWARE);
	}

	if (opendev(&fd)) {
		close(dumpfd);
		free(buf);
		return (EX_SOFTWARE);
	}

	bs.ctrl_num = ctl;
	bs.chip_num = chip;

	/* First uint32_t in file shall contain block count */
	if (write(dumpfd, &cparams, sizeof(cparams)) < (int)sizeof(cparams)) {
		error("Error writing to dumpfile!");
		close(fd);
		close(dumpfd);
		free(buf);
		return (EX_SOFTWARE);
	}

	/*
	 * First loop acquires blocks states and writes them to
	 * the dump file.
	 */
	for (blkidx = 0; blkidx < cparams.blocks; blkidx++) {
		bs.block_num = blkidx;
		err = ioctl(fd, NANDSIM_GET_BLOCK_STATE, &bs);
		if (err) {
			error("Could not get bad block(%d) for "
			    "controller (%d)!", blkidx, ctl);
			close(fd);
			close(dumpfd);
			free(buf);
			return (EX_SOFTWARE);
		}

		bwritten = write(dumpfd, &bs, sizeof(bs));
		if (bwritten != sizeof(bs)) {
			error("Error writing to dumpfile");
			close(fd);
			close(dumpfd);
			free(buf);
			return (EX_SOFTWARE);
		}
	}

	/* Second loop dumps the data */
	for (blkidx = 0; blkidx < cparams.blocks; blkidx++) {
		debug("Block#%d...", blkidx);
		dump.block_num = blkidx;

		err = ioctl(fd, NANDSIM_DUMP, &dump);
		if (err) {
			error("Could not dump ctrl#%d chip#%d "
			    "block#%d", ctl, chip, blkidx);
			err = EX_SOFTWARE;
			break;
		}

		bwritten = write(dumpfd, dump.data, dump.len);
		if (bwritten != dump.len) {
			error("Error writing to dumpfile");
			err = EX_SOFTWARE;
			break;
		}
		debug("OK!\n");
		totalwritten += bwritten;
	}
	printf("%d out of %d B written.\n", totalwritten, dump.len * blkidx);

	close(fd);
	close(dumpfd);
	free(buf);
	return (err);
}