From patchwork Tue Mar 29 12:35:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: thomas.langer@lantiq.com X-Patchwork-Id: 88760 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id B9EFFB6F10 for ; Tue, 29 Mar 2011 23:45:40 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 62BF428098; Tue, 29 Mar 2011 14:45:39 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id e1NYqdoh+WLE; Tue, 29 Mar 2011 14:45:39 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 06F6C28099; Tue, 29 Mar 2011 14:45:38 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 4395428099 for ; Tue, 29 Mar 2011 14:45:36 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VL12aUN+W0pZ for ; Tue, 29 Mar 2011 14:45:35 +0200 (CEST) X-Greylist: delayed 593 seconds by postgrey-1.27 at theia; Tue, 29 Mar 2011 14:45:33 CEST X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from smtp2.infineon.com (smtp2.infineon.com [217.10.60.23]) by theia.denx.de (Postfix) with ESMTPS id 046C328098 for ; Tue, 29 Mar 2011 14:45:33 +0200 (CEST) X-SBRS: None Received: from unknown (HELO mucse413.eu.infineon.com) ([172.23.29.11]) by smtp2.infineon.com with ESMTP/TLS/RC4-MD5; 29 Mar 2011 14:21:57 +0200 Received: from mucse402.eu.infineon.com ([172.23.7.65]) by mucse413.eu.infineon.com ([172.23.29.11]) with mapi; Tue, 29 Mar 2011 14:35:39 +0200 From: To: Date: Tue, 29 Mar 2011 14:35:14 +0200 Thread-Topic: [PATCH] fix redundant environment for serial flash Thread-Index: AcvuDcD06M19QdoBQ8yZZ/yOkr97wQ== Message-ID: <3E5A05AB39640047B326BEF8E2FEBBDB0AAB40F20A@mucse402.eu.infineon.com> Accept-Language: de-DE, en-US Content-Language: de-DE X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: de-DE, en-US MIME-Version: 1.0 Subject: [U-Boot] [PATCH] fix redundant environment for serial flash X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This patch fixes problems in the handling of redundant environment in env_sf.c The major problem are double calls of free() on the allocated buffers, which damages the internal data of malloc and crashes on next call. In addition, the selection of the active environment had errors and compiler warnings, which are corrected by this patch. Signed-off-by: Thomas Langer --- This patch is done and tested against the version 2010.12 and applies also cleanly against the u-boot/master. --- a/common/env_sf.c +++ b/common/env_sf.c @@ -59,7 +59,6 @@ DECLARE_GLOBAL_DATA_PTR; extern uchar default_environment[]; char * env_name_spec = "SPI Flash"; -env_t *env_ptr; static struct spi_flash *env_flash; @@ -79,7 +78,7 @@ int saveenv(void) char *saved_buffer = NULL; u32 sector = 1; int ret; - char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; + char flag = OBSOLETE_FLAG; if (!env_flash) { env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, @@ -159,7 +158,7 @@ int saveenv(void) gd->env_valid = (gd->env_valid == 2 ? 1 : 2); - printf("Valid environment: %d\n", gd->env_valid); + printf("Valid environment: %d\n", (int)gd->env_valid); done: if (saved_buffer) @@ -174,25 +173,20 @@ void env_relocate_spec(void) env_t *tmp_env1 = NULL; env_t *tmp_env2 = NULL; env_t *ep = NULL; - uchar flag1, flag2; - /* current_env is set only in case both areas are valid! */ - int current_env = 0; tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); if (!tmp_env1 || !tmp_env2) { - free(tmp_env1); - free(tmp_env2); set_default_env("!malloc() failed"); - return; + goto out; } 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) { set_default_env("!spi_flash_probe() failed"); - return; + goto out; } ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, @@ -204,33 +198,30 @@ void env_relocate_spec(void) if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) crc1_ok = 1; - flag1 = tmp_env1->flags; ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, tmp_env2); if (!ret) { if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) crc2_ok = 1; - flag2 = tmp_env2->flags; } if (!crc1_ok && !crc2_ok) { - free(tmp_env1); - free(tmp_env2); set_default_env("!bad CRC"); - return; + goto err_read; } else if (crc1_ok && !crc2_ok) { gd->env_valid = 1; - ep = tmp_env1; } else if (!crc1_ok && crc2_ok) { + gd->env_valid = 2; + } else if (tmp_env1->flags == ACTIVE_FLAG && + tmp_env2->flags == OBSOLETE_FLAG) { gd->env_valid = 1; - } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { - gd->env_valid = 1; - } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { + } else if (tmp_env1->flags == OBSOLETE_FLAG && + tmp_env2->flags == ACTIVE_FLAG) { gd->env_valid = 2; - } else if (flag1 == flag2) { + } else if (tmp_env1->flags == tmp_env2->flags) { gd->env_valid = 2; - } else if (flag1 == 0xFF) { + } else if (tmp_env1->flags == 0xFF) { gd->env_valid = 2; } else { /* @@ -240,8 +231,6 @@ void env_relocate_spec(void) gd->env_valid = 2; } - free(env_ptr); - if (gd->env_valid == 1) ep = tmp_env1; else @@ -257,10 +246,6 @@ err_read: spi_flash_free(env_flash); env_flash = NULL; out: - if (tmp_env1) - free(tmp_env1); - if (tmp_env2) - free(tmp_env2); free(tmp_env1); free(tmp_env2);