Skip to contentSkip to conversation

lib: tlv_eeprom: refactor API

RFC New Josua Mayer 1 participant 4 messages May 16, 2023 Library code

lore
Share & tools
lore
#0 lore raw

v2 cover lib: tlv_eeprom: refactor API

The existing tlv_eeprom functionality has been designed as a single application living as a uboot command.
The split into individual library and command, as well as attempts using this functionality extensively for board-identification have revealed several short-comings, such as:
- Inconsistent naming convention
- Inconsistent order of arguments
- Stateful functions
- Complex inter-dependencies between functions
- Control of low-level information such as header checksums by command
- Difficult to extend
- No clear separation between eeprom access and tlv format
- No support for multiple entries with same code (e.g. vendor extension)

Introduce a new API with clear names and support for duplicate tlv codes. Further rewrite the tlv_eeprom command, and the solidrun clearfog board logic to utilize these new functions.

This is the second draft, I am looking for:
- comments on the library functions names + descriptions
- comments on the use of udevice pointers, especially wrt. "tlv_eeprom_get_by_index" function that I am not sure about keeping

Please note that there are still bugs and untested functions in this version. I am planning to provide a complete result in the coming weeks.

Josua Mayer (3):
lib: add tlv_eeprom library
mvebu: clearfog: convert tlv parsing to use new library
cmd: tlv_eeprom: port to new shared tlv library

 board/solidrun/common/tlv_data.c |  46 +--
 cmd/Kconfig                      |   7 +-
 cmd/tlv_eeprom.c                 | 507 +++++---------------------
 configs/clearfog_defconfig       |   2 +
 include/tlv_eeprom.h             | 242 ++++++++++---
 lib/Kconfig                      |   2 +
 lib/Makefile                     |   2 +
 lib/tlv/Kconfig                  |  18 +
 lib/tlv/Makefile                 |   4 +
 lib/tlv/tlv_eeprom.c             | 599 +++++++++++++++++++++++++++++++
 10 files changed, 918 insertions(+), 511 deletions(-)
 create mode 100644 lib/tlv/Kconfig
 create mode 100644 lib/tlv/Makefile
 create mode 100644 lib/tlv/tlv_eeprom.c
Cc Stefan Roese <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Signature · 1 line
2.35.3
#2 lore raw

RFC v2 2/3 mvebu: clearfog: convert tlv parsing to use new library

Update the existing code reading tlv data from eeprom to use the new tlv library functions rather than relying on tlv_eeprom command internals.

---
 board/solidrun/common/tlv_data.c | 46 ++++++++++++--------------------
 configs/clearfog_defconfig       |  4 ++-
 2 files changed, 20 insertions(+), 30 deletions(-)
board/solidrun/common/tlv_data.c +17 -29
index 11d6e4a1380..31b4b473c75 100644
@@ -5,6 +5,7 @@
 
 #include <common.h>
 #include <compiler.h>
+#include <linux/err.h>
 #include <tlv_eeprom.h>
 #include "tlv_data.h"
 
@@ -50,44 +51,31 @@ static void parse_tlv_vendor_ext(struct tlvinfo_tlv *tlv_entry,
 	td->ram_size = val[5];
 }
 
