2022-08-30 10:01:12 -05:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2022-06-26 18:26:22 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
***************************************************************************/
|
2012-01-27 10:45:29 -06:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "xilinx_bit.h"
|
|
|
|
#include "pld.h"
|
2009-12-03 06:14:28 -06:00
|
|
|
#include <helper/log.h>
|
2006-11-22 07:03:10 -06:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2021-04-19 15:04:30 -05:00
|
|
|
#include <helper/system.h>
|
2006-11-22 07:03:10 -06:00
|
|
|
|
2009-06-17 19:29:45 -05:00
|
|
|
static int read_section(FILE *input_file, int length_size, char section,
|
2012-01-27 10:45:29 -06:00
|
|
|
uint32_t *buffer_length, uint8_t **buffer)
|
2006-11-22 07:03:10 -06:00
|
|
|
{
|
2009-06-18 02:07:12 -05:00
|
|
|
uint8_t length_buffer[4];
|
2009-04-19 15:51:16 -05:00
|
|
|
int length;
|
2006-11-22 07:03:10 -06:00
|
|
|
char section_char;
|
|
|
|
int read_count;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
if ((length_size != 2) && (length_size != 4)) {
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("BUG: length_size neither 2 nor 4");
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
}
|
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
read_count = fread(§ion_char, 1, 1, input_file);
|
|
|
|
if (read_count != 1)
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (section_char != section)
|
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
read_count = fread(length_buffer, 1, length_size, input_file);
|
|
|
|
if (read_count != length_size)
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (length_size == 4)
|
|
|
|
length = be_to_h_u32(length_buffer);
|
2012-01-27 10:45:29 -06:00
|
|
|
else /* (length_size == 2) */
|
2006-11-22 07:03:10 -06:00
|
|
|
length = be_to_h_u16(length_buffer);
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (buffer_length)
|
|
|
|
*buffer_length = length;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
*buffer = malloc(length);
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
read_count = fread(*buffer, 1, length, input_file);
|
|
|
|
if (read_count != length)
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 10:14:54 -06:00
|
|
|
int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char *filename)
|
2006-11-22 07:03:10 -06:00
|
|
|
{
|
|
|
|
FILE *input_file;
|
|
|
|
struct stat input_stat;
|
|
|
|
int read_count;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (!filename || !bit_file)
|
2011-12-28 05:56:08 -06:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
if (stat(filename, &input_stat) == -1) {
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
}
|
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
if (S_ISDIR(input_stat.st_mode)) {
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("%s is a directory", filename);
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
}
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2009-06-23 17:49:06 -05:00
|
|
|
if (input_stat.st_size == 0) {
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("Empty file %s", filename);
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
}
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
input_file = fopen(filename, "rb");
|
2021-07-03 11:51:20 -05:00
|
|
|
if (!input_file) {
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
}
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2012-01-27 10:45:29 -06:00
|
|
|
read_count = fread(bit_file->unknown_header, 1, 13, input_file);
|
|
|
|
if (read_count != 13) {
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("couldn't read unknown_header from file '%s'", filename);
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
}
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (read_section(input_file, 2, 'a', NULL, &bit_file->source_file) != ERROR_OK)
|
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (read_section(input_file, 2, 'b', NULL, &bit_file->part_name) != ERROR_OK)
|
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
if (read_section(input_file, 2, 'c', NULL, &bit_file->date) != ERROR_OK)
|
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
|
|
|
|
if (read_section(input_file, 2, 'd', NULL, &bit_file->time) != ERROR_OK)
|
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
|
|
|
|
|
|
|
if (read_section(input_file, 4, 'e', &bit_file->length, &bit_file->data) != ERROR_OK)
|
|
|
|
return ERROR_PLD_FILE_LOAD_FAILED;
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2020-08-18 11:56:53 -05:00
|
|
|
LOG_DEBUG("bit_file: %s %s %s,%s %" PRIu32 "", bit_file->source_file, bit_file->part_name,
|
2006-11-22 07:03:10 -06:00
|
|
|
bit_file->date, bit_file->time, bit_file->length);
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
fclose(input_file);
|
2009-06-17 19:29:45 -05:00
|
|
|
|
2006-11-22 07:03:10 -06:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|