OpenOCD
raw_bit.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2022 by Daniel Anselmi *
5  * danselmi@gmx.ch *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "raw_bit.h"
13 #include "pld.h"
14 
15 #include <helper/system.h>
16 #include <helper/log.h>
17 
18 
19 int cpld_read_raw_bit_file(struct raw_bit_file *bit_file, const char *filename)
20 {
21  FILE *input_file = fopen(filename, "rb");
22 
23  if (!input_file) {
24  LOG_ERROR("Couldn't open %s: %s", filename, strerror(errno));
26  }
27 
28  fseek(input_file, 0, SEEK_END);
29  long length = ftell(input_file);
30  fseek(input_file, 0, SEEK_SET);
31 
32  if (length < 0) {
33  fclose(input_file);
34  LOG_ERROR("Failed to get length of file %s: %s", filename, strerror(errno));
36  }
37  bit_file->length = (size_t)length;
38 
39  bit_file->data = malloc(bit_file->length);
40  if (!bit_file->data) {
41  fclose(input_file);
42  LOG_ERROR("Out of memory");
44  }
45 
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;
52  }
53 
54  return ERROR_OK;
55 }
uint8_t length
Definition: esp_usb_jtag.c:1
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define ERROR_OK
Definition: log.h:164
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:62
int cpld_read_raw_bit_file(struct raw_bit_file *bit_file, const char *filename)
Definition: raw_bit.c:19
uint8_t * data
Definition: raw_bit.h:16
size_t length
Definition: raw_bit.h:15
#define NULL
Definition: usb.h:16