<commit_before>/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2020 Intel Corporation
 */

#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "ifpga_sec_mgr.h"

static struct ifpga_sec_mgr *sec_mgr;

static void set_rsu_control(struct ifpga_sec_mgr *smgr, uint32_t ctrl)
{
	if (smgr && smgr->rsu_control)
		*smgr->rsu_control = ctrl;
}

static uint32_t get_rsu_control(struct ifpga_sec_mgr *smgr)
{
	if (smgr && smgr->rsu_control)
		return *smgr->rsu_control;
	return 0;
}

static void set_rsu_status(struct ifpga_sec_mgr *smgr, uint32_t status,
	uint32_t progress)
{
	if (smgr && smgr->rsu_status)
		*smgr->rsu_status = IFPGA_RSU_STATUS(status, progress);
}

static void get_rsu_status(struct ifpga_sec_mgr *smgr, uint32_t *status,
	uint32_t *progress)
{
	if (smgr && smgr->rsu_status) {
		if (status)
			*status = IFPGA_RSU_GET_STAT(*smgr->rsu_status);
		if (progress)
			*progress = IFPGA_RSU_GET_PROG(*smgr->rsu_status);
	}
}

static void sig_handler(int sig, siginfo_t *info, void *data)
{
	(void)(info);
	(void)(data);

	switch (sig) {
	case SIGINT:
		if (sec_mgr) {
			dev_info(sec_mgr, "Interrupt secure flash update"
				" by keyboard\n");
			set_rsu_control(sec_mgr, IFPGA_RSU_ABORT);
		}
		break;
	default:
		break;
	}
}

static void log_time(time_t t, const char *msg)
{
	uint32_t h = 0;
	uint32_t m = 0;
	uint32_t s = 0;

	if (t < 60) {
		s = (uint32_t)t;
	} else if (t < 3600) {
		s = (uint32_t)(t % 60);
		m = (uint32_t)(t / 60);
	} else {
		s = (uint32_t)(t % 60);
		m = (uint32_t)((t % 3600) / 60);
		h = (uint32_t)(t / 3600);
	}
	printf("%s - %02u:%02u:%02u\n", msg, h, m, s);
}

static int start_flash_update(struct ifpga_sec_mgr *smgr)
{
	if (!smgr)
		return -ENODEV;

	if (!smgr->ops || !smgr->ops->prepare)
		return -EINVAL;

	return smgr->ops->prepare(smgr);
}