-static void parse_tlv_data(u8 *eeprom, struct tlvinfo_header *hdr,
-			   struct tlvinfo_tlv *entry, struct tlv_data *td)
+static void parse_tlv_data(u8 *eeprom, struct tlvinfo_priv *tlv,
+						   struct tlv_data *td)
 {
-	unsigned int tlv_offset, tlv_len;
-
-	tlv_offset = sizeof(struct tlvinfo_header);
-	tlv_len = sizeof(struct tlvinfo_header) + be16_to_cpu(hdr->totallen);
-	while (tlv_offset < tlv_len) {
-		entry = (struct tlvinfo_tlv *)&eeprom[tlv_offset];
-
-		switch (entry->type) {
-		case TLV_CODE_PRODUCT_NAME:
-			store_product_name(entry, td);
-			break;
-		case TLV_CODE_VENDOR_EXT:
-			parse_tlv_vendor_ext(entry, td);
-			break;
-		default:
-			break;
-		}
-
-		tlv_offset += sizeof(struct tlvinfo_tlv) + entry->length;
-	}
+	struct tlvinfo_tlv *entry;
+
+	entry = tlv_entry_next_by_code(tlv, NULL, TLV_CODE_PRODUCT_NAME);
+	if (!IS_ERR(entry))
+		store_product_name(entry, td);
+
+	entry = tlv_entry_next_by_code(tlv, NULL, TLV_CODE_VENDOR_EXT);
+	if (!IS_ERR(entry))
+		parse_tlv_vendor_ext(entry, td);
 }
 
 void read_tlv_data(struct tlv_data *td)
 {
 	u8 eeprom_data[TLV_TOTAL_LEN_MAX];
-	struct tlvinfo_header *tlv_hdr;
-	struct tlvinfo_tlv *tlv_entry;
-	int ret, i;
+	struct tlvinfo_priv *priv;
+	int i;
 
 	for (i = 0; i < 2; i++) {
-		ret = read_tlvinfo_tlv_eeprom(eeprom_data, &tlv_hdr,
-					      &tlv_entry, i);
-		if (ret < 0)
+		priv = tlv_eeprom_read(tlv_eeprom_get_by_index(i), 0, eeprom_data, ARRAY_SIZE(eeprom_data));
+		if (IS_ERR(priv))
 			continue;
-		parse_tlv_data(eeprom_data, tlv_hdr, tlv_entry, td);
+		parse_tlv_data(eeprom_data, priv, td);
 	}
 }
 
configs/clearfog_defconfig +3 -1
index b3ed1ec7bbe..fa86b23ef40 100644
@@ -35,7 +35,7 @@ CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 # CONFIG_SPL_SHARES_INIT_SP_ADDR is not set
 CONFIG_SPL_I2C=y
 CONFIG_SYS_MAXARGS=32
-CONFIG_CMD_TLV_EEPROM=y
+# CONFIG_CMD_TLV_EEPROM is not set
 CONFIG_SPL_CMD_TLV_EEPROM=y
 # CONFIG_CMD_FLASH is not set
 CONFIG_CMD_GPIO=y
@@ -81,3 +81,5 @@ CONFIG_USB=y
 CONFIG_USB_XHCI_HCD=y
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR=0x1
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET=0x0
+CONFIG_EEPROM_TLV_LIB=y
+CONFIG_SPL_EEPROM_TLV_LIB=y
Signature · 1 line
2.35.3
#1 lore raw

RFC v2 1/3 lib: add tlv_eeprom library

In reply to Josua Mayer

Create a new tlv library by rewriting parts, and reusing other parts of the tlv_eeprom command. This library is intended to simplify reading tlv data during system initialisation from board files, as well as increase maintainability by defining a clear API and functionally decouple from the command implementation.

Cc Stefan Roese <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
---
 cmd/Kconfig          |   2 +
 include/tlv_eeprom.h | 272 ++++++++++++++++++++
 lib/Kconfig          |   2 +
 lib/Makefile         |   2 +
 lib/tlv/Kconfig      |  18 ++
 lib/tlv/Makefile     |   4 +
 lib/tlv/tlv_eeprom.c | 599 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 899 insertions(+)
 create mode 100644 lib/tlv/Kconfig
 create mode 100644 lib/tlv/Makefile
 create mode 100644 lib/tlv/tlv_eeprom.c
cmd/Kconfig +2 -0
index 365371fb511..9c2149dc881 100644
@@ -191,6 +191,7 @@ config CMD_REGINFO
 config CMD_TLV_EEPROM
 	bool "tlv_eeprom"
 	depends on I2C_EEPROM
+	depends on !EEPROM_TLV_LIB
 	select CRC32
 	help
 	  Display and program the system EEPROM data block in ONIE Tlvinfo
@@ -199,6 +200,7 @@ config CMD_TLV_EEPROM
 config SPL_CMD_TLV_EEPROM
 	bool "tlv_eeprom for SPL"
 	depends on SPL_I2C_EEPROM
+	depends on !SPL_EEPROM_TLV_LIB
 	select SPL_DRIVERS_MISC
 	select SPL_CRC32
 	help
include/tlv_eeprom.h +272 -0

282 lines not on this page · raw message

lib/Kconfig +2 -0
index c8b3ec1ec9c..5e8bdb66c7e 100644
@@ -1111,3 +1111,5 @@ menu "FWU Multi Bank Updates"
 source lib/fwu_updates/Kconfig
 
 endmenu
