summaryrefslogtreecommitdiff
path: root/src/pld
diff options
context:
space:
mode:
authordrath <drath@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2006-11-22 13:03:10 +0000
committerdrath <drath@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2006-11-22 13:03:10 +0000
commite9297b40b994f071474210e7d9e224d50e25fcaf (patch)
treec04957b7721ab6825e73fad0775677643f82fc23 /src/pld
parent03e8f264f4c66baec9b86778d3488b23e0a8c0b6 (diff)
downloadopenocd+libswd-e9297b40b994f071474210e7d9e224d50e25fcaf.tar.gz
openocd+libswd-e9297b40b994f071474210e7d9e224d50e25fcaf.tar.bz2
openocd+libswd-e9297b40b994f071474210e7d9e224d50e25fcaf.tar.xz
openocd+libswd-e9297b40b994f071474210e7d9e224d50e25fcaf.zip
- added a PLD (programmable logic device) subsystem for FPGA, CPLD etc. configuration
- added support for loading .bit files into Xilinx Virtex-II devices - added support for the Gateworks GW16012 JTAG dongle - merged CFI fixes from XScale branch - a few minor fixes git-svn-id: svn://svn.berlios.de/openocd/trunk@116 b42882b7-edfa-0310-969c-e2dbd0fdcd60
Diffstat (limited to 'src/pld')
-rw-r--r--src/pld/Makefile.am5
-rw-r--r--src/pld/pld.c218
-rw-r--r--src/pld/pld.h49
-rw-r--r--src/pld/virtex2.c264
-rw-r--r--src/pld/virtex2.h31
-rw-r--r--src/pld/xilinx_bit.c146
-rw-r--r--src/pld/xilinx_bit.h38
7 files changed, 751 insertions, 0 deletions
diff --git a/src/pld/Makefile.am b/src/pld/Makefile.am
new file mode 100644
index 00000000..e3386257
--- /dev/null
+++ b/src/pld/Makefile.am
@@ -0,0 +1,5 @@
+INCLUDES = -I$(top_srcdir)/src/server -I$(top_srcdir)/src/helper -I$(top_srcdir)/src/jtag $(all_includes)
+METASOURCES = AUTO
+noinst_LIBRARIES = libpld.a
+noinst_HEADERS = pld.h xilinx_bit.h virtex2.h
+libpld_a_SOURCES = pld.c xilinx_bit.c virtex2.c
diff --git a/src/pld/pld.c b/src/pld/pld.c
new file mode 100644
index 00000000..a20f9d94
--- /dev/null
+++ b/src/pld/pld.c
@@ -0,0 +1,218 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Dominic Rath *
+ * Dominic.Rath@gmx.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pld.h"
+
+#include "jtag.h"
+#include "command.h"
+#include "log.h"
+#include "time_support.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+/* pld drivers
+ */
+extern pld_driver_t virtex2_pld;
+
+pld_driver_t *pld_drivers[] =
+{
+ &virtex2_pld,
+ NULL,
+};
+
+pld_device_t *pld_devices;
+static command_t *pld_cmd;
+
+int handle_pld_devices_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+int handle_pld_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+int handle_pld_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+
+int pld_init(struct command_context_s *cmd_ctx)
+{
+ if (pld_devices)
+ {
+ register_command(cmd_ctx, pld_cmd, "devices", handle_pld_devices_command, COMMAND_EXEC,
+ "list configured pld devices");
+ register_command(cmd_ctx, pld_cmd, "load", handle_pld_load_command, COMMAND_EXEC,
+ "load configuration <file> into programmable logic device");
+ }
+
+ return ERROR_OK;
+}
+
+pld_device_t *get_pld_device_by_num(int num)
+{
+ pld_device_t *p;
+ int i = 0;
+
+ for (p = pld_devices; p; p = p->next)
+ {
+ if (i++ == num)
+ {
+ return p;
+ }
+ }
+
+ return NULL;
+}
+
+/* pld device <driver> [driver_options ...]
+ */
+int handle_pld_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ int i;
+ int found = 0;
+
+ if (argc < 1)
+ {
+ WARNING("incomplete 'pld bank' configuration");
+ return ERROR_OK;
+ }
+
+ for (i = 0; pld_drivers[i]; i++)
+ {
+ if (strcmp(args[0], pld_drivers[i]->name) == 0)
+ {
+ pld_device_t *p, *c;
+
+ /* register pld specific commands */
+ if (pld_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
+ {
+ ERROR("couldn't register '%s' commands", args[0]);
+ exit(-1);
+ }
+
+ c = malloc(sizeof(pld_device_t));
+ c->driver = pld_drivers[i];
+ c->next = NULL;
+
+ if (pld_drivers[i]->pld_device_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
+ {
+ ERROR("'%s' driver rejected pld device", args[0]);
+ free(c);
+ return ERROR_OK;
+ }
+
+ /* put pld device in linked list */
+ if (pld_devices)
+ {
+ /* find last pld device */
+ for (p = pld_devices; p && p->next; p = p->next);
+ if (p)
+ p->next = c;
+ }
+ else
+ {
+ pld_devices = c;
+ }
+
+ found = 1;
+ }
+ }
+
+ /* no matching pld driver found */
+ if (!found)
+ {
+ ERROR("pld driver '%s' not found", args[0]);
+ exit(-1);
+ }
+
+ return ERROR_OK;
+}
+
+int handle_pld_devices_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ pld_device_t *p;
+ int i = 0;
+
+ if (!pld_devices)
+ {
+ command_print(cmd_ctx, "no pld devices configured");
+ return ERROR_OK;
+ }
+
+ for (p = pld_devices; p; p = p->next)
+ {
+ command_print(cmd_ctx, "#%i: %s", i++, p->driver->name);
+ }
+
+ return ERROR_OK;
+}
+
+int handle_pld_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ int retval;
+ struct timeval start, end, duration;
+ pld_device_t *p;
+
+ gettimeofday(&start, NULL);
+
+ if (argc < 2)
+ {
+ command_print(cmd_ctx, "usage: pld load <device#> <file>");
+ return ERROR_OK;
+ }
+
+ p = get_pld_device_by_num(strtoul(args[0], NULL, 0));
+ if (!p)
+ {
+ command_print(cmd_ctx, "pld device '#%s' is out of bounds", args[0]);
+ return ERROR_OK;
+ }
+
+ if ((retval = p->driver->load(p, args[1])) != ERROR_OK)
+ {
+ command_print(cmd_ctx, "failed loading file %s to pld device %i",
+ args[1], strtoul(args[0], NULL, 0));
+ switch (retval)
+ {
+ }
+ }
+ else
+ {
+ gettimeofday(&end, NULL);
+ timeval_subtract(&duration, &end, &start);
+
+ command_print(cmd_ctx, "loaded file %s to pld device %i in %is %ius",
+ args[1], strtoul(args[0], NULL, 0), duration.tv_sec, duration.tv_usec);
+ }
+
+ return ERROR_OK;
+}
+
+int pld_register_commands(struct command_context_s *cmd_ctx)
+{
+ pld_cmd = register_command(cmd_ctx, NULL, "pld", NULL, COMMAND_ANY, "programmable logic device commands");
+
+ register_command(cmd_ctx, pld_cmd, "device", handle_pld_device_command, COMMAND_CONFIG, NULL);
+
+ return ERROR_OK;
+}
diff --git a/src/pld/pld.h b/src/pld/pld.h
new file mode 100644
index 00000000..e4cfc071
--- /dev/null
+++ b/src/pld/pld.h
@@ -0,0 +1,49 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Dominic Rath *
+ * Dominic.Rath@gmx.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifndef PLD_H
+#define PLD_H
+
+#include "command.h"
+
+struct pld_device_s;
+
+typedef struct pld_driver_s
+{
+ char *name;
+ int (*register_commands)(struct command_context_s *cmd_ctx);
+ int (*pld_device_command)(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct pld_device_s *pld_device);
+ int (*load)(struct pld_device_s *pld_device, char *filename);
+} pld_driver_t;
+
+typedef struct pld_device_s
+{
+ pld_driver_t *driver;
+ void *driver_priv;
+ struct pld_device_s *next;
+} pld_device_t;
+
+extern int pld_register_commands(struct command_context_s *cmd_ctx);
+extern int pld_init(struct command_context_s *cmd_ctx);
+extern pld_device_t *get_pld_device_by_num(int num);
+
+#define ERROR_PLD_DEVICE_INVALID (-1000)
+#define ERROR_PLD_FILE_LOAD_FAILED (-1001)
+
+#endif /* PLD_H */
diff --git a/src/pld/virtex2.c b/src/pld/virtex2.c
new file mode 100644
index 00000000..8624367f
--- /dev/null
+++ b/src/pld/virtex2.c
@@ -0,0 +1,264 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Dominic Rath *
+ * Dominic.Rath@gmx.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "virtex2.h"
+
+#include "pld.h"
+#include "xilinx_bit.h"
+#include "command.h"
+#include "log.h"
+#include "jtag.h"
+
+#include <stdlib.h>
+
+int virtex2_register_commands(struct command_context_s *cmd_ctx);
+int virtex2_pld_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct pld_device_s *pld_device);
+int virtex2_load(struct pld_device_s *pld_device, char *filename);
+
+pld_driver_t virtex2_pld =
+{
+ .name = "virtex2",
+ .register_commands = virtex2_register_commands,
+ .pld_device_command = virtex2_pld_device_command,
+ .load = virtex2_load,
+};
+
+int virtex2_set_instr(int chain_pos, u32 new_instr)
+{
+ jtag_device_t *device = jtag_get_device(chain_pos);
+
+ if (buf_get_u32(device->cur_instr, 0, device->ir_length) != new_instr)
+ {
+ scan_field_t field;
+
+ field.device = chain_pos;
+ field.num_bits = device->ir_length;
+ field.out_value = calloc(CEIL(field.num_bits, 8), 1);
+ buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
+ field.out_mask = NULL;
+ field.in_value = NULL;
+ field.in_check_value = NULL;
+ field.in_check_mask = NULL;
+ field.in_handler = NULL;
+ field.in_handler_priv = NULL;
+
+ jtag_add_ir_scan(1, &field, TAP_RTI);
+
+ free(field.out_value);
+ }
+
+ return ERROR_OK;
+}
+
+int virtex2_send_32(struct pld_device_s *pld_device, int num_words, u32 *words)
+{
+ virtex2_pld_device_t *virtex2_info = pld_device->driver_priv;
+ scan_field_t scan_field;
+ u8 *values;
+ int i;
+
+ values = malloc(num_words * 4);
+
+ scan_field.device = virtex2_info->chain_pos;
+ scan_field.num_bits = num_words * 32;
+ scan_field.out_value = values;
+ scan_field.out_mask = NULL;
+ scan_field.in_value = NULL;
+ scan_field.in_check_value = NULL;
+ scan_field.in_check_mask = NULL;
+ scan_field.in_handler = NULL;
+ scan_field.in_handler_priv = NULL;
+
+ for (i = 0; i < num_words; i++)
+ buf_set_u32(values + 4 * i, 0, 32, flip_u32(*words++, 32));
+
+ virtex2_set_instr(virtex2_info->chain_pos, 0x5); /* CFG_IN */
+
+ jtag_add_dr_scan(1, &scan_field, TAP_PD);
+
+ free(values);
+
+ return ERROR_OK;
+}
+
+int virtex2_jtag_buf_to_u32(u8 *in_buf, void *priv)
+{
+ u32 *dest = priv;
+ *dest = flip_u32(le_to_h_u32(in_buf), 32);
+ return ERROR_OK;
+}
+
+int virtex2_receive_32(struct pld_device_s *pld_device, int num_words, u32 *words)
+{
+ virtex2_pld_device_t *virtex2_info = pld_device->driver_priv;
+ scan_field_t scan_field;
+
+ scan_field.device = virtex2_info->chain_pos;
+ scan_field.num_bits = 32;
+ scan_field.out_value = NULL;
+ scan_field.out_mask = NULL;
+ scan_field.in_value = NULL;
+ scan_field.in_check_value = NULL;
+ scan_field.in_check_mask = NULL;
+ scan_field.in_handler = virtex2_jtag_buf_to_u32;
+
+ virtex2_set_instr(virtex2_info->chain_pos, 0x4); /* CFG_OUT */
+
+ while (num_words--)
+ {
+ scan_field.in_handler_priv = words++;
+ jtag_add_dr_scan(1, &scan_field, TAP_PD);
+ }
+
+ return ERROR_OK;
+}
+
+int virtex2_read_stat(struct pld_device_s *pld_device, u32 *status)
+{
+ u32 data[5];
+
+ jtag_add_statemove(TAP_TLR);
+
+ data[0] = 0xaa995566; /* synch word */
+ data[1] = 0x2800E001; /* Type 1, read, address 7, 1 word */
+ data[2] = 0x20000000; /* NOOP (Type 1, read, address 0, 0 words */
+ data[3] = 0x20000000; /* NOOP */
+ data[4] = 0x20000000; /* NOOP */
+ virtex2_send_32(pld_device, 5, data);
+
+ virtex2_receive_32(pld_device, 1, status);
+
+ jtag_execute_queue();
+
+ DEBUG("status: 0x%8.8x", *status);
+
+ return ERROR_OK;
+}
+
+int virtex2_load(struct pld_device_s *pld_device, char *filename)
+{
+ virtex2_pld_device_t *virtex2_info = pld_device->driver_priv;
+ xilinx_bit_file_t bit_file;
+ int retval;
+ int i;
+
+ scan_field_t field;
+
+ field.device = virtex2_info->chain_pos;
+ field.out_mask = NULL;
+ field.in_value = NULL;
+ field.in_check_value = NULL;
+ field.in_check_mask = NULL;
+ field.in_handler = NULL;
+ field.in_handler_priv = NULL;
+
+ if ((retval = xilinx_read_bit_file(&bit_file, filename)) != ERROR_OK)
+ return retval;
+
+ jtag_add_end_state(TAP_RTI);
+ virtex2_set_instr(virtex2_info->chain_pos, 0xb); /* JPROG_B */
+ jtag_execute_queue();
+ jtag_add_sleep(1000);
+
+ virtex2_set_instr(virtex2_info->chain_pos, 0x5); /* CFG_IN */
+ jtag_execute_queue();
+
+ for (i = 0; i < bit_file.length; i++)
+ bit_file.data[i] = flip_u32(bit_file.data[i], 8);
+
+ field.num_bits = bit_file.length * 8;
+ field.out_value = bit_file.data;
+
+ jtag_add_dr_scan(1, &field, TAP_PD);
+ jtag_execute_queue();
+
+ jtag_add_statemove(TAP_TLR);
+
+ jtag_add_end_state(TAP_RTI);
+ virtex2_set_instr(virtex2_info->chain_pos, 0xc); /* JSTART */
+ jtag_add_runtest(13, TAP_RTI);
+ virtex2_set_instr(virtex2_info->chain_pos, 0x3f); /* BYPASS */
+ virtex2_set_instr(virtex2_info->chain_pos, 0x3f); /* BYPASS */
+ virtex2_set_instr(virtex2_info->chain_pos, 0xc); /* JSTART */
+ jtag_add_runtest(13, TAP_RTI);
+ virtex2_set_instr(virtex2_info->chain_pos, 0x3f); /* BYPASS */
+ jtag_execute_queue();
+
+ return ERROR_OK;
+}
+
+int virtex2_handle_read_stat_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ pld_device_t *device;
+ virtex2_pld_device_t *virtex2_info;
+ u32 status;
+
+ if (argc < 1)
+ {
+ command_print(cmd_ctx, "usage: virtex2 read_stat <num>");
+ return ERROR_OK;
+ }
+
+ device = get_pld_device_by_num(strtoul(args[0], NULL, 0));
+ if (!device)
+ {
+ command_print(cmd_ctx, "pld device '#%s' is out of bounds", args[0]);
+ return ERROR_OK;
+ }
+
+ virtex2_info = device->driver_priv;
+
+ virtex2_read_stat(device, &status);
+
+ command_print(cmd_ctx, "virtex2 status register: 0x%8.8x", status);
+
+ return ERROR_OK;
+}
+
+int virtex2_register_commands(struct command_context_s *cmd_ctx)
+{
+ command_t *virtex2_cmd = register_command(cmd_ctx, NULL, "virtex2", NULL, COMMAND_ANY, "virtex2 specific commands");
+
+ register_command(cmd_ctx, virtex2_cmd, "read_stat", virtex2_handle_read_stat_command, COMMAND_EXEC,
+ "read Virtex-II status register");
+
+ return ERROR_OK;
+}
+
+int virtex2_pld_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct pld_device_s *pld_device)
+{
+ virtex2_pld_device_t *virtex2_info;
+
+ if (argc < 2)
+ {
+ WARNING("incomplete pld device 'virtex2' configuration");
+ return ERROR_PLD_DEVICE_INVALID;
+ }
+
+ virtex2_info = malloc(sizeof(virtex2_pld_device_t));
+ pld_device->driver_priv = virtex2_info;
+
+ virtex2_info->chain_pos = strtoul(args[1], NULL, 0);
+
+ return ERROR_OK;
+}
diff --git a/src/pld/virtex2.h b/src/pld/virtex2.h
new file mode 100644
index 00000000..1b5865dd
--- /dev/null
+++ b/src/pld/virtex2.h
@@ -0,0 +1,31 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Dominic Rath *
+ * Dominic.Rath@gmx.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifndef VIRTEX2_H
+#define VIRTEX2_H
+
+#include "pld.h"
+#include "xilinx_bit.h"
+
+typedef struct virtex2_pld_device_s
+{
+ int chain_pos;
+} virtex2_pld_device_t;
+
+#endif /* VIRTEX2_H */
diff --git a/src/pld/xilinx_bit.c b/src/pld/xilinx_bit.c
new file mode 100644
index 00000000..7bf72b2e
--- /dev/null
+++ b/src/pld/xilinx_bit.c
@@ -0,0 +1,146 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Dominic Rath *
+ * Dominic.Rath@gmx.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "xilinx_bit.h"
+
+#include "pld.h"
+#include "log.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+int read_section(FILE *input_file, int length_size, char section, u32 *buffer_length, u8 **buffer)
+{
+ u8 length_buffer[4];
+ u32 length;
+ char section_char;
+ int read_count;
+
+ if ((length_size != 2) && (length_size != 4))
+ {
+ ERROR("BUG: length_size neither 2 nor 4");
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if ((read_count = fread(&section_char, 1, 1, input_file)) != 1)
+ {
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if (section_char != section)
+ {
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if ((read_count = fread(length_buffer, 1, length_size, input_file)) != length_size)
+ {
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if (length_size == 4)
+ length = be_to_h_u32(length_buffer);
+
+ if (length_size == 2)
+ length = be_to_h_u16(length_buffer);
+
+ if (buffer_length)
+ *buffer_length = length;
+
+ *buffer = malloc(length);
+
+ if ((read_count = fread(*buffer, 1, length, input_file)) != length)
+ {
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ return ERROR_OK;
+}
+
+int xilinx_read_bit_file(xilinx_bit_file_t *bit_file, char *filename)
+{
+ FILE *input_file;
+ struct stat input_stat;
+ int read_count;
+
+ if (!filename || !bit_file)
+ return ERROR_INVALID_ARGUMENTS;
+
+ if (stat(filename, &input_stat) == -1)
+ {
+ ERROR("couldn't stat() %s: %s", filename, strerror(errno));
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if (S_ISDIR(input_stat.st_mode))
+ {
+ ERROR("%s is a directory", filename);
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if (input_stat.st_size == 0){
+ ERROR("Empty file %s", filename);
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if (!(input_file = fopen(filename, "rb")))
+ {
+ ERROR("couldn't open %s: %s", filename, strerror(errno));
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if ((read_count = fread(bit_file->unknown_header, 1, 13, input_file)) != 13)
+ {
+ ERROR("couldn't read unknown_header from file '%s'", filename);
+ return ERROR_PLD_FILE_LOAD_FAILED;
+ }
+
+ if (read_section(input_file, 2, 'a', NULL, &bit_file->source_file) != ERROR_OK)
+ return ERROR_PLD_FILE_LOAD_FAILED;
+
+ if (read_section(input_file, 2, 'b', NULL, &bit_file->part_name) != ERROR_OK)
+ return ERROR_PLD_FILE_LOAD_FAILED;
+
+ 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;
+
+ DEBUG("bit_file: %s %s %s,%s %i", bit_file->source_file, bit_file->part_name,
+ bit_file->date, bit_file->time, bit_file->length);
+
+ fclose(input_file);
+
+ return ERROR_OK;
+}
diff --git a/src/pld/xilinx_bit.h b/src/pld/xilinx_bit.h
new file mode 100644
index 00000000..505957ab
--- /dev/null
+++ b/src/pld/xilinx_bit.h
@@ -0,0 +1,38 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Dominic Rath *
+ * Dominic.Rath@gmx.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifndef XILINX_BIT_H
+#define XILINX_BIT_H
+
+#include "types.h"
+
+typedef struct xilinx_bit_file_s
+{
+ u8 unknown_header[13];
+ u8 *source_file;
+ u8 *part_name;
+ u8 *date;
+ u8 *time;
+ u32 length;
+ u8 *data;
+} xilinx_bit_file_t;
+
+int xilinx_read_bit_file(xilinx_bit_file_t *bit_file, char *filename);
+
+#endif /* XILINX_BIT_H */