OpenOCD
xilinx_bit.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "xilinx_bit.h"
13 #include "pld.h"
14 #include <helper/log.h>
15 
16 #include <helper/system.h>
17 
18 static int read_section(FILE *input_file, int length_size, char section,
19  uint32_t *buffer_length, uint8_t **buffer)
20 {
21  uint8_t length_buffer[4];
22  int length;
23  char section_char;
24  int read_count;
25 
26  if ((length_size != 2) && (length_size != 4)) {
27  LOG_ERROR("BUG: length_size neither 2 nor 4");
29  }
30 
31  read_count = fread(&section_char, 1, 1, input_file);
32  if (read_count != 1)
34 
35  if (section_char != section)
37 
38  read_count = fread(length_buffer, 1, length_size, input_file);
39  if (read_count != length_size)
41 
42  if (length_size == 4)
43  length = be_to_h_u32(length_buffer);
44  else /* (length_size == 2) */
45  length = be_to_h_u16(length_buffer);
46 
47  if (buffer_length)
48  *buffer_length = length;
49 
50  *buffer = malloc(length);
51 
52  read_count = fread(*buffer, 1, length, input_file);
53  if (read_count != length)
55 
56  return ERROR_OK;
57 }
58 
59 int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char *filename)
60 {
61  FILE *input_file;
62  int read_count;
63 
64  if (!filename || !bit_file)
66 
67  input_file = fopen(filename, "rb");
68  if (!input_file) {
69  LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));
71  }
72 
73  bit_file->source_file = NULL;
74  bit_file->part_name = NULL;
75  bit_file->date = NULL;
76  bit_file->time = NULL;
77  bit_file->data = NULL;
78 
79  read_count = fread(bit_file->unknown_header, 1, 13, input_file);
80  if (read_count != 13) {
81  LOG_ERROR("couldn't read unknown_header from file '%s'", filename);
82  fclose(input_file);
84  }
85 
86  if (read_section(input_file, 2, 'a', NULL, &bit_file->source_file) != ERROR_OK) {
87  xilinx_free_bit_file(bit_file);
88  fclose(input_file);
90  }
91 
92  if (read_section(input_file, 2, 'b', NULL, &bit_file->part_name) != ERROR_OK) {
93  xilinx_free_bit_file(bit_file);
94  fclose(input_file);
96  }
97 
98  if (read_section(input_file, 2, 'c', NULL, &bit_file->date) != ERROR_OK) {
99  xilinx_free_bit_file(bit_file);
100  fclose(input_file);
102  }
103 
104  if (read_section(input_file, 2, 'd', NULL, &bit_file->time) != ERROR_OK) {
105  xilinx_free_bit_file(bit_file);
106  fclose(input_file);
108  }
109 
110  if (read_section(input_file, 4, 'e', &bit_file->length, &bit_file->data) != ERROR_OK) {
111  xilinx_free_bit_file(bit_file);
112  fclose(input_file);
114  }
115 
116  LOG_DEBUG("bit_file: %s %s %s,%s %" PRIu32 "", bit_file->source_file, bit_file->part_name,
117  bit_file->date, bit_file->time, bit_file->length);
118 
119  fclose(input_file);
120 
121  return ERROR_OK;
122 }
123 
124 void xilinx_free_bit_file(struct xilinx_bit_file *bit_file)
125 {
126  free(bit_file->source_file);
127  free(bit_file->part_name);
128  free(bit_file->date);
129  free(bit_file->time);
130  free(bit_file->data);
131 }
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
uint8_t length
Definition: esp_usb_jtag.c:1
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:61
uint8_t * part_name
Definition: xilinx_bit.h:16
uint32_t length
Definition: xilinx_bit.h:19
uint8_t * source_file
Definition: xilinx_bit.h:15
uint8_t unknown_header[13]
Definition: xilinx_bit.h:14
uint8_t * date
Definition: xilinx_bit.h:17
uint8_t * time
Definition: xilinx_bit.h:18
uint8_t * data
Definition: xilinx_bit.h:20
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
#define NULL
Definition: usb.h:16
void xilinx_free_bit_file(struct xilinx_bit_file *bit_file)
Definition: xilinx_bit.c:124
int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char *filename)
Definition: xilinx_bit.c:59
static int read_section(FILE *input_file, int length_size, char section, uint32_t *buffer_length, uint8_t **buffer)
Definition: xilinx_bit.c:18