From patchwork Mon May 2 14:18:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625116 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=waD2KcFG; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQFf60qwz9sFr for ; Tue, 3 May 2022 00:19:18 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A238083DCA; 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=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="waD2KcFG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D299D83D07; Mon, 2 May 2022 16:18:50 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) (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 4C58883D07 for ; Mon, 2 May 2022 16:18:46 +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-x32e.google.com with SMTP id m62so8338068wme.5 for ; Mon, 02 May 2022 07:18:46 -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=I3lswXrRt2H+Zwq0pjhmHOudDil+W1xnBromOqOyhk4=; b=waD2KcFGvt/8PzS76sY+XIDwklD2NpN8aPqgufQbrbRZK9bRAqV1B1c0sGOtiDmvw6 6DUVnwdVt59XamYCCXfqxvh45ERSs/jMnBqb96o1eQFgo3aPyPTr0t9CPWd3ltMa9sMZ VCnOrfVsPDJFnvS5cSbeTqWHAzEnHs7iVCfxDzdK02pCTMLP00bpiFHwIwmMhdpTbaa8 lAgYaa7xe8ROpfXqqGT4yxxrW7cAyQKZw+JdGvEGQR+MxrVD6qFNithARiLpnI0c8Pnf cpMPVDLY5FcqP0ggOIJsJquV2BO5ozfo3b4BpudR1T5nNW5zDfKC8CrcPCqv2dNfmd1u fHCA== 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=I3lswXrRt2H+Zwq0pjhmHOudDil+W1xnBromOqOyhk4=; b=XmNugY5buIAh+Ss+Hd0gzrTsyA9DAHlx/c4KbTcbFp8NcgB3qM8+9DZP0S+xvGQZwc Pgn+sEaki2ZIkGkP/YCiEbbNgiiE7oaf9eOzjDbkIu0s5HnlD1l7OS5TVvwEtuBdE3xY 7FlT5mG8HLFU1+8gAsDR7Yg9rd+GN87CtjxF0a+ZBUpN+6PqrM/ZssNzyzXaktbaYOpN y6tFmOx01CD4/M/4aLLEbLEl/ZAz1RomttTQGBmWE18KGaPeR6eELqLBq8NHGz4D4WTr gHiypkaNSYIP0DT+xOmS3ve2CuYIZNdAlFItJnr+k61qZUn74jHutyS+wEpbJOQJn/t9 nZ8w== X-Gm-Message-State: AOAM533FfRZ9Keifi+YNZLwPLRG7qHWAJtCLgfhnHzzgD5t9hjILqdZR f/Pso6wxv2zO70kMoqJQEwCl779l+bv6sA== X-Google-Smtp-Source: ABdhPJz8cDgrPg86gJRcq+9DDJ8YtCu4Atfb1uIOZniJCdNywa/5pJ6kogMdFDNv/M8s4eQy2CBzjw== X-Received: by 2002:a05:600c:22d2:b0:393:f4be:ea1f with SMTP id 18-20020a05600c22d200b00393f4beea1fmr11247854wmg.51.1651501125582; Mon, 02 May 2022 07:18:45 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:45 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Simon Glass , Stefan Roese , Sven Auhagen Subject: [PATCH 01/12] cmd: tlv_eeprom: remove use of global variable current_dev Date: Mon, 2 May 2022 17:18:27 +0300 Message-Id: <20220502141838.15912-2-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 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 Make tlv_eeprom command device selection an explicit parameter of all function calls. Signed-off-by: Josua Mayer Reviewed-by: Stefan Roese --- cmd/tlv_eeprom.c | 50 ++++++++++++++++++++++---------------------- include/tlv_eeprom.h | 3 ++- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index bf8d453dc5..f91c11b304 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -29,18 +29,18 @@ DECLARE_GLOBAL_DATA_PTR; /* File scope function prototypes */ static bool is_checksum_valid(u8 *eeprom); -static int read_eeprom(u8 *eeprom); -static void show_eeprom(u8 *eeprom); +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 void update_crc(u8 *eeprom); -static int prog_eeprom(u8 *eeprom); +static int prog_eeprom(int devnum, u8 *eeprom); static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index); 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); -static void show_tlv_devices(void); +static void show_tlv_devices(int current_dev); /* Set to 1 if we've read EEPROM into memory */ static int has_been_read; @@ -48,7 +48,6 @@ static int has_been_read; static u8 eeprom[TLV_INFO_MAX_LEN]; static struct udevice *tlv_devices[MAX_TLV_DEVICES]; -static unsigned int current_dev; #define to_header(p) ((struct tlvinfo_header *)p) #define to_entry(p) ((struct tlvinfo_tlv *)p) @@ -125,7 +124,7 @@ static bool is_checksum_valid(u8 *eeprom) * * Read the EEPROM into memory, if it hasn't already been read. */ -static int read_eeprom(u8 *eeprom) +static int read_eeprom(int devnum, u8 *eeprom) { int ret; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); @@ -135,12 +134,11 @@ static int read_eeprom(u8 *eeprom) return 0; /* Read the header */ - ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, current_dev); + ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_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, - be16_to_cpu(eeprom_hdr->totallen), - current_dev); + be16_to_cpu(eeprom_hdr->totallen), devnum); // If the contents are invalid, start over with default contents if (!is_valid_tlvinfo_header(eeprom_hdr) || @@ -165,7 +163,7 @@ static int read_eeprom(u8 *eeprom) * * Display the contents of the EEPROM */ -static void show_eeprom(u8 *eeprom) +static void show_eeprom(int devnum, u8 *eeprom) { int tlv_end; int curr_tlv; @@ -180,7 +178,7 @@ static void show_eeprom(u8 *eeprom) return; } - printf("TLV: %u\n", current_dev); + printf("TLV: %u\n", devnum); printf("TlvInfo Header:\n"); printf(" Id String: %s\n", eeprom_hdr->signature); printf(" Version: %d\n", eeprom_hdr->version); @@ -389,7 +387,7 @@ static void update_crc(u8 *eeprom) * * Write the EEPROM data from CPU memory to the hardware. */ -static int prog_eeprom(u8 *eeprom) +static int prog_eeprom(int devnum, u8 *eeprom) { int ret = 0; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); @@ -398,7 +396,7 @@ static int prog_eeprom(u8 *eeprom) update_crc(eeprom); eeprom_len = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen); - ret = write_tlv_eeprom(eeprom, eeprom_len); + ret = write_tlv_eeprom(eeprom, eeprom_len, devnum); if (ret) { printf("Programming failed.\n"); return -1; @@ -433,11 +431,12 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { char cmd; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + static unsigned int current_dev; // If no arguments, read the EERPOM and display its contents if (argc == 1) { - read_eeprom(eeprom); - show_eeprom(eeprom); + read_eeprom(current_dev, eeprom); + show_eeprom(current_dev, eeprom); return 0; } @@ -448,7 +447,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) // Read the EEPROM contents if (cmd == 'r') { has_been_read = 0; - if (!read_eeprom(eeprom)) + if (!read_eeprom(current_dev, eeprom)) printf("EEPROM data loaded from device to memory.\n"); return 0; } @@ -463,7 +462,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) if (argc == 2) { switch (cmd) { case 'w': /* write */ - prog_eeprom(eeprom); + prog_eeprom(current_dev, eeprom); break; case 'e': /* erase */ strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING); @@ -476,7 +475,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) show_tlv_code_list(); break; case 'd': /* dev */ - show_tlv_devices(); + show_tlv_devices(current_dev); break; default: cmd_usage(cmdtp); @@ -886,7 +885,7 @@ static int set_bytes(char *buf, const char *string, int *converted_accum) return 0; } -static void show_tlv_devices(void) +static void show_tlv_devices(int current_dev) { unsigned int dev; @@ -956,14 +955,14 @@ int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num) /** * write_tlv_eeprom - write the hwinfo to i2c EEPROM */ -int write_tlv_eeprom(void *eeprom, int len) +int write_tlv_eeprom(void *eeprom, int len, int dev) { if (!(gd->flags & GD_FLG_RELOC)) return -ENODEV; - if (!tlv_devices[current_dev]) + if (!tlv_devices[dev]) return -ENODEV; - return i2c_eeprom_write(tlv_devices[current_dev], 0, eeprom, len); + return i2c_eeprom_write(tlv_devices[dev], 0, eeprom, len); } int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, @@ -1018,10 +1017,11 @@ int mac_read_from_eeprom(void) int maccount; u8 macbase[6]; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + int devnum = 0; // TODO: support multiple EEPROMs puts("EEPROM: "); - if (read_eeprom(eeprom)) { + if (read_eeprom(devnum, eeprom)) { printf("Read failed.\n"); return -1; } @@ -1086,7 +1086,7 @@ int mac_read_from_eeprom(void) * * This function must be called after relocation. */ -int populate_serial_number(void) +int populate_serial_number(int devnum) { char serialstr[257]; int eeprom_index; @@ -1095,7 +1095,7 @@ int populate_serial_number(void) if (env_get("serial#")) return 0; - if (read_eeprom(eeprom)) { + if (read_eeprom(devnum, eeprom)) { printf("Read failed.\n"); return -1; } diff --git a/include/tlv_eeprom.h b/include/tlv_eeprom.h index a2c333e744..fd45e5f6eb 100644 --- a/include/tlv_eeprom.h +++ b/include/tlv_eeprom.h @@ -84,11 +84,12 @@ int read_tlv_eeprom(void *eeprom, int offset, int len, int dev); * write_tlv_eeprom - Write the entire EEPROM binary data to the hardware * @eeprom: Pointer to buffer to hold the binary data * @len : Maximum size of buffer + * @dev : EEPROM device to write * * Note: this routine does not validate the EEPROM data. * */ -int write_tlv_eeprom(void *eeprom, int len); +int write_tlv_eeprom(void *eeprom, int len, int dev); /** * read_tlvinfo_tlv_eeprom - Read the TLV from EEPROM, and validate From patchwork Mon May 2 14:18:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625118 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=oA+is2Q8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQG727R0z9sFr for ; Tue, 3 May 2022 00:19:43 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E237383F0A; Mon, 2 May 2022 16:19:12 +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="oA+is2Q8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 64BE383F05; Mon, 2 May 2022 16:18:54 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) (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 D693883DDC for ; Mon, 2 May 2022 16:18:47 +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-x32d.google.com with SMTP id n126-20020a1c2784000000b0038e8af3e788so8532341wmn.1 for ; Mon, 02 May 2022 07:18:47 -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=jpE+8U5CeIbRfOKTQ+ArhMSRJE6mOd6BkFUO7axoz/M=; b=oA+is2Q88QFov9JFfuLdVQDXLYdYSxuimOFF7SKCWOQhLOay6QEsca+EV5Vr6RV39e h96bh0/PcwyW+GFolLky5asRKd9qo070Cy6m9c5A7jZbE6OdgiaDp5dqEBvS40Sp9+8k pTjPqDNA9w4F6u2Ltkgouw4VHwD0bcptEB30hKwfZTFm5QBZhkZUkPgZkt6LoOO6LZk4 3XHo7FLfKLY+srXzFi53RiY5S4rxxMB9+mUeO4lX0LaFgCMIIQJDaebL77k5G7TbrZqM mKWvPQ76POIKAe2vbyBTrlccauFt9YyTZxoknW5fMJPxguoUVXJVWFXznvGGPNEQ3bl7 tP1w== 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=jpE+8U5CeIbRfOKTQ+ArhMSRJE6mOd6BkFUO7axoz/M=; b=GirBWKNx465LQM2IlZSlhwu+Cwx1mFViXL+NIOldqlf6qUcoBY/RTl6GUYFg37GxqR dDx2UJ3AeTc5iJE4YIc/dBcPBuSLn4TEWTNrwsf5IXFxRWPJwi8bROTfbbI5tJIXhANm 1mzVJrnXjOEL4ebhPsxl8owHTkDuX1dJgT2TrMF3DbaxmXxkWIOQorsB7Qas44ll4JEm YR77CHgxawCSAJA+Y/PHfBGJTumMT75ZgPB9d8iVZi4uyDSNjPW242YxJdkc8CpRnTBe X32BGRqCt0m6XrgNFyljzpKTdRnONnTuc6OBCL+DO/AuziTaRg3Js3q+Dk0FX+PnP2KI nRfQ== X-Gm-Message-State: AOAM531I4n2VwdtA+NLGgVQcwaXJ2vCswHKBbNyFS1/c4GL48TUjcNM8 jasbHtwev+jgC6ecpGGJHJYTdcZowD2sBg== X-Google-Smtp-Source: ABdhPJzbObItBwUlscEIoSvk6L+cxNee4F2AcosgSLG6mayMPpeOhfxVaKk6R2I1EN+tXZX64wxRLw== X-Received: by 2002:a05:600c:3ca7:b0:38e:50d2:27fe with SMTP id bg39-20020a05600c3ca700b0038e50d227femr15112549wmb.159.1651501127270; Mon, 02 May 2022 07:18:47 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:46 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Stefan Roese , Simon Glass , Sven Auhagen Subject: [PATCH 02/12] cmd: tlv_eeprom: remove use of global variable has_been_read Date: Mon, 2 May 2022 17:18:28 +0300 Message-Id: <20220502141838.15912-3-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 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 has_been_read is only used as an optimization for do_tlv_eeprom. Explicitly use and set inside this function, thus making read_eeprom stateless. Signed-off-by: Josua Mayer Reviewed-by: Stefan Roese --- cmd/tlv_eeprom.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index f91c11b304..bfd4882e0d 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -42,8 +42,6 @@ static int set_date(char *buf, const char *string); static int set_bytes(char *buf, const char *string, int *converted_accum); static void show_tlv_devices(int current_dev); -/* Set to 1 if we've read EEPROM into memory */ -static int has_been_read; /* The EERPOM contents after being read into memory */ static u8 eeprom[TLV_INFO_MAX_LEN]; @@ -130,9 +128,6 @@ static int read_eeprom(int devnum, u8 *eeprom) struct tlvinfo_header *eeprom_hdr = to_header(eeprom); struct tlvinfo_tlv *eeprom_tlv = to_entry(&eeprom[HDR_SIZE]); - if (has_been_read) - return 0; - /* Read the header */ ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, devnum); /* If the header was successfully read, read the TLVs */ @@ -149,10 +144,8 @@ static int read_eeprom(int devnum, u8 *eeprom) update_crc(eeprom); } - has_been_read = 1; - #ifdef DEBUG - show_eeprom(eeprom); + show_eeprom(devnum, eeprom); #endif return ret; @@ -432,10 +425,15 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) char cmd; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); static unsigned int current_dev; + /* Set to devnum if we've read EEPROM into memory */ + static int has_been_read = -1; // If no arguments, read the EERPOM and display its contents if (argc == 1) { - read_eeprom(current_dev, eeprom); + if (has_been_read != current_dev) { + if (read_eeprom(current_dev, eeprom) == 0) + has_been_read = current_dev; + } show_eeprom(current_dev, eeprom); return 0; } @@ -446,14 +444,16 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) // Read the EEPROM contents if (cmd == 'r') { - has_been_read = 0; - if (!read_eeprom(current_dev, eeprom)) + has_been_read = -1; + if (read_eeprom(current_dev, eeprom) == 0) { printf("EEPROM data loaded from device to memory.\n"); + has_been_read = current_dev; + } return 0; } // Subsequent commands require that the EEPROM has already been read. - if (!has_been_read) { + if (has_been_read != current_dev) { printf("Please read the EEPROM data first, using the 'tlv_eeprom read' command.\n"); return 0; } @@ -509,7 +509,6 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return 0; } current_dev = devnum; - has_been_read = 0; } else { cmd_usage(cmdtp); } From patchwork Mon May 2 14:18:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625117 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=gf9aD7x3; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQFt4J9vz9sFr for ; Tue, 3 May 2022 00:19:30 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 622DE83F3E; Mon, 2 May 2022 16:19:04 +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="gf9aD7x3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 68BB483E2B; Mon, 2 May 2022 16:18:55 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) (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 8121783DCA for ; Mon, 2 May 2022 16:18:49 +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-x330.google.com with SMTP id n126-20020a1c2784000000b0038e8af3e788so8532396wmn.1 for ; Mon, 02 May 2022 07:18:49 -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=R+Xq6j8xj8SUJtOPYP8uJ3Z1GfGTgPXg+SElUTKb+iY=; b=gf9aD7x3pJ5x5UgquPHAOT30Bx8lCfyThoud/AbRZpadogxxBd6h1OXXr1sfudWOxi hrE+3vuqZ8jeDSd2Lo2xQxUFP9eqVR8mWPEnElqP7yzjT9UmkB0b+Sb/pMvayayWqOvt 5kxIPgAX6pxg1ejDe0DY0G9Vahu0HzT/UbkFOQwIoOwUtSKzVlxAJ1IlzX4Nseba7AI6 EGAyF36/6BDhj7eIkDjOXu45NpcHDSIyecW3qkPcwrLjscywGQBCcf/jthHvgiXP7SQ8 L2TCOHVEOsOj3oLzy4fY333H1CRh3FTu+sLbJ8POHXTpQb2jcUukxn7dz/NoOshDa5a5 PkOA== 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=R+Xq6j8xj8SUJtOPYP8uJ3Z1GfGTgPXg+SElUTKb+iY=; b=1zBK2Uv5JAsvOqLmcqGkFm97yjr3PwYFbvdm5WIJ5E8jRDozsipLcTRa8e6q4SgJu7 NqFlzN8N4os8FOH0BgicbLYdLJxNHIEg2jr5cwVCeIPw1KRDk3sjhNryTJjjl4Me1Wz1 oaUxdKnQ3SjahbZ4UfdJ4xjXbQoN1fqDOc82SeCjTxNScjfsEb0mIvLladU2+4WWWBkT k/HiH8mIzmEG80RAQMCpDsd4fKnnscRaBXFKQBFYYYXLEhyYigI4pPLYxeuZsq05ji7A ppFXKmh0BE3DXIY34Be0wX7ax8MicZryy1PkVs9tdLzEY9t7PlXa/L57Mw+Oz6LZOBSw 8/GQ== X-Gm-Message-State: AOAM530DC6JTrgYB9+ZBeVrXwSWw371vPWwkzbo/jTwtewy/44ZK0Fnc WcveLYprGwrhLgxN7mbW//6Swpm0uwr+BA== X-Google-Smtp-Source: ABdhPJxtOAOHj3jJWQa0MjTn6Gl70obt1ec/SkgiKUlq7DYbaAihXruc+/KB/Ut4BxqDkEIWDXk5IA== X-Received: by 2002:a7b:c8d6:0:b0:394:25ff:2de with SMTP id f22-20020a7bc8d6000000b0039425ff02demr12202370wml.154.1651501128912; Mon, 02 May 2022 07:18:48 -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.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:48 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Simon Glass , Sven Auhagen , Stefan Roese Subject: [PATCH 03/12] cmd: tlv_eeprom: do_tlv_eeprom: stop using non-api read_eeprom function Date: Mon, 2 May 2022 17:18:29 +0300 Message-Id: <20220502141838.15912-4-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 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 the scope of do_tlv_eeprom, the error-checking provided by the read_eeprom function is not required. Instead use the API function read_tlv_eeprom. Signed-off-by: Josua Mayer Reviewed-by: Stefan Roese --- cmd/tlv_eeprom.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index bfd4882e0d..00c5b5f840 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -431,7 +431,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) // If no arguments, read the EERPOM and display its contents if (argc == 1) { if (has_been_read != current_dev) { - if (read_eeprom(current_dev, eeprom) == 0) + if (read_tlv_eeprom(eeprom, 0, TLV_INFO_MAX_LEN, current_dev) == 0) has_been_read = current_dev; } show_eeprom(current_dev, eeprom); @@ -445,7 +445,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) // Read the EEPROM contents if (cmd == 'r') { has_been_read = -1; - if (read_eeprom(current_dev, eeprom) == 0) { + if (read_tlv_eeprom(eeprom, 0, TLV_INFO_MAX_LEN, current_dev) == 0) { printf("EEPROM data loaded from device to memory.\n"); has_been_read = current_dev; } From patchwork Mon May 2 14:18:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625120 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=Pp8ntINz; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQHN1sXVz9sFr for ; Tue, 3 May 2022 00:20:48 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 46B3C83E0E; Mon, 2 May 2022 16:19:25 +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="Pp8ntINz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1200A83F18; Mon, 2 May 2022 16:19:06 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) (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 73B0883E4C for ; Mon, 2 May 2022 16:18: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=josua@solid-run.com Received: by mail-wr1-x42e.google.com with SMTP id w4so19735665wrg.12 for ; Mon, 02 May 2022 07:18:51 -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=PnvIjd7JJs13GraonEHvcsoLZPWxv/qyf3d1YW19Jxc=; b=Pp8ntINzCUFxui+Qi6tM1rUsjY9GoqxI3BvpFXTj+lLzctDXooSx4fwsYJXUrKx7O1 2T06SNDpOm1QGCAC6ivIT+dpP8Q7ZfV2+EwB/t7crGf73KluHqLFpz5CgwWIuvdtHtl9 Ncj6VX/QrpMFCA67jFrt65bb56lLDgPaHcsHZw0pI5LQGawneT35WCONRiogn5CJPCXm 9hjr+e9CNQmdWug3ngIQVEXbOGRLiKS4tddcuqnOG9mSa7FQHzUkiAwKVHWWmq0IcC4s DafHsf6cWfyKIE7GV58sXo3uA1G11dlp/uY28HCEkhQzK5X+trMry1stMWSI9p83z2NK tHIA== 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=PnvIjd7JJs13GraonEHvcsoLZPWxv/qyf3d1YW19Jxc=; b=M/fKVCNHrFijJ2A5Ms6YIqBM14Vppn2SqrUQIetYMu8cpJBtpxkzQgadw8bv+1M62H T2M/J7ZVfzawe/TkRg9y/QmiUEJCKVz1pwgsuc5Vd86e79y95rmcNgYMpdsgQsQ8DXLN oo6f+ZAMvXUKPegOIXWoy3AtH6LElqb037u/Aeb4chCYYxXxbg/LquvGkzWl+SNb9yG3 BIf8jwa1P3skFhIxmwbYp94GbLE/2JATEdZtn/kgYmCox5dHcf48yXBEUG0SfEs40eqW vXG6P04eQszGdkPXkBegz4VUU5MxkyQmqDpI4sZ6sIQu/YuGSkfNPB1ZJDB3VPLPjqPt xmKw== X-Gm-Message-State: AOAM532w+AGBzuOMjHkaSGlD9nOySq9yfrOibdtzrNauzPXqPYOGnzz2 QXn2wG2rylcRp2/BbpAFOXjfQPy48zbfSw== X-Google-Smtp-Source: ABdhPJwoMnMMQZKyO1RUSi+0DnHA/LwWuHnIsfaXPnlU3NrVYK7aSuiL5hF3tME1lGVprHqahpRC9w== X-Received: by 2002:a5d:4e82:0:b0:20a:dda9:1160 with SMTP id e2-20020a5d4e82000000b0020adda91160mr9688931wru.582.1651501130641; Mon, 02 May 2022 07:18:50 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:50 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Stefan Roese , Sven Auhagen , Simon Glass Subject: [PATCH 04/12] cmd: tlv_eeprom: convert functions used by command to api functions Date: Mon, 2 May 2022 17:18:30 +0300 Message-Id: <20220502141838.15912-5-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 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 - prog_eeprom: write_tlvinfo_tlv_eeprom - update_crc: tlvinfo_update_crc - is_valid_tlv: is_valid_tlvinfo_entry - is_checksum_valid: tlvinfo_check_crc Signed-off-by: Josua Mayer --- cmd/tlv_eeprom.c | 56 +++++++++++++++---------------------------- include/tlv_eeprom.h | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 37 deletions(-) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index 00c5b5f840..1b4f2537f6 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -28,13 +28,9 @@ DECLARE_GLOBAL_DATA_PTR; #define MAX_TLV_DEVICES 2 /* File scope function prototypes */ -static bool is_checksum_valid(u8 *eeprom); 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 void update_crc(u8 *eeprom); -static int prog_eeprom(int devnum, u8 *eeprom); -static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index); 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); @@ -58,18 +54,6 @@ static inline bool is_digit(char c) return (c >= '0' && c <= '9'); } -/** - * is_valid_tlv - * - * Perform basic sanity checks on a TLV field. The TLV is pointed to - * by the parameter provided. - * 1. The type code is not reserved (0x00 or 0xFF) - */ -static inline bool is_valid_tlv(struct tlvinfo_tlv *tlv) -{ - return((tlv->type != 0x00) && (tlv->type != 0xFF)); -} - /** * is_hex * @@ -83,14 +67,12 @@ static inline u8 is_hex(char p) } /** - * is_checksum_valid - * * Validate the checksum in the provided TlvInfo EEPROM data. First, * verify that the TlvInfo header is valid, then make sure the last * TLV is a CRC-32 TLV. Then calculate the CRC over the EEPROM data * and compare it to the value stored in the EEPROM CRC-32 TLV. */ -static bool is_checksum_valid(u8 *eeprom) +bool tlvinfo_check_crc(u8 *eeprom) { struct tlvinfo_header *eeprom_hdr = to_header(eeprom); struct tlvinfo_tlv *eeprom_crc; @@ -137,11 +119,11 @@ static int read_eeprom(int devnum, u8 *eeprom) // If the contents are invalid, start over with default contents if (!is_valid_tlvinfo_header(eeprom_hdr) || - !is_checksum_valid(eeprom)) { + !tlvinfo_check_crc(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); + tlvinfo_update_crc(eeprom); } #ifdef DEBUG @@ -183,7 +165,7 @@ static void show_eeprom(int devnum, u8 *eeprom) tlv_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen); while (curr_tlv < tlv_end) { eeprom_tlv = to_entry(&eeprom[curr_tlv]); - if (!is_valid_tlv(eeprom_tlv)) { + if (!is_valid_tlvinfo_entry(eeprom_tlv)) { printf("Invalid TLV field starting at EEPROM offset %d\n", curr_tlv); return; @@ -193,7 +175,7 @@ static void show_eeprom(int devnum, u8 *eeprom) } printf("Checksum is %s.\n", - is_checksum_valid(eeprom) ? "valid" : "invalid"); + tlvinfo_check_crc(eeprom) ? "valid" : "invalid"); #ifdef DEBUG printf("EEPROM dump: (0x%x bytes)", TLV_INFO_MAX_LEN); @@ -340,13 +322,13 @@ static void decode_tlv(struct tlvinfo_tlv *tlv) } /** - * update_crc + * tlvinfo_update_crc * * This function updates the CRC-32 TLV. If there is no CRC-32 TLV, then * one is added. This function should be called after each update to the * EEPROM structure, to make sure the CRC is always correct. */ -static void update_crc(u8 *eeprom) +void tlvinfo_update_crc(u8 *eeprom) { struct tlvinfo_header *eeprom_hdr = to_header(eeprom); struct tlvinfo_tlv *eeprom_crc; @@ -376,20 +358,20 @@ static void update_crc(u8 *eeprom) } /** - * prog_eeprom + * write_tlvinfo_tlv_eeprom * - * Write the EEPROM data from CPU memory to the hardware. + * Write the TLV data from CPU memory to the hardware. */ -static int prog_eeprom(int devnum, u8 *eeprom) +int write_tlvinfo_tlv_eeprom(void *eeprom, int dev) { int ret = 0; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); int eeprom_len; - update_crc(eeprom); + tlvinfo_update_crc(eeprom); eeprom_len = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen); - ret = write_tlv_eeprom(eeprom, eeprom_len, devnum); + ret = write_tlv_eeprom(eeprom, eeprom_len, dev); if (ret) { printf("Programming failed.\n"); return -1; @@ -462,13 +444,13 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) if (argc == 2) { switch (cmd) { case 'w': /* write */ - prog_eeprom(current_dev, eeprom); + write_tlvinfo_tlv_eeprom(eeprom, current_dev); break; case 'e': /* erase */ strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING); eeprom_hdr->version = TLV_INFO_VERSION; eeprom_hdr->totallen = cpu_to_be16(0); - update_crc(eeprom); + tlvinfo_update_crc(eeprom); printf("EEPROM data in memory reset.\n"); break; case 'l': /* list */ @@ -549,7 +531,7 @@ U_BOOT_CMD(tlv_eeprom, 4, 1, do_tlv_eeprom, * An offset from the beginning of the EEPROM is returned in the * eeprom_index parameter if the TLV is found. */ -static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index) +bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index) { struct tlvinfo_header *eeprom_hdr = to_header(eeprom); struct tlvinfo_tlv *eeprom_tlv; @@ -561,7 +543,7 @@ static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index) eeprom_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen); while (*eeprom_index < eeprom_end) { eeprom_tlv = to_entry(&eeprom[*eeprom_index]); - if (!is_valid_tlv(eeprom_tlv)) + if (!is_valid_tlvinfo_entry(eeprom_tlv)) return false; if (eeprom_tlv->type == tcode) return true; @@ -594,7 +576,7 @@ static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code) eeprom_hdr->totallen = cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) - tlength); - update_crc(eeprom); + tlvinfo_update_crc(eeprom); return true; } return false; @@ -695,7 +677,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); - update_crc(eeprom); + tlvinfo_update_crc(eeprom); return true; } @@ -986,7 +968,7 @@ int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, be16_to_cpu(tlv_hdr->totallen), dev_num); if (ret < 0) return ret; - if (!is_checksum_valid(eeprom)) + if (!tlvinfo_check_crc(eeprom)) return -EINVAL; *hdr = tlv_hdr; diff --git a/include/tlv_eeprom.h b/include/tlv_eeprom.h index fd45e5f6eb..30626a1067 100644 --- a/include/tlv_eeprom.h +++ b/include/tlv_eeprom.h @@ -111,6 +111,51 @@ 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); +/** + * Write TLV data to the EEPROM. + * + * - Only writes length of actual tlv data + * - updates checksum + * + * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer + * of size at least TLV_INFO_MAX_LEN. + * @dev : EEPROM device to write + * + */ +int write_tlvinfo_tlv_eeprom(void *eeprom, int dev); + +/** + * tlvinfo_find_tlv + * + * This function finds the TLV with the supplied code in the EERPOM. + * An offset from the beginning of the EEPROM is returned in the + * eeprom_index parameter if the TLV is found. + */ +bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index); + +/** + * tlvinfo_update_crc + * + * This function updates the CRC-32 TLV. If there is no CRC-32 TLV, then + * one is added. This function should be called after each update to the + * EEPROM structure, to make sure the CRC is always correct. + * + * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer + * of size at least TLV_INFO_MAX_LEN. + */ +void tlvinfo_update_crc(u8 *eeprom); + +/** + * Validate the checksum in the provided TlvInfo EEPROM data. First, + * verify that the TlvInfo header is valid, then make sure the last + * TLV is a CRC-32 TLV. Then calculate the CRC over the EEPROM data + * and compare it to the value stored in the EEPROM CRC-32 TLV. + * + * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer + * of size at least TLV_INFO_MAX_LEN. + */ +bool tlvinfo_check_crc(u8 *eeprom); + #else /* !CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */ static inline int read_tlv_eeprom(void *eeprom, int offset, int len, int dev) @@ -150,4 +195,16 @@ static inline bool is_valid_tlvinfo_header(struct tlvinfo_header *hdr) (be16_to_cpu(hdr->totallen) <= TLV_TOTAL_LEN_MAX)); } +/** + * is_valid_tlv + * + * Perform basic sanity checks on a TLV field. The TLV is pointed to + * by the parameter provided. + * 1. The type code is not reserved (0x00 or 0xFF) + */ +static inline bool is_valid_tlvinfo_entry(struct tlvinfo_tlv *tlv) +{ + return((tlv->type != 0x00) && (tlv->type != 0xFF)); +} + #endif /* __TLV_EEPROM_H_ */ From patchwork Mon May 2 14:18:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625119 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=eDxTaUPf; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQGM1rfjz9sFr for ; Tue, 3 May 2022 00:19:55 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6F7FF83E57; Mon, 2 May 2022 16:19:17 +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="eDxTaUPf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 01C9083F31; Mon, 2 May 2022 16:18:59 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) (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 EE98183EA1 for ; Mon, 2 May 2022 16:18:52 +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-x32a.google.com with SMTP id k126so4945912wme.2 for ; Mon, 02 May 2022 07:18:52 -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=qbO6aL6G+/vzLp7Yljhst3oH1eKkmIu+Wvi18N1cmeo=; b=eDxTaUPfHoVuEHSR6ukhS/9zkNwXHYwF+UYAwNZ/NlCbWkWBs0/iSSVDAyWwhdxIHL IHF8DIMre+gB/HWN1XhxQp8UEdVjYHijplUsYZcN9BWhzCFonDLfAKIk5pAAGqU6RRIs qMWA5eDRrjxcG56XjEnSaJchqjC77r3kbak74BhYwmwwd/X869H5FLHdK0HdMxHdlYp8 bqSDKK/aWklp2swl8aOV4a+ljftOUN0B/Lrk/oy1hx2rdVR87e12drJBG2AFkr46heh5 WYYPg3gJR8dOeLArDleNimYOsaRkPFFtcz4nBIIAag8ZbYBWsth8PfDc/WjS4C5JjgHP OkJg== 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=qbO6aL6G+/vzLp7Yljhst3oH1eKkmIu+Wvi18N1cmeo=; b=E8xU7VIOCfW0zT4FcSAMUy408YjMuK7oSwWYzZeoK6o+WETkOoMSpCg8Ed4nvOKC/s gbA0weoa5Y/nhvp0aTK6grkOk0nj3RKJ/ERiNAD7g6IG73ydspFLIq5YiSLyZwKf/mi+ y6WRVOvzFv4k1hT23vrNe7Wk909Tm8hM8jAjVlyHMJIfc4r6MPX6qjNoZiMG3weMNo3y rnXa7TJEAID69Is5JADELHelE9shIcLyfKGkCCAMowlbydH8o/Zm6XsUdaRCIfy/Qpe2 79dNG2kJWxCe3JUZSeExC5HnYsYyHdtF7v8G5eEC6C3HBJB/M9FblJPd816vfkehAtzC /GJQ== X-Gm-Message-State: AOAM531X6dfJFkIi/HX2CnwNwLqsP4yo/iSOsXHi0pCXFjusJu1vUbxt U5HgJjuwq4jDXUZlnQvWbesBCVTR+EE/Uw== X-Google-Smtp-Source: ABdhPJyyMAf4LTvEox82+2nOwzAQ0T6RnxihSm2O3CDMmfgox63Q05VrlHbgUCC2xojU+9jPlv5T+A== X-Received: by 2002:a05:600c:3494:b0:394:3fd1:2228 with SMTP id a20-20020a05600c349400b003943fd12228mr3015566wmq.65.1651501132317; Mon, 02 May 2022 07:18:52 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:51 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer Subject: [PATCH 05/12] cmd: tlv_eeprom: remove empty function implementations from header Date: Mon, 2 May 2022 17:18:31 +0300 Message-Id: <20220502141838.15912-6-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 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 tlv_eeprom exposed functions are independent from platforms, hence no stubs are required. Signed-off-by: Josua Mayer --- include/tlv_eeprom.h | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/include/tlv_eeprom.h b/include/tlv_eeprom.h index 30626a1067..55fd72d6d2 100644 --- a/include/tlv_eeprom.h +++ b/include/tlv_eeprom.h @@ -65,7 +65,8 @@ struct __attribute__ ((__packed__)) tlvinfo_tlv { #define TLV_CODE_VENDOR_EXT 0xFD #define TLV_CODE_CRC_32 0xFE -#if CONFIG_IS_ENABLED(CMD_TLV_EEPROM) +/* how many EEPROMs can be used */ +#define TLV_MAX_DEVICES 2 /** * read_tlv_eeprom - Read the EEPROM binary data from the hardware @@ -156,27 +157,6 @@ void tlvinfo_update_crc(u8 *eeprom); */ bool tlvinfo_check_crc(u8 *eeprom); -#else /* !CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */ - -static inline int read_tlv_eeprom(void *eeprom, int offset, int len, int dev) -{ - return -ENOSYS; -} - -static inline int write_tlv_eeprom(void *eeprom, int len) -{ - return -ENOSYS; -} - -static inline int -read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, - struct tlvinfo_tlv **first_entry, int dev) -{ - return -ENOSYS; -} - -#endif /* CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */ - /** * is_valid_tlvinfo_header * From patchwork Mon May 2 14:18:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625121 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=gtU/uARt; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQHX2qp5z9sFr for ; Tue, 3 May 2022 00:20:56 +1000 (AEST) 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) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 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 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 * From patchwork Mon May 2 14:18:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625123 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=CBu6BbOC; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQHw4srTz9sFr for ; Tue, 3 May 2022 00:21:16 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5301983F4C; Mon, 2 May 2022 16:19:40 +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="CBu6BbOC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 16B2D83E2B; Mon, 2 May 2022 16:19:15 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) (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 D1C2F83E57 for ; Mon, 2 May 2022 16:18:56 +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-x334.google.com with SMTP id a14-20020a7bc1ce000000b00393fb52a386so10271522wmj.1 for ; Mon, 02 May 2022 07:18:56 -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=9+JoX+a5/XHvP+SPO9QZQSty5KA5/c2sKXAMSgLwCO8=; b=CBu6BbOCo/eFr8YVYkpukjCRloKDOhNdkRVTCZHL1GrZ1D2OuVAyQYOkVyDyCg8rrn IPILg7DvmtFCbLgrWor1as7itYDv9fSwRPrvneqBtFZH8yBpCXslcmSDcBSRA3tD0Tp7 KZB+YJdW26MxGh+e8Lq2L3hQRIqKc3bzmen75qZbapceDcHoAW7SIBchf5aeRIlfjucU 3wRMWgM0svPcDvv+R0MPfWbeVDrXwcyN+51q0DDjr5KH2Z5NZEEc6NNqcFORhNIVFJYF Le29Y+VfDEAly4a0s4+n/fN4ZOnj3vZTtdCAoRzB47w15zRhnX0/IMFUT2zThzKawc9Q MtHg== 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=9+JoX+a5/XHvP+SPO9QZQSty5KA5/c2sKXAMSgLwCO8=; b=ONj5YtWcRgQUCNmiZ/Amx+ymOTw5OjmC0X2gXF0/HfbYnzk/m0cb/hyiiI60Rx2Idu Lcc4sQplWlMNgy1DVfxbEGnwQo6TgqLpCjG06RK9+0Czwg+4NPElFvA+QAV+VIgE/Oi8 XCfHYyqBdvoNye0XOA/CtRgciy3UgmgJEVnaXQzvxtYVya2yF2cNhowGY8tl9CkjJwam iZOGHPQUbLloHMH3UD3utsPDsc6D8ehoPo5uN53i+E1tF2LZ7hEic9c+NmrL7STu1iMt 8WYgiuWmyDIrAAom/61YbQkPg4j5JBTi4cfU0b58FcR25s/ffIoGGtgSKSYmvaacSZG7 MDKQ== X-Gm-Message-State: AOAM530KNT+4gzSk2Ad/8vLTdADp07rYjOaV9zq1ysqOcv+NBngv7HG3 Re/kAxeto1O/YqJOGpP8nixJRW8rqjjACA== X-Google-Smtp-Source: ABdhPJyluSCIoePr6nDWiBCg/b+AzTqACMAuNriscNcXcgBEdYh0qB2YLa/IqQKu7qJ02x5tF+ArfQ== X-Received: by 2002:a05:600c:48b:b0:394:2ee9:5847 with SMTP id d11-20020a05600c048b00b003942ee95847mr10106777wme.117.1651501136156; Mon, 02 May 2022 07:18:56 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:55 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Sven Auhagen , Simon Glass , Stefan Roese Subject: [PATCH 07/12] cmd: tlv_eeprom: hide access to static tlv_devices array behind accessor Date: Mon, 2 May 2022 17:18:33 +0300 Message-Id: <20220502141838.15912-8-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 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 The tlv_eeprom command logic checks the static tlv_devices array to validate the eeprom number. This array will be move to a separate tlv library. Hide this access behind a new function exists_tlv_eeprom. Signed-off-by: Josua Mayer --- cmd/tlv_eeprom.c | 12 ++++++++++-- include/tlv_eeprom.h | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index 57468edb1c..f51a9666bf 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -44,6 +44,14 @@ static struct udevice *tlv_devices[MAX_TLV_DEVICES]; #define to_header(p) ((struct tlvinfo_header *)p) #define to_entry(p) ((struct tlvinfo_tlv *)p) +/** + * Check whether eeprom device exists. + */ +bool exists_tlv_eeprom(int dev) +{ + return dev < TLV_MAX_DEVICES && tlv_devices[dev] != 0; +} + static inline bool is_digit(char c) { return (c >= '0' && c <= '9'); @@ -481,7 +489,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) unsigned int devnum; devnum = simple_strtoul(argv[2], NULL, 0); - if (devnum > MAX_TLV_DEVICES || !tlv_devices[devnum]) { + if (!exists_tlv_eeprom(devnum)) { printf("Invalid device number\n"); return 0; } @@ -866,7 +874,7 @@ static void show_tlv_devices(int current_dev) unsigned int dev; for (dev = 0; dev < MAX_TLV_DEVICES; dev++) - if (tlv_devices[dev]) + if (exists_tlv_eeprom(dev)) printf("TLV: %u%s\n", dev, (dev == current_dev) ? " (*)" : ""); } diff --git a/include/tlv_eeprom.h b/include/tlv_eeprom.h index dc7952da6b..c81c58837d 100644 --- a/include/tlv_eeprom.h +++ b/include/tlv_eeprom.h @@ -69,6 +69,13 @@ struct __attribute__ ((__packed__)) tlvinfo_tlv { /* how many EEPROMs can be used */ #define TLV_MAX_DEVICES 2 +/** + * Check whether eeprom device exists. + * + * @dev: EEPROM device to check. + */ +bool exists_tlv_eeprom(int dev); + /** * read_tlv_eeprom - Read the EEPROM binary data from the hardware * @eeprom: Pointer to buffer to hold the binary data From patchwork Mon May 2 14:18:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625122 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=Tshe6O4e; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQHk45L5z9sFr for ; Tue, 3 May 2022 00:21:06 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4FC8283F48; Mon, 2 May 2022 16:19:36 +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="Tshe6O4e"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EA37E83F41; Mon, 2 May 2022 16:19:14 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) (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 8592283E2B for ; Mon, 2 May 2022 16:18:58 +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-x42f.google.com with SMTP id x18so19811688wrc.0 for ; Mon, 02 May 2022 07:18:58 -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=wGMHe/PCd4/rGZ6MJVjLTBbGFAYDP8InsfnSbSBeXvU=; b=Tshe6O4eaS6kVffK4b4/ykSOVIJZrIR48HGdZ1m4un/YFkeXx6FHK7mkux9+y9u4YJ MUKBcHpSbjYHURjlqy1nBbBxZEStmWT4Su7gVufSb6XJmvgXqKIEpVfkASyECJ5/gcp/ b/0/cotqJzCDmk3GGQLw4fwD5N9uqW/1iYxg8GLd5quFhBEDrEeXK81gePHTj/zftyMK Lcwm8VZyW16vu35XL12+aD72epN22BjvvxHOpuaB5qg+L5nwMrvUD74oSdJeHECol4fO q0IHqgMH5bl7BIo8fOdKFLCTMB8bveyQ/h1tkn3vC6LUeGztfpf+0SSrsJOUWtz5HRhU Oz3Q== 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=wGMHe/PCd4/rGZ6MJVjLTBbGFAYDP8InsfnSbSBeXvU=; b=Y0QYz3pm09esCrnkhkFRexuwJyoso9jpZ6uzIJZFfC0Yyewtf7OSWx+ReSs+QGVNCp nAiCHlMVMrT9WUYGPJyf4MEMS1rkh3+laulUNJVUUeiSKOr3/OtlM8FHBNqiY4a4xvb/ n5bYH7yVRBkCZ3tPL63RiI1GOeqIAbW3RZfRaYBoUCj27TnAb/+Os02+PHBA+ijOXI9S +AYBliZrIkwo/WP4bpK4c+B6f/R+gzXjTaCBI/X8vopcFlqkiOIMZruM66CyWXbafBnw AZZ7QD0LiCXPyF68Wx81dN6MeMvpFYODCGhO1KsFNJBbjLP2nQP7srY8nVlo7coCxaEU IYyw== X-Gm-Message-State: AOAM531FkfklfELxDNLSpmrjlAay5AwBhWXmHnzS2GBbi1cGtVxPB6qG ineHsckgqT/kwMyrCF1ePXHyfBqT3pFMpA== X-Google-Smtp-Source: ABdhPJyJ+7rwcuQyVYvStCQKmCc2+bzqhTx0yd8wAgDlfH12c/ZVrr6tto1rIYIz2JoRpEakwe7m4A== X-Received: by 2002:a05:6000:186f:b0:20c:5f3d:44a4 with SMTP id d15-20020a056000186f00b0020c5f3d44a4mr5099406wri.152.1651501137850; Mon, 02 May 2022 07:18:57 -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.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:57 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Simon Glass , Stefan Roese , Sven Auhagen Subject: [PATCH 08/12] cmd: tlv_eeprom: clean up two defines for one thing Date: Mon, 2 May 2022 17:18:34 +0300 Message-Id: <20220502141838.15912-9-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 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 MAX_TLV_DEVICES defined in C, and TLV_MAX_DEVICES defined in the header serve the same purpose. Replace all occurences of the former by the latter. Signed-off-by: Josua Mayer --- cmd/tlv_eeprom.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index f51a9666bf..c110927cb5 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -25,8 +25,6 @@ DECLARE_GLOBAL_DATA_PTR; -#define MAX_TLV_DEVICES 2 - /* File scope function prototypes */ static int read_eeprom(int devnum, u8 *eeprom); static void show_eeprom(int devnum, u8 *eeprom); @@ -39,7 +37,7 @@ static void show_tlv_devices(int current_dev); /* The EERPOM contents after being read into memory */ static u8 eeprom[TLV_INFO_MAX_LEN]; -static struct udevice *tlv_devices[MAX_TLV_DEVICES]; +static struct udevice *tlv_devices[TLV_MAX_DEVICES]; #define to_header(p) ((struct tlvinfo_header *)p) #define to_entry(p) ((struct tlvinfo_tlv *)p) @@ -873,7 +871,7 @@ static void show_tlv_devices(int current_dev) { unsigned int dev; - for (dev = 0; dev < MAX_TLV_DEVICES; dev++) + for (dev = 0; dev < TLV_MAX_DEVICES; dev++) if (exists_tlv_eeprom(dev)) printf("TLV: %u%s\n", dev, (dev == current_dev) ? " (*)" : ""); @@ -890,7 +888,7 @@ static int find_tlv_devices(struct udevice **tlv_devices_p) ret = uclass_next_device_check(&dev)) { if (ret == 0) tlv_devices_p[count_dev++] = dev; - if (count_dev >= MAX_TLV_DEVICES) + if (count_dev >= TLV_MAX_DEVICES) break; } @@ -899,7 +897,7 @@ static int find_tlv_devices(struct udevice **tlv_devices_p) static struct udevice *find_tlv_device_by_index(int dev_num) { - struct udevice *local_tlv_devices[MAX_TLV_DEVICES] = {}; + struct udevice *local_tlv_devices[TLV_MAX_DEVICES] = {}; struct udevice **tlv_devices_p; int ret; @@ -926,7 +924,7 @@ int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num) { struct udevice *dev; - if (dev_num >= MAX_TLV_DEVICES) + if (dev_num >= TLV_MAX_DEVICES) return -EINVAL; dev = find_tlv_device_by_index(dev_num); From patchwork Mon May 2 14:18:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625125 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=D18B03dR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQJL5ck8z9sFr for ; Tue, 3 May 2022 00:21:38 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id ADDD083FF8; Mon, 2 May 2022 16:19:46 +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="D18B03dR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DB9C583F41; Mon, 2 May 2022 16:19:17 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) (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 2157D83F30 for ; Mon, 2 May 2022 16:19:00 +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-x434.google.com with SMTP id w4so19736176wrg.12 for ; Mon, 02 May 2022 07:19:00 -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=/fO23zuFWOLr+X0v+/1dwUZrwZxMAf4XBX/IXfQ6kiM=; b=D18B03dRA4N/fp+fSUkiy5wgS6Ii9HPyxn4wk3l65R9xOeSzQoIhuPejRncqOZXPVg C3JcYuDnv6t6zHI1B0n0wIi0g3pXTaIa8NYIOm+gQAdBhl1D7VS/JCoyOVkPDQNoY337 8l38TvMc1ia6ZyHQNPFoRHxNpwpkCYwGKt16Uu0m50CTUX0EWDueVwCY+qSEQnh4vInc vc6wqTH0VpOdr3Ewbgg1xBCK8gw66sl85mQlDDTYvnmsYmyPPuVtcq7c/HvqTqAKTV9J UqI2/+aGnU4Ux5TpZ8sLUEIaMjknmi1syUZeCOS1LI/GRJTzg82raUjVoZtep3o9w3Z+ qU8g== 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=/fO23zuFWOLr+X0v+/1dwUZrwZxMAf4XBX/IXfQ6kiM=; b=Nc3E4aQ59WPZKHck9sPh0yS7y90UG+3Sn9CkN7AeQkto5gJkY543GA14oP72yWErkd ab/Ahrzpcs/UJCQ5+1w4R49WYBRYxsPCpsSfCwN3VlehK7gk+AH9jSek/+wo3JC8EhMV RVGFs1xr2Yg2mz/z589Rk0RN1IiIOlTEOBD+iyTAkrFlP2thhIyHu9iFOLpgxmQ4qLgg rj5zxADUfSkaQ7y+OBIIfyxV9P22NQiQ/e6K5UN/5LlpzybPh/O2X2v1JC2i3RjBkplm vxAVCM9goVDGi2TClFFgKP3BS3NCNmZ1jWeyeAcJG7ztbUnCNoqAzOT6KgYiKchG8Mup cBiQ== X-Gm-Message-State: AOAM532oockSYSGerPu8ZikBNgXFo45PSXtlCabx79hSmyC7Qn/dbyPl 4+3Z+MmJKGyBJqHGJPLqkxnBOMLOvoLogA== X-Google-Smtp-Source: ABdhPJxobVPPVfhaYePmqqPnTVubXCInUU7dVTQul0x8caz3Fnskb2PPxu3V9PdnD2CN9k9KsDKilQ== X-Received: by 2002:a5d:64c1:0:b0:20c:6ff9:3a61 with SMTP id f1-20020a5d64c1000000b0020c6ff93a61mr1247661wri.709.1651501139509; Mon, 02 May 2022 07:18:59 -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.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:18:58 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Simon Glass , Sven Auhagen , Stefan Roese Subject: [PATCH 09/12] cmd: tlv_eeprom: add my copyright Date: Mon, 2 May 2022 17:18:35 +0300 Message-Id: <20220502141838.15912-10-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 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 While each of the previous individual changes is small, accumulated they amount to a substantial effort. Explicitly add a Copyright declaration. Signed-off-by: Josua Mayer --- cmd/tlv_eeprom.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index c110927cb5..c66116b2c4 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -7,6 +7,7 @@ * Copyright (C) 2014 Srideep * Copyright (C) 2013 Miles Tseng * Copyright (C) 2014,2016 david_yang + * Copyright (C) 2022 Josua Mayer */ #include From patchwork Mon May 2 14:18:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625127 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=XZPqqreu; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQJm0FjBz9sFr for ; Tue, 3 May 2022 00:21:59 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C5C7184004; Mon, 2 May 2022 16:19: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=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="XZPqqreu"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AC1AC83937; Mon, 2 May 2022 16:19:38 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) (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 8F5C783D07 for ; Mon, 2 May 2022 16:19:05 +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-x434.google.com with SMTP id t6so19790556wra.4 for ; Mon, 02 May 2022 07:19:05 -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=bVbHu4IFzWF4uHWOz6QY5f5gSWEloC4ft+MncTcpDyM=; b=XZPqqreuu2nN54cLb2v+2lf5V60UxFpPWzooypHTrGwq4UPSqGrOUpWPFsqXbdPNRV PxxX1jXlsqEjDC6c1lInoY8U4qixg52JLm+NrqtPJU7igkra24GxKSPIsht7p8IYBp4/ pDuo1fZ8trch6qoej7Yvfu0rE2kbLzaobuIaXIssvwRyRbApLq+PhS2du2DYPpLkvgns K+oAbqpGH4zQOkWM+I2+tMMUSMjjAdm+mIyues6BBE6vtnVjcqu9flOJKxidlnx8RUho /d4GfgsdQR0cLN5tVaEz+IVDhywWC5A1/jsNH7LJxdJXa+lFEHtefxflwYBIMwGqkxME YV0Q== 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=bVbHu4IFzWF4uHWOz6QY5f5gSWEloC4ft+MncTcpDyM=; b=rvIPNpGh4/KB7l6lSiNK/5zZYsw0WBaczLjEcCkivasANqLcYl/KeMZXAbT9LQYoMk zF1j2iQzzwjCJowlFX4pnK4dxDnmZ3EwCBaYb5PdXDQbGTXuYMrEcp+ydNr/JVazFuDE Bvyffn9yIb4InGmrpBYflQPxU5Nu7+hTWVLnIEmWUanUJ0cHH0MnWgvgqXWocAgeHONV /8Kqsx5Ak88L4aMaBPYHjlnQ65fvuhU0Qxt4ZIkdtTgpKaJTdT6ibzMeAWk2wX2/FOt3 UxPFxxCFco6+UPYPKtzSyyj3K4iXL+jgseyKiiBTGazpPwq1hMDUhiW1hkiAjTAG7L7F scnQ== X-Gm-Message-State: AOAM531eST+8HOMBP2JbUi9jXXpo2xwHu5lXAXQzmzs8VIktOezeBJXz P4zfEP4K9YlTmzr870cYyE1j3SiKofF2NgNa X-Google-Smtp-Source: ABdhPJwSFH5uGM4XuIyaOBGLzfDUqP62B+BHoakGHZJytbikviRk1ZP25OderRXDJlhZ6l5Z686cMQ== X-Received: by 2002:a5d:47a6:0:b0:20c:5f3d:44b8 with SMTP id 6-20020a5d47a6000000b0020c5f3d44b8mr5006087wrb.216.1651501143665; Mon, 02 May 2022 07:19:03 -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.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:19:03 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Simon Glass , Michal Simek , Kory Maincent , Ovidiu Panait , Ashok Reddy Soma , Stefan Roese , Sven Auhagen , Heinrich Schuchardt , Alexandru Gagniuc , Philippe Reynes , Bin Meng , Eugen Hristev , Masahisa Kojima , =?utf-8?q?Pali_Roh=C3=A1r?= , Loic Poulain , Patrick Delaunay Subject: [PATCH 10/12] cmd: tlv_eeprom: split off tlv library from command Date: Mon, 2 May 2022 17:18:36 +0300 Message-Id: <20220502141838.15912-11-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 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 The eeprom command includes functions for reading and writing tlv-formatted data from an eeprom, as well as an implementation of the cli command tlv_eeprom. Split off the parsing, read and write into a standalone tlv library. Signed-off-by: Josua Mayer --- cmd/Kconfig | 2 + cmd/tlv_eeprom.c | 742 +----------------------------------------- lib/Kconfig | 2 + lib/Makefile | 2 + lib/tlv/Kconfig | 15 + lib/tlv/Makefile | 5 + lib/tlv/tlv_eeprom.c | 750 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 786 insertions(+), 732 deletions(-) create mode 100644 lib/tlv/Kconfig create mode 100644 lib/tlv/Makefile create mode 100644 lib/tlv/tlv_eeprom.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 2b575a2b42..821b5e9d6b 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -166,6 +166,7 @@ config CMD_REGINFO config CMD_TLV_EEPROM bool "tlv_eeprom" + select EEPROM_TLV_LIB depends on I2C_EEPROM help Display and program the system EEPROM data block in ONIE Tlvinfo @@ -173,6 +174,7 @@ config CMD_TLV_EEPROM config SPL_CMD_TLV_EEPROM bool "tlv_eeprom for SPL" + select SPL_EEPROM_TLV_LIB depends on SPL_I2C_EEPROM select SPL_DRIVERS_MISC help diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index c66116b2c4..99b79cad8b 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -10,131 +10,26 @@ * Copyright (C) 2022 Josua Mayer */ -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "tlv_eeprom.h" - -DECLARE_GLOBAL_DATA_PTR; +#include +#include +#include +#include /* File scope function prototypes */ -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 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); static void show_tlv_devices(int current_dev); +static inline const char *tlv_type2name(u8 type); +static void decode_tlv(struct tlvinfo_tlv *tlv); +static int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); +static void show_tlv_code_list(void); /* The EERPOM contents after being read into memory */ static u8 eeprom[TLV_INFO_MAX_LEN]; -static struct udevice *tlv_devices[TLV_MAX_DEVICES]; - #define to_header(p) ((struct tlvinfo_header *)p) #define to_entry(p) ((struct tlvinfo_tlv *)p) -/** - * Check whether eeprom device exists. - */ -bool exists_tlv_eeprom(int dev) -{ - return dev < TLV_MAX_DEVICES && tlv_devices[dev] != 0; -} - -static inline bool is_digit(char c) -{ - return (c >= '0' && c <= '9'); -} - -/** - * is_hex - * - * Tests if character is an ASCII hex digit - */ -static inline u8 is_hex(char p) -{ - return (((p >= '0') && (p <= '9')) || - ((p >= 'A') && (p <= 'F')) || - ((p >= 'a') && (p <= 'f'))); -} - -/** - * Validate the checksum in the provided TlvInfo EEPROM data. First, - * verify that the TlvInfo header is valid, then make sure the last - * TLV is a CRC-32 TLV. Then calculate the CRC over the EEPROM data - * and compare it to the value stored in the EEPROM CRC-32 TLV. - */ -bool tlvinfo_check_crc(u8 *eeprom) -{ - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - struct tlvinfo_tlv *eeprom_crc; - unsigned int calc_crc; - unsigned int stored_crc; - - // Is the eeprom header valid? - if (!is_valid_tlvinfo_header(eeprom_hdr)) - return false; - - // Is the last TLV a CRC? - 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, - 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) | - eeprom_crc->value[3]; - return calc_crc == stored_crc; -} - -/** - * read_eeprom - * - * Read the EEPROM into memory, if it hasn't already been read. - */ -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[TLV_INFO_HEADER_SIZE]); - - /* Read the header */ - 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, TLV_INFO_HEADER_SIZE, - be16_to_cpu(eeprom_hdr->totallen), devnum); - - // If the contents are invalid, start over with default contents - if (!is_valid_tlvinfo_header(eeprom_hdr) || - !tlvinfo_check_crc(eeprom)) { - strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING); - eeprom_hdr->version = TLV_INFO_VERSION; - eeprom_hdr->totallen = cpu_to_be16(0); - tlvinfo_update_crc(eeprom); - } - -#ifdef DEBUG - show_eeprom(devnum, eeprom); -#endif - - return ret; -} - /** * show_eeprom * @@ -323,70 +218,10 @@ static void decode_tlv(struct tlvinfo_tlv *tlv) printf("%-20s 0x%02X %3d %s\n", name, tlv->type, tlv->length, value); } -/** - * tlvinfo_update_crc - * - * This function updates the CRC-32 TLV. If there is no CRC-32 TLV, then - * one is added. This function should be called after each update to the - * EEPROM structure, to make sure the CRC is always correct. - */ -void tlvinfo_update_crc(u8 *eeprom) -{ - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - struct tlvinfo_tlv *eeprom_crc; - unsigned int calc_crc; - int eeprom_index; - - // Discover the CRC TLV - if (!tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) { - unsigned int totallen = be16_to_cpu(eeprom_hdr->totallen); - - if ((totallen + TLV_INFO_ENTRY_SIZE + 4) > TLV_TOTAL_LEN_MAX) - return; - 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; - eeprom_crc->length = 4; - - // Calculate the checksum - calc_crc = crc32(0, (void *)eeprom, - 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; - eeprom_crc->value[3] = (calc_crc >> 0) & 0xFF; -} - -/** - * write_tlvinfo_tlv_eeprom - * - * Write the TLV data from CPU memory to the hardware. - */ -int write_tlvinfo_tlv_eeprom(void *eeprom, int dev) -{ - int ret = 0; - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - int eeprom_len; - - tlvinfo_update_crc(eeprom); - - 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"); - return -1; - } - - printf("Programming passed.\n"); - return 0; -} - /** * show_tlv_code_list - Display the list of TLV codes and names */ -void show_tlv_code_list(void) +static void show_tlv_code_list(void) { int i; @@ -404,7 +239,7 @@ void show_tlv_code_list(void) * * This function implements the tlv_eeprom command. */ -int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +static int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { char cmd; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); @@ -526,348 +361,6 @@ U_BOOT_CMD(tlv_eeprom, 4, 1, do_tlv_eeprom, " - List the understood TLV codes and names.\n" ); -/** - * tlvinfo_find_tlv - * - * This function finds the TLV with the supplied code in the EERPOM. - * An offset from the beginning of the EEPROM is returned in the - * eeprom_index parameter if the TLV is found. - */ -bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index) -{ - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - struct tlvinfo_tlv *eeprom_tlv; - int eeprom_end; - - // Search through the TLVs, looking for the first one which matches the - // supplied type code. - *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 += TLV_INFO_ENTRY_SIZE + eeprom_tlv->length; - } - return(false); -} - -/** - * tlvinfo_delete_tlv - * - * This function deletes the TLV with the specified type code from the - * EEPROM. - */ -bool tlvinfo_delete_tlv(u8 *eeprom, u8 code) -{ - int eeprom_index; - int tlength; - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - struct tlvinfo_tlv *eeprom_tlv; - - // 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 = TLV_INFO_ENTRY_SIZE + eeprom_tlv->length; - memcpy(&eeprom[eeprom_index], &eeprom[eeprom_index + tlength], - TLV_INFO_HEADER_SIZE + - be16_to_cpu(eeprom_hdr->totallen) - eeprom_index - - tlength); - eeprom_hdr->totallen = - cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) - - tlength); - tlvinfo_update_crc(eeprom); - return true; - } - return false; -} - -/** - * 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. - */ -#define MAX_TLV_VALUE_LEN 256 -bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval) -{ - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - struct tlvinfo_tlv *eeprom_tlv; - int new_tlv_len = 0; - u32 value; - char data[MAX_TLV_VALUE_LEN]; - int eeprom_index; - - // Encode each TLV type into the format to be stored in the EERPOM - switch (tcode) { - case TLV_CODE_PRODUCT_NAME: - case TLV_CODE_PART_NUMBER: - case TLV_CODE_SERIAL_NUMBER: - case TLV_CODE_LABEL_REVISION: - case TLV_CODE_PLATFORM_NAME: - case TLV_CODE_ONIE_VERSION: - case TLV_CODE_MANUF_NAME: - case TLV_CODE_MANUF_COUNTRY: - case TLV_CODE_VENDOR_NAME: - case TLV_CODE_DIAG_VERSION: - case TLV_CODE_SERVICE_TAG: - strncpy(data, strval, MAX_TLV_VALUE_LEN); - new_tlv_len = min_t(size_t, MAX_TLV_VALUE_LEN, strlen(strval)); - break; - case TLV_CODE_DEVICE_VERSION: - value = simple_strtoul(strval, NULL, 0); - if (value >= 256) { - printf("ERROR: Device version must be 255 or less. Value supplied: %u", - value); - return false; - } - data[0] = value & 0xFF; - new_tlv_len = 1; - break; - case TLV_CODE_MAC_SIZE: - value = simple_strtoul(strval, NULL, 0); - if (value >= 65536) { - printf("ERROR: MAC Size must be 65535 or less. Value supplied: %u", - value); - return false; - } - data[0] = (value >> 8) & 0xFF; - data[1] = value & 0xFF; - new_tlv_len = 2; - break; - case TLV_CODE_MANUF_DATE: - if (set_date(data, strval) != 0) - return false; - new_tlv_len = 19; - break; - case TLV_CODE_MAC_BASE: - if (set_mac(data, strval) != 0) - return false; - new_tlv_len = 6; - break; - case TLV_CODE_CRC_32: - printf("WARNING: The CRC TLV is set automatically and cannot be set manually.\n"); - return false; - case TLV_CODE_VENDOR_EXT: - default: - if (set_bytes(data, strval, &new_tlv_len) != 0) - return false; - break; - } - - // Is there room for this TLV? - 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; - } - - // Add TLV at the end, overwriting CRC TLV if it exists - if (tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) - eeprom_hdr->totallen = - cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) - - TLV_INFO_ENTRY_SIZE - 4); - else - 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; - memcpy(eeprom_tlv->value, data, new_tlv_len); - - // Update the total length and calculate (add) a new CRC-32 TLV - eeprom_hdr->totallen = cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) + - TLV_INFO_ENTRY_SIZE + new_tlv_len); - tlvinfo_update_crc(eeprom); - - return true; -} - -/** - * set_mac - * - * Converts a string MAC address into a binary buffer. - * - * This function takes a pointer to a MAC address string - * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number). - * The string format is verified and then converted to binary and - * stored in a buffer. - */ -static int set_mac(char *buf, const char *string) -{ - char *p = (char *)string; - int i; - int err = 0; - char *end; - - if (!p) { - printf("ERROR: NULL mac addr string passed in.\n"); - return -1; - } - - if (strlen(p) != 17) { - printf("ERROR: MAC address strlen() != 17 -- %zu\n", strlen(p)); - printf("ERROR: Bad MAC address format: %s\n", string); - return -1; - } - - for (i = 0; i < 17; i++) { - if ((i % 3) == 2) { - if (p[i] != ':') { - err++; - printf("ERROR: mac: p[%i] != :, found: `%c'\n", - i, p[i]); - break; - } - continue; - } else if (!is_hex(p[i])) { - err++; - printf("ERROR: mac: p[%i] != hex digit, found: `%c'\n", - i, p[i]); - break; - } - } - - if (err != 0) { - printf("ERROR: Bad MAC address format: %s\n", string); - return -1; - } - - /* Convert string to binary */ - for (i = 0, p = (char *)string; i < 6; i++) { - buf[i] = p ? hextoul(p, &end) : 0; - if (p) - p = (*end) ? end + 1 : end; - } - - if (!is_valid_ethaddr((u8 *)buf)) { - printf("ERROR: MAC address must not be 00:00:00:00:00:00, a multicast address or FF:FF:FF:FF:FF:FF.\n"); - printf("ERROR: Bad MAC address format: %s\n", string); - return -1; - } - - return 0; -} - -/** - * set_date - * - * Validates the format of the data string - * - * This function takes a pointer to a date string (i.e. MM/DD/YYYY hh:mm:ss) - * and validates that the format is correct. If so the string is copied - * to the supplied buffer. - */ -static int set_date(char *buf, const char *string) -{ - int i; - - if (!string) { - printf("ERROR: NULL date string passed in.\n"); - return -1; - } - - if (strlen(string) != 19) { - printf("ERROR: Date strlen() != 19 -- %zu\n", strlen(string)); - printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", - string); - return -1; - } - - for (i = 0; string[i] != 0; i++) { - switch (i) { - case 2: - case 5: - if (string[i] != '/') { - printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", - string); - return -1; - } - break; - case 10: - if (string[i] != ' ') { - printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", - string); - return -1; - } - break; - case 13: - case 16: - if (string[i] != ':') { - printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", - string); - return -1; - } - break; - default: - if (!is_digit(string[i])) { - printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", - string); - return -1; - } - break; - } - } - - strcpy(buf, string); - return 0; -} - -/** - * set_bytes - * - * Converts a space-separated string of decimal numbers into a - * buffer of bytes. - * - * This function takes a pointer to a space-separated string of decimal - * numbers (i.e. "128 0x55 0321") with "C" standard radix specifiers - * and converts them to an array of bytes. - */ -static int set_bytes(char *buf, const char *string, int *converted_accum) -{ - char *p = (char *)string; - int i; - uint byte; - - if (!p) { - printf("ERROR: NULL string passed in.\n"); - return -1; - } - - /* Convert string to bytes */ - for (i = 0, p = (char *)string; (i < TLV_VALUE_MAX_LEN) && (*p != 0); - i++) { - while ((*p == ' ') || (*p == '\t') || (*p == ',') || - (*p == ';')) { - p++; - } - if (*p != 0) { - if (!is_digit(*p)) { - printf("ERROR: Non-digit found in byte string: (%s)\n", - string); - return -1; - } - byte = simple_strtoul(p, &p, 0); - if (byte >= 256) { - printf("ERROR: The value specified is greater than 255: (%u) in string: %s\n", - byte, string); - return -1; - } - buf[i] = byte & 0xFF; - } - } - - if (i == TLV_VALUE_MAX_LEN && (*p != 0)) { - printf("ERROR: Trying to assign too many bytes (max: %d) in string: %s\n", - TLV_VALUE_MAX_LEN, string); - return -1; - } - - *converted_accum = i; - return 0; -} - static void show_tlv_devices(int current_dev) { unsigned int dev; @@ -877,218 +370,3 @@ static void show_tlv_devices(int current_dev) printf("TLV: %u%s\n", dev, (dev == current_dev) ? " (*)" : ""); } - -static int find_tlv_devices(struct udevice **tlv_devices_p) -{ - int ret; - int count_dev = 0; - struct udevice *dev; - - for (ret = uclass_first_device_check(UCLASS_I2C_EEPROM, &dev); - dev; - ret = uclass_next_device_check(&dev)) { - if (ret == 0) - tlv_devices_p[count_dev++] = dev; - if (count_dev >= TLV_MAX_DEVICES) - break; - } - - return (count_dev == 0) ? -ENODEV : 0; -} - -static struct udevice *find_tlv_device_by_index(int dev_num) -{ - struct udevice *local_tlv_devices[TLV_MAX_DEVICES] = {}; - struct udevice **tlv_devices_p; - int ret; - - if (gd->flags & (GD_FLG_RELOC | GD_FLG_SPL_INIT)) { - /* Assume BSS is initialized; use static data */ - if (tlv_devices[dev_num]) - return tlv_devices[dev_num]; - tlv_devices_p = tlv_devices; - } else { - tlv_devices_p = local_tlv_devices; - } - - ret = find_tlv_devices(tlv_devices_p); - if (ret == 0 && tlv_devices_p[dev_num]) - return tlv_devices_p[dev_num]; - - return NULL; -} - -/** - * read_tlv_eeprom - read the hwinfo from i2c EEPROM - */ -int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num) -{ - struct udevice *dev; - - if (dev_num >= TLV_MAX_DEVICES) - return -EINVAL; - - dev = find_tlv_device_by_index(dev_num); - if (!dev) - return -ENODEV; - - return i2c_eeprom_read(dev, offset, eeprom, len); -} - -/** - * write_tlv_eeprom - write the hwinfo to i2c EEPROM - */ -int write_tlv_eeprom(void *eeprom, int len, int dev) -{ - if (!(gd->flags & GD_FLG_RELOC)) - return -ENODEV; - if (!tlv_devices[dev]) - return -ENODEV; - - return i2c_eeprom_write(tlv_devices[dev], 0, eeprom, len); -} - -int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, - struct tlvinfo_tlv **first_entry, int dev_num) -{ - int ret; - struct tlvinfo_header *tlv_hdr; - struct tlvinfo_tlv *tlv_ent; - - /* Read TLV header */ - ret = read_tlv_eeprom(eeprom, 0, TLV_INFO_HEADER_SIZE, dev_num); - if (ret < 0) - return ret; - - tlv_hdr = eeprom; - if (!is_valid_tlvinfo_header(tlv_hdr)) - return -EINVAL; - - /* Read TLV entries */ - tlv_ent = to_entry(&tlv_hdr[1]); - ret = read_tlv_eeprom(tlv_ent, TLV_INFO_HEADER_SIZE, - be16_to_cpu(tlv_hdr->totallen), dev_num); - if (ret < 0) - return ret; - if (!tlvinfo_check_crc(eeprom)) - return -EINVAL; - - *hdr = tlv_hdr; - *first_entry = tlv_ent; - - return 0; -} - -/** - * mac_read_from_eeprom - * - * Read the MAC addresses from EEPROM - * - * This function reads the MAC addresses from EEPROM and sets the - * appropriate environment variables for each one read. - * - * The environment variables are only set if they haven't been set already. - * This ensures that any user-saved variables are never overwritten. - * - * This function must be called after relocation. - */ -int mac_read_from_eeprom(void) -{ - unsigned int i; - int eeprom_index; - struct tlvinfo_tlv *eeprom_tlv; - int maccount; - u8 macbase[6]; - struct tlvinfo_header *eeprom_hdr = to_header(eeprom); - int devnum = 0; // TODO: support multiple EEPROMs - - puts("EEPROM: "); - - if (read_eeprom(devnum, eeprom)) { - printf("Read failed.\n"); - return -1; - } - - maccount = 1; - if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_SIZE, &eeprom_index)) { - eeprom_tlv = to_entry(&eeprom[eeprom_index]); - maccount = (eeprom_tlv->value[0] << 8) | eeprom_tlv->value[1]; - } - - memcpy(macbase, "\0\0\0\0\0\0", 6); - if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_BASE, &eeprom_index)) { - eeprom_tlv = to_entry(&eeprom[eeprom_index]); - memcpy(macbase, eeprom_tlv->value, 6); - } - - for (i = 0; i < maccount; i++) { - if (is_valid_ethaddr(macbase)) { - char ethaddr[18]; - char enetvar[11]; - - sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", - macbase[0], macbase[1], macbase[2], - macbase[3], macbase[4], macbase[5]); - sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); - /* Only initialize environment variables that are blank - * (i.e. have not yet been set) - */ - if (!env_get(enetvar)) - env_set(enetvar, ethaddr); - - macbase[5]++; - if (macbase[5] == 0) { - macbase[4]++; - if (macbase[4] == 0) { - macbase[3]++; - if (macbase[3] == 0) { - macbase[0] = 0; - macbase[1] = 0; - macbase[2] = 0; - } - } - } - } - } - - printf("%s v%u len=%u\n", eeprom_hdr->signature, eeprom_hdr->version, - be16_to_cpu(eeprom_hdr->totallen)); - - return 0; -} - -/** - * populate_serial_number - read the serial number from EEPROM - * - * This function reads the serial number from the EEPROM and sets the - * appropriate environment variable. - * - * The environment variable is only set if it has not been set - * already. This ensures that any user-saved variables are never - * overwritten. - * - * This function must be called after relocation. - */ -int populate_serial_number(int devnum) -{ - char serialstr[257]; - int eeprom_index; - struct tlvinfo_tlv *eeprom_tlv; - - if (env_get("serial#")) - return 0; - - if (read_eeprom(devnum, eeprom)) { - printf("Read failed.\n"); - return -1; - } - - if (tlvinfo_find_tlv(eeprom, TLV_CODE_SERIAL_NUMBER, &eeprom_index)) { - eeprom_tlv = to_entry(&eeprom[eeprom_index]); - memcpy(serialstr, eeprom_tlv->value, eeprom_tlv->length); - serialstr[eeprom_tlv->length] = 0; - env_set("serial#", serialstr); - } - - return 0; -} diff --git a/lib/Kconfig b/lib/Kconfig index 858be14f09..4497efbbf4 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -909,3 +909,5 @@ config PHANDLE_CHECK_SEQ phandles in fdtdec_get_alias_seq() function. endmenu + +source lib/tlv/Kconfig diff --git a/lib/Makefile b/lib/Makefile index d9b1811f75..1bc6dad8ac 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -91,6 +91,8 @@ obj-$(CONFIG_LIBAVB) += libavb/ obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += libfdt/ obj-$(CONFIG_$(SPL_TPL_)OF_REAL) += fdtdec_common.o fdtdec.o +obj-$(CONFIG_$(SPL_)EEPROM_TLV_LIB) += tlv/ + ifdef CONFIG_SPL_BUILD obj-$(CONFIG_SPL_YMODEM_SUPPORT) += crc16-ccitt.o obj-$(CONFIG_$(SPL_TPL_)HASH) += crc16-ccitt.o diff --git a/lib/tlv/Kconfig b/lib/tlv/Kconfig new file mode 100644 index 0000000000..b3912ada78 --- /dev/null +++ b/lib/tlv/Kconfig @@ -0,0 +1,15 @@ +config EEPROM_TLV_LIB + bool "Enable EEPROM TLV library" + depends on I2C_EEPROM + help + Selecting this option will enable the shared EEPROM TLV library code. + It provides functions for reading, writing and parsing of + TLV-encoded data from EEPROMs. + +config SPL_EEPROM_TLV_LIB + bool "Enable EEPROM TLV library for SPL" + depends on SPL_I2C_EEPROM + help + Selecting this option will enable the shared EEPROM TLV library code. + It provides functions for reading, writing and parsing of + TLV-encoded data from EEPROMs. diff --git a/lib/tlv/Makefile b/lib/tlv/Makefile new file mode 100644 index 0000000000..8e96752e75 --- /dev/null +++ b/lib/tlv/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# (C) Copyright 2017 Linaro + +obj-$(CONFIG_EEPROM_TLV_LIB) += tlv_eeprom.o diff --git a/lib/tlv/tlv_eeprom.c b/lib/tlv/tlv_eeprom.c new file mode 100644 index 0000000000..464f0aa1fa --- /dev/null +++ b/lib/tlv/tlv_eeprom.c @@ -0,0 +1,750 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * See file CREDITS for list of people who contributed to this + * project. + * + * Copyright (C) 2013 Curt Brune + * Copyright (C) 2014 Srideep + * Copyright (C) 2013 Miles Tseng + * Copyright (C) 2014,2016 david_yang + * Copyright (C) 2022 Josua Mayer + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tlv_eeprom.h" + +DECLARE_GLOBAL_DATA_PTR; + +/* File scope function prototypes */ +static int read_eeprom(int devnum, u8 *eeprom); +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); + +/* The EERPOM contents after being read into memory */ +static u8 eeprom[TLV_INFO_MAX_LEN]; + +static struct udevice *tlv_devices[TLV_MAX_DEVICES]; + +#define to_header(p) ((struct tlvinfo_header *)p) +#define to_entry(p) ((struct tlvinfo_tlv *)p) + +/** + * Check whether eeprom device exists. + */ +bool exists_tlv_eeprom(int dev) +{ + return dev < TLV_MAX_DEVICES && tlv_devices[dev] != 0; +} + +static inline bool is_digit(char c) +{ + return (c >= '0' && c <= '9'); +} + +/** + * is_hex + * + * Tests if character is an ASCII hex digit + */ +static inline u8 is_hex(char p) +{ + return (((p >= '0') && (p <= '9')) || + ((p >= 'A') && (p <= 'F')) || + ((p >= 'a') && (p <= 'f'))); +} + +/** + * Validate the checksum in the provided TlvInfo EEPROM data. First, + * verify that the TlvInfo header is valid, then make sure the last + * TLV is a CRC-32 TLV. Then calculate the CRC over the EEPROM data + * and compare it to the value stored in the EEPROM CRC-32 TLV. + */ +bool tlvinfo_check_crc(u8 *eeprom) +{ + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + struct tlvinfo_tlv *eeprom_crc; + unsigned int calc_crc; + unsigned int stored_crc; + + // Is the eeprom header valid? + if (!is_valid_tlvinfo_header(eeprom_hdr)) + return false; + + // Is the last TLV a CRC? + 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, + 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) | + eeprom_crc->value[3]; + return calc_crc == stored_crc; +} + +/** + * read_eeprom + * + * Read the EEPROM into memory, if it hasn't already been read. + */ +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[TLV_INFO_HEADER_SIZE]); + + /* Read the header */ + 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, TLV_INFO_HEADER_SIZE, + be16_to_cpu(eeprom_hdr->totallen), devnum); + + // If the contents are invalid, start over with default contents + if (!is_valid_tlvinfo_header(eeprom_hdr) || + !tlvinfo_check_crc(eeprom)) { + strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING); + eeprom_hdr->version = TLV_INFO_VERSION; + eeprom_hdr->totallen = cpu_to_be16(0); + tlvinfo_update_crc(eeprom); + } + +#ifdef DEBUG + show_eeprom(devnum, eeprom); +#endif + + return ret; +} + +/** + * tlvinfo_update_crc + * + * This function updates the CRC-32 TLV. If there is no CRC-32 TLV, then + * one is added. This function should be called after each update to the + * EEPROM structure, to make sure the CRC is always correct. + */ +void tlvinfo_update_crc(u8 *eeprom) +{ + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + struct tlvinfo_tlv *eeprom_crc; + unsigned int calc_crc; + int eeprom_index; + + // Discover the CRC TLV + if (!tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) { + unsigned int totallen = be16_to_cpu(eeprom_hdr->totallen); + + if ((totallen + TLV_INFO_ENTRY_SIZE + 4) > TLV_TOTAL_LEN_MAX) + return; + 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; + eeprom_crc->length = 4; + + // Calculate the checksum + calc_crc = crc32(0, (void *)eeprom, + 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; + eeprom_crc->value[3] = (calc_crc >> 0) & 0xFF; +} + +/** + * write_tlvinfo_tlv_eeprom + * + * Write the TLV data from CPU memory to the hardware. + */ +int write_tlvinfo_tlv_eeprom(void *eeprom, int dev) +{ + int ret = 0; + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + int eeprom_len; + + tlvinfo_update_crc(eeprom); + + 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"); + return -1; + } + + printf("Programming passed.\n"); + return 0; +} + +/** + * tlvinfo_find_tlv + * + * This function finds the TLV with the supplied code in the EERPOM. + * An offset from the beginning of the EEPROM is returned in the + * eeprom_index parameter if the TLV is found. + */ +bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index) +{ + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + struct tlvinfo_tlv *eeprom_tlv; + int eeprom_end; + + // Search through the TLVs, looking for the first one which matches the + // supplied type code. + *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 += TLV_INFO_ENTRY_SIZE + eeprom_tlv->length; + } + return(false); +} + +/** + * tlvinfo_delete_tlv + * + * This function deletes the TLV with the specified type code from the + * EEPROM. + */ +bool tlvinfo_delete_tlv(u8 *eeprom, u8 code) +{ + int eeprom_index; + int tlength; + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + struct tlvinfo_tlv *eeprom_tlv; + + // 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 = TLV_INFO_ENTRY_SIZE + eeprom_tlv->length; + memcpy(&eeprom[eeprom_index], &eeprom[eeprom_index + tlength], + TLV_INFO_HEADER_SIZE + + be16_to_cpu(eeprom_hdr->totallen) - eeprom_index - + tlength); + eeprom_hdr->totallen = + cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) - + tlength); + tlvinfo_update_crc(eeprom); + return true; + } + return false; +} + +/** + * 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. + */ +#define MAX_TLV_VALUE_LEN 256 +bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval) +{ + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + struct tlvinfo_tlv *eeprom_tlv; + int new_tlv_len = 0; + u32 value; + char data[MAX_TLV_VALUE_LEN]; + int eeprom_index; + + // Encode each TLV type into the format to be stored in the EERPOM + switch (tcode) { + case TLV_CODE_PRODUCT_NAME: + case TLV_CODE_PART_NUMBER: + case TLV_CODE_SERIAL_NUMBER: + case TLV_CODE_LABEL_REVISION: + case TLV_CODE_PLATFORM_NAME: + case TLV_CODE_ONIE_VERSION: + case TLV_CODE_MANUF_NAME: + case TLV_CODE_MANUF_COUNTRY: + case TLV_CODE_VENDOR_NAME: + case TLV_CODE_DIAG_VERSION: + case TLV_CODE_SERVICE_TAG: + strncpy(data, strval, MAX_TLV_VALUE_LEN); + new_tlv_len = min_t(size_t, MAX_TLV_VALUE_LEN, strlen(strval)); + break; + case TLV_CODE_DEVICE_VERSION: + value = simple_strtoul(strval, NULL, 0); + if (value >= 256) { + printf("ERROR: Device version must be 255 or less. Value supplied: %u", + value); + return false; + } + data[0] = value & 0xFF; + new_tlv_len = 1; + break; + case TLV_CODE_MAC_SIZE: + value = simple_strtoul(strval, NULL, 0); + if (value >= 65536) { + printf("ERROR: MAC Size must be 65535 or less. Value supplied: %u", + value); + return false; + } + data[0] = (value >> 8) & 0xFF; + data[1] = value & 0xFF; + new_tlv_len = 2; + break; + case TLV_CODE_MANUF_DATE: + if (set_date(data, strval) != 0) + return false; + new_tlv_len = 19; + break; + case TLV_CODE_MAC_BASE: + if (set_mac(data, strval) != 0) + return false; + new_tlv_len = 6; + break; + case TLV_CODE_CRC_32: + printf("WARNING: The CRC TLV is set automatically and cannot be set manually.\n"); + return false; + case TLV_CODE_VENDOR_EXT: + default: + if (set_bytes(data, strval, &new_tlv_len) != 0) + return false; + break; + } + + // Is there room for this TLV? + 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; + } + + // Add TLV at the end, overwriting CRC TLV if it exists + if (tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) + eeprom_hdr->totallen = + cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) - + TLV_INFO_ENTRY_SIZE - 4); + else + 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; + memcpy(eeprom_tlv->value, data, new_tlv_len); + + // Update the total length and calculate (add) a new CRC-32 TLV + eeprom_hdr->totallen = cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) + + TLV_INFO_ENTRY_SIZE + new_tlv_len); + tlvinfo_update_crc(eeprom); + + return true; +} + +/** + * set_mac + * + * Converts a string MAC address into a binary buffer. + * + * This function takes a pointer to a MAC address string + * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number). + * The string format is verified and then converted to binary and + * stored in a buffer. + */ +static int set_mac(char *buf, const char *string) +{ + char *p = (char *)string; + int i; + int err = 0; + char *end; + + if (!p) { + printf("ERROR: NULL mac addr string passed in.\n"); + return -1; + } + + if (strlen(p) != 17) { + printf("ERROR: MAC address strlen() != 17 -- %zu\n", strlen(p)); + printf("ERROR: Bad MAC address format: %s\n", string); + return -1; + } + + for (i = 0; i < 17; i++) { + if ((i % 3) == 2) { + if (p[i] != ':') { + err++; + printf("ERROR: mac: p[%i] != :, found: `%c'\n", + i, p[i]); + break; + } + continue; + } else if (!is_hex(p[i])) { + err++; + printf("ERROR: mac: p[%i] != hex digit, found: `%c'\n", + i, p[i]); + break; + } + } + + if (err != 0) { + printf("ERROR: Bad MAC address format: %s\n", string); + return -1; + } + + /* Convert string to binary */ + for (i = 0, p = (char *)string; i < 6; i++) { + buf[i] = p ? hextoul(p, &end) : 0; + if (p) + p = (*end) ? end + 1 : end; + } + + if (!is_valid_ethaddr((u8 *)buf)) { + printf("ERROR: MAC address must not be 00:00:00:00:00:00, a multicast address or FF:FF:FF:FF:FF:FF.\n"); + printf("ERROR: Bad MAC address format: %s\n", string); + return -1; + } + + return 0; +} + +/** + * set_date + * + * Validates the format of the data string + * + * This function takes a pointer to a date string (i.e. MM/DD/YYYY hh:mm:ss) + * and validates that the format is correct. If so the string is copied + * to the supplied buffer. + */ +static int set_date(char *buf, const char *string) +{ + int i; + + if (!string) { + printf("ERROR: NULL date string passed in.\n"); + return -1; + } + + if (strlen(string) != 19) { + printf("ERROR: Date strlen() != 19 -- %zu\n", strlen(string)); + printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", + string); + return -1; + } + + for (i = 0; string[i] != 0; i++) { + switch (i) { + case 2: + case 5: + if (string[i] != '/') { + printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", + string); + return -1; + } + break; + case 10: + if (string[i] != ' ') { + printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", + string); + return -1; + } + break; + case 13: + case 16: + if (string[i] != ':') { + printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", + string); + return -1; + } + break; + default: + if (!is_digit(string[i])) { + printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n", + string); + return -1; + } + break; + } + } + + strcpy(buf, string); + return 0; +} + +/** + * set_bytes + * + * Converts a space-separated string of decimal numbers into a + * buffer of bytes. + * + * This function takes a pointer to a space-separated string of decimal + * numbers (i.e. "128 0x55 0321") with "C" standard radix specifiers + * and converts them to an array of bytes. + */ +static int set_bytes(char *buf, const char *string, int *converted_accum) +{ + char *p = (char *)string; + int i; + uint byte; + + if (!p) { + printf("ERROR: NULL string passed in.\n"); + return -1; + } + + /* Convert string to bytes */ + for (i = 0, p = (char *)string; (i < TLV_VALUE_MAX_LEN) && (*p != 0); + i++) { + while ((*p == ' ') || (*p == '\t') || (*p == ',') || + (*p == ';')) { + p++; + } + if (*p != 0) { + if (!is_digit(*p)) { + printf("ERROR: Non-digit found in byte string: (%s)\n", + string); + return -1; + } + byte = simple_strtoul(p, &p, 0); + if (byte >= 256) { + printf("ERROR: The value specified is greater than 255: (%u) in string: %s\n", + byte, string); + return -1; + } + buf[i] = byte & 0xFF; + } + } + + if (i == TLV_VALUE_MAX_LEN && (*p != 0)) { + printf("ERROR: Trying to assign too many bytes (max: %d) in string: %s\n", + TLV_VALUE_MAX_LEN, string); + return -1; + } + + *converted_accum = i; + return 0; +} + +static int find_tlv_devices(struct udevice **tlv_devices_p) +{ + int ret; + int count_dev = 0; + struct udevice *dev; + + for (ret = uclass_first_device_check(UCLASS_I2C_EEPROM, &dev); + dev; + ret = uclass_next_device_check(&dev)) { + if (ret == 0) + tlv_devices_p[count_dev++] = dev; + if (count_dev >= TLV_MAX_DEVICES) + break; + } + + return (count_dev == 0) ? -ENODEV : 0; +} + +static struct udevice *find_tlv_device_by_index(int dev_num) +{ + struct udevice *local_tlv_devices[TLV_MAX_DEVICES] = {}; + struct udevice **tlv_devices_p; + int ret; + + if (gd->flags & (GD_FLG_RELOC | GD_FLG_SPL_INIT)) { + /* Assume BSS is initialized; use static data */ + if (tlv_devices[dev_num]) + return tlv_devices[dev_num]; + tlv_devices_p = tlv_devices; + } else { + tlv_devices_p = local_tlv_devices; + } + + ret = find_tlv_devices(tlv_devices_p); + if (ret == 0 && tlv_devices_p[dev_num]) + return tlv_devices_p[dev_num]; + + return NULL; +} + +/** + * read_tlv_eeprom - read the hwinfo from i2c EEPROM + */ +int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num) +{ + struct udevice *dev; + + if (dev_num >= TLV_MAX_DEVICES) + return -EINVAL; + + dev = find_tlv_device_by_index(dev_num); + if (!dev) + return -ENODEV; + + return i2c_eeprom_read(dev, offset, eeprom, len); +} + +/** + * write_tlv_eeprom - write the hwinfo to i2c EEPROM + */ +int write_tlv_eeprom(void *eeprom, int len, int dev) +{ + if (!(gd->flags & GD_FLG_RELOC)) + return -ENODEV; + if (!tlv_devices[dev]) + return -ENODEV; + + return i2c_eeprom_write(tlv_devices[dev], 0, eeprom, len); +} + +int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, + struct tlvinfo_tlv **first_entry, int dev_num) +{ + int ret; + struct tlvinfo_header *tlv_hdr; + struct tlvinfo_tlv *tlv_ent; + + /* Read TLV header */ + ret = read_tlv_eeprom(eeprom, 0, TLV_INFO_HEADER_SIZE, dev_num); + if (ret < 0) + return ret; + + tlv_hdr = eeprom; + if (!is_valid_tlvinfo_header(tlv_hdr)) + return -EINVAL; + + /* Read TLV entries */ + tlv_ent = to_entry(&tlv_hdr[1]); + ret = read_tlv_eeprom(tlv_ent, TLV_INFO_HEADER_SIZE, + be16_to_cpu(tlv_hdr->totallen), dev_num); + if (ret < 0) + return ret; + if (!tlvinfo_check_crc(eeprom)) + return -EINVAL; + + *hdr = tlv_hdr; + *first_entry = tlv_ent; + + return 0; +} + +/** + * mac_read_from_eeprom + * + * Read the MAC addresses from EEPROM + * + * This function reads the MAC addresses from EEPROM and sets the + * appropriate environment variables for each one read. + * + * The environment variables are only set if they haven't been set already. + * This ensures that any user-saved variables are never overwritten. + * + * This function must be called after relocation. + */ +int mac_read_from_eeprom(void) +{ + unsigned int i; + int eeprom_index; + struct tlvinfo_tlv *eeprom_tlv; + int maccount; + u8 macbase[6]; + struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + int devnum = 0; // TODO: support multiple EEPROMs + + puts("EEPROM: "); + + if (read_eeprom(devnum, eeprom)) { + printf("Read failed.\n"); + return -1; + } + + maccount = 1; + if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_SIZE, &eeprom_index)) { + eeprom_tlv = to_entry(&eeprom[eeprom_index]); + maccount = (eeprom_tlv->value[0] << 8) | eeprom_tlv->value[1]; + } + + memcpy(macbase, "\0\0\0\0\0\0", 6); + if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_BASE, &eeprom_index)) { + eeprom_tlv = to_entry(&eeprom[eeprom_index]); + memcpy(macbase, eeprom_tlv->value, 6); + } + + for (i = 0; i < maccount; i++) { + if (is_valid_ethaddr(macbase)) { + char ethaddr[18]; + char enetvar[11]; + + sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", + macbase[0], macbase[1], macbase[2], + macbase[3], macbase[4], macbase[5]); + sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); + /* Only initialize environment variables that are blank + * (i.e. have not yet been set) + */ + if (!env_get(enetvar)) + env_set(enetvar, ethaddr); + + macbase[5]++; + if (macbase[5] == 0) { + macbase[4]++; + if (macbase[4] == 0) { + macbase[3]++; + if (macbase[3] == 0) { + macbase[0] = 0; + macbase[1] = 0; + macbase[2] = 0; + } + } + } + } + } + + printf("%s v%u len=%u\n", eeprom_hdr->signature, eeprom_hdr->version, + be16_to_cpu(eeprom_hdr->totallen)); + + return 0; +} + +/** + * populate_serial_number - read the serial number from EEPROM + * + * This function reads the serial number from the EEPROM and sets the + * appropriate environment variable. + * + * The environment variable is only set if it has not been set + * already. This ensures that any user-saved variables are never + * overwritten. + * + * This function must be called after relocation. + */ +int populate_serial_number(int devnum) +{ + char serialstr[257]; + int eeprom_index; + struct tlvinfo_tlv *eeprom_tlv; + + if (env_get("serial#")) + return 0; + + if (read_eeprom(devnum, eeprom)) { + printf("Read failed.\n"); + return -1; + } + + if (tlvinfo_find_tlv(eeprom, TLV_CODE_SERIAL_NUMBER, &eeprom_index)) { + eeprom_tlv = to_entry(&eeprom[eeprom_index]); + memcpy(serialstr, eeprom_tlv->value, eeprom_tlv->length); + serialstr[eeprom_tlv->length] = 0; + env_set("serial#", serialstr); + } + + return 0; +} From patchwork Mon May 2 14:18:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625124 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=zVVZYaOB; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQJ80wXhz9sFr for ; Tue, 3 May 2022 00:21:28 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5A96383FAA; Mon, 2 May 2022 16:19:43 +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="zVVZYaOB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D61AA83FD3; Mon, 2 May 2022 16:19:19 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) (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 3D3A583F48 for ; Mon, 2 May 2022 16:19:06 +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-x333.google.com with SMTP id bg25so8351191wmb.4 for ; Mon, 02 May 2022 07:19:06 -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=284XuEMBbatVt2TWhLTPU72cOz3QQqokcWPJ5yX6+sw=; b=zVVZYaOBXmGEzceN9o/9ZouL3yIPJiU7z0mZh9Cy5SuP2XDiZ8KRzjOuK39l9bohVt oU7zRSdEl+lR6vqbBZSq+Cdg/OeLn5ygoG1MSt4wjp2ZWY+KqA/RxUK1DY5tVwOZ8kkh bLPuEik7OZhA5DZDQTxxUuZveTtxAVo6QzzDBjvyJKqxPn2kIpD22hFF0KT447kxhmYc +lFajlhAyvLIaT7Z1AFFTipaDzKWAn5088PlrwH3QCbIQ3DACremazzT2nNY1Jp3HkHa /Ep9IBU4e33YhpmT1jEtKqIYr19HpdoUwjJ9R6VhSRBq5T4STbBbCNde+eGrTEKczlSX gjuw== 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=284XuEMBbatVt2TWhLTPU72cOz3QQqokcWPJ5yX6+sw=; b=R21k5++pK0IZPNza6AgAQZE2EQGxT/ZBxLXeCatsfDqtbA9f5PELk50sdnXH6dps+f X7rNC3XwioJ6rnSbKXla1CfPUh8D70nSxgK3f9g1CrhwvtQsVBL+xYITR22gnUjD06pb ZWYmBXkFi5HjHZftQuO8X4ac7N0dqckJrIyeIRdzpaaq10ahy+RP32pCArce9m9yO5c0 u0hQObmT8/7t19MgmWhX+1yxPyQ0ARa6xKSsTKCdk3k7MiDmGtyYOapROCQZ4uGlKfLx 9TGc9HPbPxb9QNLZ36dQm6C1klKswZiZDWIvJNdXsyCvDLFXDFzmLwNLuG/az5eqHWE/ WsXw== X-Gm-Message-State: AOAM532eNbEAwCyal+5KI4KZCEVBkCnLFIdqi4HSgcBKI37IDtDdjFVk VDxwLDx3pEnzK5gniJ2bike3aRGzjBSPHw== X-Google-Smtp-Source: ABdhPJxbap9m02Zk+yKY2qx7ZpG4v1+Gr+DRuopzJAurt1gtT5jkjbzEKBx+ZTtf7yw5nY5ebaoxKg== X-Received: by 2002:a05:600c:4f89:b0:392:8959:f8e7 with SMTP id n9-20020a05600c4f8900b003928959f8e7mr15020330wmq.164.1651501145451; Mon, 02 May 2022 07:19:05 -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.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 07:19:04 -0700 (PDT) From: Josua Mayer To: u-boot@lists.denx.de Cc: Josua Mayer , Stefan Roese Subject: [PATCH 11/12] arm: mvebu: clearfog: enable tlv library for spl in favour of eeprom cmd Date: Mon, 2 May 2022 17:18:37 +0300 Message-Id: <20220502141838.15912-12-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 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 The board file used CONFIG_SPL_CMD_TLV_EEPROM as a library to facilitate reading tlv data with the memory size from eeprom. Since the tlv library has been split off, only CONFIG_SPL_EEPROM_TLV_LIB is required now. Signed-off-by: Josua Mayer --- configs/clearfog_defconfig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/configs/clearfog_defconfig b/configs/clearfog_defconfig index 880f16a6e0..87ba6f29a7 100644 --- a/configs/clearfog_defconfig +++ b/configs/clearfog_defconfig @@ -12,6 +12,7 @@ CONFIG_DM_GPIO=y CONFIG_DEFAULT_DEVICE_TREE="armada-388-clearfog" CONFIG_SPL_TEXT_BASE=0x40000030 CONFIG_SPL_SERIAL=y +CONFIG_SPL_DRIVERS_MISC=y CONFIG_SPL=y CONFIG_DEBUG_UART_BASE=0xd0012000 CONFIG_DEBUG_UART_CLOCK=250000000 @@ -26,7 +27,6 @@ CONFIG_SYS_CONSOLE_INFO_QUIET=y CONFIG_DISPLAY_BOARDINFO_LATE=y CONFIG_SPL_I2C=y CONFIG_CMD_TLV_EEPROM=y -CONFIG_SPL_CMD_TLV_EEPROM=y # CONFIG_CMD_FLASH is not set CONFIG_CMD_GPIO=y CONFIG_CMD_I2C=y @@ -71,3 +71,4 @@ CONFIG_SYS_NS16550=y CONFIG_KIRKWOOD_SPI=y CONFIG_USB=y CONFIG_USB_XHCI_HCD=y +CONFIG_SPL_EEPROM_TLV_LIB=y From patchwork Mon May 2 14:18:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josua Mayer X-Patchwork-Id: 1625126 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=solid-run-com.20210112.gappssmtp.com header.i=@solid-run-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=JONg9uxq; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KsQJY0tn1z9sFr for ; Tue, 3 May 2022 00:21:49 +1000 (AEST) 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) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 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 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 *