361c11b9dbdc145f2f795241a2430fde8aaeb2f8
[fw/openocd] / 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         read_count = fread(bit_file->unknown_header, 1, 13, input_file);
91         if (read_count != 13) {
92                 LOG_ERROR("couldn't read unknown_header from file '%s'", filename);
93                 return ERROR_PLD_FILE_LOAD_FAILED;
94         }
95
96         if (read_section(input_file, 2, 'a', NULL, &bit_file->source_file) != ERROR_OK)
97                 return ERROR_PLD_FILE_LOAD_FAILED;
98
99         if (read_section(input_file, 2, 'b', NULL, &bit_file->part_name) != ERROR_OK)
100                 return ERROR_PLD_FILE_LOAD_FAILED;
101
102         if (read_section(input_file, 2, 'c', NULL, &bit_file->date) != ERROR_OK)
103                 return ERROR_PLD_FILE_LOAD_FAILED;
104
105         if (read_section(input_file, 2, 'd', NULL, &bit_file->time) != ERROR_OK)
106                 return ERROR_PLD_FILE_LOAD_FAILED;
107
108         if (read_section(input_file, 4, 'e', &bit_file->length, &bit_file->data) != ERROR_OK)
109                 return ERROR_PLD_FILE_LOAD_FAILED;
110
111         LOG_DEBUG("bit_file: %s %s %s,%s %" PRIu32 "", bit_file->source_file, bit_file->part_name,
112                 bit_file->date, bit_file->time, bit_file->length);
113
114         fclose(input_file);
115
116         return ERROR_OK;
117 }