public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Josua Mayer <josua@solid-run.com>
To: u-boot@lists.denx.de
Cc: Josua Mayer <josua@solid-run.com>, Stefan Roese <sr@denx.de>,
	Sven Auhagen <Sven.Auhagen@voleatech.de>,
	Simon Glass <sjg@chromium.org>
Subject: [PATCH 06/12] cmd: tlv_eeprom: move missing declarations and defines to header
Date: Mon,  2 May 2022 17:18:32 +0300	[thread overview]
Message-ID: <20220502141838.15912-7-josua@solid-run.com> (raw)
In-Reply-To: <20220502141838.15912-1-josua@solid-run.com>

In preparation of splitting the tlv_eeprom command into a separate
library, add function declarations and defines used by the command logic
to the tlv_eeprom header file.

Signed-off-by: Josua Mayer <josua@solid-run.com>
---
 cmd/tlv_eeprom.c     | 59 ++++++++++++++++++++------------------------
 include/tlv_eeprom.h | 29 ++++++++++++++++++++--
 2 files changed, 54 insertions(+), 34 deletions(-)

diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c
index 1b4f2537f6..57468edb1c 100644
--- a/cmd/tlv_eeprom.c
+++ b/cmd/tlv_eeprom.c
@@ -31,8 +31,6 @@ DECLARE_GLOBAL_DATA_PTR;
 static int read_eeprom(int devnum, u8 *eeprom);
 static void show_eeprom(int devnum, u8 *eeprom);
 static void decode_tlv(struct tlvinfo_tlv *tlv);
-static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code);
-static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval);
 static int set_mac(char *buf, const char *string);
 static int set_date(char *buf, const char *string);
 static int set_bytes(char *buf, const char *string, int *converted_accum);
@@ -46,9 +44,6 @@ static struct udevice *tlv_devices[MAX_TLV_DEVICES];
 #define to_header(p) ((struct tlvinfo_header *)p)
 #define to_entry(p) ((struct tlvinfo_tlv *)p)
 
