mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2026-06-08 01:54:44 +00:00
dm: Fix some issues from string operations
The patch fix some string operations issues and also improve readability of several snippet. Tracked-On: #2133 Signed-off-by: Shuo A Liu <shuo.a.liu@intel.com> Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "dm.h"
|
||||
#include "vmmapi.h"
|
||||
#include "acpi.h"
|
||||
#include "inout.h"
|
||||
@@ -155,41 +156,34 @@ pci_parse_slot_usage(char *aopt)
|
||||
int
|
||||
parse_bdf(char *s, int *bus, int *dev, int *func, int base)
|
||||
{
|
||||
int i;
|
||||
int nums[3] = {-1, -1, -1};
|
||||
char *str;
|
||||
char *s_bus, *s_dev, *s_func;
|
||||
char *str, *cp;
|
||||
int ret = 0;
|
||||
|
||||
if (bus) *bus = 0;
|
||||
if (dev) *dev = 0;
|
||||
if (func) *func = 0;
|
||||
str = s;
|
||||
for (i = 0, errno = 0; i < 3; i++) {
|
||||
nums[i] = (int)strtol(str, &str, base);
|
||||
if (errno == ERANGE || *str == '\0' || s == str)
|
||||
break;
|
||||
str++;
|
||||
str = cp = strdup(s);
|
||||
bus ? *bus = 0 : 0;
|
||||
dev ? *dev = 0 : 0;
|
||||
func ? *func = 0 : 0;
|
||||
s_bus = s_dev = s_func = NULL;
|
||||
s_dev = strsep(&cp, ":/.");
|
||||
if (cp) {
|
||||
s_func = strsep(&cp, ":/.");
|
||||
if (cp) {
|
||||
s_bus = s_dev;
|
||||
s_dev = s_func;
|
||||
s_func = strsep(&cp, ":/.");
|
||||
}
|
||||
}
|
||||
|
||||
if (s == str || errno == ERANGE)
|
||||
{
|
||||
printf("%s: parse_bdf error!\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
switch (i) {
|
||||
case 0:
|
||||
if (dev) *dev = nums[0];
|
||||
break;
|
||||
case 1:
|
||||
if (dev) *dev = nums[0];
|
||||
if (func) *func = nums[1];
|
||||
break;
|
||||
case 2:
|
||||
if (bus) *bus = nums[0];
|
||||
if (dev) *dev = nums[1];
|
||||
if (func) *func = nums[2];
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
if (s_dev && dev)
|
||||
ret |= dm_strtoi(s_dev, &s_dev, base, dev);
|
||||
if (s_func && func)
|
||||
ret |= dm_strtoi(s_func, &s_func, base, func);
|
||||
if (s_bus && bus)
|
||||
ret |= dm_strtoi(s_bus, &s_bus, base, bus);
|
||||
free(str);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
@@ -208,35 +202,22 @@ pci_parse_slot(char *opt)
|
||||
}
|
||||
|
||||
emul = config = NULL;
|
||||
cp = strchr(str, ',');
|
||||
if (cp != NULL) {
|
||||
*cp = '\0';
|
||||
emul = cp + 1;
|
||||
cp = strchr(emul, ',');
|
||||
if (cp != NULL) {
|
||||
*cp = '\0';
|
||||
config = cp + 1;
|
||||
if (*config == 'b') {
|
||||
b = config;
|
||||
cp = config + 1;
|
||||
if (*cp == ',') {
|
||||
*cp = '\0';
|
||||
config = cp + 1;
|
||||
} else {
|
||||
b = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
cp = str;
|
||||
str = strsep(&cp, ",");
|
||||
if (cp) {
|
||||
emul = strsep(&cp, ",");
|
||||
/* for boot device */
|
||||
if (cp && *cp == 'b' && *(cp+1) == ',')
|
||||
b = strsep(&cp, ",");
|
||||
config = cp;
|
||||
} else {
|
||||
pci_parse_slot_usage(opt);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* <bus>:<slot>:<func> */
|
||||
if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0) {
|
||||
fprintf(stderr, "pci bdf parse fail\n");
|
||||
if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0)
|
||||
snum = -1;
|
||||
}
|
||||
|
||||
if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
|
||||
fnum < 0 || fnum >= MAXFUNCS) {
|
||||
|
||||
@@ -52,10 +52,11 @@ int guest_domid = 1;
|
||||
int
|
||||
acrn_parse_gvtargs(char *arg)
|
||||
{
|
||||
if (parse_bdf(arg, &gvt_low_gm_sz, &gvt_high_gm_sz,
|
||||
&gvt_fence_sz, 10) != 0) {
|
||||
if (dm_strtoi(arg, &arg, 10, &gvt_low_gm_sz) != 0 ||
|
||||
dm_strtoi(arg, &arg, 10, &gvt_high_gm_sz) != 0 ||
|
||||
dm_strtoi(arg, &arg, 10, &gvt_fence_sz) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("passed gvt-g optargs low_gm %d, high_gm %d, fence %d\n",
|
||||
gvt_low_gm_sz, gvt_high_gm_sz, gvt_fence_sz);
|
||||
|
||||
|
||||
@@ -186,6 +186,7 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
|
||||
uint8_t h_cfg[PCI_REGMAX + 1];
|
||||
uint32_t m_off, m_num;
|
||||
struct npk_reg_default_val *d;
|
||||
char *cp;
|
||||
|
||||
if (npk_in_use) {
|
||||
WPRINTF(("NPK is already in use\n"));
|
||||
@@ -212,8 +213,8 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
|
||||
*/
|
||||
|
||||
/* get the master offset and the number for this guest */
|
||||
if ((opts == NULL) || (sscanf(opts, "%u/%u", &m_off, &m_num) != 2)
|
||||
|| !valid_param(m_off, m_num)) {
|
||||
if ((opts == NULL) || dm_strtoui(opts, &cp, 10, &m_off) || *cp != '/' ||
|
||||
dm_strtoui(cp + 1, &cp, 10, &m_num) || !valid_param(m_off, m_num)) {
|
||||
m_off = 256;
|
||||
m_num = 256;
|
||||
}
|
||||
@@ -228,8 +229,7 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
|
||||
/* traverse the driver folder, and try to find the NPK BDF# */
|
||||
while ((dent = readdir(dir)) != NULL) {
|
||||
if (strncmp(dent->d_name, "0000:", 5) != 0 ||
|
||||
parse_bdf((dent->d_name + 5),
|
||||
&b, &s, &f, 10) != 0)
|
||||
parse_bdf((dent->d_name + 5), &b, &s, &f, 16) != 0)
|
||||
continue;
|
||||
else
|
||||
break;
|
||||
|
||||
@@ -208,7 +208,7 @@ connect_coreu_daemon()
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, COREU_SERVICE_NAME, sizeof(&COREU_SERVICE_NAME));
|
||||
strncpy(addr.sun_path, COREU_SERVICE_NAME, sizeof(addr.sun_path));
|
||||
|
||||
ret = connect(fd, &addr, sizeof(struct sockaddr_un));
|
||||
if (ret < 0) {
|
||||
|
||||
Reference in New Issue
Block a user