<commit_before>/* Copyright (c) 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 <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int (*ec_command_proto)(int command, int version,
			const void *outdata, int outsize,
			void *indata, int insize);

int (*ec_readmem)(int offset, int bytes, void *dest);

int ec_max_outsize, ec_max_insize;
void *ec_outbuf;
void *ec_inbuf;
static int command_offset;

int comm_init_dev(const char *device_name) __attribute__((weak));
int comm_init_lpc(void) __attribute__((weak));
int comm_init_i2c(void) __attribute__((weak));

static int fake_readmem(int offset, int bytes, void *dest)
{
	struct ec_params_read_memmap p;
	int c;
	char *buf;

	p.offset = offset;

	if (bytes) {
		p.size = bytes;
		c = ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p),
			       dest, p.size);
		if (c < 0)
			return c;
		return p.size;
	}

	p.size = EC_MEMMAP_TEXT_MAX;

	c = ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p), dest, p.size);
	if (c < 0)
		return c;

	buf = dest;
	for (c = 0; c < EC_MEMMAP_TEXT_MAX; c++) {
		if (buf[c] == 0)
			return c;
	}

	buf[EC_MEMMAP_TEXT_MAX - 1] = 0;
	return EC_MEMMAP_TEXT_MAX - 1;
}

void set_command_offset(int offset)
{
	command_offset = offset;
}

int ec_command(int command, int version,
	       const void *outdata, int outsize,
	       void *indata, int insize)
{
	/* Offset command code to support sub-devices */
	return ec_command_proto(command_offset + command, version,
				outdata, outsize,
				indata, insize);
}

int comm_init(int interfaces, const char *device_name)
{
	struct ec_response_get_protocol_info info;

	/* Default memmap access */
	ec_readmem = fake_readmem;

	/* Prefer new /dev method */
	if ((interfaces & COMM_DEV) && comm_init_dev &&
	    !comm_init_dev(device_name))
		goto init_ok;

	/* Fallback to direct LPC on x86 */
	if ((interfaces & COMM_LPC) && comm_init_lpc && !comm_init_lpc())
		goto init_ok;

	/* Fallback to direct i2c on ARM */
	if ((interfaces & COMM_I2C) && comm_init_i2c && !comm_init_i2c())
		goto init_ok;

	/* Give up */
	fprintf(stderr, "Unable to establish host communication\n");
	return 1;

 init_ok:
	/* Allocate shared I/O buffers */
	ec_outbuf = malloc(ec_max_outsize);
	ec_inbuf = malloc(ec_max_insize);
	if (!ec_outbuf || !ec_inbuf) {
		fprintf(stderr, "Unable to allocate buffers\n");
		return 1;
	}

	/* read max request / response size from ec for protocol v3+ */
	if (ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0, &info,
		sizeof(info)) == sizeof(info)) {
		ec_max_outsize = info.max_request_packet_size -
			 sizeof(struct ec_host_request);
		ec_max_insize = info.max_response_packet_size -
			sizeof(struct ec_host_response);

		ec_outbuf = realloc(ec_outbuf, ec_max_outsize);
		ec_inbuf = realloc(ec_inbuf, ec_max_insize);

		if (!ec_outbuf || !ec_inbuf) {
			fprintf(stderr, "Unable to reallocate buffers\n");
			return 1;
		}
	}

	return 0;

}<commit_msg>ectool: Do not increase buffer size after probe max size from ec<commit_after>/* Copyright (c) 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 <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int (*ec_command_proto)(int command, int version,
			const void *outdata, int outsize,
			void *indata, int insize);

int (*ec_readmem)(int offset, int bytes, void *dest);

int ec_max_outsize, ec_max_insize;
void *ec_outbuf;
void *ec_inbuf;
static int command_offset;

int comm_init_dev(const char *device_name) __attribute__((weak));
int comm_init_lpc(void) __attribute__((weak));
int comm_init_i2c(void) __attribute__((weak));

static int fake_readmem(int offset, int bytes, void *dest)
{
	struct ec_params_read_memmap p;
	int c;
	char *buf;

	p.offset = offset;

	if (bytes) {
		p.size = bytes;
		c = ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p),
			       dest, p.size);
		if (c < 0)
			return c;
		return p.size;
	}

	p.size = EC_MEMMAP_TEXT_MAX;

	c = ec_command(EC_CMD_READ_MEMMAP, 0, &p, sizeof(p), dest, p.size);
	if (c < 0)
		return c;

	buf = dest;
	for (c = 0; c < EC_MEMMAP_TEXT_MAX; c++) {
		if (buf[c] == 0)
			return c;
	}

	buf[EC_MEMMAP_TEXT_MAX - 1] = 0;
	return EC_MEMMAP_TEXT_MAX - 1;
}

void set_command_offset(int offset)
{
	command_offset = offset;
}

int ec_command(int command, int version,
	       const void *outdata, int outsize,
	       void *indata, int insize)
{
	/* Offset command code to support sub-devices */
	return ec_command_proto(command_offset + command, version,
				outdata, outsize,
				indata, insize);
}

int comm_init(int interfaces, const char *device_name)
{
	struct ec_response_get_protocol_info info;
	int new_size;

	/* Default memmap access */
	ec_readmem = fake_readmem;

	/* Prefer new /dev method */
	if ((interfaces & COMM_DEV) && comm_init_dev &&
	    !comm_init_dev(device_name))
		goto init_ok;

	/* Fallback to direct LPC on x86 */
	if ((interfaces & COMM_LPC) && comm_init_lpc && !comm_init_lpc())
		goto init_ok;

	/* Fallback to direct i2c on ARM */
	if ((interfaces & COMM_I2C) && comm_init_i2c && !comm_init_i2c())
		goto init_ok;

	/* Give up */
	fprintf(stderr, "Unable to establish host communication\n");
	return 1;

 init_ok:
	/* Allocate shared I/O buffers */
	ec_outbuf = malloc(ec_max_outsize);
	ec_inbuf = malloc(ec_max_insize);
	if (!ec_outbuf || !ec_inbuf) {
		fprintf(stderr, "Unable to allocate buffers\n");
		return 1;
	}

	/*
	 * read max request / response size from ec and reduce buffer size
	 * if the size supported by ec is less than the default value.
	 */
	if (ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0, &info,
		sizeof(info)) == sizeof(info)) {
		new_size = info.max_request_packet_size -
			sizeof(struct ec_host_request);
		if (ec_max_outsize > new_size) {
			ec_max_outsize = new_size;
			ec_outbuf = realloc(ec_outbuf, ec_max_outsize);
		}
		new_size = info.max_response_packet_size -
			sizeof(struct ec_host_response);

		if (ec_max_insize > new_size) {
			ec_max_insize = new_size;
			ec_inbuf = realloc(ec_inbuf, ec_max_insize);
		}

		if (!ec_outbuf || !ec_inbuf) {
			fprintf(stderr, "Unable to reallocate buffers\n");
			return 1;
		}
	}

	return 0;

}
{'fraction_non_alphanumeric': 0.12105263157894737, 'fraction_numerical': 0.008204334365325078, 'mean_word_length': 4.063479623824452, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '5127954', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2437, 'n_words': 658}