aboutsummaryrefslogtreecommitdiff
path: root/src/pld/raw_bit.c
blob: 0c3b92e7e960886365ad7252f7bca6c2e15539f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: GPL-2.0-or-later

/***************************************************************************
 *   Copyright (C) 2022 by Daniel Anselmi                                  *
 *   danselmi@gmx.ch                                                       *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "raw_bit.h"
#include "pld.h"

#include <helper/system.h>
#include <helper/log.h>


int cpld_read_raw_bit_file(struct raw_bit_file *bit_file, const char *filename)
{
	FILE *input_file = fopen(filename, "rb");

	if (!input_file) {
		LOG_ERROR("Couldn't open %s: %s", filename, strerror(errno));
		return ERROR_PLD_FILE_LOAD_FAILED;
	}

	fseek(input_file, 0, SEEK_END);
	long length = ftell(input_file);
	fseek(input_file, 0, SEEK_SET);

	if (length < 0) {
		fclose(input_file);
		LOG_ERROR("Failed to get length of file %s: %s", filename, strerror(errno));
		return ERROR_PLD_FILE_LOAD_FAILED;
	}
	bit_file->length = (size_t)length;

	bit_file->data = malloc(bit_file->length);
	if (!bit_file->data) {
		fclose(input_file);
		LOG_ERROR("Out of memory");
		return ERROR_PLD_FILE_LOAD_FAILED;
	}

	size_t read_count = fread(bit_file->data, sizeof(char), bit_file->length, input_file);
	fclose(input_file);
	if (read_count != bit_file->length) {
		free(bit_file->data);
		bit_file->data = NULL;
		return ERROR_PLD_FILE_LOAD_FAILED;
	}

	return ERROR_OK;
}