tlv_eeprom: add is_valid_tlvinfo()
authorHugo Villeneuve <hvilleneuve@dimonoff.com>
Wed, 5 Nov 2025 17:19:23 +0000 (12:19 -0500)
committerHugo Villeneuve <hvilleneuve@dimonoff.com>
Mon, 17 Nov 2025 15:36:11 +0000 (10:36 -0500)
Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
src/tlv_eeprom.c
src/tlv_eeprom.h

index 0b1dbc2..135803a 100644 (file)
@@ -64,6 +64,35 @@ static inline u8 is_hex(char p)
                ((p >= 'a') && (p <= 'f')));
 }
 
+/**
+ *  is_valid_tlvinfo_header
+ *
+ *  Perform sanity checks on the first 11 bytes of the TlvInfo EEPROM
+ *  data pointed to by the parameter:
+ *      1. First 8 bytes contain null-terminated ASCII string "TlvInfo"
+ *      2. Version byte is 1
+ *      3. Total length bytes contain value which is less than or equal
+ *         to the allowed maximum (2048-11)
+ *
+ */
+static inline bool is_valid_tlvinfo_header(struct tlvinfo_header *hdr)
+{
+       return ((strcmp(hdr->signature, TLV_INFO_ID_STRING) == 0) &&
+               (hdr->version == TLV_INFO_VERSION) &&
+               (be16_to_cpu(hdr->totallen) <= TLV_TOTAL_LEN_MAX));
+}
+
+bool is_valid_tlvinfo(u8 *eeprom)
+{
+       struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
+
+       if (!is_valid_tlvinfo_header(eeprom_hdr) ||
+           !is_checksum_valid(eeprom))
+               return false;
+
+       return true;
+}
+
 /**
  *  is_checksum_valid
  *
@@ -99,6 +128,16 @@ static bool is_checksum_valid(u8 *eeprom)
        return calc_crc == stored_crc;
 }
 
+void tlvinfo_default_tlv(u8 *eeprom)
+{
+       struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
+
+       strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING);
+       eeprom_hdr->version = TLV_INFO_VERSION;
+       eeprom_hdr->totallen = cpu_to_be16(0);
+       update_crc(eeprom);
+}
+
 /**
  *  read_eeprom
  *
index 10c479a..081c091 100644 (file)
@@ -113,24 +113,8 @@ int write_tlv_eeprom(void *eeprom, int len, int dev);
 int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
                            struct tlvinfo_tlv **first_entry, int dev);
 
-/**
- *  is_valid_tlvinfo_header
- *
- *  Perform sanity checks on the first 11 bytes of the TlvInfo EEPROM
- *  data pointed to by the parameter:
- *      1. First 8 bytes contain null-terminated ASCII string "TlvInfo"
- *      2. Version byte is 1
- *      3. Total length bytes contain value which is less than or equal
- *         to the allowed maximum (2048-11)
- *
- */
-static inline bool is_valid_tlvinfo_header(struct tlvinfo_header *hdr)
-{
-       return ((strcmp(hdr->signature, TLV_INFO_ID_STRING) == 0) &&
-               (hdr->version == TLV_INFO_VERSION) &&
-               (be16_to_cpu(hdr->totallen) <= TLV_TOTAL_LEN_MAX));
-}
-
+void tlvinfo_default_tlv(u8 *eeprom);
+bool is_valid_tlvinfo(u8 *eeprom);
 void show_eeprom(int devnum, u8 *eeprom);
 bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index);
 bool tlvinfo_delete_tlv(u8 *eeprom, u8 code);