コード例 #1
0
ファイル: flash_init_cal.c プロジェクト: mutteri/bladeRF
int cmd_flash_init_cal(struct cli_state *state, int argc, char **argv)
{
    int rv;
    bool ok;
    uint16_t dac;
    bladerf_fpga_size fpga_size;
    struct bladerf_image *image = NULL;
    uint32_t page, count;

    if(argc != 3 && argc != 4) {
        return CLI_RET_NARGS;
    }

    rv = str2fpga(argv[1], &fpga_size);
    if (rv != 0) {
        cli_err(state, argv[0], "Invalid FPGA provided.\n");
        return rv;
    }

    dac = str2uint(argv[2], 0, 0xffff, &ok);
    if(!ok) {
        cli_err(state, argv[0], "Invalid VCTCXO trim value provided.\n");
        return CLI_RET_INVPARAM;
    }

    image = bladerf_alloc_cal_image(fpga_size, dac);
    if (!image) {
        return CLI_RET_MEM;
    }

    if (argc == 3) {
        rv = flash_check_state(state, argv[0]);
        if (rv != 0) {
            goto cmd_flash_init_cal_out;
        }

        rv = bladerf_erase_flash(state->dev, BLADERF_FLASH_EB_CAL,
                                 BLADERF_FLASH_EB_LEN_CAL);
        if (rv != 0) {
            goto cmd_flash_init_cal_out;
        }

        page = BLADERF_FLASH_TO_PAGES(image->address);
        count = BLADERF_FLASH_TO_PAGES(image->length);

        rv = bladerf_write_flash(state->dev, image->data, page, count);
        if(rv < 0) {
            cli_err(state, argv[0],
            "Failed to write calibration data.\n"
            "\n"
            "    This may have resulted in a corrupted flash. If the device fails to\n"
            "    boot at the next power cycle, re-flash the firmware.\n"
            "\n"
            "    See the following page for more information:\n"
            "      https://github.com/Nuand/bladeRF/wiki/Upgrading-bladeRF-firmware\n"
            );

            state->last_lib_error = rv;
            rv = CLI_RET_LIBBLADERF;
        } else {
            rv = 0;
        }
    } else {
        char *filename;
        assert(argc == 4);

        filename = input_expand_path(argv[3]);
        rv = bladerf_image_write(image, filename);
        free(filename);
    }

cmd_flash_init_cal_out:
    bladerf_free_image(image);
    return rv;
}
コード例 #2
0
ファイル: activity.cpp プロジェクト: Zhanyin/taomee
uint32_t activity_get_activity_reward_ex(i_mysql_iface *p_mysql_conn, c_memcached *p_memcached, uint32_t user_id, uint32_t msg_type, char *p_request_body, uint32_t request_len)
{
    if(check_val_len(msg_type, request_len, sizeof(db_request_get_activity_reward_ex_t)) != 0)
    {
        return ERR_MSG_LEN;
    }

	db_request_get_activity_reward_ex_t *req = (db_request_get_activity_reward_ex_t *)p_request_body;
	db_return_get_activity_reward_ex_t  res = {0};

	uint8_t status = NOT_FINISH;
    MYSQL_ROW row = NULL;
	MYSQL *p_conn = NULL;

	sprintf(g_sql_str, "SELECT flag FROM db_monster_%d.t_activity_%d WHERE user_id = %u AND activity_id = %u AND reward_id = %u limit 1;", DB_ID(user_id), TABLE_ID(user_id),  user_id, req->activity_id, req->level_id);
	int result_count = p_mysql_conn->select_first_row(&row, g_sql_str);
    if(result_count <= 0)
    {
        KCRIT_LOG(user_id, "sql exev failed(%s)", g_sql_str);
        return ERR_SQL_ERR;
    }
    if(row != NULL)
    {
        if(str2uint(&status, row[0]) != 0)
        {
            KCRIT_LOG(user_id, "convert status %s to uint8 failed(%s).", row[0], g_sql_str);
            return ERR_SQL_ERR;
        }
		if(status != FINISH_HAVENOT_REWARD)
		{
			KCRIT_LOG(user_id, "this activity level have rewarded or not finished");
			return ERR_SQL_ERR;
		}

		if(req->reward_id == TASK_REWARD_COIN)
		{
			sprintf(g_sql_str, "UPDATE db_monster_%d.t_role_%d set coins = coins + %u where user_id = %u;", DB_ID(user_id), TABLE_ID(user_id), req->reward_num, user_id);
		}
		else if(req->reward_id == TASK_REWARD_EXP)
		{
		}
		else if(req->reward_id >= ITEM_ID_BEGIN && req->reward_id <= ITEM_ID_END)
		{
			sprintf(g_sql_str, "INSERT INTO db_monster_%d.t_stuff_%d(user_id, stuff_id, stuff_num) values(%u, %u, %u) ON DUPLICATE KEY UPDATE stuff_num = stuff_num + %u;", DB_ID(user_id), TABLE_ID(user_id), user_id, req->reward_id, req->reward_num, req->reward_num);
		}
		else if(req->reward_id >= PET_ID_BEGIN && req->reward_id <= PET_ID_END)
		{
			sprintf(g_sql_str, "INSERT INTO db_monster_%d.t_pet_%d(user_id, pet_id, total_num, follow_num) values(%u, %u, 1, 1) ON DUPLICATE KEY UPDATE total_num = total_num + 1, follow_num = follow_num + 1;", DB_ID(user_id), TABLE_ID(user_id), user_id, req->reward_id);
		}
		else if(req->reward_id == TASK_REWARD_NONE)
		{
			goto end;
		}
		else
		{
			KCRIT_LOG(user_id, "reward id error.");
			return ERR_ITEM_NOT_EXIST;
		}

		p_conn = p_mysql_conn->get_conn();
		if (NULL == p_conn)
		{
			 KCRIT_LOG(user_id, "get mysql conn failed.");
			 return ERR_SQL_ERR;
		}

		if (mysql_autocommit(p_conn, false) != 0)
		{
			 KCRIT_LOG(user_id, "close mysql_autocommit failed.");
			 return ERR_SQL_ERR;
		}

		if (p_mysql_conn->execsql(g_sql_str) < 0)
		{
			 KCRIT_LOG(user_id, "[msg:%u]sql exec failed(%s).", msg_type, p_mysql_conn->get_last_errstr());
			 mysql_rollback(p_conn);
			 mysql_autocommit(p_conn, true);
			 return ERR_SQL_ERR;
		}

		sprintf(g_sql_str, "UPDATE db_monster_%d.t_activity_%d SET flag = %u WHERE user_id = %u AND activity_id = %u AND reward_id = %u;", DB_ID(user_id), TABLE_ID(user_id), FINISH_HAVE_REWARD, user_id, req->activity_id, req->level_id);
		if (p_mysql_conn->execsql(g_sql_str) < 0)
		{
			 KCRIT_LOG(user_id, "[msg:%u]sql exec failed(%s).", msg_type, p_mysql_conn->get_last_errstr());
			 mysql_rollback(p_conn);
			 mysql_autocommit(p_conn, true);
			 return ERR_SQL_ERR;
		}

		if (mysql_commit(p_conn) != 0)
	    {
	        KCRIT_LOG(user_id, "mysql_commit() failed.");
	        mysql_rollback(p_conn);
	        mysql_autocommit(p_conn, true);
	        return ERR_SQL_ERR;
	    }

	    mysql_autocommit(p_conn, true);

        if(req->reward_id == TASK_REWARD_COIN)
        {
            role_t cached_role = {{0}};
            if(p_memcached->get_role(user_id, &cached_role) == 0)
            {
                cached_role.coins += req->reward_num;
                p_memcached->set_role(user_id, &cached_role);
            }
        }
    }

end:
	res.reward_id = req->reward_id;
	res.reward_num = req->reward_num;
	g_pack.pack((char *)&res, sizeof(res));
	return 0;
}
コード例 #3
0
ファイル: printset_xb.c プロジェクト: wxh0000mm/bladeRF
int set_xbio_base(struct cli_state *state, int argc, char **argv, bool is_dir)
{
    int rv   = CLI_RET_OK;
    int *err = &state->last_lib_error;

    const struct xb_gpio_lut *pin;
    const char *suffix = is_dir ? "_dir" : "";

    uint32_t val;
    bool ok;

    switch (argc) {
        case 0:
        case 1:
        case 2:
            printf("Usage: set xb_gpio%s <name> <value>\n\n", suffix);

            printf("<name> describes what to set. It may be one of the "
                   "following:\n\n");

            printf("  \"reg\" or \"register\" - Set the entire GPIO%s register "
                   "value.\n\n",
                   is_dir ? " direction" : "");

            printf("  One of the pin names listed by: \"print xb_gpio%s "
                   "list\"\n\n",
                   suffix);

            if (is_dir) {
                printf("Specify <value> as \"input\" (0) or \"output\" (1).\n");
            }

            break;

        case 4:
            /* Specifying a pin by name */
            rv = str2xbgpio(state, argv[2], &pin, false);
            if (rv == 0) {
                val = UINT32_MAX;

                if (is_dir) {
                    if (!strcasecmp(argv[3], "in") ||
                        !strcasecmp(argv[3], "input")) {
                        val = 0;
                    } else if (!strcasecmp(argv[3], "out") ||
                               !strcasecmp(argv[3], "output")) {
                        val = 1;
                    }
                }

                if (!is_dir || val == UINT32_MAX) {
                    val = str2uint(argv[3], 0, 1, &ok);
                    if (!ok) {
                        cli_err_nnl(state, "Invalid pin value", "%s\n",
                                    argv[3]);
                        return CLI_RET_INVPARAM;
                    }
                }

                if (val == 1) {
                    val = pin->bitmask;
                }

                if (is_dir) {
                    rv = bladerf_expansion_gpio_dir_masked_write(
                        state->dev, pin->bitmask, val);
                } else {
                    rv = bladerf_expansion_gpio_masked_write(state->dev,
                                                             pin->bitmask, val);
                }

                if (rv < 0) {
                    *err = rv;
                    rv   = CLI_RET_LIBBLADERF;
                } else {
                    rv = print_xbio_base(state, 3, argv, is_dir);
                }
            } else if (!strcasecmp("reg", argv[2]) ||
                       !strcasecmp("register", argv[2])) {
                /* Specifying a write of the entire XB GPIO (Direction) reg */
                val = str2uint(argv[3], 0, UINT32_MAX, &ok);
                if (!ok) {
                    cli_err_nnl(state, "Invalid register value", "%s\n",
                                argv[3]);
                    return CLI_RET_INVPARAM;
                }

                if (is_dir) {
                    rv = bladerf_expansion_gpio_dir_write(state->dev, val);
                } else {
                    rv = bladerf_expansion_gpio_write(state->dev, val);
                }

                if (rv < 0) {
                    *err = rv;
                    rv   = CLI_RET_LIBBLADERF;
                } else {
                    rv = print_xbio_base(state, 3, argv, is_dir);
                }

            } else {
                cli_err_nnl(state, "Invalid pin name or option", "%s\n",
                            argv[2]);
                rv = CLI_RET_INVPARAM;
            }
            break;


        default:
            rv = CLI_RET_NARGS;
    }

    return rv;
}
コード例 #4
0
ファイル: activity.cpp プロジェクト: Zhanyin/taomee
uint32_t activity_get_invite_info(i_mysql_iface *p_mysql_conn, c_memcached *p_memcached, uint32_t user_id, uint32_t msg_type, char *p_request_body, uint32_t request_len)
{
    if(check_val_len(msg_type, request_len, sizeof(uint32_t)) != 0)
    {
        return ERR_MSG_LEN;
    }

	uint32_t activity_id = (uint32_t)(*p_request_body);
	db_return_get_invite_info_t res = {0};
	uint16_t idx = 0;
	uint8_t status = NOT_FINISH;
    MYSQL_ROW row = NULL;
	MYSQL *p_conn = p_mysql_conn->get_conn();

	if (NULL == p_conn)
	{
		 KCRIT_LOG(user_id, "get mysql conn failed.");
		 return ERR_SQL_ERR;
	}
	if (mysql_autocommit(p_conn, false) != 0)
	{
		 KCRIT_LOG(user_id, "close mysql_autocommit failed.");
		 return ERR_SQL_ERR;
	}

	sprintf(g_sql_str, "SELECT invite_num, qualified_num FROM db_monster_%d.t_invite_%d WHERE user_id = %u;", DB_ID(user_id), TABLE_ID(user_id),  user_id);
	int result_count = p_mysql_conn->select_first_row(&row, g_sql_str);
    if(result_count < 0)
    {
        KCRIT_LOG(user_id, "sql exev failed(%s)", g_sql_str);
        return ERR_SQL_ERR;
    }
    if(row != NULL)
    {
        if(str2uint(&res.invite_num, row[0]) != 0)
        {
            KCRIT_LOG(user_id, "convert invite_num %s to uint16 failed(%s).", row[0], g_sql_str);
            return ERR_SQL_ERR;
        }

        if(str2uint(&res.qualified_num, row[1]) != 0)
        {
            KCRIT_LOG(user_id, "convert qualified_num %s to uint16 failed(%s).", row[1], g_sql_str);
            return ERR_SQL_ERR;
        }
    }

	sprintf(g_sql_str, "SELECT flag FROM db_monster_%d.t_activity_%d WHERE user_id = %u AND activity_id = %u ORDER BY reward_id;", DB_ID(user_id), TABLE_ID(user_id),  user_id, activity_id);
	result_count = p_mysql_conn->select_first_row(&row, g_sql_str);
	if(result_count < 0)
	{
		 KCRIT_LOG(user_id, "sql exev failed(%s)", g_sql_str);
		 return ERR_SQL_ERR;
	}

	for(idx=0; idx<3; idx++, row = p_mysql_conn->select_next_row(true))
	{
		switch(idx+1)
		{
			case 1:
				status = res.invite_num >= 1?FINISH_HAVENOT_REWARD:NOT_FINISH;
				break;
			case 2:
				status = res.invite_num >= 3?FINISH_HAVENOT_REWARD:NOT_FINISH;
				break;
			case 3:
				status = res.qualified_num >= 3 && res.invite_num >= 5?FINISH_HAVENOT_REWARD:NOT_FINISH;
				break;
		}

		if(row)
		{
			if(str2uint(&res.level_status[idx], row[0]) != 0)
			{
				 KCRIT_LOG(user_id, "convert level_status %s to uint8 failed(%s).", row[0], g_sql_str);
				 return ERR_SQL_ERR;
			}
			if(res.level_status[idx] != NOT_FINISH || status == NOT_FINISH)
			{
				continue;
			}
		}
		res.level_status[idx] = status;

		sprintf(g_sql_str, "INSERT INTO db_monster_%d.t_activity_%d(user_id, activity_id, reward_id, flag) VALUES(%u, %u, %u, %u) ON DUPLICATE KEY UPDATE flag = %u;", DB_ID(user_id), TABLE_ID(user_id),  user_id, activity_id, idx+1, status, status);
		if (p_mysql_conn->execsql(g_sql_str) < 0)
		{
			 KCRIT_LOG(user_id, "[msg:%u]sql exec failed(%s).", msg_type, p_mysql_conn->get_last_errstr());
			 mysql_rollback(p_conn);
			 mysql_autocommit(p_conn, true);
			 return ERR_SQL_ERR;
		}
	}

    if (mysql_commit(p_conn) != 0)
    {
        KCRIT_LOG(user_id, "mysql_commit() failed.");
        mysql_rollback(p_conn);
        mysql_autocommit(p_conn, true);
        return ERR_SQL_ERR;
    }

    mysql_autocommit(p_conn, true);

	g_pack.pack((char *)&res, sizeof(res));
    return 0;
}
コード例 #5
0
ファイル: poke.c プロジェクト: Konstanty/bladeRF
int cmd_poke(struct cli_state *state, int argc, char **argv)
{
    /* Valid commands:
        poke dac <address> <value>
        poke lms <address> <value>
        poke si  <address> <value>
    */
    int rv = CLI_RET_OK;
    int status;
    bool ok;
    int (*f)(struct bladerf *, uint8_t, uint8_t);
    unsigned int address, value;

    if (!cli_device_is_opened(state)) {
        return CLI_RET_NODEV;
    }

    if( argc == 4 ) {

        /* Parse the value */
        if( argc == 4 ) {
            value = str2uint( argv[3], 0, MAX_VALUE, &ok );
            if( !ok ) {
                cli_err(state, argv[0],
                        "Invalid number of addresses provided (%s)", argv[3]);
                return CLI_RET_INVPARAM;
            }
        }

        /* Are we reading from the DAC? */
        if( strcasecmp( argv[1], "dac" ) == 0 ) {
            /* Parse address */
            address = str2uint( argv[2], 0, DAC_MAX_ADDRESS, &ok );
            if( !ok ) {
                invalid_address(state, argv[0], argv[2]);
                rv = CLI_RET_INVPARAM;
            } else {
                /* TODO: Point function pointer */
                /* f = vctcxo_dac_write */
                f = NULL;
            }
        }

        /* Are we reading from the LMS6002D */
        else if( strcasecmp( argv[1], "lms" ) == 0 ) {
            /* Parse address */
            address = str2uint( argv[2], 0, LMS_MAX_ADDRESS, &ok );
            if( !ok ) {
                invalid_address(state, argv[0], argv[2]);
                rv = CLI_RET_INVPARAM;
            } else {
                f = bladerf_lms_write;
            }
        }

        /* Are we reading from the Si5338? */
        else if( strcasecmp( argv[1], "si" ) == 0 ) {
            /* Parse address */
            address = str2uint( argv[2], 0, SI_MAX_ADDRESS, &ok );
            if( !ok ) {
                invalid_address(state, argv[0], argv[2]);
                rv = CLI_RET_INVPARAM;
            } else {
                f = bladerf_si5338_write;
            }
        }

        /* I guess we aren't reading from anything :( */
        else {
            cli_err(state, argv[0], "%s is not a pokeable device\n", argv[1] );
            rv = CLI_RET_INVPARAM;
        }

        /* Write the value to the address */
        if( rv == CLI_RET_OK && f ) {
            status = f( state->dev, (uint8_t)address, (uint8_t)value );
            if (status < 0) {
                state->last_lib_error = status;
                rv = CLI_RET_LIBBLADERF;
            } else {
                printf( "  0x%2.2x: 0x%2.2x\n", address, value );
                if (f == bladerf_lms_write) {
                    uint8_t readback;
                    int status = bladerf_lms_read(state->dev,
                                                  (uint8_t)address,
                                                  &readback);
                    if (status == 0) {
                        lms_reg_info(address, readback);
                        putchar('\n'); /* To be consistent with peek output */
                    }
                }
            }
        }

    } else {
        cli_err(state, argv[0], "Invalid number of arguments (%d)\n", argc);
        rv = CLI_RET_INVPARAM;
    }
    return rv;
}
コード例 #6
0
ファイル: flash_init_cal.c プロジェクト: 13572293130/bladeRF
int cmd_flash_init_cal(struct cli_state *state, int argc, char **argv)
{
    int rv;
    bool ok;
    uint16_t dac;
    bladerf_fpga_size fpga_size;
    struct bladerf_image *image = NULL;

    if(argc != 3 && argc != 4) {
        return CMD_RET_NARGS;
    }

    rv = str2fpga(argv[1], &fpga_size);
    if (rv != 0) {
        cli_err(state, argv[0], "Invalid FPGA provided.");
        return rv;
    }

    dac = str2uint(argv[2], 0, 0xffff, &ok);
    if(!ok) {
        cli_err(state, argv[0], "Invalid VCTCXO trim value provided.");
        return CMD_RET_INVPARAM;
    }

    image = bladerf_alloc_cal_image(fpga_size, dac);
    if (!image) {
        return CMD_RET_MEM;
    }

    if (argc == 3) {

        if (!cli_device_is_opened(state)) {
            rv = CMD_RET_NODEV;
            goto cmd_flash_init_cal_out;
        }

        rv = bladerf_program_flash_unaligned(state->dev, image->address,
                                             image->data, image->length);
        if(rv < 0) {
            cli_err(state, argv[0],
            "Failed to write calibration data.\n"
            "\n"
            "This may have resulted in a corrupted flash. If the device fails to\n"
            "boot at the next power cycle, re-flash the firmware.\n"
            "\n"
            "See the following page for more information:\n"
            "  https://github.com/Nuand/bladeRF/wiki/Upgrading-bladeRF-firmware\n"
            );

            state->last_lib_error = rv;
            rv = CMD_RET_LIBBLADERF;
        } else {
            rv = 0;
        }
    } else {
        assert(argc == 4);
        rv = bladerf_image_write(image, argv[3]);
    }

cmd_flash_init_cal_out:
    bladerf_free_image(image);
    return rv;
}
コード例 #7
0
ファイル: viewscript.c プロジェクト: trailofbits/cb-multios
static int handleget() {
    var_t *var = NULL;
    viewvar_t *view = NULL;
    char out[12] = {0};
    char *name = strtok(NULL, ' ');
    char *arg = NULL;
    char *subarg = NULL;
    uint32_t val = 0;
    uint32_t mask = 0;
    int idx = 0;
    int i = 0;

    if (!name || cgc_strlen(name) == 0)
        return ERRNONAME;

    if (!(var = getvar(name)))
        return ERRNOSUCHVAR;

    switch (var->type) {
        case NUMTYPE:
            int2str(out, sizeof(out), ((numvar_t*)var)->num);
            break;

        case ARRTYPE:
            if (!(arg = strtok(NULL, ' ')))
                return ERRNOARG;
            
            idx = str2uint(arg);

            if (idx >= ((arrvar_t*)var)->size/sizeof(int))
                return ERRINVALIDIDX;

            int2str(out, sizeof(out), ((int*)((arrvar_t*)var)->arr)[idx]);
            break;

        case VIEWTYPE:
            view = (viewvar_t*)var;

            if (!(arg = strtok(NULL, ' ')))
                return ERRNOARG;
            
            idx = str2uint(arg)*view->view->size;

            if (view->bytesize) {
                #ifndef PATCHED
                if (!(idx+view->view->size < calc_bytesize(view->bytesize)))
                #else
                if (!(idx+view->view->size < calc_bytesize(view->bytesize) && calc_bytesize(view->bytesize) < view->arr->size))
                #endif
                    return ERRINVALIDIDX;
            } else if (idx+view->view->size > view->arr->size) {
                return ERRINVALIDIDX;
            }

            for(i=0; i < view->view->size; i++) 
                val |=  view->arr->arr[i+idx] << (i*8);

            if (view->view->sign) {
                if (view->view->size == sizeof(int))
                    int2str(out, sizeof(out), val);
                else if (view->view->size == sizeof(short))
                    int2str(out, sizeof(out), (short)val);
                else if (view->view->size == sizeof(char))
                    int2str(out, sizeof(out), (char)val);
            } else {
                uint2str(out, sizeof(out), val);
            }

            break;
        default:
            return ERRNOSUCHTYPE;
    }

    SSENDL(cgc_strlen(out),out);
    return 0;
}
コード例 #8
0
ファイル: ipmi_main.c プロジェクト: elitak/ipmitool
/* ipmi_parse_options  -  helper function to handle parsing command line options
 *
 * @argc:	count of options
 * @argv:	list of options
 * @cmdlist:	list of supported commands
 * @intflist:	list of supported interfaces
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_main(int argc, char ** argv,
		struct ipmi_cmd * cmdlist,
		struct ipmi_intf_support * intflist)
{
	struct ipmi_intf_support * sup;
	int privlvl = 0;
	uint8_t target_addr = 0;
	uint8_t target_channel = 0;

	uint8_t transit_addr = 0;
	uint8_t transit_channel = 0;
	uint8_t target_lun     = 0;
	uint8_t arg_addr = 0, addr;
	uint16_t my_long_packet_size=0;
	uint8_t my_long_packet_set=0;
	uint8_t lookupbit = 0x10;	/* use name-only lookup by default */
	int retry = 0;
	uint32_t timeout = 0;
	int authtype = -1;
	char * tmp_pass = NULL;
	char * tmp_env = NULL;
	char * hostname = NULL;
	char * username = NULL;
	char * password = NULL;
	char * intfname = NULL;
	char * progname = NULL;
	char * oemtype  = NULL;
	char * sdrcache = NULL;
	unsigned char * kgkey = NULL;
	char * seloem   = NULL;
	int port = 0;
	int devnum = 0;
	int cipher_suite_id = 3; /* See table 22-19 of the IPMIv2 spec */
	int argflag, i, found;
	int rc = -1;
	char sol_escape_char = SOL_ESCAPE_CHARACTER_DEFAULT;
	char * devfile  = NULL;

	/* save program name */
	progname = strrchr(argv[0], '/');
	progname = ((progname == NULL) ? argv[0] : progname+1);
	signal(SIGINT, ipmi_catch_sigint);

	while ((argflag = getopt(argc, (char **)argv, OPTION_STRING)) != -1)
	{
		switch (argflag) {
		case 'I':
			if (intfname) {
				free(intfname);
				intfname = NULL;
			}
			intfname = strdup(optarg);
			if (intfname == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			if (intflist != NULL) {
				found = 0;
				for (sup=intflist; sup->name != NULL; sup++) {
					if (strncmp(sup->name, intfname, strlen(intfname)) == 0 &&
							strncmp(sup->name, intfname, strlen(sup->name)) == 0 &&
							sup->supported == 1)
						found = 1;
				}
				if (!found) {
					lprintf(LOG_ERR, "Interface %s not supported", intfname);
					goto out_free;
				}
			}
			break;
		case 'h':
			ipmi_option_usage(progname, cmdlist, intflist);
			rc = 0;
			goto out_free;
			break;
		case 'V':
			printf("%s version %s\n", progname, VERSION);
			rc = 0;
			goto out_free;
			break;
		case 'd':
			if (str2int(optarg, &devnum) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-d'.");
				rc = -1;
				goto out_free;
			}
			/* Check if device number is -gt 0; I couldn't find limit for
			 * kernels > 2.6, thus right side is unlimited.
			 */
			if (devnum < 0) {
				lprintf(LOG_ERR, "Device number %i is out of range.", devnum);
				rc = -1;
				goto out_free;
			}
			break;
		case 'p':
			if (str2int(optarg, &port) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-p'.");
				rc = -1;
				goto out_free;
			}
			/* Check if port is -gt 0 && port is -lt 65535 */
			if (port < 0 || port > 65535) {
				lprintf(LOG_ERR, "Port number %i is out of range.", port);
				rc = -1;
				goto out_free;
			}
			break;
		case 'C':
			if (str2int(optarg, &cipher_suite_id) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-C'.");
				rc = -1;
				goto out_free;
			}
			/* add check Cipher is -gt 0 */
			if (cipher_suite_id < 0) {
				lprintf(LOG_ERR, "Cipher suite ID %i is invalid.", cipher_suite_id);
				rc = -1;
				goto out_free;
			}
			break;
		case 'v':
			verbose++;
			break;
		case 'c':
			csv_output = 1;
			break;
		case 'H':
			if (hostname) {
				free(hostname);
				hostname = NULL;
			}
			hostname = strdup(optarg);
			if (hostname == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'f':
			if (password) {
				free(password);
				password = NULL;
			}
			password = ipmi_password_file_read(optarg);
			if (password == NULL)
				lprintf(LOG_ERR, "Unable to read password "
						"from file %s", optarg);
			break;
		case 'a':
#ifdef HAVE_GETPASSPHRASE
			tmp_pass = getpassphrase("Password: "******"Password: "******"%s: malloc failure", progname);
					goto out_free;
				}
			}
			break;
		case 'k':
			if (kgkey) {
				free(kgkey);
				kgkey = NULL;
			}
			kgkey = strdup(optarg);
			if (kgkey == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'K':
			if ((tmp_env = getenv("IPMI_KGKEY"))) {
				if (kgkey) {
					free(kgkey);
					kgkey = NULL;
				}
				kgkey = strdup(tmp_env);
				if (kgkey == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			} else {
				lprintf(LOG_WARN, "Unable to read kgkey from environment");
			}
			break;
		case 'y':
			if (kgkey) {
				free(kgkey);
				kgkey = NULL;
			}
			kgkey = ipmi_parse_hex(optarg);
			if (kgkey == NULL) {
				goto out_free;
			}
			break;
		case 'Y':
#ifdef HAVE_GETPASSPHRASE
			tmp_pass = getpassphrase("Key: ");
#else
			tmp_pass = getpass("Key: ");
#endif
			if (tmp_pass != NULL) {
				if (kgkey) {
					free(kgkey);
					kgkey = NULL;
				}
				kgkey = strdup(tmp_pass);
				tmp_pass = NULL;
				if (kgkey == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			}
			break;
		case 'U':
			if (username) {
				free(username);
				username = NULL;
			}
			if (strlen(optarg) > 16) {
				lprintf(LOG_ERR, "Username is too long (> 16 bytes)");
				goto out_free;
			}
			username = strdup(optarg);
			if (username == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'S':
			if (sdrcache) {
				free(sdrcache);
				sdrcache = NULL;
			}
			sdrcache = strdup(optarg);
			if (sdrcache == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'D':
			/* check for subsequent instance of -D */
			if (devfile) {
				/* free memory for previous string */
				free(devfile);
			}
			devfile = strdup(optarg);
			if (devfile == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
#ifdef ENABLE_ALL_OPTIONS
		case 'o':
			if (oemtype) {
				free(oemtype);
				oemtype = NULL;
			}
			oemtype = strdup(optarg);
			if (oemtype == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			if (strncmp(oemtype, "list", 4) == 0 ||
					strncmp(oemtype, "help", 4) == 0) {
				ipmi_oem_print();
				rc = 0;
				goto out_free;
			}
			break;
		case 'g':
			/* backwards compatible oem hack */
			if (oemtype) {
				free(oemtype);
				oemtype = NULL;
			}
			oemtype = strdup("intelwv2");
			break;
		case 's':
			/* backwards compatible oem hack */
			if (oemtype) {
				free(oemtype);
				oemtype = NULL;
			}
			oemtype = strdup("supermicro");
			break;
		case 'P':
			if (password) {
				free(password);
				password = NULL;
			}
			password = strdup(optarg);
			if (password == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}

			/* Prevent password snooping with ps */
			i = strlen(optarg);
			memset(optarg, 'X', i);
			break;
		case 'E':
			if ((tmp_env = getenv("IPMITOOL_PASSWORD"))) {
				if (password) {
					free(password);
					password = NULL;
				}
				password = strdup(tmp_env);
				if (password == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			}
			else if ((tmp_env = getenv("IPMI_PASSWORD"))) {
				if (password) {
					free(password);
					password = NULL;
				}
				password = strdup(tmp_env);
				if (password == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			}
			else {
				lprintf(LOG_WARN, "Unable to read password from environment");
			}
			break;
		case 'L':
			i = strlen(optarg);
			if ((i > 0) && (optarg[i-1] == '+')) {
				lookupbit = 0;
				optarg[i-1] = 0;
			}
			privlvl = str2val(optarg, ipmi_privlvl_vals);
			if (privlvl == 0xFF) {
				lprintf(LOG_WARN, "Invalid privilege level %s", optarg);
			}
			break;
		case 'A':
			authtype = str2val(optarg, ipmi_authtype_session_vals);
			break;
		case 't':
			if (str2uchar(optarg, &target_addr) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-t'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'b':
			if (str2uchar(optarg, &target_channel) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-b'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'T':
			if (str2uchar(optarg, &transit_addr) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-T'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'B':
			if (str2uchar(optarg, &transit_channel) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-B'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'l':
			if (str2uchar(optarg, &target_lun) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-l'.");
				rc = 1;
				goto out_free;
			}
			break;
		case 'm':
			if (str2uchar(optarg, &arg_addr) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-m'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'e':
			sol_escape_char = optarg[0];
			break;
		case 'O':
			if (seloem) {
				free(seloem);
				seloem = NULL;
			}
			seloem = strdup(optarg);
			if (seloem == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'z':
			if (str2ushort(optarg, &my_long_packet_size) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-z'.");
				rc = -1;
				goto out_free;
			}
			break;
		/* Retry and Timeout */
		case 'R':
			if (str2int(optarg, &retry) != 0 || retry < 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-R'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'N':
			if (str2uint(optarg, &timeout) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-N'.");
				rc = -1;
				goto out_free;
			}
			break;
#endif
		default:
			ipmi_option_usage(progname, cmdlist, intflist);
			goto out_free;
		}
	}

	/* check for command before doing anything */
	if (argc-optind > 0 &&
			strncmp(argv[optind], "help", 4) == 0) {
		ipmi_cmd_print(cmdlist);
		rc = 0;
		goto out_free;
	}

	/*
	 * If the user has specified a hostname (-H option)
	 * then this is a remote access session.
	 *
	 * If no password was specified by any other method
	 * and the authtype was not explicitly set to NONE
	 * then prompt the user.
	 */
	if (hostname != NULL && password == NULL &&
			(authtype != IPMI_SESSION_AUTHTYPE_NONE || authtype < 0)) {
#ifdef HAVE_GETPASSPHRASE
		tmp_pass = getpassphrase("Password: "******"Password: "******"%s: malloc failure", progname);
				goto out_free;
			}
		}
	}

	/* if no interface was specified but a
	 * hostname was then use LAN by default
	 * otherwise the default is hardcoded
	 * to use the first entry in the list
	 */
	if (intfname == NULL && hostname != NULL) {
		intfname = strdup("lan");
		if (intfname == NULL) {
			lprintf(LOG_ERR, "%s: malloc failure", progname);
			goto out_free;
		}
	}

	if (password != NULL && intfname != NULL) {
		if (strcmp(intfname, "lan") == 0 && strlen(password) > 16) {
			lprintf(LOG_ERR, "%s: password is longer than 16 bytes.", intfname);
			rc = -1;
			goto out_free;
		} else if (strcmp(intfname, "lanplus") == 0 && strlen(password) > 20) {
			lprintf(LOG_ERR, "%s: password is longer than 20 bytes.", intfname);
			rc = -1;
			goto out_free;
		}
	} /* if (password != NULL && intfname != NULL) */

	/* load interface */
	ipmi_main_intf = ipmi_intf_load(intfname);
	if (ipmi_main_intf == NULL) {
		lprintf(LOG_ERR, "Error loading interface %s", intfname);
		goto out_free;
	}

	/* setup log */
	log_init(progname, 0, verbose);

	/* run OEM setup if found */
	if (oemtype != NULL &&
	    ipmi_oem_setup(ipmi_main_intf, oemtype) < 0) {
		lprintf(LOG_ERR, "OEM setup for \"%s\" failed", oemtype);
		goto out_free;
	}

	/* set session variables */
	if (hostname != NULL)
		ipmi_intf_session_set_hostname(ipmi_main_intf, hostname);
	if (username != NULL)
		ipmi_intf_session_set_username(ipmi_main_intf, username);
	if (password != NULL)
		ipmi_intf_session_set_password(ipmi_main_intf, password);
	if (kgkey != NULL)
		ipmi_intf_session_set_kgkey(ipmi_main_intf, kgkey);
	if (port > 0)
		ipmi_intf_session_set_port(ipmi_main_intf, port);
	if (authtype >= 0)
		ipmi_intf_session_set_authtype(ipmi_main_intf, (uint8_t)authtype);
	if (privlvl > 0)
		ipmi_intf_session_set_privlvl(ipmi_main_intf, (uint8_t)privlvl);
	else
		ipmi_intf_session_set_privlvl(ipmi_main_intf,
				IPMI_SESSION_PRIV_ADMIN);	/* default */
	/* Adding retry and timeout for interface that support it */
	if (retry > 0)
		ipmi_intf_session_set_retry(ipmi_main_intf, retry);
	if (timeout > 0)
		ipmi_intf_session_set_timeout(ipmi_main_intf, timeout);

	ipmi_intf_session_set_lookupbit(ipmi_main_intf, lookupbit);
	ipmi_intf_session_set_sol_escape_char(ipmi_main_intf, sol_escape_char);
	ipmi_intf_session_set_cipher_suite_id(ipmi_main_intf, cipher_suite_id);

	ipmi_main_intf->devnum = devnum;

	/* setup device file if given */
	ipmi_main_intf->devfile = devfile;

	/* Open the interface with the specified or default IPMB address */
	ipmi_main_intf->my_addr = arg_addr ? arg_addr : IPMI_BMC_SLAVE_ADDR;
	if (ipmi_main_intf->open != NULL)
		ipmi_main_intf->open(ipmi_main_intf);

	/*
	 * Attempt picmg discovery of the actual interface address unless
	 * the users specified an address.
	 *	Address specification always overrides discovery
	 */
	if (picmg_discover(ipmi_main_intf) && !arg_addr) {
		lprintf(LOG_DEBUG, "Running PICMG Get Address Info");
		addr = ipmi_picmg_ipmb_address(ipmi_main_intf);
		lprintf(LOG_INFO,  "Discovered IPMB-0 address 0x%x", addr);
	}

	/*
	 * If we discovered the ipmb address and it is not the same as what we
	 * used for open, Set the discovered IPMB address as my address if the
	 * interface supports it.
	 */
	if (addr != 0 && addr != ipmi_main_intf->my_addr &&
						ipmi_main_intf->set_my_addr) {
		/*
		 * Only set the interface address on interfaces which support
		 * it
		 */
		(void) ipmi_main_intf->set_my_addr(ipmi_main_intf, addr);
	}

	/* If bridging addresses are specified, handle them */
	if (transit_addr > 0 || target_addr > 0) {
		/* sanity check, transit makes no sense without a target */
		if ((transit_addr != 0 || transit_channel != 0) &&
			target_addr == 0) {
			lprintf(LOG_ERR,
				"Transit address/channel %#x/%#x ignored. "
				"Target address must be specified!",
				transit_addr, transit_channel);
			goto out_free;
		}
		ipmi_main_intf->target_addr = target_addr;
		ipmi_main_intf->target_lun = target_lun ;
		ipmi_main_intf->target_channel = target_channel ;

		ipmi_main_intf->transit_addr    = transit_addr;
		ipmi_main_intf->transit_channel = transit_channel;


		/* must be admin level to do this over lan */
		ipmi_intf_session_set_privlvl(ipmi_main_intf, IPMI_SESSION_PRIV_ADMIN);
		/* Get the ipmb address of the targeted entity */
		ipmi_main_intf->target_ipmb_addr =
					ipmi_picmg_ipmb_address(ipmi_main_intf);
		lprintf(LOG_DEBUG, "Specified addressing     Target  %#x:%#x Transit %#x:%#x",
					   ipmi_main_intf->target_addr,
					   ipmi_main_intf->target_channel,
					   ipmi_main_intf->transit_addr,
					   ipmi_main_intf->transit_channel);
		if (ipmi_main_intf->target_ipmb_addr) {
			lprintf(LOG_INFO, "Discovered Target IPMB-0 address %#x",
					   ipmi_main_intf->target_ipmb_addr);
		}
	}

	lprintf(LOG_DEBUG, "Interface address: my_addr %#x "
			   "transit %#x:%#x target %#x:%#x "
			   "ipmb_target %#x\n",
			ipmi_main_intf->my_addr,
			ipmi_main_intf->transit_addr,
			ipmi_main_intf->transit_channel,
			ipmi_main_intf->target_addr,
			ipmi_main_intf->target_channel,
			ipmi_main_intf->target_ipmb_addr);

	/* parse local SDR cache if given */
	if (sdrcache != NULL) {
		ipmi_sdr_list_cache_fromfile(ipmi_main_intf, sdrcache);
	}
	/* Parse SEL OEM file if given */
	if (seloem != NULL) {
		ipmi_sel_oem_init(seloem);
	}

	/* Enable Big Buffer when requested */
	ipmi_main_intf->channel_buf_size = 0;
	if ( my_long_packet_size != 0 ) {
		printf("Setting large buffer to %i\n", my_long_packet_size);
		if (ipmi_kontronoem_set_large_buffer( ipmi_main_intf, my_long_packet_size ) == 0)
		{
			my_long_packet_set = 1;
			ipmi_main_intf->channel_buf_size = my_long_packet_size;
		}
	}

	ipmi_main_intf->cmdlist = cmdlist;

	/* now we finally run the command */
	if (argc-optind > 0)
		rc = ipmi_cmd_run(ipmi_main_intf, argv[optind], argc-optind-1,
				&(argv[optind+1]));
	else
		rc = ipmi_cmd_run(ipmi_main_intf, NULL, 0, NULL);

	if (my_long_packet_set == 1) {
		/* Restore defaults */
		ipmi_kontronoem_set_large_buffer( ipmi_main_intf, 0 );
	}

	/* clean repository caches */
	ipmi_cleanup(ipmi_main_intf);

	/* call interface close function if available */
	if (ipmi_main_intf->opened > 0 && ipmi_main_intf->close != NULL)
		ipmi_main_intf->close(ipmi_main_intf);

	out_free:
	log_halt();

	if (intfname != NULL) {
		free(intfname);
		intfname = NULL;
	}
	if (hostname != NULL) {
		free(hostname);
		hostname = NULL;
	}
	if (username != NULL) {
		free(username);
		username = NULL;
	}
	if (password != NULL) {
		free(password);
		password = NULL;
	}
	if (oemtype != NULL) {
		free(oemtype);
		oemtype = NULL;
	}
	if (seloem != NULL) {
		free(seloem);
		seloem = NULL;
	}
	if (kgkey != NULL) {
		free(kgkey);
		kgkey = NULL;
	}
	if (sdrcache != NULL) {
		free(sdrcache);
		sdrcache = NULL;
	}
	if (devfile) {
		free(devfile);
		devfile = NULL;
	}

	return rc;
}