From patchwork Fri Mar 8 06:24:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 159960 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp8337152jad; Thu, 7 Mar 2019 22:26:27 -0800 (PST) X-Google-Smtp-Source: APXvYqwrqc567DywE632yb4Pt7a4q1Tiln6aU7JDI1hXSjGriCyEmDLic4aBxSf4kFQAXmpPpmK8 X-Received: by 2002:a25:282:: with SMTP id 124mr10369388ybc.5.1552026387308; Thu, 07 Mar 2019 22:26:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552026387; cv=none; d=google.com; s=arc-20160816; b=Mx+KR6NdKhUjdR5FkcWKlVIy2ddWMzGTILq48Kr/YWi4gup+tO0S3Abb0FRfRq3jlg d0gKqpXbIWmN6HGTNyO6FifviZHxFupEfV8oAaNww6Op2/6eSeSBaP87coUPb2ZkKzl+ 3Wl2e/XucPgqpZLTlQaUIW/n2aEsLrIC9LF0n4YxoQxn6mL6YCNki0lRkFV6CwRlnoDY +9zmWUV7CKFsVkg8ZE8DxnKkSdiEBpyERj9VBZABvd8gK50u6BveO6iQI2gDH/NNgNHx nMGgJWEBCW2V4mVmagJP3blZj4U1qyciY7kQTSq5qHm+H4t9zt12O+6zI502qRshSFV+ DIAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:message-id:date:to:from; bh=VfqxmYXemTvkHW9YKXRUvqG8y5D2YZM8P/ASjmYR0OY=; b=xG79ndHXOOuaenYAHCbGM+hAUARJwJK9O5C4HOY3Y2NbSoXIdnRDCjZeHfubOFmdWI ObY9pFXzBWxujPVn1RpmclG+LYfusMY6IqhVDBLxbTqkYuBi+fR7Ik331k9kt4zeD9lX oEZefUG9u4BAF/8zOFe0+k88Iqj4DKDHNtTQtxSjaVhB7+tlY2xj/IcnF6WBSmeLifB0 /hE4iDfEE93U6rNsOTjQPCBD7yaM7fLW+x/XpkTQj1Rx4Aqf/wI6tHfo5CkClhqbS74x XMkhBOhG3tnHXs6vCvw2nP1ol6lHEst2tU/HDgApG1cGj0gzusN1eCg4jJt8NPpiC9/8 Oorw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d134si4166659ywb.430.2019.03.07.22.26.27 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 07 Mar 2019 22:26:27 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([127.0.0.1]:37875 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h28xe-0000ZE-Se for patch@linaro.org; Fri, 08 Mar 2019 01:26:26 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60093) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h28wN-00007l-3g for qemu-devel@nongnu.org; Fri, 08 Mar 2019 01:25:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h28wM-00038R-57 for qemu-devel@nongnu.org; Fri, 08 Mar 2019 01:25:07 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49364) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h28wJ-00033C-LP; Fri, 08 Mar 2019 01:25:03 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 521BE81E15; Fri, 8 Mar 2019 06:25:02 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3632B600C8; Fri, 8 Mar 2019 06:24:57 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 98CA31138660; Fri, 8 Mar 2019 07:24:55 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Fri, 8 Mar 2019 07:24:55 +0100 Message-Id: <20190308062455.29755-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 08 Mar 2019 06:25:02 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v7] pflash: Require backend size to match device, improve errors 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: kwolf@redhat.com, qemu-block@nongnu.org, alex.bennee@linaro.org, philmd@redhat.com, mreitz@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Alex Bennée We reject undersized backends with a rather enigmatic "failed to read the initial flash content" error. We happily accept oversized images, ignoring their tail. Throwing away parts of firmware that way is pretty much certain to end in an even more enigmatic failure to boot. Require the backend's size to match the device's size exactly. Report mismatch as "device requires N bytes, block backend 'NAME' provides M bytes". Improve the error for actual read failures to "can't read initial flash content from block backend 'NAME'. We disabled code to limit device sizes to 8, 16, 32 or 64MiB more than a decade ago in commit 95d1f3edd5e and c8b153d7949, v0.9.1. Bury. Signed-off-by: Alex Bennée Signed-off-by: Markus Armbruster --- hw/block/pflash_cfi01.c | 31 ++++++++++++++++++++++--------- hw/block/pflash_cfi02.c | 31 +++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 17 deletions(-) -- 2.17.2 diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 9d1c356eb6..2e3a05c0b3 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -730,13 +730,6 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) } device_len = sector_len_per_device * blocks_per_device; - /* XXX: to be fixed */ -#if 0 - if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) && - total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024)) - return NULL; -#endif - memory_region_init_rom_device( &pfl->mem, OBJECT(dev), &pflash_cfi01_ops, @@ -763,12 +756,32 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) } if (pfl->blk) { + /* + * Validate the backing store is the right size for pflash + * devices. If the user supplies a larger file we ignore the + * tail. + */ + int64_t backing_len = blk_getlength(pfl->blk); + + if (backing_len < 0) { + error_setg(errp, "can't get size of block backend '%s'", + blk_name(pfl->blk)); + return; + } + if (backing_len != total_len) { + error_setg(errp, "device requires %" PRIu64 " bytes, " + "block backend '%s' provides %" PRIu64 " bytes", + total_len, blk_name(pfl->blk), backing_len); + return; + } + /* read the initial flash content */ ret = blk_pread(pfl->blk, 0, pfl->storage, total_len); - if (ret < 0) { vmstate_unregister_ram(&pfl->mem, DEVICE(pfl)); - error_setg(errp, "failed to read the initial flash content"); + error_setg(errp, "can't read initial flash content" + " from block backend '%s'", + blk_name(pfl->blk)); return; } } diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c index 33779ce807..45b88d65d8 100644 --- a/hw/block/pflash_cfi02.c +++ b/hw/block/pflash_cfi02.c @@ -550,12 +550,6 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) } chip_len = pfl->sector_len * pfl->nb_blocs; - /* XXX: to be fixed */ -#if 0 - if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) && - total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024)) - return NULL; -#endif memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), pfl->be ? &pflash_cfi02_ops_be : &pflash_cfi02_ops_le, @@ -581,11 +575,32 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) } if (pfl->blk) { + /* + * Validate the backing store is the right size for pflash + * devices. If the user supplies a larger file we ignore the + * tail. + */ + int64_t backing_len = blk_getlength(pfl->blk); + + if (backing_len < 0) { + error_setg(errp, "can't get size of block backend '%s'", + blk_name(pfl->blk)); + return; + } + if (backing_len != chip_len) { + error_setg(errp, "device requires %" PRIu32 " bytes, " + "block backend '%s' provides %" PRIu64 " bytes", + chip_len, blk_name(pfl->blk), backing_len); + return; + } + /* read the initial flash content */ ret = blk_pread(pfl->blk, 0, pfl->storage, chip_len); if (ret < 0) { - vmstate_unregister_ram(&pfl->orig_mem, DEVICE(pfl)); - error_setg(errp, "failed to read the initial flash content"); + vmstate_unregister_ram(&pfl->mem, DEVICE(pfl)); + error_setg(errp, "can't read initial flash content" + " from block backend '%s'", + blk_name(pfl->blk)); return; } }