From nobody Sat May 18 07:48:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551267509910799.6463988938395; Wed, 27 Feb 2019 03:38:29 -0800 (PST) Received: from localhost ([127.0.0.1]:42455 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyxXe-0007Wc-HU for importer@patchew.org; Wed, 27 Feb 2019 06:38:26 -0500 Received: from eggs.gnu.org ([209.51.188.92]:38797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyxP9-0000dN-7c for qemu-devel@nongnu.org; Wed, 27 Feb 2019 06:29:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyx9z-00063s-VJ for qemu-devel@nongnu.org; Wed, 27 Feb 2019 06:14:01 -0500 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]:53201) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyx9w-0005yy-2p for qemu-devel@nongnu.org; Wed, 27 Feb 2019 06:13:58 -0500 Received: by mail-wm1-x32e.google.com with SMTP id m1so5442871wml.2 for ; Wed, 27 Feb 2019 03:13:53 -0800 (PST) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id o12sm37324747wre.0.2019.02.27.03.13.51 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 27 Feb 2019 03:13:51 -0800 (PST) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 57C101FF80; Wed, 27 Feb 2019 11:13:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ykOljB2vbGcyFYJUaA+gCQdraZEMWCskvExfV6LRvOY=; b=vkuTAXrP6u6T0V8QldDRsScLFXQcqOpsgjOxNx9rhYtX12K8761OUvHxPq+KIBWguh bNyaoaNzS4ddFaE1gUJDfUhAOmnw+pULxwnB9lKf2kfk1k+erIWJUs6pGl4oRPH6My/i cbJIQdxsURCUOyJeCs6h64UeHl0lsSeaPX96rlvBLGoFr8YzsusgS4neg/YlvueOFtmH ClNDM9pJ3m9AZryT3RAyAtrvHKrWeQYER2gdbG/lvJU3U7d79laeKGJ74L/FBAEWPyzZ /aoF8V5GhEqxZekuoLhu+e5NSaBvn6WVwBABYFi/J+GXACR+v2Q55A5rxP6gxnQ3e5XR c/gQ== 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:mime-version :content-transfer-encoding; bh=ykOljB2vbGcyFYJUaA+gCQdraZEMWCskvExfV6LRvOY=; b=HC0T/bg4qkUGWb7hf0JGw3oHOXodUALSiMv9Wmfc1zD+uhVNy53mEFSwPeENN/KP/q C/G/oBUYu33EjT8M4XKRfRlXRg6N03ebJj0vyWOzHLQz4qT7zhfZuUqHGf2IxkluZz31 8EmOzrfYlA+wADRI60C6UxEXUmp6Z8fQHZ8TS0CY6ZyjDYWDRxmM27eTkKMPN7bQCCmA VNYUqOY5Tl0xwt1jvReb+7pU8vgOADbdLNN7lisd/yWqGjayMZp0qGFeoQ0mW8381LDf nlqaGxnk6cP8MMFyZ/MdzUbHz61T+8cJiaubNdH0zyIXPzx5611CfrOplyS3TlRJ+O4N tN7w== X-Gm-Message-State: AHQUAubiGA/JmlUre9XntMPa+S/rO3gfWRIrdTFyjulBrl5BwVoxrZjj ceWlQSw/Pt4ZD6ycgvN6s96gBhdPV0M= X-Google-Smtp-Source: AHgI3Ibd8W7LZhrKyXXcTTGS0Z5arQObJfUSTlcKB7yyBYfibj6Qxve1hVEAeWSQV8h1C910IdWKcQ== X-Received: by 2002:a1c:dc8a:: with SMTP id t132mr2090726wmg.48.1551266032303; Wed, 27 Feb 2019 03:13:52 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: armbru@redhat.com Date: Wed, 27 Feb 2019 11:13:47 +0000 Message-Id: <20190227111347.15063-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::32e Subject: [Qemu-devel] [PATCH v5] hw/block: better reporting on pflash backing file mismatch 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: Laszlo Ersek , =?UTF-8?q?Alex=20Benn=C3=A9e?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) It looks like there was going to be code to check we had some sort of alignment so lets replace it with an actual check. This is a bit more useful than the enigmatic "failed to read the initial flash content" when we attempt to read the number of bytes the device should have. This is a potential confusing stumbling block when you move from using -bios to using -drive if=3Dpflash,file=3Dblob,format=3Draw,readonly for loading your firmware code. To mitigate that we automatically pad in the read-only case and warn the user when we have performed magic to enable things to Just Work (tm). Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Laszlo Ersek --- v3 - tweak commit title/commentary - use total_len instead of device_len for checks - if the device is read-only do the padding for them - accept baking_len > total_len (how to warn_report with NULL *errp?) v4 - error check blk_getlength - optimise memset and use NOR erase pattern - restore singular device (overly confusing) - add warn_report for when we do magic v5 - remove mention of null padding - use %zu for size_t fmt string - add Laszlo r-b --- hw/block/pflash_cfi01.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 9d1c356eb6..d8cfa4789a 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -45,6 +45,7 @@ #include "qemu/bitops.h" #include "qemu/host-utils.h" #include "qemu/log.h" +#include "qemu/error-report.h" #include "hw/sysbus.h" #include "sysemu/sysemu.h" #include "trace.h" @@ -730,13 +731,6 @@ static void pflash_cfi01_realize(DeviceState *dev, Err= or **errp) } device_len =3D sector_len_per_device * blocks_per_device; =20 - /* XXX: to be fixed */ -#if 0 - if (total_len !=3D (8 * 1024 * 1024) && total_len !=3D (16 * 1024 * 10= 24) && - total_len !=3D (32 * 1024 * 1024) && total_len !=3D (64 * 1024 * 1= 024)) - return NULL; -#endif - memory_region_init_rom_device( &pfl->mem, OBJECT(dev), &pflash_cfi01_ops, @@ -763,6 +757,38 @@ static void pflash_cfi01_realize(DeviceState *dev, Err= or **errp) } =20 if (pfl->blk) { + /* + * Validate the backing store is the right size for pflash + * devices. It should be padded to a multiple of the flash + * block size. If the device is read-only we can elide the + * check and just pad the region first. If the user supplies a + * larger file we ignore the tail. + */ + int64_t backing_len =3D blk_getlength(pfl->blk); + if (backing_len < 0) { + error_setg(errp, "unable to check size of backing file"); + return; + } + + if (backing_len < total_len) { + if (pfl->ro) { + size_t pad_bytes =3D total_len - backing_len; + /* pad with NOR erase pattern */ + memset((uint8_t*)pfl->storage + backing_len, 0xff, pad_byt= es); + warn_report("device needs %" PRIu64 + " bytes, padded with %zu 0xff bytes", + total_len, pad_bytes); + total_len =3D backing_len; + } else { + error_setg(errp, "device needs %" PRIu64 " bytes, " + "backing file provides only %" PRIu64 " bytes", + total_len, backing_len); + return; + } + } else if (backing_len > total_len) { + warn_report("device needs %" PRIu64 " bytes, rest ignored", to= tal_len); + } + /* read the initial flash content */ ret =3D blk_pread(pfl->blk, 0, pfl->storage, total_len); =20 --=20 2.20.1