-#define HDR_SIZE sizeof(struct tlvinfo_header)
-#define ENT_SIZE sizeof(struct tlvinfo_tlv)
-
 static inline bool is_digit(char c)
 {
 	return (c >= '0' && c <= '9');
@@ -84,14 +79,14 @@ bool tlvinfo_check_crc(u8 *eeprom)
 		return false;
 
 	// Is the last TLV a CRC?
-	eeprom_crc = to_entry(&eeprom[HDR_SIZE +
-		be16_to_cpu(eeprom_hdr->totallen) - (ENT_SIZE + 4)]);
+	eeprom_crc = to_entry(&eeprom[TLV_INFO_HEADER_SIZE +
+		be16_to_cpu(eeprom_hdr->totallen) - (TLV_INFO_ENTRY_SIZE + 4)]);
 	if (eeprom_crc->type != TLV_CODE_CRC_32 || eeprom_crc->length != 4)
 		return false;
 
 	// Calculate the checksum
 	calc_crc = crc32(0, (void *)eeprom,
-			 HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
+			 TLV_INFO_HEADER_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
 	stored_crc = (eeprom_crc->value[0] << 24) |
 		(eeprom_crc->value[1] << 16) |
 		(eeprom_crc->value[2] <<  8) |
@@ -108,13 +103,13 @@ static int read_eeprom(int devnum, u8 *eeprom)
 {
 	int ret;
 	struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
-	struct tlvinfo_tlv *eeprom_tlv = to_entry(&eeprom[HDR_SIZE]);
+	struct tlvinfo_tlv *eeprom_tlv = to_entry(&eeprom[TLV_INFO_HEADER_SIZE]);
 
 	/* Read the header */
-	ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, devnum);
+	ret = read_tlv_eeprom((void *)eeprom_hdr, 0, TLV_INFO_HEADER_SIZE, devnum);
 	/* If the header was successfully read, read the TLVs */
 	if (ret == 0 && is_valid_tlvinfo_header(eeprom_hdr))
-		ret = read_tlv_eeprom((void *)eeprom_tlv, HDR_SIZE,
+		ret = read_tlv_eeprom((void *)eeprom_tlv, TLV_INFO_HEADER_SIZE,
 				      be16_to_cpu(eeprom_hdr->totallen), devnum);
 
 	// If the contents are invalid, start over with default contents
@@ -161,8 +156,8 @@ static void show_eeprom(int devnum, u8 *eeprom)
 
 	printf("TLV Name             Code Len Value\n");
 	printf("-------------------- ---- --- -----\n");
-	curr_tlv = HDR_SIZE;
-	tlv_end  = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
+	curr_tlv = TLV_INFO_HEADER_SIZE;
+	tlv_end  = TLV_INFO_HEADER_SIZE + be16_to_cpu(eeprom_hdr->totallen);
 	while (curr_tlv < tlv_end) {
 		eeprom_tlv = to_entry(&eeprom[curr_tlv]);
 		if (!is_valid_tlvinfo_entry(eeprom_tlv)) {
@@ -171,7 +166,7 @@ static void show_eeprom(int devnum, u8 *eeprom)
 			return;
 		}
 		decode_tlv(eeprom_tlv);
-		curr_tlv += ENT_SIZE + eeprom_tlv->length;
+		curr_tlv += TLV_INFO_ENTRY_SIZE + eeprom_tlv->length;
 	}
 
 	printf("Checksum is %s.\n",
@@ -339,10 +334,10 @@ void tlvinfo_update_crc(u8 *eeprom)
 	if (!tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) {
 		unsigned int totallen = be16_to_cpu(eeprom_hdr->totallen);
 
-		if ((totallen + ENT_SIZE + 4) > TLV_TOTAL_LEN_MAX)
+		if ((totallen + TLV_INFO_ENTRY_SIZE + 4) > TLV_TOTAL_LEN_MAX)
 			return;
-		eeprom_index = HDR_SIZE + totallen;
-		eeprom_hdr->totallen = cpu_to_be16(totallen + ENT_SIZE + 4);
+		eeprom_index = TLV_INFO_HEADER_SIZE + totallen;
+		eeprom_hdr->totallen = cpu_to_be16(totallen + TLV_INFO_ENTRY_SIZE + 4);
 	}
 	eeprom_crc = to_entry(&eeprom[eeprom_index]);
 	eeprom_crc->type = TLV_CODE_CRC_32;
@@ -350,7 +345,7 @@ void tlvinfo_update_crc(u8 *eeprom)
 
 	// Calculate the checksum
 	calc_crc = crc32(0, (void *)eeprom,
-			 HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
+			 TLV_INFO_HEADER_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
 	eeprom_crc->value[0] = (calc_crc >> 24) & 0xFF;
 	eeprom_crc->value[1] = (calc_crc >> 16) & 0xFF;
 	eeprom_crc->value[2] = (calc_crc >>  8) & 0xFF;
@@ -370,7 +365,7 @@ int write_tlvinfo_tlv_eeprom(void *eeprom, int dev)
 
 	tlvinfo_update_crc(eeprom);
 
-	eeprom_len = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
+	eeprom_len = TLV_INFO_HEADER_SIZE + be16_to_cpu(eeprom_hdr->totallen);
 	ret = write_tlv_eeprom(eeprom, eeprom_len, dev);
 	if (ret) {
 		printf("Programming failed.\n");
@@ -539,15 +534,15 @@ bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index)
 
 	// Search through the TLVs, looking for the first one which matches the
 	// supplied type code.
-	*eeprom_index = HDR_SIZE;
-	eeprom_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
+	*eeprom_index = TLV_INFO_HEADER_SIZE;
+	eeprom_end = TLV_INFO_HEADER_SIZE + be16_to_cpu(eeprom_hdr->totallen);
 	while (*eeprom_index < eeprom_end) {
 		eeprom_tlv = to_entry(&eeprom[*eeprom_index]);
 		if (!is_valid_tlvinfo_entry(eeprom_tlv))
 			return false;
 		if (eeprom_tlv->type == tcode)
 			return true;
-		*eeprom_index += ENT_SIZE + eeprom_tlv->length;
+		*eeprom_index += TLV_INFO_ENTRY_SIZE + eeprom_tlv->length;
 	}
 	return(false);
 }
@@ -558,7 +553,7 @@ bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index)
  *  This function deletes the TLV with the specified type code from the
  *  EEPROM.
  */
-static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code)
+bool tlvinfo_delete_tlv(u8 *eeprom, u8 code)
 {
 	int eeprom_index;
 	int tlength;
@@ -568,9 +563,9 @@ static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code)
 	// Find the TLV and then move all following TLVs "forward"
 	if (tlvinfo_find_tlv(eeprom, code, &eeprom_index)) {
 		eeprom_tlv = to_entry(&eeprom[eeprom_index]);
-		tlength = ENT_SIZE + eeprom_tlv->length;
+		tlength = TLV_INFO_ENTRY_SIZE + eeprom_tlv->length;
 		memcpy(&eeprom[eeprom_index], &eeprom[eeprom_index + tlength],
-		       HDR_SIZE +
+		       TLV_INFO_HEADER_SIZE +
 		       be16_to_cpu(eeprom_hdr->totallen) - eeprom_index -
 		       tlength);
 		eeprom_hdr->totallen =
@@ -589,7 +584,7 @@ static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code)
  *  the format in which it will be stored in the EEPROM.
  */
 #define MAX_TLV_VALUE_LEN   256
-static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
+bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
 {
 	struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
 	struct tlvinfo_tlv *eeprom_tlv;
@@ -656,7 +651,7 @@ static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
 	}
 
 	// Is there room for this TLV?
-	if ((be16_to_cpu(eeprom_hdr->totallen) + ENT_SIZE + new_tlv_len) >
+	if ((be16_to_cpu(eeprom_hdr->totallen) + TLV_INFO_ENTRY_SIZE + new_tlv_len) >
 			TLV_TOTAL_LEN_MAX) {
 		printf("ERROR: There is not enough room in the EERPOM to save data.\n");
 		return false;
@@ -666,9 +661,9 @@ static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
 	if (tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index))
 		eeprom_hdr->totallen =
 			cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) -
-					ENT_SIZE - 4);
+					TLV_INFO_ENTRY_SIZE - 4);
 	else