+
+source lib/tlv/Kconfig
lib/Makefile +2 -0
index 8d8ccc8bbc3..21c0d18bb8d 100644
@@ -149,6 +149,8 @@ obj-$(CONFIG_LIB_ELF) += elf.o
 
 obj-$(CONFIG_$(SPL_TPL_)SEMIHOSTING) += semihosting.o
 
+obj-y += tlv/
+
 #
 # Build a fast OID lookup registry from include/linux/oid_registry.h
 #
lib/tlv/Kconfig new file +18 -0
new file mode 100644
index 00000000000..688b0317a5d
@@ -0,0 +1,18 @@
+config EEPROM_TLV_LIB
+	bool "Enable EEPROM TLV library"
+	depends on I2C_EEPROM
+	select CRC32
+	help
+	  Selecting this option will enable the shared EEPROM TLV library code.
+	  It provides functions for reading, writing and parsing of
+	  TLV-encoded data from EEPROMs.
+
+config SPL_EEPROM_TLV_LIB
+	bool "Enable EEPROM TLV library for SPL"
+	depends on SPL_I2C_EEPROM
+	select SPL_CRC32
+	select SPL_DRIVERS_MISC
+	help
+	  Selecting this option will enable the shared EEPROM TLV library code.
+	  It provides functions for reading, writing and parsing of
+	  TLV-encoded data from EEPROMs.
lib/tlv/Makefile new file +4 -0
new file mode 100644
index 00000000000..93b8042d449
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+
+obj-$(CONFIG_$(SPL_)EEPROM_TLV_LIB) += tlv_eeprom.o
lib/tlv/tlv_eeprom.c new file +599 -0

599 lines not on this page · raw message

Signature · 1 line
2.35.3
#3 lore raw

RFC v2 3/3 cmd: tlv_eeprom: port to new shared tlv library

In reply to Josua Mayer

Rewrite tlv_eeprom command for using the new tlv library, and drop all unused functions. From this point on, tlv_eeprom command internal functions shall not be reused externally.

mac_read_from_eeprom & populate_serial_number are kept in place for now as is, however these probably deserve a new location.

---
 cmd/Kconfig                |   9 +-
 cmd/tlv_eeprom.c           | 507 +++++++------------------------------
 configs/clearfog_defconfig |   2 +-
 include/tlv_eeprom.h       | 148 -----------
 4 files changed, 92 insertions(+), 574 deletions(-)
cmd/Kconfig +2 -7
index 9c2149dc881..4b7ea8cd358 100644
@@ -190,19 +190,14 @@ config CMD_REGINFO
 
 config CMD_TLV_EEPROM
 	bool "tlv_eeprom"
-	depends on I2C_EEPROM
-	depends on !EEPROM_TLV_LIB
-	select CRC32
+	depends on EEPROM_TLV_LIB
 	help
 	  Display and program the system EEPROM data block in ONIE Tlvinfo
 	  format. TLV stands for Type-Length-Value.
 
 config SPL_CMD_TLV_EEPROM
 	bool "tlv_eeprom for SPL"
-	depends on SPL_I2C_EEPROM
-	depends on !SPL_EEPROM_TLV_LIB
-	select SPL_DRIVERS_MISC
-	select SPL_CRC32
+	depends on SPL_EEPROM_TLV_LIB
 	help
 	  Read system EEPROM data block in ONIE Tlvinfo format from SPL.
 
cmd/tlv_eeprom.c +89 -418

672 lines not on this page · raw message

configs/clearfog_defconfig +1 -1
index fa86b23ef40..98a87068f59 100644
@@ -35,7 +35,7 @@ CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 # CONFIG_SPL_SHARES_INIT_SP_ADDR is not set
 CONFIG_SPL_I2C=y
 CONFIG_SYS_MAXARGS=32
-# CONFIG_CMD_TLV_EEPROM is not set
+CONFIG_CMD_TLV_EEPROM=y
 CONFIG_SPL_CMD_TLV_EEPROM=y
 # CONFIG_CMD_FLASH is not set
 CONFIG_CMD_GPIO=y
include/tlv_eeprom.h +0 -148

158 lines not on this page · raw message

Signature · 1 line
2.35.3