From: Hugo Villeneuve Date: Wed, 5 Nov 2025 17:19:23 +0000 (-0500) Subject: tlv_eeprom: add is_valid_tlvinfo() X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=bea032a7946421a08dddae37957f72fe076984ae;p=mtlv.git tlv_eeprom: add is_valid_tlvinfo() Signed-off-by: Hugo Villeneuve --- diff --git a/src/tlv_eeprom.c b/src/tlv_eeprom.c index 0b1dbc2..135803a 100644 --- a/src/tlv_eeprom.c +++ b/src/tlv_eeprom.c @@ -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 * diff --git a/src/tlv_eeprom.h b/src/tlv_eeprom.h index 10c479a..081c091 100644 --- a/src/tlv_eeprom.h +++ b/src/tlv_eeprom.h @@ -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);