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 <sys/stat.h>
17 #include <helper/system.h>
18 
19 static int read_section(FILE *input_file, int length_size, char section,
20  uint32_t *buffer_length, uint8_t **buffer)
21 {
22  uint8_t length_buffer[4];
23  int length;
24  char section_char;
25  int read_count;
26 
27  if ((length_size != 2) && (length_size != 4)) {
28  LOG_ERROR("BUG: length_size neither 2 nor 4");
30  }
31 
32  read_count = fread(&section_char, 1, 1, input_file);
33  if (read_count != 1)
35 
36  if (section_char != section)
38 
39  read_count = fread(length_buffer, 1, length_size, input_file);
40  if (read_count != length_size)
42 
43  if (length_size == 4)
44  length = be_to_h_u32(length_buffer);
45  else /* (length_size == 2) */
46  length = be_to_h_u16(length_buffer);
47 
48  if (buffer_length)
49  *buffer_length = length;
50 
51  *buffer = malloc(length);
52 
53  read_count = fread(*buffer, 1, length, input_file);
54  if (read_count != length)
56 
57  return ERROR_OK;
58 }
59 
60 int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char *filename)
61 {
62  FILE *input_file;
63  struct stat input_stat;
64  int read_count;
65 
66  if (!filename || !bit_file)
68 
69  if (stat(filename, &input_stat) == -1) {
70  LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
72  }
73 
74  if (S_ISDIR(input_stat.st_mode)) {
75  LOG_ERROR("%s is a directory", filename);
77  }
78 
79  if (input_stat.st_size == 0) {
80  LOG_ERROR("Empty file %s", filename);
82  }
83 
84  input_file = fopen(filename, "rb");
85  if (!input_file) {
86  LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));
88  }
89 
90  bit_file->source_file = NULL;
91  bit_file->part_name = NULL;
92  bit_file->date = NULL;
93  bit_file->time = NULL;
94  bit_file->data = NULL;
95 
96  read_count = fread(bit_file->unknown_header, 1, 13, input_file);
97  if (read_count != 13) {
98  LOG_ERROR("couldn't read unknown_header from file '%s'", filename);
99  fclose(input_file);
101  }
102 
103  if (read_section(input_file, 2, 'a', NULL, &bit_file->source_file) != ERROR_OK) {
104  xilinx_free_bit_file(bit_file);
105  fclose(input_file);
107  }
108 
109  if (read_section(input_file, 2, 'b', NULL, &bit_file->part_name) != ERROR_OK) {
110  xilinx_free_bit_file(bit_file);
111  fclose(input_file);
113  }
114 
115  if (read_section(input_file, 2, 'c', NULL, &bit_file->date) != ERROR_OK) {
116  xilinx_free_bit_file(bit_file);
117  fclose(input_file);
119  }
120 
121  if (read_section(input_file, 2, 'd', NULL, &bit_file->time) != ERROR_OK) {
122  xilinx_free_bit_file(bit_file);
123  fclose(input_file);
125  }
126 
127  if (read_section(input_file, 4, 'e', &bit_file->length, &bit_file->data) != ERROR_OK) {
128  xilinx_free_bit_file(bit_file);
129  fclose(input_file);
131  }
132 
133  LOG_DEBUG("bit_file: %s %s %s,%s %" PRIu32 "", bit_file->source_file, bit_file->part_name,
134  bit_file->date, bit_file->time, bit_file->length);
135 
136  fclose(input_file);
137 
138  return ERROR_OK;
139 }
140 
141 void xilinx_free_bit_file(struct xilinx_bit_file *bit_file)
142 {
143  free(bit_file->source_file);
144  free(bit_file->part_name);
145  free(bit_file->date);
146  free(bit_file->time);
147  free(bit_file->data);
148 }
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
uint8_t length
Definition: esp_usb_jtag.c:1
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:39
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:141
int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char *filename)
Definition: xilinx_bit.c:60
static int read_section(FILE *input_file, int length_size, char section, uint32_t *buffer_length, uint8_t **buffer)
Definition: xilinx_bit.c:19