dm: hw: Replace sscanf with permitted string API

Replace sscanf in device model hw directory

Tracked-On: #2079
Signed-off-by: Long Liu <long.liu@intel.com>
Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
Reviewed-by: <yonghua.huang@intel.com>
Acked-by: Yu Wang <yu1.wang@intel.com>
This commit is contained in:
Long Liu 2018-12-12 08:05:10 +00:00 committed by wenlingz
parent 63b814e7e9
commit 5ab68eb97b
7 changed files with 56 additions and 17 deletions

View File

@ -152,6 +152,46 @@ pci_parse_slot_usage(char *aopt)
fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt);
}
int
parse_bdf(char *s, int *bus, int *dev, int *func, int base)
{
int i;
int nums[3] = {-1, -1, -1};
char *str;
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++;
}
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;
}
int
pci_parse_slot(char *opt)
{
@ -193,15 +233,9 @@ pci_parse_slot(char *opt)
}
/* <bus>:<slot>:<func> */
if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
bnum = 0;
/* <slot>:<func> */
if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
fnum = 0;
/* <slot> */
if (sscanf(str, "%d", &snum) != 1)
snum = -1;
}
if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0) {
fprintf(stderr, "pci bdf parse fail\n");
snum = -1;
}
if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||

View File

@ -84,7 +84,7 @@ check_msi_capability(char *dev_name)
struct pci_device *phys_dev;
/* only check the MSI/MSI-x capability for PCI device */
if (sscanf(dev_name, "%x:%x.%x", &bus, &slot, &func) != 3)
if (parse_bdf(dev_name, &bus, &slot, &func, 16) != 0)
return 0;
phys_dev = pci_device_find_by_slot(0, bus, slot, func);
@ -168,7 +168,7 @@ update_pt_info(uint16_t phys_bdf)
LIST_FOREACH(group, &gsg_head, gsg_list) {
for (i = 0; i < (group->shared_dev_num); i++) {
name = group->dev[i].dev_name;
if (sscanf(name, "%x:%x.%x", &bus, &slot, &func) != 3)
if (parse_bdf(name, &bus, &slot, &func, 16) != 0)
continue;
if (phys_bdf == (PCI_BDF(bus, slot, func)))

View File

@ -52,8 +52,8 @@ int guest_domid = 1;
int
acrn_parse_gvtargs(char *arg)
{
if (sscanf(arg, " %d %d %d", &gvt_low_gm_sz,
&gvt_high_gm_sz, &gvt_fence_sz) != 3) {
if (parse_bdf(arg, &gvt_low_gm_sz, &gvt_high_gm_sz,
&gvt_fence_sz, 10) != 0) {
return -1;
}
printf("passed gvt-g optargs low_gm %d, high_gm %d, fence %d\n",

View File

@ -89,6 +89,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include "dm.h"
#include "vmmapi.h"
@ -226,7 +227,9 @@ 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 (sscanf(dent->d_name, "0000:%x:%x.%x", &b, &s, &f) != 3)
if (strncmp(dent->d_name, "0000:", 5) != 0 ||
parse_bdf((dent->d_name + 5),
&b, &s, &f, 10) != 0)
continue;
else
break;

View File

@ -1003,7 +1003,7 @@ passthru_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
}
opt = strsep(&opts, ",");
if (sscanf(opt, "%x/%x/%x", &bus, &slot, &func) != 3) {
if (parse_bdf(opt, &bus, &slot, &func, 16) != 0) {
warnx("Invalid passthru BDF options:%s", opt);
return -EINVAL;
}

View File

@ -2182,7 +2182,7 @@ vmei_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
int i, rc;
char *endptr = NULL;
char *opt;
unsigned int bus = 0, slot = 0, func = 0;
int bus = 0, slot = 0, func = 0;
char name[DEV_NAME_SIZE + 1];
vmei_debug = 0;
@ -2191,7 +2191,7 @@ vmei_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
goto init;
while ((opt = strsep(&opts, ",")) != NULL) {
if (sscanf(opt, "%x/%x/%x", &bus, &slot, &func) == 3)
if (parse_bdf(opt, &bus, &slot, &func, 16) == 0)
continue;
if (!strncmp(opt, "d", 1)) {
vmei_debug = strtoul(opt + 1, &endptr, 10);

View File

@ -316,6 +316,8 @@ void update_pt_info(uint16_t phys_bdf);
int check_gsi_sharing_violation(void);
int pciaccess_init(void);
void pciaccess_cleanup(void);
int parse_bdf(char *s, int *bus, int *dev, int *func, int base);
/**
* @brief Set virtual PCI device's configuration space in 1 byte width