From patchwork Sun Oct 25 06:04:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1387123 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.a=rsa-sha256 header.s=badeba3b8450 header.b=R+jOJfDx; dkim-atps=neutral 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 (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CJnWk03RXz9sVH for ; Sun, 25 Oct 2020 17:05:37 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8E2E08249B; Sun, 25 Oct 2020 07:05:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="R+jOJfDx"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EC7D88248F; Sun, 25 Oct 2020 07:05:00 +0100 (CET) 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,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (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 8CEFB82479 for ; Sun, 25 Oct 2020 07:04:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1603605895; bh=4cO9ODulUKfsjskdkKp0io3W/uZC38V7lWyWJ8WXtKA=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=R+jOJfDxYFl6YEj0kJ1f75UJeEiF83zxNbkcKXnJQmQKBwGK3ImlhLp8apAoG74o+ FWmYx1qbtcmChnGyXJZQoPMAcirbHi5MXfcTrFE0R4Mbm7PO627JaygB1Rxb/skBsw IxUTWw//g5sdg2pxks9HdUbl2nkIpm2rookLiX4g= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from LT02.fritz.box ([178.202.41.107]) by mail.gmx.com (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MQMyZ-1kjXb42v69-00MIED; Sun, 25 Oct 2020 07:04:55 +0100 From: Heinrich Schuchardt To: Simon Glass Cc: u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH 4/5] sandbox: implement reset Date: Sun, 25 Oct 2020 07:04:40 +0100 Message-Id: <20201025060441.11961-5-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201025060441.11961-1-xypron.glpk@gmx.de> References: <20201025060441.11961-1-xypron.glpk@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IxGRwPYTKoTfpKXXJDK6Zt4ZCyPwCseP2n TGRdMVlyulOGKKI4QFeiU0yDBwR+En1M0GMqWmO8xcJ9FoVjfHERbS3qpCql+mHjh3JgZey4t Vc7C7RwRUg4WHQ8S9T/Q== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.3 at phobos.denx.de X-Virus-Status: Clean Up to now the sandbox would shutdown upon a cold reset request. Instead it should be reset. In our coding we use static variables. The only safe way to return to an initial state is to relaunch the U-Boot binary. The reset implementation uses a longjmp() to return to the main() function and then relaunches U-Boot using execv(). Signed-off-by: Heinrich Schuchardt --- arch/sandbox/cpu/os.c | 14 ++++++++++++++ arch/sandbox/cpu/start.c | 22 ++++++++++++++++++++++ arch/sandbox/cpu/state.c | 1 + arch/sandbox/include/asm/u-boot-sandbox.h | 3 +++ drivers/sysreset/sysreset_sandbox.c | 3 +++ include/os.h | 5 +++++ 6 files changed, 48 insertions(+) -- 2.28.0 diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index c461fb0db0..ed044e87fb 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -817,3 +817,17 @@ void *os_find_text_base(void) return base; } + +void os_relaunch(int argc, char *argv[]) +{ + char **args; + + args = calloc(argc + 1, sizeof(char *)); + if (!args) + goto out; + memcpy(args, argv, sizeof(char *) * argc); + args[argc] = NULL; + execv(args[0], args); +out: + os_exit(1); +} diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index c6a2bbe468..ee1d4b9581 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -14,11 +15,15 @@ #include #include #include +#include #include #include DECLARE_GLOBAL_DATA_PTR; +/* longjmp buffer for reset */ +static struct jmp_buf_data reset_jmp; + /* Compare two options so that they can be sorted into alphabetical order */ static int h_compare_opt(const void *p1, const void *p2) { @@ -394,12 +399,29 @@ void state_show(struct sandbox_state *state) printf("\n"); } +void sandbox_reset(void) +{ + /* Do this here while it still has an effect */ + os_fd_restore(); + if (state_uninit()) + os_exit(2); + + if (dm_uninit()) + os_exit(2); + + /* This is considered normal termination for now */ + longjmp(&reset_jmp, 1); +} + int main(int argc, char *argv[]) { struct sandbox_state *state; gd_t data; int ret; + if (setjmp(&reset_jmp)) + os_relaunch(argc, argv); + memset(&data, '\0', sizeof(data)); gd = &data; gd->arch.text_base = os_find_text_base(); diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c index 34b6fff7e7..59f37fab0b 100644 --- a/arch/sandbox/cpu/state.c +++ b/arch/sandbox/cpu/state.c @@ -358,6 +358,7 @@ void state_reset_for_test(struct sandbox_state *state) /* No reset yet, so mark it as such. Always allow power reset */ state->last_sysreset = SYSRESET_COUNT; state->sysreset_allowed[SYSRESET_POWER_OFF] = true; + state->sysreset_allowed[SYSRESET_COLD] = true; state->allow_memio = false; memset(&state->wdt, '\0', sizeof(state->wdt)); diff --git a/arch/sandbox/include/asm/u-boot-sandbox.h b/arch/sandbox/include/asm/u-boot-sandbox.h index 798d003077..b1bdcbcde5 100644 --- a/arch/sandbox/include/asm/u-boot-sandbox.h +++ b/arch/sandbox/include/asm/u-boot-sandbox.h @@ -84,6 +84,9 @@ void sandbox_set_enable_pci_map(int enable); */ int sandbox_read_fdt_from_file(void); +/* Reset sandbox */ +void sandbox_reset(void); + /* Exit sandbox (quit U-Boot) */ void sandbox_exit(void); diff --git a/drivers/sysreset/sysreset_sandbox.c b/drivers/sysreset/sysreset_sandbox.c index 69c22a7000..c92132798c 100644 --- a/drivers/sysreset/sysreset_sandbox.c +++ b/drivers/sysreset/sysreset_sandbox.c @@ -56,6 +56,9 @@ static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type) switch (type) { case SYSRESET_COLD: state->last_sysreset = type; + if (!state->sysreset_allowed[type]) + return -EACCES; + sandbox_reset(); break; case SYSRESET_POWER_OFF: state->last_sysreset = type; diff --git a/include/os.h b/include/os.h index 1874ae674f..187dbf06f2 100644 --- a/include/os.h +++ b/include/os.h @@ -355,4 +355,9 @@ int os_read_file(const char *name, void **bufp, int *sizep); */ void *os_find_text_base(void); +/** + * os_relaunch() - restart the sandbox + */ +void os_relaunch(int argc, char *argv[]); + #endif