mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2026-06-07 17:46:15 +00:00
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:
@@ -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 ||
|
||||
|
||||
Reference in New Issue
Block a user