From 0310208383b863c9e4506bc151b8912f17402a6e Mon Sep 17 00:00:00 2001 From: Alexander Amelkin Date: Sun, 17 Jun 2018 13:34:56 +0300 Subject: [PATCH] mc: Code refactor to reduce copy-paste ratio * Replace with a macro all calls to val2str() for completion codes * Replace with a macro all calls to val2str() for manufacturer name * Replace with a macro all calls to val2str() for product name * Add ipmi24toh() and ipmi32toh() helpers, unify ipmi*toh() interface Signed-off-by: Alexander Amelkin --- include/ipmitool/helper.h | 33 +++++++++++++++++++++++++---- include/ipmitool/ipmi_mc.h | 11 ++++++++-- include/ipmitool/ipmi_strings.h | 2 ++ lib/ipmi_ime.c | 3 +-- lib/ipmi_mc.c | 37 +++++++++++++++------------------ 5 files changed, 58 insertions(+), 28 deletions(-) diff --git a/include/ipmitool/helper.h b/include/ipmitool/helper.h index 8f9c825..e8624e0 100644 --- a/include/ipmitool/helper.h +++ b/include/ipmitool/helper.h @@ -113,13 +113,13 @@ uint16_t ipmi_get_oem_id(struct ipmi_intf *intf); /* le16toh(), hto16le(), et. al. don't exist for Windows or Apple */ /* For portability, let's simply define our own versions here */ -static inline uint16_t ipmi16toh(uint16_t le) +static inline uint16_t ipmi16toh(void *ipmi16) { - uint8_t *data = (uint8_t *)≤ + uint8_t *ipmi = (uint8_t *)ipmi16; uint16_t h; - h = data[1] << 8; /* MSB */ - h |= data[0]; /* LSB */ + h = ipmi[1] << 8; /* MSB */ + h |= ipmi[0]; /* LSB */ return h; } @@ -130,6 +130,31 @@ static inline void htoipmi16(uint16_t h, uint8_t *ipmi) ipmi[1] = h >> 8; /* MSB */ } +static inline uint32_t ipmi24toh(void *ipmi24) +{ + uint8_t *ipmi = (uint8_t *)ipmi24; + uint32_t h = 0; + + h = ipmi[2] << 16; + h |= ipmi[1] << 8; + h |= ipmi[0]; + + return h; +} + +static inline uint32_t ipmi32toh(void *ipmi32) +{ + uint8_t *ipmi = (uint8_t *)ipmi32; + uint32_t h; + + h = ipmi[3] << 24; + h |= ipmi[2] << 16; + h |= ipmi[1] << 8; + h |= ipmi[0]; + + return h; +} + #define ipmi_open_file_read(file) ipmi_open_file(file, 0) #define ipmi_open_file_write(file) ipmi_open_file(file, 1) diff --git a/include/ipmitool/ipmi_mc.h b/include/ipmitool/ipmi_mc.h index 902b42c..9ca6bf4 100644 --- a/include/ipmitool/ipmi_mc.h +++ b/include/ipmitool/ipmi_mc.h @@ -35,6 +35,13 @@ #include #include +#include + +#define OEM_MFG_STRING(oem) val2str(IPM_DEV_MANUFACTURER_ID(oem),\ + ipmi_oem_info) +#define OEM_PROD_STRING(oem, p) oemval2str(IPM_DEV_MANUFACTURER_ID(oem),\ + ipmi16toh(p),\ + ipmi_oem_product_info) #define BMC_GET_DEVICE_ID 0x01 #define BMC_COLD_RESET 0x02 @@ -85,8 +92,8 @@ struct ipm_devid_rsp { #define IPM_DEV_IPMI_VERSION_MINOR(x) \ ((x & IPM_DEV_IPMI_VER_MINOR_MASK) >> IPM_DEV_IPMI_VER_MINOR_SHIFT) -#define IPM_DEV_MANUFACTURER_ID(x) \ - ((uint32_t) ((x[2] & 0x0F) << 16 | x[1] << 8 | x[0])) +#define IPM_DEV_MANUFACTURER_ID_MASK 0x0FFFFF +#define IPM_DEV_MANUFACTURER_ID(x) (ipmi24toh(x) & IPM_DEV_MANUFACTURER_ID_MASK) #define IPM_DEV_ADTL_SUPPORT_BITS (8) diff --git a/include/ipmitool/ipmi_strings.h b/include/ipmitool/ipmi_strings.h index 63bbea9..e898ce3 100644 --- a/include/ipmitool/ipmi_strings.h +++ b/include/ipmitool/ipmi_strings.h @@ -35,6 +35,8 @@ #include +#define CC_STRING(cc) val2str(cc, completion_code_vals) + extern const struct valstr completion_code_vals[]; extern const struct valstr entity_id_vals[]; extern const struct valstr entity_device_type_vals[]; diff --git a/lib/ipmi_ime.c b/lib/ipmi_ime.c index 6bdca3b..081c3e0 100755 --- a/lib/ipmi_ime.c +++ b/lib/ipmi_ime.c @@ -240,8 +240,7 @@ static int ImeGetInfo(struct ipmi_intf *intf) { rc = IME_SUCCESS; printf("Manufacturer Name : %s\n", - val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), - ipmi_oem_info) ); + OEM_MFG_STRING(devid->manufacturer_id)); printf("Product ID : %u (0x%02x%02x)\n", buf2short((uint8_t *)(devid->product_id)), diff --git a/lib/ipmi_mc.c b/lib/ipmi_mc.c index 5841e86..3cbcd40 100644 --- a/lib/ipmi_mc.c +++ b/lib/ipmi_mc.c @@ -91,7 +91,7 @@ ipmi_mc_reset(struct ipmi_intf * intf, int cmd) return (-1); } else if (rsp->ccode > 0) { lprintf(LOG_ERR, "MC reset command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return (-1); } @@ -287,7 +287,7 @@ ipmi_mc_get_enables(struct ipmi_intf * intf) } if (rsp->ccode > 0) { lprintf(LOG_ERR, "Get Global Enables command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } @@ -337,7 +337,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv) } if (rsp->ccode > 0) { lprintf(LOG_ERR, "Get Global Enables command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } @@ -379,7 +379,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv) } else if (rsp->ccode > 0) { lprintf(LOG_ERR, "Set Global Enables command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } @@ -429,7 +429,7 @@ ipmi_mc_get_deviceid(struct ipmi_intf * intf) } if (rsp->ccode > 0) { lprintf(LOG_ERR, "Get Device ID command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } @@ -447,16 +447,13 @@ ipmi_mc_get_deviceid(struct ipmi_intf * intf) printf("Manufacturer ID : %lu\n", (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id)); printf("Manufacturer Name : %s\n", - val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), - ipmi_oem_info) ); + OEM_MFG_STRING(devid->manufacturer_id)); printf("Product ID : %u (0x%02x%02x)\n", buf2short((uint8_t *)(devid->product_id)), devid->product_id[1], devid->product_id[0]); - product=oemval2str(IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), - (devid->product_id[1]<<8)+devid->product_id[0], - ipmi_oem_product_info); + product = OEM_PROD_STRING(devid->manufacturer_id, devid->product_id); if (product!=NULL) { printf("Product Name : %s\n", product); @@ -585,7 +582,7 @@ static int ipmi_mc_get_selftest(struct ipmi_intf * intf) if (rsp->ccode) { lprintf(LOG_ERR, "Bad response: (%s)", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } @@ -738,15 +735,15 @@ ipmi_mc_get_watchdog(struct ipmi_intf * intf) if (rsp->ccode) { lprintf(LOG_ERR, "Get Watchdog Timer command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } wdt_res = (struct ipm_get_watchdog_rsp *) rsp->data; /* Convert 100ms intervals to seconds */ - init_cnt = (double)ipmi16toh(wdt_res->init_cnt_le) / 10.0; - pres_cnt = (double)ipmi16toh(wdt_res->pres_cnt_le) / 10.0; + init_cnt = (double)ipmi16toh(&wdt_res->init_cnt_le) / 10.0; + pres_cnt = (double)ipmi16toh(&wdt_res->pres_cnt_le) / 10.0; printf("Watchdog Timer Use: %s (0x%02x)\n", wdt_use[IPMI_WDT_GET(wdt_res->use, USE)]->get, wdt_res->use); @@ -941,7 +938,7 @@ ipmi_mc_set_watchdog(struct ipmi_intf * intf, int argc, char *argv[]) rc = rsp->ccode; if (rc) { lprintf(LOG_ERR, "Set Watchdog Timer command failed: %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); goto out; } @@ -1000,7 +997,7 @@ ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf) if (rsp->ccode) { lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed! %s", - val2str(rsp->ccode, completion_code_vals)); + CC_STRING(rsp->ccode)); return -1; } @@ -1035,9 +1032,9 @@ ipmi_mc_rst_watchdog(struct ipmi_intf * intf) if (rsp->ccode) { lprintf(LOG_ERR, "Reset Watchdog Timer command failed: %s", - (rsp->ccode == IPM_WATCHDOG_RESET_ERROR) ? - "Attempt to reset uninitialized watchdog" : - val2str(rsp->ccode, completion_code_vals)); + (rsp->ccode == IPM_WATCHDOG_RESET_ERROR) + ? "Attempt to reset uninitialized watchdog" + : CC_STRING(rsp->ccode)); return -1; } @@ -1375,7 +1372,7 @@ ipmi_sysinfo_main(struct ipmi_intf *intf, int argc, char ** argv, int is_set) } else if (rc > 0) { lprintf(LOG_ERR, "%s command failed: %s", argv[0], - val2str(rc, completion_code_vals)); + CC_STRING(rc)); } return rc; }