asix-sigma: Cleanup the bit2bitbang
Rework the bit2bitbang function. Remarkable changes are: - The function was renamed to sigma_fw_2_bitbang() - We use glib function calls to work with the file - We mmap the file containing firmware instead of opening the file and then reading it into a buffer. - The magic firmware transformation is now type-safe. - Documentation and comments were added where applicable. Signed-off-by: Marek Vasut <marex@denx.de>
This commit is contained in:
parent
8bbf7627df
commit
d485d44361
|
@ -299,85 +299,76 @@ static int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *d
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Generate the bitbang stream for programming the FPGA. */
|
/*
|
||||||
static int bin2bitbang(const char *filename,
|
* Read the firmware from a file and transform it into a series of bitbang
|
||||||
unsigned char **buf, size_t *buf_size)
|
* pulses used to program the FPGA. Note that the *bb_cmd must be free()'d
|
||||||
|
* by the caller of this function.
|
||||||
|
*/
|
||||||
|
static int sigma_fw_2_bitbang(const char *filename,
|
||||||
|
uint8_t **bb_cmd, gsize *bb_cmd_size)
|
||||||
{
|
{
|
||||||
FILE *f;
|
GMappedFile *file;
|
||||||
unsigned long file_size;
|
GError *error;
|
||||||
unsigned long offset = 0;
|
gsize i, file_size, bb_size;
|
||||||
unsigned char *p;
|
gchar *firmware;
|
||||||
uint8_t *firmware;
|
uint8_t *bb_stream, *bbs;
|
||||||
unsigned long fwsize = 0;
|
uint32_t imm;
|
||||||
const int buffer_size = 65536;
|
int bit, v;
|
||||||
size_t i;
|
int ret = SR_OK;
|
||||||
int c, bit, v;
|
|
||||||
uint32_t imm = 0x3f6df2ab;
|
|
||||||
|
|
||||||
f = g_fopen(filename, "rb");
|
/*
|
||||||
if (!f) {
|
* Map the file and make the mapped buffer writable.
|
||||||
sr_err("g_fopen(\"%s\", \"rb\")", filename);
|
* NOTE: Using writable=TRUE does _NOT_ mean that file that is mapped
|
||||||
return SR_ERR;
|
* will be modified. It will not be modified until someone uses
|
||||||
}
|
* g_file_set_contents() on it.
|
||||||
|
*/
|
||||||
|
error = NULL;
|
||||||
|
file = g_mapped_file_new(filename, TRUE, &error);
|
||||||
|
g_assert_no_error(error);
|
||||||
|
|
||||||
if (-1 == fseek(f, 0, SEEK_END)) {
|
file_size = g_mapped_file_get_length(file);
|
||||||
sr_err("fseek on %s failed", filename);
|
firmware = g_mapped_file_get_contents(file);
|
||||||
fclose(f);
|
g_assert(firmware);
|
||||||
return SR_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
file_size = ftell(f);
|
/* Weird magic transformation below, I have no idea what it does. */
|
||||||
|
imm = 0x3f6df2ab;
|
||||||
fseek(f, 0, SEEK_SET);
|
for (i = 0; i < file_size; i++) {
|
||||||
|
|
||||||
if (!(firmware = g_try_malloc(buffer_size))) {
|
|
||||||
sr_err("%s: firmware malloc failed", __func__);
|
|
||||||
fclose(f);
|
|
||||||
return SR_ERR_MALLOC;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((c = getc(f)) != EOF) {
|
|
||||||
imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
|
imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
|
||||||
firmware[fwsize++] = c ^ imm;
|
firmware[i] ^= imm & 0xff;
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if(fwsize != file_size) {
|
|
||||||
sr_err("%s: Error reading firmware", filename);
|
|
||||||
fclose(f);
|
|
||||||
g_free(firmware);
|
|
||||||
return SR_ERR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*buf_size = fwsize * 2 * 8;
|
/*
|
||||||
|
* Now that the firmware is "transformed", we will transcribe the
|
||||||
|
* firmware blob into a sequence of toggles of the Dx wires. This
|
||||||
|
* sequence will be fed directly into the Sigma, which must be in
|
||||||
|
* the FPGA bitbang programming mode.
|
||||||
|
*/
|
||||||
|
|
||||||
*buf = p = (unsigned char *)g_try_malloc(*buf_size);
|
/* Each bit of firmware is transcribed as two toggles of Dx wires. */
|
||||||
if (!p) {
|
bb_size = file_size * 8 * 2;
|
||||||
sr_err("%s: buf/p malloc failed", __func__);
|
bb_stream = (uint8_t *)g_try_malloc(bb_size);
|
||||||
g_free(firmware);
|
if (!bb_stream) {
|
||||||
return SR_ERR_MALLOC;
|
sr_err("%s: Failed to allocate bitbang stream", __func__);
|
||||||
|
ret = SR_ERR_MALLOC;
|
||||||
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < fwsize; ++i) {
|
bbs = bb_stream;
|
||||||
for (bit = 7; bit >= 0; --bit) {
|
for (i = 0; i < file_size; i++) {
|
||||||
v = firmware[i] & 1 << bit ? 0x40 : 0x00;
|
for (bit = 7; bit >= 0; bit--) {
|
||||||
p[offset++] = v | 0x01;
|
v = (firmware[i] & (1 << bit)) ? 0x40 : 0x00;
|
||||||
p[offset++] = v;
|
*bbs++ = v | 0x01;
|
||||||
|
*bbs++ = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(firmware);
|
/* The transformation completed successfully, return the result. */
|
||||||
|
*bb_cmd = bb_stream;
|
||||||
|
*bb_cmd_size = bb_size;
|
||||||
|
|
||||||
if (offset != *buf_size) {
|
exit:
|
||||||
g_free(*buf);
|
g_mapped_file_unref(file);
|
||||||
sr_err("Error reading firmware %s "
|
return ret;
|
||||||
"offset=%ld, file_size=%ld, buf_size=%zd.",
|
|
||||||
filename, offset, file_size, *buf_size);
|
|
||||||
|
|
||||||
return SR_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return SR_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear_helper(void *priv)
|
static void clear_helper(void *priv)
|
||||||
|
@ -575,7 +566,7 @@ static int upload_firmware(int firmware_idx, struct dev_context *devc)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
/* Prepare firmware. */
|
/* Prepare firmware. */
|
||||||
ret = bin2bitbang(firmware, &buf, &buf_size);
|
ret = sigma_fw_2_bitbang(firmware, &buf, &buf_size);
|
||||||
if (ret != SR_OK) {
|
if (ret != SR_OK) {
|
||||||
sr_err("An error occured while reading the firmware: %s",
|
sr_err("An error occured while reading the firmware: %s",
|
||||||
firmware);
|
firmware);
|
||||||
|
|
Loading…
Reference in New Issue