diff --git a/src/hal/drivers/mesa-hostmot2/bitfile.c b/src/hal/drivers/mesa-hostmot2/bitfile.c index ac10300..2a26b1e 100644 --- a/src/hal/drivers/mesa-hostmot2/bitfile.c +++ b/src/hal/drivers/mesa-hostmot2/bitfile.c @@ -23,6 +23,8 @@ // +#include + #include "rtapi.h" #include "rtapi_app.h" #include "rtapi_string.h" @@ -37,20 +39,22 @@ static int bitfile_do_small_chunk(const struct firmware *fw, bitfile_chunk_t *chunk, int *i) { - if (*i + 2 > fw->size) { + if ((*i) + 2 > fw->size) { HM2_PRINT_NO_LL("bitfile chunk extends past end of firmware\n"); return -ENODATA; } - chunk->size = (fw->data[*i] * 256) + fw->data[*i + 1]; + chunk->size = (fw->data[(*i)] * 256) + fw->data[(*i) + 1]; (*i) += 2; + HM2_PRINT_NO_LL("small chunk size is %d\n", chunk->size); - if (*i + chunk->size > fw->size) { + if ((*i) + chunk->size > fw->size) { HM2_PRINT_NO_LL("bitfile chunk extends past end of firmware\n"); return -ENODATA; } - chunk->data = &fw->data[*i]; + chunk->data = &fw->data[(*i)]; + HM2_PRINT_NO_LL("small chunk data pointer is %p\n", chunk->data); if (chunk->data[chunk->size - 1] != '\0') { HM2_PRINT_NO_LL("bitfile small chunk is not NULL terminated\n"); @@ -66,21 +70,23 @@ static int bitfile_do_small_chunk(const struct firmware *fw, bitfile_chunk_t *ch static int bitfile_do_big_chunk(const struct firmware *fw, bitfile_chunk_t *chunk, int *i) { - if (*i + 4 > fw->size) { + if ((*i) + 4 > fw->size) { HM2_PRINT_NO_LL("bitfile chunk extends past end of firmware\n"); return -ENODATA; } - chunk->size = ((uint32_t)fw->data[*i] << 24) + ((uint32_t)fw->data[*i + 1] << 16) + ((uint32_t)fw->data[*i + 2] << 8) + fw->data[*i + 3]; + chunk->size = ((uint32_t)fw->data[(*i)] << 24) + ((uint32_t)fw->data[(*i) + 1] << 16) + ((uint32_t)fw->data[(*i) + 2] << 8) + fw->data[(*i) + 3]; (*i) += 4; + HM2_PRINT_NO_LL("big chunk size is %d\n", chunk->size); - if (*i + chunk->size > fw->size) { + if ((*i) + chunk->size > fw->size) { HM2_PRINT_NO_LL("bitfile chunk extends past end of firmware\n"); return -ENODATA; } - chunk->data = &fw->data[*i]; + chunk->data = &fw->data[(*i)]; (*i) += chunk->size; + HM2_PRINT_NO_LL("big chunk data pointer is %p\n", chunk->data); return 0; } @@ -93,6 +99,12 @@ static int bitfile_parse_and_verify_chunk(const struct firmware *fw, bitfile_t * tag = fw->data[*i]; (*i) ++; + HM2_PRINT_NO_LL("chunk tag is %c\n", tag); + + if ((*i) > fw->size) { + HM2_PRINT_NO_LL("bitfile chunk '%c' size fell off the end!\n", tag); + return -ENODATA; + } switch (tag) { case 'a': @@ -165,6 +177,8 @@ int bitfile_parse_and_verify(const struct firmware *fw, bitfile_t *bitfile) { // verify the header // + HM2_PRINT_NO_LL("fw size is %d\n", fw->size); + if (fw->size < BITFILE_HEADERLEN) { HM2_PRINT_NO_LL("bitfile is too short\n"); return -ENODATA; @@ -183,7 +197,9 @@ int bitfile_parse_and_verify(const struct firmware *fw, bitfile_t *bitfile) { // while (i < fw->size) { + HM2_PRINT_NO_LL("checking chunk at offset %d\n", i); r = bitfile_parse_and_verify_chunk(fw, bitfile, &i); + HM2_PRINT_NO_LL("chunk done at offset %d\n", i); if (r != 0) return r; } @@ -216,7 +232,7 @@ int bitfile_parse_and_verify(const struct firmware *fw, bitfile_t *bitfile) { // is based on the serial interface, and the data needs to be reversed // -static u8 bitfile_reverse_bits(u8 data) { +u8 bitfile_reverse_bits(u8 data) { static const u8 swaptab[256] = { 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, diff --git a/src/hal/drivers/mesa-hostmot2/bitfile.h b/src/hal/drivers/mesa-hostmot2/bitfile.h index 9ed664b..dd8db68 100644 --- a/src/hal/drivers/mesa-hostmot2/bitfile.h +++ b/src/hal/drivers/mesa-hostmot2/bitfile.h @@ -44,6 +44,7 @@ typedef struct { int bitfile_parse_and_verify(const struct firmware *fw, bitfile_t *bitfile); void bitfile_reverse_bits_of_chunk(bitfile_chunk_t *chunk); +u8 bitfile_reverse_bits(u8 data); diff --git a/src/hal/drivers/mesa-hostmot2/hm2_7i43.c b/src/hal/drivers/mesa-hostmot2/hm2_7i43.c index 3846556..95f65dd 100644 --- a/src/hal/drivers/mesa-hostmot2/hm2_7i43.c +++ b/src/hal/drivers/mesa-hostmot2/hm2_7i43.c @@ -27,6 +27,7 @@ #include "hal.h" +#include "hal/drivers/mesa-hostmot2/bitfile.h" #include "hal/drivers/mesa-hostmot2/hostmot2-lowlevel.h" #include "hal/drivers/mesa-hostmot2/hm2_7i43.h" @@ -282,12 +283,8 @@ int hm2_7i43_program_fpga(hm2_lowlevel_io_t *this, const bitfile_t *bitfile) { // select the CPLD's data address hm2_7i43_epp_addr8(0, board); - for (i = 0; i < (bitfile->e.size & ~0x3); i += 4, firmware += 4) { - hm2_7i43_epp_write32(*(u32 *)firmware, board); - } - - for (; i < bitfile->e.size; i ++, firmware ++) { - hm2_7i43_epp_write(*(u8 *)firmware, board); + for (i = 0; i < bitfile->e.size; i ++, firmware ++) { + hm2_7i43_epp_write(bitfile_reverse_bits(*(u8 *)firmware), board); } end_time = rtapi_get_time(); diff --git a/src/hal/drivers/mesa-hostmot2/hm2_pci.c b/src/hal/drivers/mesa-hostmot2/hm2_pci.c index 3ae6721..5465d09 100644 --- a/src/hal/drivers/mesa-hostmot2/hm2_pci.c +++ b/src/hal/drivers/mesa-hostmot2/hm2_pci.c @@ -26,6 +26,7 @@ #include "hal.h" +#include "bitfile.h" #include "hostmot2-lowlevel.h" #include "hm2_pci.h" @@ -175,7 +176,7 @@ static int hm2_plx9030_program_fpga(hm2_lowlevel_io_t *this, const bitfile_t *bi // program the FPGA for (i = 0; i < bitfile->e.size; i ++) { - outb(bitfile->e.data[i], board->data_base_addr); + outb(bitfile_reverse_bits(bitfile->e.data[i]), board->data_base_addr); } // all bytes transferred, make sure FPGA is all set up now diff --git a/src/hal/drivers/mesa-hostmot2/hostmot2.c b/src/hal/drivers/mesa-hostmot2/hostmot2.c index 18d7281..bc8fda3 100644 --- a/src/hal/drivers/mesa-hostmot2/hostmot2.c +++ b/src/hal/drivers/mesa-hostmot2/hostmot2.c @@ -908,8 +908,6 @@ int hm2_register(hm2_lowlevel_io_t *llio, char *config_string) { } } - bitfile_reverse_bits_of_chunk(&bitfile.e); - if (llio->reset != NULL) { r = llio->reset(llio); if (r != 0) {