<commit_before>/* Copyright 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "cros_ec_dev.h"
#include "comm-host.h"
#include "ec_commands.h"
#include "misc_util.h"

static int fd = -1;

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(t) (sizeof(t) / sizeof(t[0]))
#endif

static const char *const meanings[] = {
	"SUCCESS",
	"INVALID_COMMAND",
	"ERROR",
	"INVALID_PARAM",
	"ACCESS_DENIED",
	"INVALID_RESPONSE",
	"INVALID_VERSION",
	"INVALID_CHECKSUM",
	"IN_PROGRESS",
	"UNAVAILABLE",
	"TIMEOUT",
	"OVERFLOW",
	"INVALID_HEADER",
	"REQUEST_TRUNCATED",
	"RESPONSE_TOO_BIG",
	"BUS_ERROR",
	"BUSY",
	"INVALID_HEADER_VERSION",
	"INVALID_HEADER_CRC",
	"INVALID_DATA_CRC",
	"DUP_UNAVAILABLE",
};

static const char *strresult(int i)
{
	if (i < 0 || i >= ARRAY_SIZE(meanings))
		return "<unknown>";
	return meanings[i];
}

/* Old ioctl format, used by Chrome OS 3.18 and older */

static int ec_command_dev(int command, int version, const void *outdata,
			  int outsize, void *indata, int insize)
{
	struct cros_ec_command s_cmd;
	int r;

	s_cmd.command = command;
	s_cmd.version = version;
	s_cmd.result = 0xff;
	s_cmd.outsize = outsize;
	s_cmd.outdata = (uint8_t *)outdata;
	s_cmd.insize = insize;
	s_cmd.indata = (uint8_t *)(indata);

	r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
	if (r < 0) {
		fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d (%s)\n",
			r, errno, strerror(errno), s_cmd.result,
			strresult(s_cmd.result));
		if (errno == EAGAIN && s_cmd.result == EC_RES_IN_PROGRESS) {
			s_cmd.command = EC_CMD_RESEND_RESPONSE;
			r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
			if (r < 0) {
				fprintf(stderr,
					"ioctl %d, errno %d (%s), EC result %d (%s)\n",
					r, errno, strerror(errno), s_cmd.result,
					strresult(s_cmd.result));
			}
		}
	}
	if (r >= 0 && s_cmd.result != EC_RES_SUCCESS) {
		fprintf(stderr, "EC result %d (%s)\n", s_cmd.result,
			strresult(s_cmd.result));
		return -EECRESULT - s_cmd.result;
	}

	return r;
}

static int ec_readmem_dev(int offset, int bytes, void *dest)
{
	struct cros_ec_readmem s_mem;
	struct ec_params_read_memmap r_mem;
	int r;
	static int fake_it;

	if (!fake_it) {
		s_mem.offset = offset;
		s_mem.bytes = bytes;
		s_mem.buffer = (char *)(dest);
		r = ioctl(fd, CROS_EC_DEV_IOCRDMEM, &s_mem);
		if (r < 0 && errno == ENOTTY)
			fake_it = 1;
		else
			return r;
	}

	r_mem.offset = offset;
	r_mem.size = bytes;
	return ec_command_dev(EC_CMD_READ_MEMMAP, 0, &r_mem, sizeof(r_mem),
			      dest, bytes);
}

/* New ioctl format, used by Chrome OS 4.4 and later as well as upstream 4.0+ */

static int ec_command_dev_v2(int command, int version, const void *outdata,
			     int outsize, void *indata, int insize)
{
	struct cros_ec_command_v2 *s_cmd;
	int r;

	assert(outsize == 0 || outdata != NULL);
	assert(insize == 0 || indata != NULL);

	s_cmd = (struct cros_ec_command_v2 *)(malloc(
		sizeof(struct cros_ec_command_v2) + MAX(outsize, insize)));
	if (s_cmd == NULL)
		return -EC_RES_ERROR;

	s_cmd->command = command;
	s_cmd->version = version;
	s_cmd->result = 0xff;
	s_cmd->outsize = outsize;
	s_cmd->insize = insize;
	memcpy(s_cmd->data, outdata, outsize);

	r = ioctl(fd, CROS_EC_DEV_IOCXCMD_V2, s_cmd);
	if (r < 0) {
		fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d (%s)\n",
			r, errno, strerror(errno), s_cmd->result,
			strresult(s_cmd->result));
		if (errno == EAGAIN && s_cmd->result == EC_RES_IN_PROGRESS) {
			s_cmd->command = EC_CMD_RESEND_RESPONSE;
			r = ioctl(fd, CROS_EC_DEV_IOCXCMD_V2, &s_cmd);
			if (r < 0) {
				fprintf(stderr,
					"ioctl %d, errno %d (%s), EC result %d (%s)\n",
					r, errno, strerror(errno),
					s_cmd->result,
					strresult(s_cmd->result));
			}
		}
	}
	if (r >= 0) {
		memcpy(indata, s_cmd->data, MIN(r, insize));
		if (s_cmd->result != EC_RES_SUCCESS) {
			fprintf(stderr, "EC result %d (%s)\n", s_cmd->result,
				strresult(s_cmd->result));
			r = -EECRESULT - s_cmd->result;
		}
	}
	free(s_cmd);

	return r;
}
<commit_msg>util/comm-dev.c: Fix passing wrong parameter to ioctl<commit_after>/* Copyright 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "cros_ec_dev.h"
#include "comm-host.h"
#include "ec_commands.h"
#include "misc_util.h"

static int fd = -1;

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(t) (sizeof(t) / sizeof(t[0]))
#endif

static const char *const meanings[] = {
	"SUCCESS",
	"INVALID_COMMAND",
	"ERROR",
	"INVALID_PARAM",
	"ACCESS_DENIED",
	"INVALID_RESPONSE",
	"INVALID_VERSION",
	"INVALID_CHECKSUM",
	"IN_PROGRESS",
	"UNAVAILABLE",
	"TIMEOUT",
	"OVERFLOW",
	"INVALID_HEADER",
	"REQUEST_TRUNCATED",
	"RESPONSE_TOO_BIG",
	"BUS_ERROR",
	"BUSY",
	"INVALID_HEADER_VERSION",
	"INVALID_HEADER_CRC",
	"INVALID_DATA_CRC",
	"DUP_UNAVAILABLE",
};

static const char *strresult(int i)
{
	if (i < 0 || i >= ARRAY_SIZE(meanings))
		return "<unknown>";
	return meanings[i];
}

/* Old ioctl format, used by Chrome OS 3.18 and older */

