jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / pld / raw_bit.c
blob0c3b92e7e960886365ad7252f7bca6c2e15539f4
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2022 by Daniel Anselmi *
5 * danselmi@gmx.ch *
6 ***************************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
12 #include "raw_bit.h"
13 #include "pld.h"
15 #include <helper/system.h>
16 #include <helper/log.h>
19 int cpld_read_raw_bit_file(struct raw_bit_file *bit_file, const char *filename)
21 FILE *input_file = fopen(filename, "rb");
23 if (!input_file) {
24 LOG_ERROR("Couldn't open %s: %s", filename, strerror(errno));
25 return ERROR_PLD_FILE_LOAD_FAILED;
28 fseek(input_file, 0, SEEK_END);
29 long length = ftell(input_file);
30 fseek(input_file, 0, SEEK_SET);
32 if (length < 0) {
33 fclose(input_file);
34 LOG_ERROR("Failed to get length of file %s: %s", filename, strerror(errno));
35 return ERROR_PLD_FILE_LOAD_FAILED;
37 bit_file->length = (size_t)length;
39 bit_file->data = malloc(bit_file->length);
40 if (!bit_file->data) {
41 fclose(input_file);
42 LOG_ERROR("Out of memory");
43 return ERROR_PLD_FILE_LOAD_FAILED;
46 size_t read_count = fread(bit_file->data, sizeof(char), bit_file->length, input_file);
47 fclose(input_file);
48 if (read_count != bit_file->length) {
49 free(bit_file->data);
50 bit_file->data = NULL;
51 return ERROR_PLD_FILE_LOAD_FAILED;
54 return ERROR_OK;