#include "pld.h"
#include <helper/log.h>
-#include <sys/stat.h>
#include <helper/system.h>
static int read_section(FILE *input_file, int length_size, char section,
int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char *filename)
{
FILE *input_file;
- struct stat input_stat;
int read_count;
if (!filename || !bit_file)
return ERROR_COMMAND_SYNTAX_ERROR;
- if (stat(filename, &input_stat) == -1) {
- LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
- return ERROR_PLD_FILE_LOAD_FAILED;
- }
-
- if (S_ISDIR(input_stat.st_mode)) {
- LOG_ERROR("%s is a directory", filename);
- return ERROR_PLD_FILE_LOAD_FAILED;
- }
-
- if (input_stat.st_size == 0) {
- LOG_ERROR("Empty file %s", filename);
- return ERROR_PLD_FILE_LOAD_FAILED;
- }
-
input_file = fopen(filename, "rb");
if (!input_file) {
LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));
read_count = fread(bit_file->unknown_header, 1, 13, input_file);
if (read_count != 13) {
LOG_ERROR("couldn't read unknown_header from file '%s'", filename);
+ fclose(input_file);
return ERROR_PLD_FILE_LOAD_FAILED;
}
if (read_section(input_file, 2, 'a', NULL, &bit_file->source_file) != ERROR_OK) {
xilinx_free_bit_file(bit_file);
+ fclose(input_file);
return ERROR_PLD_FILE_LOAD_FAILED;
}
if (read_section(input_file, 2, 'b', NULL, &bit_file->part_name) != ERROR_OK) {
xilinx_free_bit_file(bit_file);
+ fclose(input_file);
return ERROR_PLD_FILE_LOAD_FAILED;
}
if (read_section(input_file, 2, 'c', NULL, &bit_file->date) != ERROR_OK) {
xilinx_free_bit_file(bit_file);
+ fclose(input_file);
return ERROR_PLD_FILE_LOAD_FAILED;
}
if (read_section(input_file, 2, 'd', NULL, &bit_file->time) != ERROR_OK) {
xilinx_free_bit_file(bit_file);
+ fclose(input_file);
return ERROR_PLD_FILE_LOAD_FAILED;
}
if (read_section(input_file, 4, 'e', &bit_file->length, &bit_file->data) != ERROR_OK) {
xilinx_free_bit_file(bit_file);
+ fclose(input_file);
return ERROR_PLD_FILE_LOAD_FAILED;
}