Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C399AC4332F for ; Fri, 9 Dec 2022 22:02:21 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B617D852A0; Fri, 9 Dec 2022 23:02:18 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seaofdirac.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=permerror (0-bit key) header.d=seaofdirac.org header.i=@seaofdirac.org header.b="bsSy8hvE"; dkim=pass (2048-bit key; secure) header.d=seaofdirac.org header.i=@seaofdirac.org header.b="fWKEnPfs"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C8FE5852BB; Fri, 9 Dec 2022 23:02:16 +0100 (CET) Received: from singularity.seaofdirac.org (singularity.seaofdirac.org [71.19.156.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E426985296 for ; Fri, 9 Dec 2022 23:02:13 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seaofdirac.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=derek@seaofdirac.org DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/simple; d=seaofdirac.org; i=@seaofdirac.org; q=dns/txt; s=2020073000e; t=1670623331; h=date : from : to : cc : subject : message-id : mime-version : content-type : bimi-selector; bh=mBNvsJdR1TJoKXZXI5imi0k1okt6dbTARejovgb94aE=; b=bsSy8hvEsWdoXPfUFjy6jxaR7NgLyg36DFbGRAF7UB7KpdjhiYFPC63b4yV3uzT65LK3e t8F/miOxWJE03PODw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=seaofdirac.org; i=@seaofdirac.org; q=dns/txt; s=2020051800; t=1670623331; h=date : from : to : cc : subject : message-id : mime-version : content-type : bimi-selector; bh=mBNvsJdR1TJoKXZXI5imi0k1okt6dbTARejovgb94aE=; b=fWKEnPfsrZCLe7ct/MK+hdL7LuOAW7QzZ7AhCGx6n8uTWL6QzJOrfy7zzCQiQc42k/LUk bGsHErj1nCvMOfLYcigZWCtGkkTK8SwRcz0CSWrwEPnJeH2w5DbsHZFXlWSmYgjiO/gvU6r GV1BG5atbutzknp+vhX69ag1mkgj7A6yQDF8MIcAQap+lN9sdcC3CfEiHhTMLKJeCb9w1N8 adYaWuK/bXZdasMwcqvd++IHLwogJeRahoShFcmJz27HBEbJGxxyAqzXzQWzRT8BKHEzTzH JB1PSIza5UbLLO+FxouPHcl1pAShG7YcTaGXkc2zwtXLcax+nKEIn4//KJCQ== Received: (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) (Authenticated sender: derek@SEAOFDIRAC.ORG) by singularity.seaofdirac.org (Postfix) with ESMTPSA id 016283FF21; Fri, 9 Dec 2022 17:02:10 -0500 (EST) Message-ID: Subject: [PATCH v3 1/1] arm: mvebu: Espressobin: Fix default env variables From: Derek LaHousse To: u-boot@lists.denx.de Cc: sr@denx.de, kostap@marvell.com, pali@kernel.org Date: Fri, 09 Dec 2022 17:04:01 -0500 Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.3-1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Default env variables on Espressobin boards are broken since commit c4df0f6f315c ("arm: mvebu: Espressobin: Set default value for $fdtfile env variable") as well as the 'env default -a' command. The algorithm to find free space in the default_environment[] array returns after the first env variable instead of the correct position of the last variable, where there is allocated free space. This causes that U-Boot board_late_init() function to overwrite a portion of the default environment with $ethXaddr and $fdtfile variables immediately after the first env variable and so it is overwriting other variables. This patch also adds an additional null byte to terminate the environment array. But U-Boot board_late_init() function do not fill this nul byte explicitly. And because of that, U-Boot is later trying to interpret remaining buffer as a continuation of variable list. Normally buffer should be empty but due to the above issue, it contains garbage from remaining env variables. For example 'env default -a' command results in damaging variable names. It was observed that scritaddr variable name was changed to criptaddr (without leading 's'). This bug was reported and discussed on the Armbian forum: https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=138136 Fix these issues in two steps: 1) Change code which finds free space for dynamic env variables in    default_environment[] array by jumping to the end of the variable list instead of jumping after the first defined variable. [By Derek] 2) Add code which appends terminating nul byte as indication of the end of the env list, after the last nul term env string. [By Pali] Fixes: c4df0f6f315c ("arm: mvebu: Espressobin: Set default value for $fdtfile env variable") Signed-off-by: Derek LaHousse Signed-off-by: Pali Rohár --- Changes in v3: - End of line wrap fix Changes in v2: - Include Pali's end-of-array null - Better tags - More documentation of what is fixed diff --git a/board/Marvell/mvebu_armada-37xx/board.c b/board/Marvell/mvebu_armada-37xx/board.c index c6ecc323bb99..44c72344e8be 100644 --- a/board/Marvell/mvebu_armada-37xx/board.c +++ b/board/Marvell/mvebu_armada-37xx/board.c @@ -99,9 +99,16 @@ int board_late_init(void) if (!of_machine_is_compatible("globalscale,espressobin")) return 0; - /* Find free buffer in default_environment[] for new variables */ - while (*ptr != '\0' && *(ptr+1) != '\0') ptr++; - ptr += 2; + /* + * Find free space for new variables in default_environment[] array. + * Free space is after the last variable, each variable is termined + * by nul byte and after the last variable is additional nul byte. + * Move ptr to the position where new variable can be filled. + */ + while (*ptr != '\0') { + do { ptr++; } while (*ptr != '\0'); + ptr++; + } /* * Ensure that 'env default -a' does not erase permanent MAC addresses @@ -145,6 +152,13 @@ int board_late_init(void) strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin- emmc.dtb"); else strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin.dtb"); + ptr += strlen(ptr) + 1; + + /* + * After the last variable (which is nul term string) append another nul + * byte which terminates the list. So everything after ptr is ignored. + */ + *ptr = '\0'; return 0; }