From patchwork Tue Mar 31 16:04:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Delaunay X-Patchwork-Id: 244651 List-Id: U-Boot discussion From: patrick.delaunay at st.com (Patrick Delaunay) Date: Tue, 31 Mar 2020 18:04:20 +0200 Subject: [PATCH 03/16] arm: stm32mp: reset to default environment when serial# change In-Reply-To: <20200331180330.1.Ied6708bad5048382a57618f95d67c549aae49f42@changeid> References: <20200331180330.1.Ied6708bad5048382a57618f95d67c549aae49f42@changeid> Message-ID: <20200331180330.3.I8f6df6d28ce5b4b601ced711af3699d95e1576fb@changeid> Serial number is first checked and, in case of mismatch, all environment variables are reset to their default value. This patch allows to detect that environment is saved in a removable device, as a SD card, and reused on a other board, potentially with incompatible variables. Signed-off-by: Patrick Delaunay --- arch/arm/mach-stm32mp/cpu.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/arm/mach-stm32mp/cpu.c b/arch/arm/mach-stm32mp/cpu.c index 9aa5794334..365c2aa4f7 100644 --- a/arch/arm/mach-stm32mp/cpu.c +++ b/arch/arm/mach-stm32mp/cpu.c @@ -511,8 +511,9 @@ __weak int setup_mac_address(void) return -EINVAL; } pr_debug("OTP MAC address = %pM\n", enetaddr); - ret = !eth_env_set_enetaddr("ethaddr", enetaddr); - if (!ret) + + ret = eth_env_set_enetaddr("ethaddr", enetaddr); + if (ret) pr_err("Failed to set mac address %pM from OTP: %d\n", enetaddr, ret); #endif @@ -522,13 +523,13 @@ __weak int setup_mac_address(void) static int setup_serial_number(void) { + char *serial_env; char serial_string[25]; u32 otp[3] = {0, 0, 0 }; struct udevice *dev; int ret; - if (env_get("serial#")) - return 0; + serial_env = env_get("serial#"); ret = uclass_get_device_by_driver(UCLASS_MISC, DM_GET_DRIVER(stm32mp_bsec), @@ -542,6 +543,15 @@ static int setup_serial_number(void) return ret; sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]); + + if (serial_env) { + if (!strcmp(serial_string, serial_env)) + return 0; + /* For invalid enviromnent (serial# change), reset to default */ + env_set_default("serial number mismatch", 0); + } + + /* save serial number */ env_set("serial#", serial_string); return 0; @@ -549,9 +559,9 @@ static int setup_serial_number(void) int arch_misc_init(void) { + setup_serial_number(); setup_boot_mode(); setup_mac_address(); - setup_serial_number(); return 0; }