From nobody Fri May 17 11:58:44 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1712565433; cv=none; d=zohomail.com; s=zohoarc; b=lsFYVs+tXVHFIFx8N/gk0x6iiULP063jSl3OtvnzHovfnR//5E4JNQIab6fcBaGDCQAqPgRO4R9XvnepQX/FoC6G5c5bWuFbHRt/PBFFAqA85OCoW06sk+4G9XpCe8ptXOYdQuwb8vSpUXlhDDx0UolcU8Xb0rdzx3b8d+M+e00= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1712565433; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=m6BQV7fTkLyG6qLOnaR99Tjx9wX3tBZOaYxcFxZPlB4=; b=kMy78Vy+RLnJ4VukiFcL9oe6oNZVxteIx6cKHqzlaPU43po8d3Q8F/YkOFfOIJ55cSqmxRN3ke5eD1wY354BiecZG7vz4/vhN0gDygDFUPuEz+zv24BavpYU4zcTL9SxO4i5T+my0tuhf/yKeon7ji8G+vm2R103xaIRBoa9IRU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1712565433750268.6391522040402; Mon, 8 Apr 2024 01:37:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkUI-0005ks-M4; Mon, 08 Apr 2024 04:36:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkUG-0005kV-4q for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:36:20 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rtkUC-0002kX-D6 for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:36:17 -0400 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a51c6e91793so189029766b.0 for ; Mon, 08 Apr 2024 01:36:16 -0700 (PDT) Received: from m1x-phil.lan ([176.176.144.67]) by smtp.gmail.com with ESMTPSA id md16-20020a170906ae9000b00a51e004de4asm256150ejb.11.2024.04.08.01.36.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Apr 2024 01:36:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712565374; x=1713170174; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=m6BQV7fTkLyG6qLOnaR99Tjx9wX3tBZOaYxcFxZPlB4=; b=kMKPzEyUD/F0sbYq/nQ82zPTsTn7vQ81d+XVIqLKRNOVuEG6Q6cCJcGasr/rARUQcs VDySXTWgEtPxeBAHb2Sci16CH/x2yjEcK1kCUBeJhMwZY3cL2JVdrZMutsu+D1s3aoth Igi49chemjMhA2glyL52lho4KhvrfjdDnDHOLs+HLaDyt1XSnOz5NU7rUNNe9k9Rh8iH 1xYL2BAO8v1Xz8y0+Fu2NVaR6MNMfFg0nVRsRvfZG19BmmLevMRlibts2ZeJeBuPWDCW enyBcUc//EAaj8IqZxYbajadKFAAa9wxscG9dSI57SPN622NkN0Z+EhSruzjgyXejduH qGZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565374; x=1713170174; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m6BQV7fTkLyG6qLOnaR99Tjx9wX3tBZOaYxcFxZPlB4=; b=gSBTT++mvBNP1a8T63otJ/HpXo7Ad7NvMd6xHtF3eWv/j6KwfKsgv7zBkhfBuYgyVB 07QPn7u/VkGLwyPl4HLgofJxsNxjU5zlZf6pLEXqf6RhPbjE56m+3KRXYTtc4Y5oA6Z5 mdFAeQ0KkWdSnAqtdBjnbCYk+iKNEAheda+hVfqIzlAsJWv8hHCOYmu/bfdiRJHumy9s lT2rbb3xogfwPw4J2jO7DH7L/J7W1cBZZXGWLTFdlcjEx4EK2qBvLtCpg6pCg4kNScDS d4ArFjoxkSwoS2qDPMFpJrkKVRsS+VlOgmk+J+PgS0I4Hd5j4fZAMK60Z1BsDR0S6gRV 9FdQ== X-Gm-Message-State: AOJu0YycEkxeF+Fl9l6GKwBTSyLATRMLwqTyiXSVY8V8CyTSSnbm5qBn ukJ1Lqw0CUEAeGoSFItobgsMG4UanC11Mll5xSCd/MBmhB2vM6aEKhencLjxYjSuclBmsAYCfF4 v X-Google-Smtp-Source: AGHT+IFZzJWTVKKw/ve7ikXpCJDTHP9hLp5EDKt5VVZPoTGhoKqAbeb5rS/mFN+gmXTTTD2AtZOoYQ== X-Received: by 2002:a17:906:bc46:b0:a51:9e23:dc44 with SMTP id s6-20020a170906bc4600b00a519e23dc44mr7328386ejv.31.1712565374465; Mon, 08 Apr 2024 01:36:14 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Qiang Liu , Mauro Matteo Cascella , Kevin Wolf , Alexander Bulekov , Hanna Reitz , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH-for-9.0? 1/3] hw/block/nand: Factor nand_load_iolen() method out Date: Mon, 8 Apr 2024 10:36:03 +0200 Message-ID: <20240408083605.55238-2-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240408083605.55238-1-philmd@linaro.org> References: <20240408083605.55238-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=philmd@linaro.org; helo=mail-ej1-x62f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1712565434890100001 Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf Reviewed-by: Richard Henderson --- hw/block/nand.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/hw/block/nand.c b/hw/block/nand.c index d1435f2207..6fa9038bb5 100644 --- a/hw/block/nand.c +++ b/hw/block/nand.c @@ -243,9 +243,25 @@ static inline void nand_pushio_byte(NANDFlashState *s,= uint8_t value) } } =20 +/* + * nand_load_block: Load block containing (s->addr + @offset). + * Returns length of data available at @offset in this block. + */ +static int nand_load_block(NANDFlashState *s, int offset) +{ + int iolen; + + s->blk_load(s, s->addr, offset); + + iolen =3D (1 << s->page_shift) - offset; + if (s->gnd) { + iolen +=3D 1 << s->oob_shift; + } + return iolen; +} + static void nand_command(NANDFlashState *s) { - unsigned int offset; switch (s->cmd) { case NAND_CMD_READ0: s->iolen =3D 0; @@ -271,12 +287,7 @@ static void nand_command(NANDFlashState *s) case NAND_CMD_NOSERIALREAD2: if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP)) break; - offset =3D s->addr & ((1 << s->addr_shift) - 1); - s->blk_load(s, s->addr, offset); - if (s->gnd) - s->iolen =3D (1 << s->page_shift) - offset; - else - s->iolen =3D (1 << s->page_shift) + (1 << s->oob_shift) - offs= et; + s->iolen =3D nand_load_block(s, s->addr & ((1 << s->addr_shift) - = 1)); break; =20 case NAND_CMD_RESET: @@ -597,12 +608,7 @@ uint32_t nand_getio(DeviceState *dev) if (!s->iolen && s->cmd =3D=3D NAND_CMD_READ0) { offset =3D (int) (s->addr & ((1 << s->addr_shift) - 1)) + s->offse= t; s->offset =3D 0; - - s->blk_load(s, s->addr, offset); - if (s->gnd) - s->iolen =3D (1 << s->page_shift) - offset; - else - s->iolen =3D (1 << s->page_shift) + (1 << s->oob_shift) - offs= et; + s->iolen =3D nand_load_block(s, offset); } =20 if (s->ce || s->iolen <=3D 0) { --=20 2.41.0 From nobody Fri May 17 11:58:44 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1712565466; cv=none; d=zohomail.com; s=zohoarc; b=cya0ykT0lSOgAQWPTRc1KKgqAzGPxbbRInnd0e9bPtJDhA2J+I9AidjJ/VAR8NqspAjriKKwooMumN/aVHZ7s1q4x8YXEiJjh786dxAjyUNta3hCP7KyGdSMbOBtvlXu5NomFMSLbpuM6Kc3YHoqHNaRsEDHVorfcjkbmUFhemw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1712565466; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6lxlzp3IlPActhEyj4Nxu8uqxhWQDk/wL/7CgsYIL7w=; b=oIl3/YZBdl5G/d0+c34kRgi7WRMPy34w2rWY2A2HhZHFcJA5Wp8wAbX17zjxmttH9QMjzthUz2NVcy1ysSB6r2wSVp4KW5eLsEhw6XujLo0/aXW/zJOu5LDngNZXmMOZoKjeJuk4FaLPRQihLPxWMxHOTdehDj6lVf15XLqXaR8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1712565466045935.6476088687981; Mon, 8 Apr 2024 01:37:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkUL-0005m0-CE; Mon, 08 Apr 2024 04:36:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkUJ-0005lg-UZ for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:36:23 -0400 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rtkUH-0002lB-Tb for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:36:23 -0400 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-56e48d0a632so2309919a12.2 for ; Mon, 08 Apr 2024 01:36:21 -0700 (PDT) Received: from m1x-phil.lan ([176.176.144.67]) by smtp.gmail.com with ESMTPSA id js23-20020a170906ca9700b00a51c6d98777sm2125981ejb.58.2024.04.08.01.36.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Apr 2024 01:36:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712565380; x=1713170180; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6lxlzp3IlPActhEyj4Nxu8uqxhWQDk/wL/7CgsYIL7w=; b=PiM4tKI7wJalGJ3b6ykWrrhCOyOHGphuvgyDhM9aDitp9eA5WlZgVE2QCfcTXQIGN7 Klc1Zytv9o7LFIpFwgg4tZVbVcfHY4YeJ3dOWGO71t5FAb4yJyc+nDqe0fHdCPU4lUvQ GSlNriDPu3VRveI4+kgjQ9xtg9mBWUj86vxw45ES8lOvNWm/caRi+PpBJuj84vuLKL8o VN/9RPmGGAC7J8IPDnAZfpGmaz/GM3oLdv/rqwprJVLitt8TNlm4AcqPNaXDy+2xA6PG NwyIas0rt9FVaPSTEacXLvUVye0ivQmChDy7kG28dp9LabJ5UDJlSeyRoQcbJQWAmF3C olFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565380; x=1713170180; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6lxlzp3IlPActhEyj4Nxu8uqxhWQDk/wL/7CgsYIL7w=; b=p1WnDbub0sZtXTlyFsydLiupv0Mg0C06nBq5N8HDViTPBqNRxjuANMHf/Gmq6ZzunS SU8Xk1gBefQw3259GxzwGFdFK6+8sZ4gTZ+w3fv6kF1n0OfezQnKXPSiszZTmh8IzZLY NfUsUKuhBMZ6ZyCbZK2+VTml/fKvwBiS0ZZ86tRGedBNG5Q9oxaqqHK+Rt2Ydk8y+zvC nSiDZE4fYjFV+/goUtfStW8mgElXx8FLu4NIooY/y7v2wRqGykHGcgvKEW1Q3S4J3ISz QBvBn1bBOXKBKmFloR3v1Wj8EtO5dyVI+xfNjsd5VwGg/uP9tXi2wY+O/Fl3Gb3d5scY fxkw== X-Gm-Message-State: AOJu0YxYO+hUpdpNf10D6q1GtDsUiDbH/sKFDzJj0eHfKcwRyGSDb/yL L0TFDJacj4MVeXBN2EYN49F6wFjYmjnmY+5WIxns0TajaqaAGzysvAed+g46aAgKC3OEdQ9x4ox 0 X-Google-Smtp-Source: AGHT+IF5SSa4t5CZPDFTEYngdcUo5zdrT63q+f6E2IQ+acTieHYFXmFr4IDhgirj+i2GE2GNV/zVjw== X-Received: by 2002:a17:907:94c7:b0:a51:d3da:99a9 with SMTP id dn7-20020a17090794c700b00a51d3da99a9mr2004574ejc.51.1712565380294; Mon, 08 Apr 2024 01:36:20 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Qiang Liu , Mauro Matteo Cascella , Kevin Wolf , Alexander Bulekov , Hanna Reitz , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH-for-9.0? 2/3] hw/block/nand: Have blk_load() return boolean indicating success Date: Mon, 8 Apr 2024 10:36:04 +0200 Message-ID: <20240408083605.55238-3-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240408083605.55238-1-philmd@linaro.org> References: <20240408083605.55238-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::532; envelope-from=philmd@linaro.org; helo=mail-ed1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1712565466798100001 Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf Reviewed-by: Richard Henderson --- hw/block/nand.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hw/block/nand.c b/hw/block/nand.c index 6fa9038bb5..3627c799b5 100644 --- a/hw/block/nand.c +++ b/hw/block/nand.c @@ -84,7 +84,11 @@ struct NANDFlashState { =20 void (*blk_write)(NANDFlashState *s); void (*blk_erase)(NANDFlashState *s); - void (*blk_load)(NANDFlashState *s, uint64_t addr, int offset); + /* + * Returns %true when block containing (@addr + @offset) is + * successfully loaded, otherwise %false. + */ + bool (*blk_load)(NANDFlashState *s, uint64_t addr, int offset); =20 uint32_t ioaddr_vmstate; }; @@ -769,11 +773,11 @@ static void glue(nand_blk_erase_, NAND_PAGE_SIZE)(NAN= DFlashState *s) } } =20 -static void glue(nand_blk_load_, NAND_PAGE_SIZE)(NANDFlashState *s, +static bool glue(nand_blk_load_, NAND_PAGE_SIZE)(NANDFlashState *s, uint64_t addr, int offset) { if (PAGE(addr) >=3D s->pages) { - return; + return false; } =20 if (s->blk) { @@ -801,6 +805,8 @@ static void glue(nand_blk_load_, NAND_PAGE_SIZE)(NANDFl= ashState *s, offset, NAND_PAGE_SIZE + OOB_SIZE - offset); s->ioaddr =3D s->io; } + + return true; } =20 static void glue(nand_init_, NAND_PAGE_SIZE)(NANDFlashState *s) --=20 2.41.0 From nobody Fri May 17 11:58:44 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1712565433; cv=none; d=zohomail.com; s=zohoarc; b=KaVJNDwa5DkR5KksAAldlomxO1esop5/glmUj5dlRB3bUuSpruJkfcBUlagDmGw1Gcnuu6sxDptNbM//uKPi2KKt//RuJs3Q9azjqGWpgmmqIXIcpxmy+FHhlj40rfrfoa8hsJwGOsheOCeU2JzmapgLoNTpz9XIa68XOPrb1dk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1712565433; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0jCU1df9n+PO9XWHeO5IZQ9CHjWOsBfm5l6WMz/CJRw=; b=cGn/Pj9H7UqkHrb95bcsx1AeanrEX96PRHTZZYUss1X+7KqJBY9egeNgO4xGf52uhiZ9G2Qld4tvd0RSPR+uVNgEr1L24r7GndoU1t53I0i25acAcxy/6k72QW26w/vy8nujZ8T8HfjpRCoOPPIZkpbfo0WnMXEWxlM/xT0ztyo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1712565433745421.3937589918895; Mon, 8 Apr 2024 01:37:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkUS-0005n5-CQ; Mon, 08 Apr 2024 04:36:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkUQ-0005ma-2Z for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:36:30 -0400 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rtkUO-0002lk-8J for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:36:29 -0400 Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-a51d05c50b2so114769866b.0 for ; Mon, 08 Apr 2024 01:36:27 -0700 (PDT) Received: from m1x-phil.lan ([176.176.144.67]) by smtp.gmail.com with ESMTPSA id qy47-20020a17090768af00b00a517995c070sm4131041ejc.33.2024.04.08.01.36.24 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Apr 2024 01:36:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712565386; x=1713170186; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0jCU1df9n+PO9XWHeO5IZQ9CHjWOsBfm5l6WMz/CJRw=; b=KoF0npzMTjBFYI+f6oRPuIhCtdI8zn/fLgw8ZBIapnFr3xxWFe3v7vEGOPSzQkERQ6 sG/6R2fZd9ciKEoSEnD0nPJDw3Sd6ZPpCO/YKPUK5AA2Fb09damLiUuRkUO8/Q0dK2l/ t+hxRtR5vz3ZDjDeb9tGk1tHFSy1AEglt0gMWZEXWFv9R2igwAS+Z13QveCKc33MNaqB qSv0elZ/r6/KFX6hCViiM/fxpQ/E3wm5Jhh1nswqrfH607W0hXxPvO4iKB8pz+mK0kng rNQlL+VWrG+OCQ+wEN7g6y/lBuZRnJPY4J8aHpoxJ7qjzS8cBuN3xO/TJcf1KgYVGfb5 cF3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712565386; x=1713170186; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0jCU1df9n+PO9XWHeO5IZQ9CHjWOsBfm5l6WMz/CJRw=; b=lPLXKxnfhn8R2R8t7yYuMUavRzdcoIFls5lrHt8LerVPYZGTCMqopC12bTfIt3I7dC KXMDfQ89BBP28+iZ+KkFPdBjq94LRoNcK/BPu5/Tq5qNlxYtZNywiitj4NySLXkn5PAD X2kxP+4f+Npbhx3ThIEVDv75DjUURN9ADLhVr6IJkdQVl8kAOGtUDZo6DG5oCjZo7x8v X5bf+IDa9E/Vd9cwQoxspQfJ2MyDUJ0TTiaydL6wlxgHclZuo95KlIl3wEYIbDAuJqAt PuiNkwdG8lQB54am41ar1o+lxh5uv5I6uX9kINUxtr1XfWWeTlclr436B41EaVHg98Il Rr0A== X-Gm-Message-State: AOJu0Yzlr5YAiKlHd37UPO4LObnZdfJxzphVgia7N/yxnNeCrOasr89k pXktbd1+bbqkEC1xWsC+nmLTOVs5qZkPNJhL9WF4yaRZmRxYtsTSOAN738tITigpgxqzT/lkNS7 O X-Google-Smtp-Source: AGHT+IH4XEQiPsIzzu/VoKqIKJ0mFQvpmq/rta23HxnxZcCKXDAPA5b2snwrDyuF99PbMQdH14GQoQ== X-Received: by 2002:a17:907:31c9:b0:a51:ddc6:edc1 with SMTP id xf9-20020a17090731c900b00a51ddc6edc1mr711411ejb.28.1712565386203; Mon, 08 Apr 2024 01:36:26 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Qiang Liu , Mauro Matteo Cascella , Kevin Wolf , Alexander Bulekov , Hanna Reitz , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH-for-9.0? 3/3] hw/block/nand: Fix out-of-bound access in NAND block buffer Date: Mon, 8 Apr 2024 10:36:05 +0200 Message-ID: <20240408083605.55238-4-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240408083605.55238-1-philmd@linaro.org> References: <20240408083605.55238-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::631; envelope-from=philmd@linaro.org; helo=mail-ej1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1712565434897100002 nand_command() and nand_getio() don't check @offset points into the block, nor the available data length (s->iolen) is not negative. In order to fix: - check the offset is in range in nand_blk_load_NAND_PAGE_SIZE(), - do not set @iolen if blk_load() failed. Reproducer: $ cat << EOF | qemu-system-arm -machine tosa \ -monitor none -serial none \ -display none -qtest stdio write 0x10000111 0x1 0xca write 0x10000104 0x1 0x47 write 0x1000ca04 0x1 0xd7 write 0x1000ca01 0x1 0xe0 write 0x1000ca04 0x1 0x71 write 0x1000ca00 0x1 0x50 write 0x1000ca04 0x1 0xd7 read 0x1000ca02 0x1 write 0x1000ca01 0x1 0x10 EOF =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D15750=3D=3DERROR: AddressSanitizer: heap-buffer-overflow on address 0= x61f000000de0 at pc 0x560e61557210 bp 0x7ffcfc4a59f0 sp 0x7ffcfc4a59e8 READ of size 1 at 0x61f000000de0 thread T0 #0 0x560e6155720f in mem_and hw/block/nand.c:101:20 #1 0x560e6155ac9c in nand_blk_write_512 hw/block/nand.c:663:9 #2 0x560e61544200 in nand_command hw/block/nand.c:293:13 #3 0x560e6153cc83 in nand_setio hw/block/nand.c:520:13 #4 0x560e61a0a69e in tc6393xb_nand_writeb hw/display/tc6393xb.c:380:13 #5 0x560e619f9bf7 in tc6393xb_writeb hw/display/tc6393xb.c:524:9 #6 0x560e647c7d03 in memory_region_write_accessor softmmu/memory.c:492:5 #7 0x560e647c7641 in access_with_adjusted_size softmmu/memory.c:554:18 #8 0x560e647c5f66 in memory_region_dispatch_write softmmu/memory.c:1514= :16 #9 0x560e6485409e in flatview_write_continue softmmu/physmem.c:2825:23 #10 0x560e648421eb in flatview_write softmmu/physmem.c:2867:12 #11 0x560e64841ca8 in address_space_write softmmu/physmem.c:2963:18 #12 0x560e61170162 in qemu_writeb tests/qtest/videzzo/videzzo_qemu.c:10= 80:5 #13 0x560e6116eef7 in dispatch_mmio_write tests/qtest/videzzo/videzzo_q= emu.c:1227:28 0x61f000000de0 is located 0 bytes to the right of 3424-byte region [0x61f00= 0000080,0x61f000000de0) allocated by thread T0 here: #0 0x560e611276cf in malloc /root/llvm-project/compiler-rt/lib/asan/asa= n_malloc_linux.cpp:145:3 #1 0x7f7959a87e98 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0= x57e98) #2 0x560e64b98871 in object_new qom/object.c:749:12 #3 0x560e64b5d1a1 in qdev_new hw/core/qdev.c:153:19 #4 0x560e61547ea5 in nand_init hw/block/nand.c:639:11 #5 0x560e619f8772 in tc6393xb_init hw/display/tc6393xb.c:558:16 #6 0x560e6390bad2 in tosa_init hw/arm/tosa.c:250:12 SUMMARY: AddressSanitizer: heap-buffer-overflow hw/block/nand.c:101:20 in m= em_and =3D=3D15750=3D=3DABORTING Broken since introduction in commit 3e3d5815cb ("NAND Flash memory emulation and ECC calculation helpers for use by NAND controllers"). Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1446 Reported-by: Qiang Liu Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf Reviewed-by: Richard Henderson --- hw/block/nand.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hw/block/nand.c b/hw/block/nand.c index 3627c799b5..d90dc965a1 100644 --- a/hw/block/nand.c +++ b/hw/block/nand.c @@ -255,7 +255,9 @@ static int nand_load_block(NANDFlashState *s, int offse= t) { int iolen; =20 - s->blk_load(s, s->addr, offset); + if (!s->blk_load(s, s->addr, offset)) { + return 0; + } =20 iolen =3D (1 << s->page_shift) - offset; if (s->gnd) { @@ -780,6 +782,10 @@ static bool glue(nand_blk_load_, NAND_PAGE_SIZE)(NANDF= lashState *s, return false; } =20 + if (offset > NAND_PAGE_SIZE + OOB_SIZE) { + return false; + } + if (s->blk) { if (s->mem_oob) { if (blk_pread(s->blk, SECTOR(addr) << BDRV_SECTOR_BITS, --=20 2.41.0