Пример #1
0
static int load_fx3(struct cli_state *s, char *file)
{
    char *expanded_path;
    int cmd_status = 0;
    int lib_status;

    if ((expanded_path = interactive_expand_path(file)) == NULL) {
        cli_err(s, "Unable to expand firmware file path: \"%s\"", file);
        cmd_status = CMD_RET_INVPARAM;
    } else {
        printf("Flashing firmware from %s...\n", expanded_path);
        lib_status = bladerf_flash_firmware(s->dev, expanded_path);

        if (lib_status < 0) {
            s->last_lib_error = lib_status;
            cmd_status = CMD_RET_LIBBLADERF;
        } else {
            printf("Done. Cycle power on the device.\n");
        }

        free(expanded_path);
    }

    return cmd_status;
}
Пример #2
0
static int load_fpga(struct cli_state *s, char *file)
{
    char *expanded_path;
    int cmd_status = 0;
    int lib_status;

    if ((expanded_path = interactive_expand_path(file)) == NULL) {
        cli_err(s, "Unable to expand FPGA file path: \"%s\"", file);
        cmd_status = CMD_RET_INVPARAM;
    } else {
        printf("Loading fpga from %s...\n", expanded_path);
        lib_status = bladerf_load_fpga(s->dev, expanded_path);

        if (lib_status < 0) {
            s->last_lib_error = lib_status;
            cmd_status = CMD_RET_LIBBLADERF;
        } else {
            printf("Done.\n");
        }

        free(expanded_path);
    }

    return cmd_status;
}
Пример #3
0
FILE *expand_and_open(const char *filename, const char *mode)
{
    char *expanded_filename;
    FILE *ret;

    expanded_filename = interactive_expand_path(filename);
    if (expanded_filename == NULL) {
        return NULL;
    }

    ret = fopen(expanded_filename, mode);
    free(expanded_filename);
    return ret;
}
Пример #4
0
int parse_cmdline(int argc, char **argv, struct params *p, struct cli_state *s)
{
    int i;
    int status = 0;
    char *sep;

    memset(p, 0, sizeof(*p));
    memset(p->serial, '0', BLADERF_SERIAL_LENGTH - 1);
    p->type = BLADERF_IMAGE_TYPE_INVALID;

    assert(argc >= 2);
    for (i = 1; i < argc && status == 0; i++) {


        /* Check for input file */
        sep = strchr(argv[i], '=');
        if (!sep) {
            if (p->img_file) {
                cli_err(s, argv[0],
                        "Only one image file parameter is permitted.");
                status = CMD_RET_INVPARAM;
            } else {
                p->img_file = interactive_expand_path(argv[i]);
            }
        } else {
            *sep = '\0';
            sep++;
            status = handle_param(argv[i], sep, p, s, argv[0]);
        }
    }

    if (status == 0) {
        if (!p->img_file) {
            cli_err(s, argv[0], "An image file parameter is required.");
            status = CMD_RET_INVPARAM;
        } else if (p->type == BLADERF_IMAGE_TYPE_RAW &&
                !(p->override_address || p->max_length == 0)) {
            cli_err(s, argv[0],
                    "An address and a length are required for type=raw.");
            status = CMD_RET_INVPARAM;
        } else if (argc > 2 && !p->data_file) {
            cli_err(s, argv[0],
                    "A data input file is required when creating an image.");
            status = CMD_RET_INVPARAM;
        }
    }

    return status;
}
Пример #5
0
/* Usage:
 *      recover <devinfo> <FX3 firmware>
 *      recover
 */
int cmd_recover(struct cli_state *state, int argc, char **argv)
{
    int status;
    const char *expanded_path;
    bool ok;
    uint8_t bus, addr;

    if (argc == 1) {
        return list_bootloader_devs(state);
    } else if (argc != 4) {
        return CMD_RET_NARGS;
    }

    bus = str2uint(argv[1], 0, UINT8_MAX, &ok);
    if (!ok) {
        cli_err(state, argv[0], "Invalid bus: %s\n", bus);
        return CMD_RET_INVPARAM;
    }

    addr = str2uint(argv[2], 0, UINT8_MAX, &ok);
    if (!ok) {
        cli_err(state, argv[0], "Invalid address: %s\n", addr);
        return CMD_RET_INVPARAM;
    }

    if ((expanded_path = interactive_expand_path(argv[3])) == NULL) {
        cli_err(state,
                "Unable to expand FX3 firmware file path: \"%s\"", argv[2]);
        return CMD_RET_INVPARAM;
    }

    status = perform_recovery(state, bus, addr, expanded_path);

    if (status == 0) {
        printf("\n");
        printf("Success! Use \"open\" to switch to this device.\n");
        printf("Note that a \"load fx3 <firmware>\" is required to "
               "write the firmware to flash.\n");
        printf("\n");
        return CMD_RET_OK;
    } else if (status == CMD_RET_NODEV) {
        printf("No devices in bootloader mode were found.\n");
        return CMD_RET_OK;
    } else {
        return status;
    }
}
Пример #6
0
static int parse_argv(struct cli_state *state, int argc, char **argv,
                      struct options *opt)
{
    bool ok;