-		eeprom_index = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
+		eeprom_index = TLV_INFO_HEADER_SIZE + be16_to_cpu(eeprom_hdr->totallen);
 	eeprom_tlv = to_entry(&eeprom[eeprom_index]);
 	eeprom_tlv->type = tcode;
 	eeprom_tlv->length = new_tlv_len;
@@ -676,7 +671,7 @@ static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
 
 	// Update the total length and calculate (add) a new CRC-32 TLV
 	eeprom_hdr->totallen = cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) +
-			ENT_SIZE + new_tlv_len);
+			TLV_INFO_ENTRY_SIZE + new_tlv_len);
 	tlvinfo_update_crc(eeprom);
 
 	return true;
@@ -954,7 +949,7 @@ int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
 	struct tlvinfo_tlv *tlv_ent;
 
 	/* Read TLV header */
-	ret = read_tlv_eeprom(eeprom, 0, HDR_SIZE, dev_num);
+	ret = read_tlv_eeprom(eeprom, 0, TLV_INFO_HEADER_SIZE, dev_num);
 	if (ret < 0)
 		return ret;
 
@@ -964,7 +959,7 @@ int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
 
 	/* Read TLV entries */
 	tlv_ent = to_entry(&tlv_hdr[1]);
-	ret = read_tlv_eeprom(tlv_ent, HDR_SIZE,
+	ret = read_tlv_eeprom(tlv_ent, TLV_INFO_HEADER_SIZE,
 			      be16_to_cpu(tlv_hdr->totallen), dev_num);
 	if (ret < 0)
 		return ret;
diff --git a/include/tlv_eeprom.h b/include/tlv_eeprom.h
index 55fd72d6d2..dc7952da6b 100644
--- a/include/tlv_eeprom.h
+++ b/include/tlv_eeprom.h
@@ -24,11 +24,11 @@ struct __attribute__ ((__packed__)) tlvinfo_header {
 };
 
 // Header Field Constants
+#define TLV_INFO_HEADER_SIZE    sizeof(struct tlvinfo_header)
 #define TLV_INFO_ID_STRING      "TlvInfo"
 #define TLV_INFO_VERSION        0x01
 #define TLV_INFO_MAX_LEN        2048
-#define TLV_TOTAL_LEN_MAX       (TLV_INFO_MAX_LEN - \
-				sizeof(struct tlvinfo_header))
+#define TLV_TOTAL_LEN_MAX       (TLV_INFO_MAX_LEN - TLV_INFO_HEADER_SIZE)
 
 /*
  * TlvInfo TLV: Layout of a TLV field
@@ -39,6 +39,7 @@ struct __attribute__ ((__packed__)) tlvinfo_tlv {
 	u8  value[0];
 };
 
+#define TLV_INFO_ENTRY_SIZE      sizeof(struct tlvinfo_tlv)
 /* Maximum length of a TLV value in bytes */
 #define TLV_VALUE_MAX_LEN        255
 
