[PATCH v2 0/2] lib: smbios: verify_checksum() is duplicate

The function verify_checksum() duplicates what table_compute_checksum() does. Replace it. table_compute_checksum() is always compiled.
Adjust table_compute_checksum() to accept const pointers.
v2: Adjust table_compute_checksum() to accept const pointers. Correct sequence of includes
Heinrich Schuchardt (2): lib: make table_compute_checksum() arguments const lib: smbios: verify_checksum() is duplicate
include/tables_csum.h | 2 +- lib/smbios-parser.c | 18 ++---------------- lib/tables_csum.c | 4 ++-- 3 files changed, 5 insertions(+), 19 deletions(-)

table_compute_checksum() does neither changes the content of the checksummed buffer nor the buffer length. Adding const to the definition makes the function wider usable.
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- v2: new patch --- include/tables_csum.h | 2 +- lib/tables_csum.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/tables_csum.h b/include/tables_csum.h index 4812333093..9207e85f91 100644 --- a/include/tables_csum.h +++ b/include/tables_csum.h @@ -17,6 +17,6 @@ * @len: configuration table size * @return: the 8-bit checksum */ -u8 table_compute_checksum(void *v, int len); +u8 table_compute_checksum(const void *v, const int len);
#endif diff --git a/lib/tables_csum.c b/lib/tables_csum.c index 636aa59676..305b1ec31c 100644 --- a/lib/tables_csum.c +++ b/lib/tables_csum.c @@ -5,9 +5,9 @@
#include <linux/types.h>
-u8 table_compute_checksum(void *v, int len) +u8 table_compute_checksum(const void *v, const int len) { - u8 *bytes = v; + const u8 *bytes = v; u8 checksum = 0; int i;

On Tue, 9 Jan 2024 at 10:53, Heinrich Schuchardt heinrich.schuchardt@canonical.com wrote:
table_compute_checksum() does neither changes the content of the checksummed buffer nor the buffer length. Adding const to the definition makes the function wider usable.
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com
v2: new patch
include/tables_csum.h | 2 +- lib/tables_csum.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/tables_csum.h b/include/tables_csum.h index 4812333093..9207e85f91 100644 --- a/include/tables_csum.h +++ b/include/tables_csum.h @@ -17,6 +17,6 @@
- @len: configuration table size
- @return: the 8-bit checksum
*/ -u8 table_compute_checksum(void *v, int len); +u8 table_compute_checksum(const void *v, const int len);
#endif diff --git a/lib/tables_csum.c b/lib/tables_csum.c index 636aa59676..305b1ec31c 100644 --- a/lib/tables_csum.c +++ b/lib/tables_csum.c @@ -5,9 +5,9 @@
#include <linux/types.h>
-u8 table_compute_checksum(void *v, int len) +u8 table_compute_checksum(const void *v, const int len) {
u8 *bytes = v;
const u8 *bytes = v; u8 checksum = 0; int i;
-- 2.43.0
Reviewed-by: Ilias Apalodimas ilias.apalodimas@linaro.org

The function verify_checksum() duplicates what table_compute_checksum() does. Replace it. table_compute_checksum() is always compiled.
Fixes: 415eab0655a8 ("smbios: add parsing API") Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com Reviewed-by: Simon Glass sjg@chromium.org --- v2: correct sequence of includes --- lib/smbios-parser.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
diff --git a/lib/smbios-parser.c b/lib/smbios-parser.c index b578c30840..110670343d 100644 --- a/lib/smbios-parser.c +++ b/lib/smbios-parser.c @@ -5,22 +5,8 @@ #define LOG_CATEGORY LOGC_BOOT
#include <smbios.h> +#include <tables_csum.h>
-static inline int verify_checksum(const struct smbios_entry *e) -{ - /* - * Checksums for SMBIOS tables are calculated to have a value, so that - * the sum over all bytes yields zero (using unsigned 8 bit arithmetic). - */ - u8 *byte = (u8 *)e; - u8 sum = 0; - - for (int i = 0; i < e->length; i++) - sum += byte[i]; - - return sum; -} - const struct smbios_entry *smbios_entry(u64 address, u32 size) { const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address; @@ -32,7 +18,7 @@ const struct smbios_entry *smbios_entry(u64 address, u32 size) if (memcmp(entry->anchor, "_SM_", 4)) return NULL;
- if (verify_checksum(entry)) + if (table_compute_checksum(entry, entry->length)) return NULL;
return entry;

On Tue, 9 Jan 2024 at 10:53, Heinrich Schuchardt heinrich.schuchardt@canonical.com wrote:
The function verify_checksum() duplicates what table_compute_checksum() does. Replace it. table_compute_checksum() is always compiled.
Fixes: 415eab0655a8 ("smbios: add parsing API") Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com Reviewed-by: Simon Glass sjg@chromium.org
v2: correct sequence of includes
lib/smbios-parser.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
diff --git a/lib/smbios-parser.c b/lib/smbios-parser.c index b578c30840..110670343d 100644 --- a/lib/smbios-parser.c +++ b/lib/smbios-parser.c @@ -5,22 +5,8 @@ #define LOG_CATEGORY LOGC_BOOT
#include <smbios.h> +#include <tables_csum.h>
-static inline int verify_checksum(const struct smbios_entry *e) -{
/*
* Checksums for SMBIOS tables are calculated to have a value, so that
* the sum over all bytes yields zero (using unsigned 8 bit arithmetic).
*/
u8 *byte = (u8 *)e;
u8 sum = 0;
for (int i = 0; i < e->length; i++)
sum += byte[i];
return sum;
-}
const struct smbios_entry *smbios_entry(u64 address, u32 size) { const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address; @@ -32,7 +18,7 @@ const struct smbios_entry *smbios_entry(u64 address, u32 size) if (memcmp(entry->anchor, "_SM_", 4)) return NULL;
if (verify_checksum(entry))
if (table_compute_checksum(entry, entry->length)) return NULL; return entry;
-- 2.43.0
Reviewed-by: Ilias Apalodimas ilias.apalodimas@linaro.org
participants (2)
-
Heinrich Schuchardt
-
Ilias Apalodimas