int bladerf_flash_firmware(struct bladerf *dev, const char *firmware_file) { int status; uint8_t *buf; size_t buf_size; const char env_override[] = "BLADERF_SKIP_FW_SIZE_CHECK"; status = file_read_buffer(firmware_file, &buf, &buf_size); if (status != 0) { return status; } /* Sanity check firmware length. * * TODO in the future, better sanity checks can be performed when * using the bladerf image format currently used to backup/restore * calibration data */ if (!getenv(env_override) && !valid_fw_size(buf_size)) { log_info("Detected potentially invalid firmware file.\n"); log_info("Define BLADERF_SKIP_FW_SIZE_CHECK in your evironment " "to skip this check.\n"); status = BLADERF_ERR_INVAL; } else { status = flash_write_fx3_fw(dev, &buf, buf_size); } free(buf); return status; }
int bladerf_load_fpga(struct bladerf *dev, const char *fpga_file) { uint8_t *buf = NULL; size_t buf_size; int status; /* TODO sanity check FPGA: * - Check for x40 vs x115 and verify FPGA image size * - Known header/footer on images? * - Checksum/hash? */ status = file_read_buffer(fpga_file, &buf, &buf_size); if (status != 0) { goto error; } if (!valid_fpga_size(buf_size)) { status = BLADERF_ERR_INVAL; goto error; } status = dev->fn->load_fpga(dev, buf, buf_size); if (status == 0) { status = bladerf_init_device(dev); } error: free(buf); return status; }
int fpga_load_from_file(struct bladerf *dev, const char *fpga_file) { uint8_t *buf = NULL; size_t buf_size; int status; status = file_read_buffer(fpga_file, &buf, &buf_size); if (status != 0) { goto error; } if (!valid_fpga_size(dev->fpga_size, buf_size)) { status = BLADERF_ERR_INVAL; goto error; } status = dev->fn->load_fpga(dev, buf, buf_size); if (status != 0) { goto error; } status = fpga_check_version(dev); if (status != 0) { goto error; } status = init_device(dev); if (status != 0) { goto error; } error: free(buf); return status; }
int bladerf_image_read(struct bladerf_image *img, const char *file) { int rv = -1; uint8_t *buf = NULL; size_t buf_len; rv = file_read_buffer(file, &buf, &buf_len); if (rv < 0) { goto bladerf_image_read_out; } rv = verify_checksum(buf, buf_len); if (rv < 0) { goto bladerf_image_read_out; } /* Note: On success, buf->data = buf, with the data memmove'd over. * Static analysis tools might indicate a false postive leak when * buf goes out of scope with rv == 0 */ rv = unpack_image(img, buf, buf_len); bladerf_image_read_out: if (rv != 0) { free(buf); } return rv; }
/*------------------------------------------------------------------------------ * Device Programming *----------------------------------------------------------------------------*/ int bladerf_flash_firmware(struct bladerf *dev, const char *firmware_file) { int status; uint8_t *buf, *buf_padded; size_t buf_size, buf_size_padded; status = file_read_buffer(firmware_file, &buf, &buf_size); if (!status) { /* Sanity check firmware * * Quick and dirty check for any absurd sizes. This is arbitrarily * chosen based upon the current FX3 image size. * * TODO This should be replaced with something that also looks for: * - Known header/footer on images? * - Checksum/hash? */ if (!getenv("BLADERF_SKIP_FW_SIZE_CHECK") && (buf_size < (50 * 1024) || (buf_size > (1 * 1024 * 1024)))) { log_info("Detected potentially invalid firmware file.\n"); log_info("Define BLADERF_SKIP_FW_SIZE_CHECK in your evironment " "to skip this check.\n"); status = BLADERF_ERR_INVAL; } else { /* Pad firmare data out to a flash sector size */ const size_t sector_size = BLADERF_FLASH_SECTOR_SIZE; size_t buf_size_padding = sector_size - (buf_size % sector_size); buf_size_padded = buf_size + buf_size_padding; buf_padded = realloc(buf, buf_size_padded); if (!buf_padded) { status = BLADERF_ERR_MEM; } else { buf = buf_padded; memset(buf + buf_size, 0xFF, buf_size_padded - buf_size); } if (!status) { status = dev->fn->flash_firmware(dev, buf, buf_size_padded); } if (!status) { if (dev->legacy & LEGACY_ALT_SETTING) { printf("DEVICE OPERATING IN LEGACY MODE, MANUAL RESET IS NECESSARY AFTER SUCCESSFUL UPGRADE\n"); } } } free(buf); } return status; }
int file_find_and_read(const char *filename, uint8_t **buf, size_t *size) { int status; char *full_path = file_find(filename); *buf = NULL; *size = 0; if (full_path != NULL) { status = file_read_buffer(full_path, buf, size); free(full_path); return status; } else { return BLADERF_ERR_NO_FILE; } }
int fpga_write_to_flash(struct bladerf *dev, const char *fpga_file) { int status; size_t buf_size; uint8_t *buf = NULL; status = file_read_buffer(fpga_file, &buf, &buf_size); if (status == 0) { if (!valid_fpga_size(dev->fpga_size, buf_size)) { status = BLADERF_ERR_INVAL; } else { status = flash_write_fpga_bitstream(dev, &buf, buf_size); } } free(buf); return status; }
int bladerf_flash_fpga(struct bladerf *dev, const char *fpga_file) { int status; uint8_t *buf; size_t buf_size; const char env_override[] = "BLADERF_SKIP_FPGA_SIZE_CHECK"; status = file_read_buffer(fpga_file, &buf, &buf_size); if (status == 0) { if (!getenv(env_override) && !valid_fpga_size(buf_size)) { log_info("Detected potentially invalid firmware file.\n"); log_info("Define BLADERF_SKIP_FPGA_SIZE_CHECK in your evironment " "to skip this check.\n"); status = BLADERF_ERR_INVAL; } else { status = flash_write_fpga_bitstream(dev, &buf, buf_size); free(buf); } } return status; }
int bladerf_load_fpga(struct bladerf *dev, const char *fpga_file) { uint8_t *buf; size_t buf_size; int status; /* TODO sanity check FPGA: * - Check for x40 vs x115 and verify FPGA image size * - Known header/footer on images? * - Checksum/hash? */ status = file_read_buffer(fpga_file, &buf, &buf_size); if (!status) { status = dev->fn->load_fpga(dev, buf, buf_size); free(buf); } if (!status) { status = bladerf_init_device(dev); } return status; }
int bladerf_flash_fpga(struct bladerf *dev, const char *fpga_file) { int status; char fpga_len[10]; uint8_t *buf, *buf_padded, *ver; size_t buf_size, buf_size_padded; int hp_idx = 0; if (strcmp("X", fpga_file) == 0) { printf("Disabling FPGA flash auto-load\n"); return (dev->fn->erase_flash(dev, flash_from_sectors(4), BLADERF_FLASH_SECTOR_SIZE) != BLADERF_FLASH_SECTOR_SIZE); } status = file_read_buffer(fpga_file, &buf, &buf_size); if (status == 0) { if ((getenv("BLADERF_SKIP_FPGA_SIZE_CHECK") == 0) && (buf_size < (1 * 1024 * 1024) || (buf_size > (5 * 1024 * 1024)))) { log_info("Detected potentially invalid firmware file.\n"); log_info("Define BLADERF_SKIP_FPGA_SIZE_CHECK in your evironment " "to skip this check.\n"); status = BLADERF_ERR_INVAL; } else { const size_t page_size = BLADERF_FLASH_SECTOR_SIZE; size_t buf_size_padding = page_size - (buf_size % page_size); /* Pad firmare data out to a flash page size */ buf_size_padded = buf_size + buf_size_padding + page_size; buf_padded = (uint8_t*)realloc(buf, buf_size_padded); if (buf_padded == NULL) { status = BLADERF_ERR_MEM; } else { buf = buf_padded; memset(buf + buf_size, 0xFF, buf_size_padded - buf_size - BLADERF_FLASH_SECTOR_SIZE); memmove(&buf[BLADERF_FLASH_PAGE_SIZE], buf, buf_size_padded - BLADERF_FLASH_SECTOR_SIZE); snprintf(fpga_len, 9, "%d", (int)buf_size); memset(buf, 0xff, BLADERF_FLASH_PAGE_SIZE); encode_field((char *)buf, BLADERF_FLASH_PAGE_SIZE, &hp_idx, "LEN", fpga_len); if (status == 0) { status = dev->fn->erase_flash(dev, flash_from_sectors(4), (uint32_t)buf_size_padded); } if (status >= 0) { status = dev->fn->write_flash(dev, flash_from_pages(1024), buf, (uint32_t)buf_size_padded); } ver = (uint8_t *)malloc(buf_size_padded); if (!ver) status = BLADERF_ERR_MEM; if (status >= 0) { status = dev->fn->read_flash(dev, flash_from_pages(1024), ver, (uint32_t)buf_size_padded); } if ((size_t)status == buf_size_padded) { status = memcmp(buf, ver, buf_size_padded); } free(ver); } } free(buf); } return status; }