| Message ID | 20200514121145.28737-4-jagan@amarulasolutions.com |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-amarula+bncBD7MFH7A7EEBBFPL6T2QKGQE5GOZF7Q@amarulasolutions.com> X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id D589E3F077 for <linux-amarula@patchwork.amarulasolutions.com>; Thu, 14 May 2020 14:12:06 +0200 (CEST) Received: by mail-pf1-f197.google.com with SMTP id 207sf2490898pfx.8 for <linux-amarula@patchwork.amarulasolutions.com>; Thu, 14 May 2020 05:12:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1589458325; cv=pass; d=google.com; s=arc-20160816; b=y8jTDXwsjZ0DPZnHc+zxCPeucXP24+daa4WfrwF/Tms9d0L9kxMUFf4ka8+cDpt3Vk 2VHUJ1Gh4ggKcCTFwJY52/uufzzuVu9KsstQ9J5PXUAhCsvQiNC9TIcE6jy1v7PoNHpT sWob8MHLCatcpO09Ezmerwe8mGOTX094ERji7XKdMGhvF3l4OZwrNUhqI/yz1q673ugA s5wPGXlO7+xw/SVIEZA27l2Jye1n2Tix52POf0iZwiangUybh4tbnh6CJSqxCRTeaPAS BePYi0pKNg1lEXZkJGyXB6FRbVx+qIzYQ79sugurIHIpc6EM1DKdq5OxlPY+muYqAx4L bOZQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=A5I6s4zWlLAVE+zUT5R48cXlF5RS8qhlRWfi5AiI3V8=; b=igI4+srKYsAGNoEsKUsViqqsECdYAGFdeSHYIbL0amvocARGe7iOnTCrhbuFqF1o64 7kjIlHkXHSLufJA65OjrjgE47hgs9ABEUoNYAbbeeu4lI3MijthvGdddr2k82aQycKrq ykVm5bB93JtO0AXK4n74oRvcy88khoL/FDkJUAJvRqvTI2fnSK2jtZch+kd9MOIEYSQg W+aLx2cpUIypVI81PNoqHGDR/OwhC5z+bHfp9y/UI2ouxrHua9HjAPa1UZ6Y3RU3UfQY 4ruUYZL9Z/643Ddn26OyuyPQOguY2eWTmCaeQaCK0P8hT7b5xmU58Ij0E5/lVDxsBlSR Bg9Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="BmYcYDT/"; spf=pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jagan@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=A5I6s4zWlLAVE+zUT5R48cXlF5RS8qhlRWfi5AiI3V8=; b=pGnZIRx0gYhgWdgrcjH2pUdPxSyjbyz1M648GQQKrc3XwGTbQ3GbcEXp2IEysvCAaK HFbOGVs9mlRShz2GLBqKCiClpVkKtjGMFLT8f/MsFdcHKT3GE8gnHSKIO4XOBd5Ax9L0 8mu/4pXgNxXBX1H9jsOOVEQ2U3lUYEa6pn1bk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=A5I6s4zWlLAVE+zUT5R48cXlF5RS8qhlRWfi5AiI3V8=; b=kbp+cdtgVORMf0NBsD9daZMVbS3MuP7EccKpCQct52sdPIPhf7w3lmy00qHjXoujCI /yzYdljl35xnOQB+qxR/Qo8egP6AL0VTraLomKMx2P8cnyGX+t9yvq2WS0Fp+rkKwX7e Lnt0eCRhOg/IdzsYlhQpDUuOQf8Y+nShCgvMW5zqeYvL53515KF/FQKeEtW3YTWgJ9ju fiM3ZaH6tma/ZJxenZc9CE95NJsN8XcmlEmATkqz4QdBScrhgzzjfvWu+/sz14XM0MK6 4UHCdZdlQT9/c6VVGgk3xfH1VdeKMSbn6Y2bgOb6hMQ5Sp9yZN5icj16T8651TRsSHIT VNmA== X-Gm-Message-State: AOAM530Gc3OBWAgitXYAeEt8BCy+8RdJcCHS+P1Ii7kLUjRI59xTrQGh UdD0+dXIan5fe+RM/a+zVwQp3rA1 X-Google-Smtp-Source: ABdhPJxGsayfNqB80z6IR06nYasKwANzV0QDf9isacBkO+GULCcxkcvelQKTRFCWsVoYLCprjHAnoQ== X-Received: by 2002:a17:902:ea8b:: with SMTP id x11mr3728916plb.205.1589458325682; Thu, 14 May 2020 05:12:05 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a63:4b5c:: with SMTP id k28ls787831pgl.0.gmail; Thu, 14 May 2020 05:12:05 -0700 (PDT) X-Received: by 2002:a63:db57:: with SMTP id x23mr3587915pgi.94.1589458325167; Thu, 14 May 2020 05:12:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589458325; cv=none; d=google.com; s=arc-20160816; b=aA8a0BEdpOyxgCpdvIFRTMTMx0EpjLG1zSmqWrVA3PPGQdefrdyfC6QxwyBJoIVKkm JWFlf3B0db89htQUOY7xxIyniGggYIGU7FHKl0DXgcOkN7nZzktmc5+0WzbDrrqhD/48 Nzw35diONXcC0ZLJfbfTaN+HTGonmxjEXUUUwFBvjztDoSG+PUJIt4nZvw+e2/Frjqnf VCDPfa8j3Y+u4WEvSPb5sRgcMAJN02ooUb362nYyYqcbwu1YQVumpX5KxqCYcSL9OUCl fsBG6O3x4FzabdSu1HD9nGsn0OTpmxtWTxK1JtKGw8I8cDGrLAo4aOsgRwlEPKlm/QLj D5Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=3as/G8MDFlVSHYKrBALlSCc5olDPxr3FJahGP4bXV4A=; b=jmTOss+vOwJFBc7jPpC5qbs/Hc9YWGuKaOY6mPMYf9BFaN+ZB8Q0QosNRA4ul5EPHm 3/1w2oeFxLxZt9SKxrTNXHCikvti/Ozw8YYnzCOfmKkIocFl3AJz3mspCpYxnnd3yiY5 CJe+tPQvTqLf5nHBUWDLYxwYxMYGxiDgkycyHHN4OWMJRbaWtdZRIzUFM6V87alYeMny S+9Dkb9ZM90uo1keKm+d4Zf7XOx0EdAsoQkgWKkZQrQoDJuz5q3nJovcvSKyHpVuxal1 KnixnWnFcjFhs7jS+llhtxpfpm9lvQZ0USKlBYJunsQumt+qrBERn1dw+NCvLYscX6i6 9vNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="BmYcYDT/"; spf=pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jagan@amarulasolutions.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id x23sor3381363pgf.25.2020.05.14.05.12.05 for <linux-amarula@amarulasolutions.com> (Google Transport Security); Thu, 14 May 2020 05:12:05 -0700 (PDT) Received-SPF: pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; X-Received: by 2002:a65:480c:: with SMTP id h12mr3879182pgs.106.1589458324877; Thu, 14 May 2020 05:12:04 -0700 (PDT) Received: from localhost.localdomain ([2405:201:c809:c7d5:713c:4a0a:47eb:ffcd]) by smtp.gmail.com with ESMTPSA id r31sm2091050pgl.86.2020.05.14.05.12.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2020 05:12:04 -0700 (PDT) From: Jagan Teki <jagan@amarulasolutions.com> To: u-boot@lists.denx.de Cc: linux-amarula@amarulasolutions.com, Jagan Teki <jagan@amarulasolutions.com>, Simon Glass <sjg@chromium.org>, Vignesh R <vigneshr@ti.com> Subject: [PATCH 3/5] env: sf: Preserve and free the previous flash Date: Thu, 14 May 2020 17:41:43 +0530 Message-Id: <20200514121145.28737-4-jagan@amarulasolutions.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200514121145.28737-1-jagan@amarulasolutions.com> References: <20200514121145.28737-1-jagan@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: jagan@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="BmYcYDT/"; spf=pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jagan@amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: <linux-amarula.amarulasolutions.com> X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: <https://groups.google.com/a/amarulasolutions.com/group/linux-amarula/post>, <mailto:linux-amarula@amarulasolutions.com> List-Help: <https://support.google.com/a/amarulasolutions.com/bin/topic.py?topic=25838>, <mailto:linux-amarula+help@amarulasolutions.com> List-Archive: <https://groups.google.com/a/amarulasolutions.com/group/linux-amarula/> List-Unsubscribe: <mailto:googlegroups-manage+476853432473+unsubscribe@googlegroups.com>, <https://groups.google.com/a/amarulasolutions.com/group/linux-amarula/subscribe> |
| Series |
sf: Cleanup
|
|
Commit Message
Jagan Teki
May 14, 2020, 12:11 p.m. UTC
env_flash is a global flash pointer, and the probe would
happen only if env_flash is NULL, but there is no checking
and free the pointer if is not NULL.
So, this patch frees the env_flash if it's not NULL, and
get the probed flash in new flash pointer and finally
assign into env_flash.
Note: Similar approach has been followed and tested in
cmd/sf.c
Cc: Simon Glass <sjg@chromium.org>
Cc: Vignesh R <vigneshr@ti.com>
Signed-off-by: Jagan Teki <jagan@amarulasolutions.com>
---
env/sf.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
Comments
Hi Jagan, On 14/05/20 05:41PM, Jagan Teki wrote: > env_flash is a global flash pointer, and the probe would > happen only if env_flash is NULL, but there is no checking > and free the pointer if is not NULL. > > So, this patch frees the env_flash if it's not NULL, and > get the probed flash in new flash pointer and finally > assign into env_flash. > > Note: Similar approach has been followed and tested in > cmd/sf.c > > Cc: Simon Glass <sjg@chromium.org> > Cc: Vignesh R <vigneshr@ti.com> > Signed-off-by: Jagan Teki <jagan@amarulasolutions.com> > --- > env/sf.c | 18 ++++++++++-------- > 1 file changed, 10 insertions(+), 8 deletions(-) > > diff --git a/env/sf.c b/env/sf.c > index 64c57f2cdf..af59c8375c 100644 > --- a/env/sf.c > +++ b/env/sf.c > @@ -50,15 +50,17 @@ static int setup_flash_device(void) > > env_flash = dev_get_uclass_priv(new); > #else > + struct spi_flash *new; > > - if (!env_flash) { > - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, > - CONFIG_ENV_SPI_CS, > - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); > - if (!env_flash) { > - env_set_default("spi_flash_probe() failed", 0); > - return -EIO; > - } > + if (env_flash) > + spi_flash_free(env_flash); > + > + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, Why not assign env_flash directly here? You do it in the very next statement anyway. I don't think the extra 'new' variable is needed. > + CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); > + env_flash = new; > + if (!new) { > + env_set_default("spi_flash_probe() failed", 0); > + return -EIO; > } > #endif > return 0; > -- > 2.20.1 >
On Fri, May 15, 2020 at 12:54 PM Pratyush Yadav <p.yadav@ti.com> wrote: > > Hi Jagan, > > On 14/05/20 05:41PM, Jagan Teki wrote: > > env_flash is a global flash pointer, and the probe would > > happen only if env_flash is NULL, but there is no checking > > and free the pointer if is not NULL. > > > > So, this patch frees the env_flash if it's not NULL, and > > get the probed flash in new flash pointer and finally > > assign into env_flash. > > > > Note: Similar approach has been followed and tested in > > cmd/sf.c > > > > Cc: Simon Glass <sjg@chromium.org> > > Cc: Vignesh R <vigneshr@ti.com> > > Signed-off-by: Jagan Teki <jagan@amarulasolutions.com> > > --- > > env/sf.c | 18 ++++++++++-------- > > 1 file changed, 10 insertions(+), 8 deletions(-) > > > > diff --git a/env/sf.c b/env/sf.c > > index 64c57f2cdf..af59c8375c 100644 > > --- a/env/sf.c > > +++ b/env/sf.c > > @@ -50,15 +50,17 @@ static int setup_flash_device(void) > > > > env_flash = dev_get_uclass_priv(new); > > #else > > + struct spi_flash *new; > > > > - if (!env_flash) { > > - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, > > - CONFIG_ENV_SPI_CS, > > - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); > > - if (!env_flash) { > > - env_set_default("spi_flash_probe() failed", 0); > > - return -EIO; > > - } > > + if (env_flash) > > + spi_flash_free(env_flash); > > + > > + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, > > Why not assign env_flash directly here? You do it in the very next > statement anyway. I don't think the extra 'new' variable is needed. If flash pointer is used free it, before probing a new flash and storing it in flash. In this scenario it requires local new pointer, it was known observation we have faced in cmd/sf.c Jagan.
On 15/05/20 01:14PM, Jagan Teki wrote: > On Fri, May 15, 2020 at 12:54 PM Pratyush Yadav <p.yadav@ti.com> wrote: > > > > Hi Jagan, > > > > On 14/05/20 05:41PM, Jagan Teki wrote: > > > env_flash is a global flash pointer, and the probe would > > > happen only if env_flash is NULL, but there is no checking > > > and free the pointer if is not NULL. > > > > > > So, this patch frees the env_flash if it's not NULL, and > > > get the probed flash in new flash pointer and finally > > > assign into env_flash. > > > > > > Note: Similar approach has been followed and tested in > > > cmd/sf.c > > > > > > Cc: Simon Glass <sjg@chromium.org> > > > Cc: Vignesh R <vigneshr@ti.com> > > > Signed-off-by: Jagan Teki <jagan@amarulasolutions.com> > > > --- > > > env/sf.c | 18 ++++++++++-------- > > > 1 file changed, 10 insertions(+), 8 deletions(-) > > > > > > diff --git a/env/sf.c b/env/sf.c > > > index 64c57f2cdf..af59c8375c 100644 > > > --- a/env/sf.c > > > +++ b/env/sf.c > > > @@ -50,15 +50,17 @@ static int setup_flash_device(void) > > > > > > env_flash = dev_get_uclass_priv(new); > > > #else > > > + struct spi_flash *new; > > > > > > - if (!env_flash) { > > > - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, > > > - CONFIG_ENV_SPI_CS, > > > - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); > > > - if (!env_flash) { > > > - env_set_default("spi_flash_probe() failed", 0); > > > - return -EIO; > > > - } > > > + if (env_flash) > > > + spi_flash_free(env_flash); > > > + > > > + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, > > > > Why not assign env_flash directly here? You do it in the very next > > statement anyway. I don't think the extra 'new' variable is needed. > > If flash pointer is used free it, before probing a new flash and > storing it in flash. Understood. > In this scenario it requires local new pointer, > it was known observation we have faced in cmd/sf.c What difference does using the local pointer make though? We don't do anything with it. We just assign it to env_flash in the next statement. This is similar to doing: a = foo(); b = a; if (a) ... The below snippet is equivalent: b = foo(); if (b) ... We can get rid of 'a' this way.
On Fri, May 15, 2020 at 2:19 PM Pratyush Yadav <p.yadav@ti.com> wrote: > > On 15/05/20 01:14PM, Jagan Teki wrote: > > On Fri, May 15, 2020 at 12:54 PM Pratyush Yadav <p.yadav@ti.com> wrote: > > > > > > Hi Jagan, > > > > > > On 14/05/20 05:41PM, Jagan Teki wrote: > > > > env_flash is a global flash pointer, and the probe would > > > > happen only if env_flash is NULL, but there is no checking > > > > and free the pointer if is not NULL. > > > > > > > > So, this patch frees the env_flash if it's not NULL, and > > > > get the probed flash in new flash pointer and finally > > > > assign into env_flash. > > > > > > > > Note: Similar approach has been followed and tested in > > > > cmd/sf.c > > > > > > > > Cc: Simon Glass <sjg@chromium.org> > > > > Cc: Vignesh R <vigneshr@ti.com> > > > > Signed-off-by: Jagan Teki <jagan@amarulasolutions.com> > > > > --- > > > > env/sf.c | 18 ++++++++++-------- > > > > 1 file changed, 10 insertions(+), 8 deletions(-) > > > > > > > > diff --git a/env/sf.c b/env/sf.c > > > > index 64c57f2cdf..af59c8375c 100644 > > > > --- a/env/sf.c > > > > +++ b/env/sf.c > > > > @@ -50,15 +50,17 @@ static int setup_flash_device(void) > > > > > > > > env_flash = dev_get_uclass_priv(new); > > > > #else > > > > + struct spi_flash *new; > > > > > > > > - if (!env_flash) { > > > > - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, > > > > - CONFIG_ENV_SPI_CS, > > > > - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); > > > > - if (!env_flash) { > > > > - env_set_default("spi_flash_probe() failed", 0); > > > > - return -EIO; > > > > - } > > > > + if (env_flash) > > > > + spi_flash_free(env_flash); > > > > + > > > > + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, > > > > > > Why not assign env_flash directly here? You do it in the very next > > > statement anyway. I don't think the extra 'new' variable is needed. > > > > If flash pointer is used free it, before probing a new flash and > > storing it in flash. > > Understood. > > > In this scenario it requires local new pointer, > > it was known observation we have faced in cmd/sf.c > > What difference does using the local pointer make though? We don't do > anything with it. We just assign it to env_flash in the next statement. > This is similar to doing: > > a = foo(); > b = a; > if (a) > ... > > The below snippet is equivalent: > > b = foo(); > if (b) > ... > > We can get rid of 'a' this way. The scenario here, seems different here we need to take care of free the old pointer and update new pointer with a global pointer like below. if (b) free(b); a = foo(); b = a; if (!a) fail; So, you mean to say as below? if (b) free(b); b = foo(); if (!b) fail; Jagan.
On 18/05/20 05:36PM, Jagan Teki wrote: > On Fri, May 15, 2020 at 2:19 PM Pratyush Yadav <p.yadav@ti.com> wrote: > > > > On 15/05/20 01:14PM, Jagan Teki wrote: > > > On Fri, May 15, 2020 at 12:54 PM Pratyush Yadav <p.yadav@ti.com> wrote: > > > > > > > > Hi Jagan, > > > > > > > > On 14/05/20 05:41PM, Jagan Teki wrote: > > > > > env_flash is a global flash pointer, and the probe would > > > > > happen only if env_flash is NULL, but there is no checking > > > > > and free the pointer if is not NULL. > > > > > > > > > > So, this patch frees the env_flash if it's not NULL, and > > > > > get the probed flash in new flash pointer and finally > > > > > assign into env_flash. > > > > > > > > > > Note: Similar approach has been followed and tested in > > > > > cmd/sf.c > > > > > > > > > > Cc: Simon Glass <sjg@chromium.org> > > > > > Cc: Vignesh R <vigneshr@ti.com> > > > > > Signed-off-by: Jagan Teki <jagan@amarulasolutions.com> > > > > > --- > > > > > env/sf.c | 18 ++++++++++-------- > > > > > 1 file changed, 10 insertions(+), 8 deletions(-) > > > > > > > > > > diff --git a/env/sf.c b/env/sf.c > > > > > index 64c57f2cdf..af59c8375c 100644 > > > > > --- a/env/sf.c > > > > > +++ b/env/sf.c > > > > > @@ -50,15 +50,17 @@ static int setup_flash_device(void) > > > > > > > > > > env_flash = dev_get_uclass_priv(new); > > > > > #else > > > > > + struct spi_flash *new; > > > > > > > > > > - if (!env_flash) { > > > > > - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, > > > > > - CONFIG_ENV_SPI_CS, > > > > > - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); > > > > > - if (!env_flash) { > > > > > - env_set_default("spi_flash_probe() failed", 0); > > > > > - return -EIO; > > > > > - } > > > > > + if (env_flash) > > > > > + spi_flash_free(env_flash); > > > > > + > > > > > + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, > > > > > > > > Why not assign env_flash directly here? You do it in the very next > > > > statement anyway. I don't think the extra 'new' variable is needed. > > > > > > If flash pointer is used free it, before probing a new flash and > > > storing it in flash. > > > > Understood. > > > > > In this scenario it requires local new pointer, > > > it was known observation we have faced in cmd/sf.c > > > > What difference does using the local pointer make though? We don't do > > anything with it. We just assign it to env_flash in the next statement. > > This is similar to doing: > > > > a = foo(); > > b = a; > > if (a) > > ... > > > > The below snippet is equivalent: > > > > b = foo(); > > if (b) > > ... > > > > We can get rid of 'a' this way. > > The scenario here, seems different here we need to take care of free > the old pointer and update new pointer with a global pointer like > below. > > if (b) > free(b); > a = foo(); > b = a; > if (!a) > fail; > > So, you mean to say as below? > > if (b) > free(b); > b = foo(); > if (!b) > fail; Yes, I mean this. Since the pointer b is freed/unassigned, the old pointer value is of no use to us. Getting rid of a (or new in case of this patch) makes the code a bit cleaner.
diff --git a/env/sf.c b/env/sf.c index 64c57f2cdf..af59c8375c 100644 --- a/env/sf.c +++ b/env/sf.c @@ -50,15 +50,17 @@ static int setup_flash_device(void) env_flash = dev_get_uclass_priv(new); #else + struct spi_flash *new; - if (!env_flash) { - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, - CONFIG_ENV_SPI_CS, - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); - if (!env_flash) { - env_set_default("spi_flash_probe() failed", 0); - return -EIO; - } + if (env_flash) + spi_flash_free(env_flash); + + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, + CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); + env_flash = new; + if (!new) { + env_set_default("spi_flash_probe() failed", 0); + return -EIO; } #endif return 0;