fix leaky file-handle in virtex2 driver
[openocd.git] / src / pld / xilinx_bit.c
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");
29 return ERROR_PLD_FILE_LOAD_FAILED;
30 }
31
32 read_count = fread(&section_char, 1, 1, input_file);
33 if (read_count != 1)
34 return ERROR_PLD_FILE_LOAD_FAILED;
35
36 if (section_char != section)
37 return ERROR_PLD_FILE_LOAD_FAILED;
38
39 read_count = fread(length_buffer, 1, length_size, input_file);
40 if (read_count != length_size)
41 return ERROR_PLD_FILE_LOAD_FAILED;
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)
55 return ERROR_PLD_FILE_LOAD_FAILED;
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)
67 return ERROR_COMMAND_SYNTAX_ERROR;
68
69 if (stat(filename, &input_stat) == -1) {
70 LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
71 return ERROR_PLD_FILE_LOAD_FAILED;
72 }
73
74 if (S_ISDIR(input_stat.st_mode)) {
75 LOG_ERROR("%s is a directory", filename);
76 return ERROR_PLD_FILE_LOAD_FAILED;
77 }
78
79 if (input_stat.st_size == 0) {
80 LOG_ERROR("Empty file %s", filename);
81 return ERROR_PLD_FILE_LOAD_FAILED;
82 }
83
84 input_file = fopen(filename, "rb");
85 if (!input_file) {
86 LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));
87 return ERROR_PLD_FILE_LOAD_FAILED;
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);
100 return ERROR_PLD_FILE_LOAD_FAILED;
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);
106 return ERROR_PLD_FILE_LOAD_FAILED;
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);
112 return ERROR_PLD_FILE_LOAD_FAILED;
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);
118 return ERROR_PLD_FILE_LOAD_FAILED;
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);
124 return ERROR_PLD_FILE_LOAD_FAILED;
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);
130 return ERROR_PLD_FILE_LOAD_FAILED;
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 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)