    if (argc != 2 && argc != 4)
        return CMD_RET_NARGS;

    if (argc >= 2) {
        if ((opt->file = interactive_expand_path(argv[1])) == NULL) {
            cli_err(state, argv[0], "Unable to expand file path: \"%s\"",
                    argv[1]);
            return CMD_RET_INVPARAM;
        }

        opt->address = CAL_PAGE;
        opt->len = CAL_BUFFER_SIZE;
        opt->override_defaults = false;
    }

    if (argc == 4) {
        opt->address = str2uint(argv[2], 0, UINT_MAX, &ok);
        if (!ok) {
            cli_err(state, argv[0], "Invalid address provided");
            return CMD_RET_INVPARAM;
        }

        opt->len = str2uint(argv[3], 0, UINT_MAX, &ok);
        if (!ok) {
            cli_err(state, argv[0], "Invalid length provided");
            return CMD_RET_INVPARAM;
        }

        opt->override_defaults = true;
    }

    return 0;
}
Пример #7
0
static int handle_param(const char *param, char *val,
                        struct params *p, struct cli_state *s,
                        const char *argv0)
{
    bool ok;
    int status = 0;

    if (!strcasecmp("data", param)) {
        free(p->data_file);
        p->data_file = interactive_expand_path(val);
    } else if (!strcasecmp("serial", param)) {
        size_t i;
        size_t len = strlen(val);

        if (len != BLADERF_SERIAL_LENGTH - 1) {
            status = CMD_RET_INVPARAM;
        } else {
            for (i = 0; i < len && status == 0; i++) {
                if (val[i] >= 'a' || val[i] <= 'f') {
                    val[i] -= 'a' - 'A';
                } else if (!((val[i] >= '0' && val[i] <= '9') ||
                             (val[i] >= 'A' && val[i] <= 'F'))) {
                    status = CMD_RET_INVPARAM;
                }
            }
        }

        if (status != 0) {
            cli_err(s, argv0, "Serial number must be %d hexadecimal digits.",
                    BLADERF_SERIAL_LENGTH - 1);
        }
    } else if (!strcasecmp("address", param)) {
        p->address = str2uint(param, 0, UINT_MAX, &ok);
        if (!ok) {
            cli_err(s, argv0, "Invalid address provided.");
            status = CMD_RET_INVPARAM;
        }
    } else if (!strcasecmp("type", param)) {
        if (!strcasecmp("raw", val)) {
            p->max_length = UINT_MAX;
            p->type = BLADERF_IMAGE_TYPE_RAW;
        } else if (!strcasecmp("cal", val)) {

            if (!p->override_address) {
                p->address = BLADERF_FLASH_ADDR_CAL;
            }

            p->max_length = BLADERF_FLASH_BYTE_LEN_CAL;
            p->type = BLADERF_IMAGE_TYPE_CALIBRATION;

        } else if (!strcasecmp("fpga40", val) ||
                   !strcasecmp("fpga115", val)) {

            if (!p->override_address) {
                p->address = BLADERF_FLASH_ADDR_FPGA;
            }

            p->max_length = BLADERF_FLASH_BYTE_LEN_FPGA;

            if (!strcasecmp("fpga40", val)) {
                p->type = BLADERF_IMAGE_TYPE_FPGA_40KLE;
            } else {
                p->type = BLADERF_IMAGE_TYPE_FPGA_115KLE;
            }

        } else if (!strcasecmp("fw", val) || !strcasecmp("firmware", val)) {
            if (!p->override_address) {
                p->address = BLADERF_FLASH_ADDR_FIRMWARE;
            }

            p->max_length = BLADERF_FLASH_BYTE_LEN_FIRMWARE;
            p->type = BLADERF_IMAGE_TYPE_FIRMWARE;

        } else {
            cli_err(s, argv0, "Invalid type provided.");
            status = CMD_RET_INVPARAM;
        }
    } else {
        cli_err(s, argv0, "Invalid parameter provided - \"%s\"", param);
        status = CMD_RET_INVPARAM;
    }

    return status;
}