@@ -155,10 +155,8 @@ static int coral_get_str(struct udevice *dev, int id, size_t size, char *val)
if (ret < 0)
return ret;
- if (size < 15)
- return -ENOSPC;
- sprintf(val, "rev%d", ret);
- break;
+
+ return snprintf(val, size, "rev%d", ret);
}
case SYSINFO_ID_BOARD_MODEL: {
int mem_config, sku_config;
@@ -173,15 +171,12 @@ static int coral_get_str(struct udevice *dev, int id, size_t size, char *val)
log_warning("Unable to read skuconfig (err=%d)\n", ret);
sku_config = ret;
model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
- snprintf(val, size, "%s (memconfig %d, SKU %d)", model,
- mem_config, sku_config);
- break;
+ return snprintf(val, size, "%s (memconfig %d, SKU %d)", model,
+ mem_config, sku_config);
}
default:
return -ENOENT;
}
-
- return 0;
}
int chromeos_get_gpio(const struct udevice *dev, const char *prop,
@@ -43,7 +43,7 @@ int __weak show_board_info(void)
}
/* Fail back to the main 'model' if available */
- if (ret)
+ if (ret <= 0)
model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
else
model = str;
@@ -79,7 +79,7 @@ static int sysinfo_gpio_get_str(struct udevice *dev, int id, size_t size, char *
strncpy(val, name, size);
val[size - 1] = '\0';
- return 0;
+ return strlen(name);
} default:
return -EINVAL;
};
@@ -48,7 +48,7 @@ static int sysinfo_rcar_get_str(struct udevice *dev, int id, size_t size, char *
case SYSINFO_ID_BOARD_MODEL:
strncpy(val, priv->boardmodel, size);
val[size - 1] = '\0';
- return 0;
+ return strlen(priv->boardmodel);
default:
return -EINVAL;
};
@@ -73,8 +73,9 @@ int sysinfo_sandbox_get_str(struct udevice *dev, int id, size_t size, char *val)
switch (id) {
case STR_VACATIONSPOT:
/* Picks a vacation spot depending on i1 and i2 */
- snprintf(val, size, vacation_spots[index]);
- return 0;
+ strncpy(val, vacation_spots[index], size);
+ val[size - 1] = '\0';
+ return strlen(vacation_spots[index]);
}
return -ENOENT;
@@ -95,10 +95,15 @@ struct sysinfo_ops {
* @dev: The sysinfo instance to gather the data.
* @id: A unique identifier for the string value to be read.
* @size: The size of the buffer to receive the string data.
+ * If the buffer is not large enough to contain the whole
+ * string, the string must be trimmed to fit in the
+ * buffer including the terminating NULL-byte.
* @val: Pointer to a buffer that receives the value read.
*
- * Return: 0 if OK, -ve on error.
+ * Return: Actual length of the string excluding the terminating
+ * NULL-byte if OK, -ve on error.
*/
+
int (*get_str)(struct udevice *dev, int id, size_t size, char *val);
/**
@@ -164,11 +169,14 @@ int sysinfo_get_int(struct udevice *dev, int id, int *val);
* hardware setup.
* @dev: The sysinfo instance to gather the data.
* @id: A unique identifier for the string value to be read.
- * @size: The size of the buffer to receive the string data.
+ * @size: The size of the buffer to receive the string data. If the buffer
+ * is not large enough to contain the whole string, the string will
+ * be trimmed to fit in the buffer including the terminating
+ * NULL-byte.
* @val: Pointer to a buffer that receives the value read.
*
- * Return: 0 if OK, -EPERM if called before sysinfo_detect(), else -ve on
- * error.
+ * Return: Actual length of the string excluding the terminating NULL-byte if
+ * OK, -EPERM if called before sysinfo_detect(), else -ve on error.
*/
int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val);
@@ -144,7 +144,7 @@ static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
int ret;
ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
- if (!ret)
+ if (ret >= 0)
return smbios_add_string(ctx, val);
}
if (IS_ENABLED(CONFIG_OF_CONTROL)) {
@@ -32,8 +32,8 @@ static int dm_test_sysinfo_gpio(struct unit_test_state *uts)
ut_assertok(sysinfo_detect(sysinfo));
ut_assertok(sysinfo_get_int(sysinfo, SYSINFO_ID_BOARD_MODEL, &val));
ut_asserteq(19, val);
- ut_assertok(sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
- buf));
+ ut_asserteq(5, sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
+ buf));
ut_asserteq_str("rev_a", buf);
/*
@@ -46,8 +46,8 @@ static int dm_test_sysinfo_gpio(struct unit_test_state *uts)
ut_assertok(sysinfo_detect(sysinfo));
ut_assertok(sysinfo_get_int(sysinfo, SYSINFO_ID_BOARD_MODEL, &val));
ut_asserteq(5, val);
- ut_assertok(sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
- buf));
+ ut_asserteq(3, sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
+ buf));
ut_asserteq_str("foo", buf);
/*
@@ -60,8 +60,8 @@ static int dm_test_sysinfo_gpio(struct unit_test_state *uts)
ut_assertok(sysinfo_detect(sysinfo));
ut_assertok(sysinfo_get_int(sysinfo, SYSINFO_ID_BOARD_MODEL, &val));
ut_asserteq(15, val);
- ut_assertok(sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
- buf));
+ ut_asserteq(7, sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
+ buf));
ut_asserteq_str("unknown", buf);
return 0;
@@ -34,8 +34,8 @@ static int dm_test_sysinfo(struct unit_test_state *uts)
&called_detect));
ut_assert(called_detect);
- ut_assertok(sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
- str));
+ ut_asserteq(6, sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
+ str));
ut_assertok(strcmp(str, "R'lyeh"));
ut_assertok(sysinfo_get_int(sysinfo, INT_TEST1, &i));
@@ -44,8 +44,8 @@ static int dm_test_sysinfo(struct unit_test_state *uts)
ut_assertok(sysinfo_get_int(sysinfo, INT_TEST2, &i));
ut_asserteq(100, i);
- ut_assertok(sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
- str));
+ ut_asserteq(7, sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
+ str));
ut_assertok(strcmp(str, "Carcosa"));
ut_assertok(sysinfo_get_int(sysinfo, INT_TEST1, &i));
@@ -54,8 +54,8 @@ static int dm_test_sysinfo(struct unit_test_state *uts)
ut_assertok(sysinfo_get_int(sysinfo, INT_TEST2, &i));
ut_asserteq(99, i);
- ut_assertok(sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
- str));
+ ut_asserteq(7, sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
+ str));
ut_assertok(strcmp(str, "Yuggoth"));
return 0;