Пример #1
0
int
main(int argc, char *argv[]) 
{
	int optind = initialize(argc, argv, SPECTRAL_ANALYSIS_GROUP, VERSION, "<sound files>");
	char *output_directory = param_get_string("call", "output_directory");
	char *prefix = param_sprintf("call", "pathname_prefix", output_directory);
	g_free(output_directory);
	FILE *index_file = NULL;
	char *index_filename = param_sprintf("call", "index_filename", prefix);
	g_free(prefix);
	if (index_filename) {
		index_file = fopen(index_filename, "w");
		g_free(index_filename);
		assert(index_file);
		setbuf(index_file, NULL);
		fprintf(index_file, "<html>\n");
	}
	int call_count = 0;
	for (int i = optind; i < argc; i++)
		extract_calls_file(argv[i], index_file, &call_count);
	if (index_file) {
		fprintf(index_file, "</html>\n");
		fclose(index_file);
	}
	return 0;
}
Пример #2
0
int
usage(struct cmd_param *params)
{
	int i;

	if (!params || !param_get_count(params)) {
		fprintf(stderr, "Usage: nandtool <command> [arguments...]\n");
		fprintf(stderr, "Arguments are in form 'name=value'.\n\n");
		fprintf(stderr, "Available commands:\n");

		for (i = 0; commands[i].name != NULL; i++)
			fprintf(stderr, "\t%s\n", commands[i].name);

		fprintf(stderr, "\n");
		fprintf(stderr, "For information about particular command, "
		    "type:\n");
		fprintf(stderr, "'nandtool help topic=<command>'\n");
	} else if (param_has_value(params, "topic")) {
		for (i = 0; commands[i].name != NULL; i++) {
			if (!strcmp(param_get_string(params, "topic"),
			    commands[i].name)) {
				fprintf(stderr, commands[i].usage, "nandtool");
				return (0);
			}
		}

		fprintf(stderr, "No such command\n");
		return (EX_SOFTWARE);
	} else {
		fprintf(stderr, "Wrong arguments given. Try: 'nandtool help'\n");
	}

	return (EX_USAGE);
}
Пример #3
0
int
param_get_boolean(struct cmd_param *params, const char *name)
{
	char *str = param_get_string(params, name);

	if (!str)
		return (0);

	if (!strcmp(str, "true") || !strcmp(str, "yes"))
		return (1);

	return (0);
}
Пример #4
0
int nand_info(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	int fd = -1, ret = 0;
	int block_size;
	off_t chip_size, media_size;
	const char *dev;

	if ((dev = param_get_string(params, "dev")) == NULL) {
		fprintf(stderr, "Please supply 'dev' parameter, eg. "
		    "'dev=/dev/gnand0'\n");
		return (1);
	}

	if ((fd = g_open(dev, 1)) == -1) {
		perrorf("Cannot open %s", dev);
		return (1);
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	if (ioctl(fd, DIOCGMEDIASIZE, &media_size) == -1) {
		perrorf("Cannot ioctl(DIOCGMEDIASIZE)");
		ret = 1;
		goto out;
	}

	block_size = chip_params.page_size * chip_params.pages_per_block;
	chip_size = block_size * chip_params.blocks;

	printf("Device:\t\t\t%s\n", dev);
	printf("Page size:\t\t%d bytes\n", chip_params.page_size);
	printf("Block size:\t\t%d bytes (%d KB)\n", block_size,
	    block_size / 1024);
	printf("OOB size per page:\t%d bytes\n", chip_params.oob_size);
	printf("Chip size:\t\t%jd MB\n", (uintmax_t)(chip_size / 1024 / 1024));
	printf("Slice size:\t\t%jd MB\n",
	    (uintmax_t)(media_size / 1024 / 1024));

out:
	g_close(fd);

	return (ret);
}
Пример #5
0
int nand_write_oob(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	struct nand_oob_rw req;
	char *dev, *in;
	int fd = -1, fd_in = -1, ret = 0;
	uint8_t *buf = NULL;
	int page;

	if (!(dev = param_get_string(params, "dev"))) {
		fprintf(stderr, "Please supply valid 'dev' parameter.\n");
		return (1);
	}

	if (!(in = param_get_string(params, "in"))) {
		fprintf(stderr, "Please supply valid 'in' parameter.\n");
		return (1);
	}

	if ((page = param_get_int(params, "page")) < 0) {
		fprintf(stderr, "Please supply valid 'page' parameter.\n");
		return (1);
	}

	if ((fd = g_open(dev, 1)) == -1) {
		perrorf("Cannot open %s", dev);
		return (1);
	}

	if ((fd_in = open(in, O_RDONLY)) == -1) {
		perrorf("Cannot open %s", in);
		ret = 1;
		goto out;
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	buf = malloc(chip_params.oob_size);
	if (buf == NULL) {
		perrorf("Cannot allocate %d bytes\n", chip_params.oob_size);
		ret = 1;
		goto out;
	}

	if (read(fd_in, buf, chip_params.oob_size) == -1) {
		perrorf("Cannot read from %s", in);
		ret = 1;
		goto out;
	}

	req.page = page;
	req.len = chip_params.oob_size;
	req.data = buf;

	if (ioctl(fd, NAND_IO_OOB_PROG, &req) == -1) {
		perrorf("Cannot write OOB to %s", dev);
		ret = 1;
		goto out;
	}

out:
	g_close(fd);
	if (fd_in != -1)
		close(fd_in);
	if (buf)
		free(buf);

	return (ret);
}
Пример #6
0
int nand_write(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	char *dev, *file;
	int in_fd = -1, ret = 0, done = 0;
	int fd, block_size, mult, pos, count;
	uint8_t *buf = NULL;

	if (!(dev = param_get_string(params, "dev"))) {
		fprintf(stderr, "Please supply 'dev' argument.\n");
		return (1);
	}

	if (!(file = param_get_string(params, "in"))) {
		fprintf(stderr, "Please supply 'in' argument.\n");
		return (1);
	}

	if ((fd = g_open(dev, 1)) == -1) {
		perrorf("Cannot open %s", dev);
		return (1);
	}

	if ((in_fd = open(file, O_RDONLY)) == -1) {
		perrorf("Cannot open file %s", file);
		ret = 1;
		goto out;
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	block_size = chip_params.page_size * chip_params.pages_per_block;

	if (param_has_value(params, "page")) {
		pos = chip_params.page_size * param_get_int(params, "page");
		mult = chip_params.page_size;
	} else if (param_has_value(params, "block")) {
		pos = block_size * param_get_int(params, "block");
		mult = block_size;
	} else if (param_has_value(params, "pos")) {
		pos = param_get_int(params, "pos");
		mult = 1;
		if (pos % chip_params.page_size) {
			fprintf(stderr, "Position must be page-size "
			    "aligned!\n");
			ret = 1;
			goto out;
		}
	} else {
		fprintf(stderr, "You must specify one of: 'block', 'page',"
		    "'pos' arguments\n");
		ret = 1;
		goto out;
	}

	if (!(param_has_value(params, "count")))
		count = mult;
	else
		count = param_get_int(params, "count") * mult;

	if (!(buf = malloc(chip_params.page_size))) {
		perrorf("Cannot allocate buffer [size %x]",
		    chip_params.page_size);
		ret = 1;
		goto out;
	}

	lseek(fd, pos, SEEK_SET);

	while (done < count) {
		if ((ret = read(in_fd, buf, chip_params.page_size)) !=
		    (int32_t)chip_params.page_size) {
			if (ret > 0) {
				/* End of file ahead, truncate here */
				break;
			} else {
				perrorf("Cannot read from %s", file);
				ret = 1;
				goto out;
			}
		}

		if ((ret = write(fd, buf, chip_params.page_size)) !=
		    (int32_t)chip_params.page_size) {
			ret = 1;
			goto out;
		}

		done += ret;
	}

out:
	g_close(fd);
	if (in_fd != -1)
		close(in_fd);
	if (buf)
		free(buf);

	return (ret);
}
Пример #7
0
int nand_read_oob(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	struct nand_oob_rw req;
	char *dev, *out;
	int fd = -1, fd_out = -1, ret = 0;
	int page;
	uint8_t *buf = NULL;

	if ((page = param_get_int(params, "page")) < 0) {
		fprintf(stderr, "You must supply valid 'page' argument.\n");
		return (1);
	}

	if (!(dev = param_get_string(params, "dev"))) {
		fprintf(stderr, "You must supply 'dev' argument.\n");
		return (1);
	}

	if ((out = param_get_string(params, "out"))) {
		if ((fd_out = open(out, O_WRONLY | O_CREAT)) == -1) {
			perrorf("Cannot open %s", out);
			ret = 1;
			goto out;
		}
	}

	if ((fd = g_open(dev, 1)) == -1) {
		perrorf("Cannot open %s", dev);
		ret = 1;
		goto out;
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	buf = malloc(chip_params.oob_size);
	if (buf == NULL) {
		perrorf("Cannot allocate %d bytes\n", chip_params.oob_size);
		ret = 1;
		goto out;
	}

	req.page = page;
	req.len = chip_params.oob_size;
	req.data = buf;

	if (ioctl(fd, NAND_IO_OOB_READ, &req) == -1) {
		perrorf("Cannot read OOB from %s", dev);
		ret = 1;
		goto out;
	}

	if (fd_out != -1)
		write(fd_out, buf, chip_params.oob_size);
	else
		hexdump(buf, chip_params.oob_size);

out:
	close(fd_out);

	if (fd != -1)
		g_close(fd);
	if (buf)
		free(buf);

	return (ret);
}
Пример #8
0
int nand_read(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	int fd = -1, out_fd = -1, done = 0, ret = 0;
	char *dev, *out;
	int pos, count, mult, block_size;
	uint8_t *buf = NULL;

	if (!(dev = param_get_string(params, "dev"))) {
		fprintf(stderr, "You must specify 'dev' parameter\n");
		return (1);
	}

	if ((out = param_get_string(params, "out"))) {
		out_fd = open(out, O_WRONLY|O_CREAT);
		if (out_fd == -1) {
			perrorf("Cannot open %s for writing", out);
			return (1);
		}
	}

	if ((fd = g_open(dev, 1)) == -1) {
		perrorf("Cannot open %s", dev);
		ret = 1;
		goto out;
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	block_size = chip_params.page_size * chip_params.pages_per_block;

	if (param_has_value(params, "page")) {
		pos = chip_params.page_size * param_get_int(params, "page");
		mult = chip_params.page_size;
	} else if (param_has_value(params, "block")) {
		pos = block_size * param_get_int(params, "block");
		mult = block_size;
	} else if (param_has_value(params, "pos")) {
		pos = param_get_int(params, "pos");
		mult = 1;
		if (pos % chip_params.page_size) {
			fprintf(stderr, "Position must be page-size aligned!\n");
			ret = 1;
			goto out;
		}
	} else {
		fprintf(stderr, "You must specify one of: 'block', 'page',"
		    "'pos' arguments\n");
		ret = 1;
		goto out;
	}

	if (!(param_has_value(params, "count")))
		count = mult;
	else
		count = param_get_int(params, "count") * mult;

	if (!(buf = malloc(chip_params.page_size))) {
		perrorf("Cannot allocate buffer [size %x]",
		    chip_params.page_size);
		ret = 1;
		goto out;
	}

	lseek(fd, pos, SEEK_SET);

	while (done < count) {
		if ((ret = read(fd, buf, chip_params.page_size)) !=
		    (int32_t)chip_params.page_size) {
			perrorf("read error (read %d bytes)", ret);
			goto out;
		}

		if (out_fd != -1) {
			done += ret;
			if ((ret = write(out_fd, buf, chip_params.page_size)) !=
			    (int32_t)chip_params.page_size) {
				perrorf("write error (written %d bytes)", ret);
				ret = 1;
				goto out;
			}
		} else {
			hexdumpoffset(buf, chip_params.page_size, done);
			done += ret;
		}
	}

out:
	g_close(fd);
	if (out_fd != -1)
		close(out_fd);
	if (buf)
		free(buf);

	return (ret);
}
Пример #9
0
int nand_erase(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	char *dev;
	int fd = -1, ret = 0;
	off_t pos, count;
	off_t start, nblocks, i;
	int block_size, mult;

	if (!(dev = param_get_string(params, "dev"))) {
		fprintf(stderr, "Please supply valid 'dev' parameter.\n");
		return (1);
	}

	if (param_has_value(params, "count"))
		count = param_get_intx(params, "count");
	else
		count = 1;

	if ((fd = g_open(dev, 1)) < 0) {
		perrorf("Cannot open %s", dev);
		return (1);
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	block_size = chip_params.page_size * chip_params.pages_per_block;

	if (param_has_value(params, "page")) {
		pos = chip_params.page_size * param_get_intx(params, "page");
		mult = chip_params.page_size;
	} else if (param_has_value(params, "block")) {
		pos = block_size * param_get_intx(params, "block");
		mult = block_size;
	} else if (param_has_value(params, "pos")) {
		pos = param_get_intx(params, "pos");
		mult = 1;
	} else {
		/* Erase whole chip */
		if (ioctl(fd, DIOCGMEDIASIZE, &count) == -1) {
			ret = 1;
			goto out;
		}

		pos = 0;
		mult = 1;
	}

	if (pos % block_size) {
		fprintf(stderr, "Position must be block-size aligned!\n");
		ret = 1;
		goto out;
	}

	count *= mult;
	start = pos / block_size;
	nblocks = count / block_size;

	for (i = 0; i < nblocks; i++) {
		if (g_delete(fd, (start + i) * block_size, block_size) == -1) {
			perrorf("Cannot erase block %d - probably a bad block",
			    start + i);
			ret = 1;
		}
	}

out:
	g_close(fd);

	return (ret);
}