static int ec_command_dev(int command, int version, const void *outdata,
			  int outsize, void *indata, int insize)
{
	struct cros_ec_command s_cmd;
	int r;

	s_cmd.command = command;
	s_cmd.version = version;
	s_cmd.result = 0xff;
	s_cmd.outsize = outsize;
	s_cmd.outdata = (uint8_t *)outdata;
	s_cmd.insize = insize;
	s_cmd.indata = (uint8_t *)(indata);

	r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
	if (r < 0) {
		fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d (%s)\n",
			r, errno, strerror(errno), s_cmd.result,
			strresult(s_cmd.result));
		if (errno == EAGAIN && s_cmd.result == EC_RES_IN_PROGRESS) {
			s_cmd.command = EC_CMD_RESEND_RESPONSE;
			r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
			if (r < 0) {
				fprintf(stderr,
					"ioctl %d, errno %d (%s), EC result %d (%s)\n",
					r, errno, strerror(errno), s_cmd.result,
					strresult(s_cmd.result));
			}
		}
	}
	if (r >= 0 && s_cmd.result != EC_RES_SUCCESS) {
		fprintf(stderr, "EC result %d (%s)\n", s_cmd.result,
			strresult(s_cmd.result));
		return -EECRESULT - s_cmd.result;
	}

	return r;
}

static int ec_readmem_dev(int offset, int bytes, void *dest)
{
	struct cros_ec_readmem s_mem;
	struct ec_params_read_memmap r_mem;
	int r;
	static int fake_it;

	if (!fake_it) {
		s_mem.offset = offset;
		s_mem.bytes = bytes;
		s_mem.buffer = (char *)(dest);
		r = ioctl(fd, CROS_EC_DEV_IOCRDMEM, &s_mem);
		if (r < 0 && errno == ENOTTY)
			fake_it = 1;
		else
			return r;
	}

	r_mem.offset = offset;
	r_mem.size = bytes;
	return ec_command_dev(EC_CMD_READ_MEMMAP, 0, &r_mem, sizeof(r_mem),
			      dest, bytes);
}

/* New ioctl format, used by Chrome OS 4.4 and later as well as upstream 4.0+ */

static int ec_command_dev_v2(int command, int version, const void *outdata,
			     int outsize, void *indata, int insize)
{
	struct cros_ec_command_v2 *s_cmd;
	int r;

	assert(outsize == 0 || outdata != NULL);
	assert(insize == 0 || indata != NULL);

	s_cmd = (struct cros_ec_command_v2 *)(malloc(
		sizeof(struct cros_ec_command_v2) + MAX(outsize, insize)));
	if (s_cmd == NULL)
		return -EC_RES_ERROR;

	s_cmd->command = command;
	s_cmd->version = version;
	s_cmd->result = 0xff;
	s_cmd->outsize = outsize;
	s_cmd->insize = insize;
	memcpy(s_cmd->data, outdata, outsize);

	r = ioctl(fd, CROS_EC_DEV_IOCXCMD_V2, s_cmd);
	if (r < 0) {
		fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d (%s)\n",
			r, errno, strerror(errno), s_cmd->result,
			strresult(s_cmd->result));
		if (errno == EAGAIN && s_cmd->result == EC_RES_IN_PROGRESS) {
			s_cmd->command = EC_CMD_RESEND_RESPONSE;
			r = ioctl(fd, CROS_EC_DEV_IOCXCMD_V2, s_cmd);
			if (r < 0) {
				fprintf(stderr,
					"ioctl %d, errno %d (%s), EC result %d (%s)\n",
					r, errno, strerror(errno),
					s_cmd->result,
					strresult(s_cmd->result));
			}
		}
	}
	if (r >= 0) {
		memcpy(indata, s_cmd->data, MIN(r, insize));
		if (s_cmd->result != EC_RES_SUCCESS) {
			fprintf(stderr, "EC result %d (%s)\n", s_cmd->result,
				strresult(s_cmd->result));
			r = -EECRESULT - s_cmd->result;
		}
	}
	free(s_cmd);

	return r;
}

{'fraction_non_alphanumeric': 0.15636953749855106, 'fraction_numerical': 0.008114060507708357, 'mean_word_length': 4.0162790697674415, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2682133', 'n_tokens_mistral': 3918, 'n_tokens_neox': 3613, 'n_words': 850}