static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,
	uint32_t offset)
{
	void *buf = NULL;
	int retry = 0;
	uint32_t length = 0;
	uint32_t to_transfer = 0;
	uint32_t one_percent = 0;
	uint32_t prog = 0;
	uint32_t old_prog = -1;
	ssize_t read_size = 0;
	int fd = -1;
	int ret = 0;

	if (!smgr)
		return -ENODEV;

	if (!smgr->ops || !smgr->ops->write_blk)
		return -EINVAL;

	fd = open(image, O_RDONLY);
	if (fd < 0) {
		dev_err(smgr,
			"Failed to open \'%s\' for RD [e:%s]\n",
			image, strerror(errno));
		return -EIO;
	}

	buf = malloc(IFPGA_RSU_DATA_BLK_SIZE);
	if (!buf) {
		dev_err(smgr, "Failed to allocate memory for flash update\n");
		close(fd);
		return -ENOMEM;
	}

	length = smgr->rsu_length;
	one_percent = length / 100;
	do {
		to_transfer = (length > IFPGA_RSU_DATA_BLK_SIZE) ?
			IFPGA_RSU_DATA_BLK_SIZE : length;
		lseek(fd, offset, SEEK_SET);
		read_size = read(fd, buf, to_transfer);
		if (read_size < 0) {
			dev_err(smgr, "Failed to read from \'%s\' [e:%s]\n",
				image, strerror(errno));
			ret = -EIO;
			goto end;
		}
		if ((uint32_t)read_size != to_transfer) {
			dev_err(smgr,
				"Read length %zd is not expected [e:%u]\n",
				read_size, to_transfer);
			ret = -EIO;
			goto end;
		}

		retry = 0;
		do {
			if (get_rsu_control(smgr) == IFPGA_RSU_ABORT) {
				ret = -EAGAIN;
				goto end;
			}
			ret = smgr->ops->write_blk(smgr, buf, offset,
				to_transfer);
			if (ret == 0)
				break;
			sleep(1);
		} while (++retry <= IFPGA_RSU_WRITE_RETRY);<commit_msg>raw/ifpga/base: check lseek failure<commit_after>/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2020 Intel Corporation
 */

#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "ifpga_sec_mgr.h"

static struct ifpga_sec_mgr *sec_mgr;

static void set_rsu_control(struct ifpga_sec_mgr *smgr, uint32_t ctrl)
{
	if (smgr && smgr->rsu_control)
		*smgr->rsu_control = ctrl;
}

static uint32_t get_rsu_control(struct ifpga_sec_mgr *smgr)
{
	if (smgr && smgr->rsu_control)
		return *smgr->rsu_control;
	return 0;
}

static void set_rsu_status(struct ifpga_sec_mgr *smgr, uint32_t status,
	uint32_t progress)
{
	if (smgr && smgr->rsu_status)
		*smgr->rsu_status = IFPGA_RSU_STATUS(status, progress);
}

static void get_rsu_status(struct ifpga_sec_mgr *smgr, uint32_t *status,
	uint32_t *progress)
{
	if (smgr && smgr->rsu_status) {
		if (status)
			*status = IFPGA_RSU_GET_STAT(*smgr->rsu_status);
		if (progress)
			*progress = IFPGA_RSU_GET_PROG(*smgr->rsu_status);
	}
}

static void sig_handler(int sig, siginfo_t *info, void *data)
{
	(void)(info);
	(void)(data);

	switch (sig) {
	case SIGINT:
		if (sec_mgr) {
			dev_info(sec_mgr, "Interrupt secure flash update"
				" by keyboard\n");
			set_rsu_control(sec_mgr, IFPGA_RSU_ABORT);
		}
		break;
	default:
		break;
	}
}

static void log_time(time_t t, const char *msg)
{
	uint32_t h = 0;
	uint32_t m = 0;
	uint32_t s = 0;

	if (t < 60) {
		s = (uint32_t)t;
	} else if (t < 3600) {
		s = (uint32_t)(t % 60);
		m = (uint32_t)(t / 60);
	} else {
		s = (uint32_t)(t % 60);
		m = (uint32_t)((t % 3600) / 60);
		h = (uint32_t)(t / 3600);
	}
	printf("%s - %02u:%02u:%02u\n", msg, h, m, s);
}

static int start_flash_update(struct ifpga_sec_mgr *smgr)
{
	if (!smgr)
		return -ENODEV;

	if (!smgr->ops || !smgr->ops->prepare)
		return -EINVAL;

	return smgr->ops->prepare(smgr);
}

static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,
	uint32_t offset)
{
	void *buf = NULL;
	int retry = 0;
	uint32_t length = 0;
	uint32_t to_transfer = 0;
	uint32_t one_percent = 0;
	uint32_t prog = 0;
	uint32_t old_prog = -1;
	ssize_t read_size = 0;
	int fd = -1;
	int ret = 0;

	if (!smgr)
		return -ENODEV;

	if (!smgr->ops || !smgr->ops->write_blk)
		return -EINVAL;

	fd = open(image, O_RDONLY);
	if (fd < 0) {
		dev_err(smgr,
			"Failed to open \'%s\' for RD [e:%s]\n",
			image, strerror(errno));
		return -EIO;
	}

	buf = malloc(IFPGA_RSU_DATA_BLK_SIZE);
	if (!buf) {
		dev_err(smgr, "Failed to allocate memory for flash update\n");
		close(fd);
		return -ENOMEM;
	}

	length = smgr->rsu_length;
	one_percent = length / 100;
	do {
		to_transfer = (length > IFPGA_RSU_DATA_BLK_SIZE) ?
			IFPGA_RSU_DATA_BLK_SIZE : length;
		if (lseek(fd, offset, SEEK_SET) < 0) {
			dev_err(smgr, "Failed to seek in \'%s\' [e:%s]\n",
				image, strerror(errno));
			ret = -EIO;
			goto end;
		}
		read_size = read(fd, buf, to_transfer);
		if (read_size < 0) {
			dev_err(smgr, "Failed to read from \'%s\' [e:%s]\n",
				image, strerror(errno));
			ret = -EIO;
			goto end;
		}
		if ((uint32_t)read_size != to_transfer) {
			dev_err(smgr,
				"Read length %zd is not expected [e:%u]\n",
				read_size, to_transfer);
			ret = -EIO;
			goto end;
		}

		retry = 0;
		do {
			if (get_rsu_control(smgr) == IFPGA_RSU_ABORT) {
				ret = -EAGAIN;
				goto end;
			}
			ret = smgr->ops->write_blk(smgr, buf, offset,
				to_transfer);
			if (ret == 0)
				break;
			sleep(1);
		} while (++retry <= IFPGA_RSU_WRITE_RETRY);
{'fraction_non_alphanumeric': 0.15354040924481083, 'fraction_numerical': 0.029000441631090827, 'mean_word_length': 3.666208791208791, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 51, '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': '3816804', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3035, 'n_words': 669}