From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 94058C433EF for ; Mon, 2 May 2022 14:20:54 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5D8AB83E96; Mon, 2 May 2022 16:19:31 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=solid-run.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.b="gtU/uARt"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8A6C783F41; Mon, 2 May 2022 16:19:12 +0200 (CEST) Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 06D0F83F06 for ; Mon, 2 May 2022 16:18:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=solid-run.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=josua@solid-run.com Received: by mail-wm1-x336.google.com with SMTP id o12-20020a1c4d0c000000b00393fbe2973dso9954361wmh.2 for ; Mon, 02 May 2022 07:18:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=solid-run-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GO0KBzslQ9TyiGIK9m/Zz0ZbLrPb/mNRGPIA3b4KMOc=; b=gtU/uARtoTk+kvZDqL7HI+pCiJZ254o9rrPjYfDkC1TDAo/eM1Zx3mubwujbCWC8k1 NrnKwZ8HhU6iFHW3luu2xaWLumZQW+snZBIcbunrcuvr+PAaw3l+V2WAoImquyJYN6eD Y+CqC+HJU2HTis3RvkCEVdnRrkh2fX1nGXPW2QHmsEaQcsiuKjQR+5YpIa/obGi8QOkb Dd2v7lgyP4M/iQZ2TfLdlTLZ/g5UALO5o1sD3TdRdhzD53zgLJj8fb9EMvWPcxpc9t2E iyXX19wQ0lpP45vMyie2nmVuMEIUWl6SkiZboIsrWnjGYRG1yd0+DKqLvCPSyXhdWdbC ywLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GO0KBzslQ9TyiGIK9m/Zz0ZbLrPb/mNRGPIA3b4KMOc=; b=6eywMGylY08yTabs9ol9Vm8td+LmK8IRFuA45yXo6L/OlCgXutWKWT4Ep9+VcQnCgC Qn+WllJ6znnaq3kugqHFEDFfofff6MmhfBjp5yjdFg/ZzkZeSAOmN5PfC9JbvoL3C52p t6XD8RR7hPR5oW/mhTIViWj/3m0CQdZZrCioPyjnRGKJ3TIBfSmtXZkdNyEdpYra1/E6 ppyvbnxbtl27zGUqpBwFaPGSjIiqPUHsQVT8gOKU04JGZ831KQFtSI5eyyCpoAk/7p+m S7A+9NmTrJAq7TVxO195wbthKD8vzxtMMWFCd+uQGWicrtqvHC4iKQkX2xrxpu+E+JRx diQw== X-Gm-Message-State: AOAM532fWsTvumodBD4ebLssQCGQe8mG5BMHGEiBLL/DFsXXCzApUOOs 2v6XzJI95yVw9VA+8uIurTX0VroPJNbpug== X-Google-Smtp-Source: ABdhPJyAvSim9BbgLwzwyUinZb53GaDX7AYp1RN/4Y/I2Susosi458MpyGxCaPDUGTGKWV318bOtcA== X-Received: by 2002:a05:600c:3d0f:b0:38e:bc5f:5515 with SMTP id bh15-20020a05600c3d0f00b0038ebc5f5515mr11179473wmb.128.1651501134124; Mon, 02 May 2022 07:18:54 -0700 (PDT) Received: from josua-work.lan (bzq-82-81-222-124.cablep.bezeqint.net. [82.81.222.124]) by smtp.gmail.com with ESMTPSA id t1-20020adfba41000000b0020c6fa5a797sm940035wrg.91.2022.05.02.07.18.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:53 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Stefan Roese , Sven Auhagen , Simon Glass Subject: [PATCH 06/12] cmd: tlv_eeprom: move missing declarations and defines to header Date: Mon, 2 May 2022 17:18:32 +0300 Message-Id: <20220502141838.15912-7-josua@solid-run.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220502141838.15912-1-josua@solid-run.com> References: <20220502141838.15912-1-josua@solid-run.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean 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 --- 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