@@ -134,6 +135,30 @@ int write_tlvinfo_tlv_eeprom(void *eeprom, int dev);
  */
 bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index);
 
+/**
+ *  tlvinfo_add_tlv
+ *
+ *  This function adds a TLV to the EEPROM, converting the value (a string) to
+ *  the format in which it will be stored in the EEPROM.
+ * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer
+ *          of size at least TLV_INFO_MAX_LEN.
+ * @code The TLV Code for the new entry.
+ * @eeprom_index success offset into EEPROM where the new entry has been stored
+ *
+ */
+bool tlvinfo_add_tlv(u8 *eeprom, int code, char *strval);
+
+/**
+ *  tlvinfo_delete_tlv
+ *
+ *  This function deletes the TLV with the specified type code from the
+ *  EEPROM.
+ * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer
+ *          of size at least TLV_INFO_MAX_LEN.
+ * @code The TLV Code of the entry to delete.
+ */
+bool tlvinfo_delete_tlv(u8 *eeprom, u8 code);
+
 /**
  *  tlvinfo_update_crc
  *
-- 
2.34.1


  parent reply	other threads:[~2022-05-02 14:20 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-02 14:18 [PATCH 00/12] split tlv_eeprom command into a separate library Josua Mayer
2022-05-02 14:18 ` [PATCH 01/12] cmd: tlv_eeprom: remove use of global variable current_dev Josua Mayer
2022-05-03  6:09   ` Stefan Roese
2022-05-03  6:52     ` Josua Mayer
2022-05-02 14:18 ` [PATCH 02/12] cmd: tlv_eeprom: remove use of global variable has_been_read Josua Mayer
2022-05-03  6:11   ` Stefan Roese
2022-05-02 14:18 ` [PATCH 03/12] cmd: tlv_eeprom: do_tlv_eeprom: stop using non-api read_eeprom function Josua Mayer
2022-05-03  6:12   ` Stefan Roese
2022-05-02 14:18 ` [PATCH 04/12] cmd: tlv_eeprom: convert functions used by command to api functions Josua Mayer
2022-05-03  6:16   ` Stefan Roese
2022-05-03  7:17     ` Josua Mayer
2022-05-03 10:54       ` Stefan Roese
2022-05-03 19:09         ` Josua Mayer
2022-05-05  5:09           ` Stefan Roese
2022-05-02 14:18 ` [PATCH 05/12] cmd: tlv_eeprom: remove empty function implementations from header Josua Mayer
2022-05-02 14:18 ` Josua Mayer [this message]
2022-05-02 14:18 ` [PATCH 07/12] cmd: tlv_eeprom: hide access to static tlv_devices array behind accessor Josua Mayer
2022-05-02 14:18 ` [PATCH 08/12] cmd: tlv_eeprom: clean up two defines for one thing Josua Mayer
2022-05-02 14:18 ` [PATCH 09/12] cmd: tlv_eeprom: add my copyright Josua Mayer
2022-05-02 14:18 ` [PATCH 10/12] cmd: tlv_eeprom: split off tlv library from command Josua Mayer
2022-05-02 14:18 ` [PATCH 11/12] arm: mvebu: clearfog: enable tlv library for spl in favour of eeprom cmd Josua Mayer
2022-05-02 14:18 ` [PATCH 12/12] lib: tlv_eeprom: add function for reading one entry into a C string Josua Mayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220502141838.15912-7-josua@solid-run.com \
    --to=josua@solid-run.com \
    --cc=Sven.Auhagen@voleatech.de \
    --cc=sjg@chromium.org \
    --cc=sr@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox