From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([209.51.188.92]:33960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hK3h2-0004BM-3R for qemu-devel@nongnu.org; Fri, 26 Apr 2019 12:27:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hK3gy-0000yx-1k for qemu-devel@nongnu.org; Fri, 26 Apr 2019 12:27:18 -0400 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]:35957) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hK3gu-0000wg-LX for qemu-devel@nongnu.org; Fri, 26 Apr 2019 12:27:14 -0400 Received: by mail-io1-xd30.google.com with SMTP id d19so3444061ioc.3 for ; Fri, 26 Apr 2019 09:27:11 -0700 (PDT) From: Stephen Checkoway Date: Fri, 26 Apr 2019 12:26:20 -0400 Message-Id: <20190426162624.55977-7-stephen.checkoway@oberlin.edu> In-Reply-To: <20190426162624.55977-1-stephen.checkoway@oberlin.edu> References: <20190426162624.55977-1-stephen.checkoway@oberlin.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH v4 06/10] block/pflash_cfi02: Fix CFI in autoselect mode List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: QEMU Developers , Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Kevin Wolf , Max Reitz , "open list:Block layer core" , Markus Armbruster , Laszlo Ersek , Laurent Vivier , Paolo Bonzini Cc: Stephen Checkoway After a flash device enters CFI mode from autoselect mode, the reset command returns the device to autoselect mode. An additional reset command is necessary to return to read array mode. Signed-off-by: Stephen Checkoway Acked-by: Thomas Huth --- hw/block/pflash_cfi02.c | 21 +++++++++++++++++---- tests/pflash-cfi02-test.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c index c4efbe8cdf..be10036886 100644 --- a/hw/block/pflash_cfi02.c +++ b/hw/block/pflash_cfi02.c @@ -61,8 +61,9 @@ do { \ */ #define PFLASH_MAX_ERASE_REGIONS 4 -/* Special write cycle for CFI queries. */ +/* Special write cycles for CFI queries. */ #define WCYCLE_CFI 7 +#define WCYCLE_AUTOSELECT_CFI 8 struct PFlashCFI02 { /*< private >*/ @@ -325,6 +326,12 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, } if (cmd == 0xF0) { + if (pfl->wcycle == WCYCLE_AUTOSELECT_CFI) { + /* Return to autoselect mode. */ + pfl->wcycle = 3; + pfl->cmd = 0x90; + return; + } goto reset_flash; } } @@ -350,7 +357,6 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, /* We're in read mode */ check_unlock0: if (masked_addr == 0x55 && cmd == 0x98) { - enter_CFI_mode: /* Enter CFI query mode */ pfl->wcycle = WCYCLE_CFI; pfl->cmd = 0x98; @@ -427,9 +433,15 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, /* Unlock bypass reset */ goto reset_flash; } - /* We can enter CFI query mode from autoselect mode */ + /* + * We can enter CFI query mode from autoselect mode, but we must + * return to autoselect mode after a reset. + */ if (masked_addr == 0x55 && cmd == 0x98) { - goto enter_CFI_mode; + /* Enter autoselect CFI query mode */ + pfl->wcycle = WCYCLE_AUTOSELECT_CFI; + pfl->cmd = 0x98; + return; } /* No break here */ default: @@ -510,6 +522,7 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, } break; case WCYCLE_CFI: /* Special value for CFI queries */ + case WCYCLE_AUTOSELECT_CFI: DPRINTF("%s: invalid write in CFI query mode\n", __func__); goto reset_flash; default: diff --git a/tests/pflash-cfi02-test.c b/tests/pflash-cfi02-test.c index 703f084c5d..c2798bbb36 100644 --- a/tests/pflash-cfi02-test.c +++ b/tests/pflash-cfi02-test.c @@ -477,6 +477,42 @@ static void test_geometry(const void *opaque) qtest_quit(qtest); } +/* + * Test that + * 1. enter autoselect mode; + * 2. enter CFI mode; and then + * 3. exit CFI mode + * leaves the flash device in autoselect mode. + */ +static void test_cfi_in_autoselect(const void *opaque) +{ + const FlashConfig *config = opaque; + QTestState *qtest; + qtest = qtest_initf("-M musicpal,accel=qtest" + " -drive if=pflash,file=%s,format=raw,copy-on-read", + image_path); + FlashConfig explicit_config = expand_config_defaults(config); + explicit_config.qtest = qtest; + const FlashConfig *c = &explicit_config; + + /* 1. Enter autoselect. */ + unlock(c); + flash_cmd(c, UNLOCK0_ADDR, AUTOSELECT_CMD); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF)); + + /* 2. Enter CFI. */ + flash_cmd(c, CFI_ADDR, CFI_CMD); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0x10)), ==, replicate(c, 'Q')); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0x11)), ==, replicate(c, 'R')); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0x12)), ==, replicate(c, 'Y')); + + /* 3. Exit CFI. */ + reset(c); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF)); + + qtest_quit(qtest); +} + static void cleanup(void *opaque) { unlink(image_path); @@ -604,6 +640,9 @@ int main(int argc, char **argv) qtest_add_data_func(path, config, test_geometry); g_free(path); } + + qtest_add_data_func("pflash-cfi02/cfi-in-autoselect", &configuration[0], + test_cfi_in_autoselect); int result = g_test_run(); cleanup(NULL); return result; -- 2.20.1 (Apple Git-117) From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.7 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8BD8C43218 for ; Fri, 26 Apr 2019 16:33:01 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7F8E4208CA for ; Fri, 26 Apr 2019 16:33:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=oberlin-edu.20150623.gappssmtp.com header.i=@oberlin-edu.20150623.gappssmtp.com header.b="O0ZhkdO4" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7F8E4208CA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=oberlin.edu Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([127.0.0.1]:49366 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hK3mW-0000KG-LO for qemu-devel@archiver.kernel.org; Fri, 26 Apr 2019 12:33:00 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hK3h2-0004BM-3R for qemu-devel@nongnu.org; Fri, 26 Apr 2019 12:27:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hK3gy-0000yx-1k for qemu-devel@nongnu.org; Fri, 26 Apr 2019 12:27:18 -0400 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]:35957) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hK3gu-0000wg-LX for qemu-devel@nongnu.org; Fri, 26 Apr 2019 12:27:14 -0400 Received: by mail-io1-xd30.google.com with SMTP id d19so3444061ioc.3 for ; Fri, 26 Apr 2019 09:27:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oberlin-edu.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sXNPBantsfkNJHGqd1x2CHXS2MLrXNyNJGuBb1+37Bk=; b=O0ZhkdO4kCR2gd5cmgcvpZWeCily9JauPO2Z6f+ztll9NtIYMVMItpzcKviq2bu07X mgLRr7070+3mF5+o0cLdbMXzGY6O1NlMb9heXBfmr1hl8fvJEnGgZ0VDdveGA63SncK6 j63F0fe5eDUC6LNeiNuNJKKjffXrHJCcPAVrCC4BiQmriPmIb7mJDKfWZ7f9ZAzwsFp/ BXN/bZjXeoh41JZs1cyZHvwgLdAMKGSTiyeOiUjiWcV8S+D4sQgZZEXAtSztLcGsrnZn 2EyvFDX7QwKNOgfMNKPcrHnq0tzBW8wSc9O6IY0vXxCpQB6+DI+P01jJE3hLeIQWuiD5 42SA== 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:content-transfer-encoding; bh=sXNPBantsfkNJHGqd1x2CHXS2MLrXNyNJGuBb1+37Bk=; b=nJydJ+6b5Je/NfQN26zYB7z7HZjSvXQ85oPlsirZ0rPKxL6QELNb/mW71FnVy0lq+R YnpStbGUha5jFzr8fhcVfo6kyLg/B0ZhIJwTEO81l7nGyJZyRLVaRlXJ07EXgQ4xZ/+v Os894EdFwmSC1z3zpEh8KIym2oXuF36y0DPMsIkZaQNSB++LFF2eZENNYd3wTTA+VCAy WfGuHrmMZWkGRq9wj/n5cJl6Q5HI5BT7ihEZHs9ErO//zCIfJEhONl/VUJ5xThl2rh0w iVnUVbT0PlDqxTtzbOTZ1qWxhtYqJ9Tqqk0pes1W2wR7AdQZ1pF3eXOCnpXOMWvwJXbm 4H+A== X-Gm-Message-State: APjAAAWFqUUJyEBzkkiBRImuCJVJMyvvBnbWV+UAVx7xrg6pivdSlKi7 4Pj7Q5mHdVP4oyiH4VEQEGi2l6DIPL2/MQ== X-Google-Smtp-Source: APXvYqzOppszHgJITBZKjAUkTmZLUfyGu8SFFL0e1xrhZHjZ5OyASkjpZpW1J6k859dIYnxa4TkIYA== X-Received: by 2002:a5d:9b02:: with SMTP id y2mr2047560ion.238.1556296030890; Fri, 26 Apr 2019 09:27:10 -0700 (PDT) Received: from worksec.oberlin.net (ip-210-181.oberlin.net. [208.66.210.181]) by smtp.gmail.com with ESMTPSA id c7sm7548700ioc.63.2019.04.26.09.27.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Apr 2019 09:27:10 -0700 (PDT) From: Stephen Checkoway To: QEMU Developers , Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Kevin Wolf , Max Reitz , "open list:Block layer core" , Markus Armbruster , Laszlo Ersek , Laurent Vivier , Paolo Bonzini Date: Fri, 26 Apr 2019 12:26:20 -0400 Message-Id: <20190426162624.55977-7-stephen.checkoway@oberlin.edu> X-Mailer: git-send-email 2.20.1 (Apple Git-117) In-Reply-To: <20190426162624.55977-1-stephen.checkoway@oberlin.edu> References: <20190426162624.55977-1-stephen.checkoway@oberlin.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::d30 Subject: [Qemu-devel] [PATCH v4 06/10] block/pflash_cfi02: Fix CFI in autoselect mode X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stephen Checkoway Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="UTF-8" Message-ID: <20190426162620.3ox_rmudn_mveWrkrkc1hn6nBY_1q1wwjREY0T9tJ7c@z> After a flash device enters CFI mode from autoselect mode, the reset command returns the device to autoselect mode. An additional reset command is necessary to return to read array mode. Signed-off-by: Stephen Checkoway Acked-by: Thomas Huth --- hw/block/pflash_cfi02.c | 21 +++++++++++++++++---- tests/pflash-cfi02-test.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c index c4efbe8cdf..be10036886 100644 --- a/hw/block/pflash_cfi02.c +++ b/hw/block/pflash_cfi02.c @@ -61,8 +61,9 @@ do { \ */ #define PFLASH_MAX_ERASE_REGIONS 4 -/* Special write cycle for CFI queries. */ +/* Special write cycles for CFI queries. */ #define WCYCLE_CFI 7 +#define WCYCLE_AUTOSELECT_CFI 8 struct PFlashCFI02 { /*< private >*/ @@ -325,6 +326,12 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, } if (cmd == 0xF0) { + if (pfl->wcycle == WCYCLE_AUTOSELECT_CFI) { + /* Return to autoselect mode. */ + pfl->wcycle = 3; + pfl->cmd = 0x90; + return; + } goto reset_flash; } } @@ -350,7 +357,6 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, /* We're in read mode */ check_unlock0: if (masked_addr == 0x55 && cmd == 0x98) { - enter_CFI_mode: /* Enter CFI query mode */ pfl->wcycle = WCYCLE_CFI; pfl->cmd = 0x98; @@ -427,9 +433,15 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, /* Unlock bypass reset */ goto reset_flash; } - /* We can enter CFI query mode from autoselect mode */ + /* + * We can enter CFI query mode from autoselect mode, but we must + * return to autoselect mode after a reset. + */ if (masked_addr == 0x55 && cmd == 0x98) { - goto enter_CFI_mode; + /* Enter autoselect CFI query mode */ + pfl->wcycle = WCYCLE_AUTOSELECT_CFI; + pfl->cmd = 0x98; + return; } /* No break here */ default: @@ -510,6 +522,7 @@ static void pflash_write(void *opaque, hwaddr offset, uint64_t value, } break; case WCYCLE_CFI: /* Special value for CFI queries */ + case WCYCLE_AUTOSELECT_CFI: DPRINTF("%s: invalid write in CFI query mode\n", __func__); goto reset_flash; default: diff --git a/tests/pflash-cfi02-test.c b/tests/pflash-cfi02-test.c index 703f084c5d..c2798bbb36 100644 --- a/tests/pflash-cfi02-test.c +++ b/tests/pflash-cfi02-test.c @@ -477,6 +477,42 @@ static void test_geometry(const void *opaque) qtest_quit(qtest); } +/* + * Test that + * 1. enter autoselect mode; + * 2. enter CFI mode; and then + * 3. exit CFI mode + * leaves the flash device in autoselect mode. + */ +static void test_cfi_in_autoselect(const void *opaque) +{ + const FlashConfig *config = opaque; + QTestState *qtest; + qtest = qtest_initf("-M musicpal,accel=qtest" + " -drive if=pflash,file=%s,format=raw,copy-on-read", + image_path); + FlashConfig explicit_config = expand_config_defaults(config); + explicit_config.qtest = qtest; + const FlashConfig *c = &explicit_config; + + /* 1. Enter autoselect. */ + unlock(c); + flash_cmd(c, UNLOCK0_ADDR, AUTOSELECT_CMD); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF)); + + /* 2. Enter CFI. */ + flash_cmd(c, CFI_ADDR, CFI_CMD); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0x10)), ==, replicate(c, 'Q')); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0x11)), ==, replicate(c, 'R')); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0x12)), ==, replicate(c, 'Y')); + + /* 3. Exit CFI. */ + reset(c); + g_assert_cmpint(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF)); + + qtest_quit(qtest); +} + static void cleanup(void *opaque) { unlink(image_path); @@ -604,6 +640,9 @@ int main(int argc, char **argv) qtest_add_data_func(path, config, test_geometry); g_free(path); } + + qtest_add_data_func("pflash-cfi02/cfi-in-autoselect", &configuration[0], + test_cfi_in_autoselect); int result = g_test_run(); cleanup(NULL); return result; -- 2.20.1 (Apple Git-117)