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 6DAC0C433F5 for ; Mon, 2 May 2022 14:21:47 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6E49E83E3B; Mon, 2 May 2022 16:19:51 +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="JONg9uxq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9764A83E4C; Mon, 2 May 2022 16:19:25 +0200 (CEST) Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) (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 6853C83F99 for ; Mon, 2 May 2022 16:19:08 +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-wr1-x42c.google.com with SMTP id e24so19741440wrc.9 for ; Mon, 02 May 2022 07:19:08 -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=Tzq+ETJEokm5r43xPm2TPG82m9Ba7F+w6Hj7yUWmt+o=; b=JONg9uxqkHO2UzahAJvSIExunPgFmQrgokXOt47x1c4QZDFUWYyF0/sVZGRI7IoBEP XucDB+3wC+/Uvv8OMId1jbAVwTifHZOybPyuAqjoZACkJc4onmuJQHj/1zrj2kghbQM6 zSL6enMRd34R8suCjKSUGpYf5+bdtIS9LFQ7POCHrpIIxndHio1sdxi8Fp/nYzH/vkJL cY+6BsKiIpyYgPzaNUmu+HT8UvaG4TDstntItFKHB9SOMPg9yLMKE+BOLEV/Wk6L5OZO 5G15AuRiVAdn0n+fHw2ngxGliHevGaNkSnNVxxW7wP+RXsoU0KB0ICtXkpBsAidR4Quw P8ig== 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=Tzq+ETJEokm5r43xPm2TPG82m9Ba7F+w6Hj7yUWmt+o=; b=gNiPVair9w9YE+xrVmWNTF39iS4HkJ8mjsebTd3H2kglcMgX1Bv/+0uY9ujqhFuO6T 7lHadiBIGoV747srzYYtSCIJZ2OpTXgeI8Vn3EJpyUzlA/17fWDceRUx087ShLqrExnx 5apamqlFQeGWtgoI2br1L9ms1w0908g8MfpyIQ7kdW7uNgT4UYSt5F9wXo/SxJLWf9wO 83XOWACNXIFdiiah4FTVttaxtWm71NM+x8/YngFoN6pAuMvJuKhrKaurF93eKHaSdkea ztt7e0InzeQRVcgnrIY14UTfHPBaPD3BhyA6jesQ58WJtCcp7JzquB43GTRdBY6jcUR7 QZjQ== X-Gm-Message-State: AOAM530RjyQqMFGP9+1Lbsfg0D0v/AYEUR7vjxjy1DvJaxTbxV9yaPAq vl8l2FGNb5fRe2Q162EIY+8aOXx5FyN8kw== X-Google-Smtp-Source: ABdhPJwyBCt2xdUtfGo8886CwGyHFsjnS27HTStgisG4DIOKH3pYkiE2R0G95KcLgzGkS91xsMGhoQ== X-Received: by 2002:a05:6000:1889:b0:20c:617a:b087 with SMTP id a9-20020a056000188900b0020c617ab087mr4649803wri.102.1651501147712; Mon, 02 May 2022 07:19:07 -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.19.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:19:07 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Simon Glass , Stefan Roese , Sven Auhagen Subject: [PATCH 12/12] lib: tlv_eeprom: add function for reading one entry into a C string Date: Mon, 2 May 2022 17:18:38 +0300 Message-Id: <20220502141838.15912-13-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 This solves the potentially common problem of getting a specific tlv entry from an eeprom in board-files, without having to introduce several variables, error handling, memcpy and 0-terminating the string. Signed-off-by: Josua Mayer --- include/tlv_eeprom.h | 12 ++++++++++++ lib/tlv/tlv_eeprom.c | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/tlv_eeprom.h b/include/tlv_eeprom.h index c81c58837d..5989c611f5 100644 --- a/include/tlv_eeprom.h +++ b/include/tlv_eeprom.h @@ -166,6 +166,18 @@ bool tlvinfo_add_tlv(u8 *eeprom, int code, char *strval); */ bool tlvinfo_delete_tlv(u8 *eeprom, u8 code); +/** + * Read the TLV entry with specified code to a buffer as terminated C string. + * @eeprom: Pointer to buffer holding the TLV EEPROM binary data. + * @code: The TLV Code of the entry to read. + * @buffer: Pointer to buffer where the value will be stored. Must have capacity + * for the string representation of the data including null terminator. + * @length: size of the buffer where the value will be stored. + * + * Return length of string on success, -1 on error. + */ +ssize_t tlvinfo_read_tlv(u8 *eeprom, u8 code, u8 *buffer, size_t length); + /** * tlvinfo_update_crc * diff --git a/lib/tlv/tlv_eeprom.c b/lib/tlv/tlv_eeprom.c index 464f0aa1fa..205960e8f2 100644 --- a/lib/tlv/tlv_eeprom.c +++ b/lib/tlv/tlv_eeprom.c @@ -350,6 +350,31 @@ bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval) return true; } +/** + * Read the TLV entry with specified code to a buffer as terminated C string. + */ +ssize_t tlvinfo_read_tlv(u8 *eeprom, u8 code, u8 *buffer, size_t length) +{ + int index; + struct tlvinfo_tlv *tlv; + + // read sku from part-number field + if (tlvinfo_find_tlv(eeprom, code, &index)) { + tlv = (struct tlvinfo_tlv *)&eeprom[index]; + if (tlv->length > length) { + pr_err("%s: tlv value (%d) larger than buffer (%zu)!\n", + __func__, tlv->length + 1, length); + return -1; + } + memcpy(buffer, tlv->value, tlv->length); + buffer[tlv->length] = 0; + + return tlv->length; + } + + return -1; +} + /** * set_mac * -- 2.34.1