From patchwork Thu Feb 18 15:00:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leif Lindholm X-Patchwork-Id: 62213 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp635727lbl; Thu, 18 Feb 2016 07:01:13 -0800 (PST) X-Received: by 10.140.109.100 with SMTP id k91mr9549245qgf.54.1455807673706; Thu, 18 Feb 2016 07:01:13 -0800 (PST) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id q3si6207281qkh.123.2016.02.18.07.01.13 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 18 Feb 2016 07:01:13 -0800 (PST) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: from localhost ([::1]:42261 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWQ4v-0000Xd-Bt for patch@linaro.org; Thu, 18 Feb 2016 10:01:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWQ4r-0000X0-AO for grub-devel@gnu.org; Thu, 18 Feb 2016 10:01:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWQ4m-0006QZ-HD for grub-devel@gnu.org; Thu, 18 Feb 2016 10:01:09 -0500 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]:34996) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWQ4m-0006QN-3H for grub-devel@gnu.org; Thu, 18 Feb 2016 10:01:04 -0500 Received: by mail-wm0-x22b.google.com with SMTP id c200so31833779wme.0 for ; Thu, 18 Feb 2016 07:01:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id; bh=yhntbOi67Phd03A8u6PnOBckXUFej1utaKpWJaLmSro=; b=HKDOeRleiCA6CT5rHmkwS2WkRMSC76QzKBfFTAqyGNMi9+KaqXhUOgCwcv4kow/b1J Y2TxuOxtBstmO+WHRN42QozoR3h37cw1x3cKtr3HenJG1ZNjUAC5crjrBuUzl7wzgXFO kDfwUHzOwLFvRqKyqgm1SKuRQUZ5TgO7UyynQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=yhntbOi67Phd03A8u6PnOBckXUFej1utaKpWJaLmSro=; b=m8XCvqi/uKmdQEUoI/Y2UNzSvBT1jmIsm2MOPbvIczGxY9ikyCZRcYP6VN/oE6vr+k asB00mmmFwkqXjKFwZ/GYALK47HVwnPxHXDdXGoRZ9Y0o8bKzys89fqpazWfhEvLqdjm +Oe/5HgNCin9UuCO/rOMqJJ3xEt+VloMFSX5Ra7wwz4prTC1OSlhbiZETxHJBKsh3e5h gxr++EeCCR3df/e0pMq5u2fqUY7N4481OqvF6mFFTL7UMy0gN5MClrIfqtqmT0PL9KxV Pf9QL3dmRu5N1Z/icpSJkoihnZE+1glBAp/0O4M35MsdZ0k6rnDUF0XdFEdF12w55RZn 8Kew== X-Gm-Message-State: AG10YOR7hZymo7RX7MSN0Pcqo9Go/sDnPZSXhCQnrYCxb8488CmrEv2JhGeLr6fpGqj8OZ1U X-Received: by 10.28.49.69 with SMTP id x66mr3652025wmx.11.1455807663359; Thu, 18 Feb 2016 07:01:03 -0800 (PST) Received: from mohikan.mushroom.smurfnet.nu (cpc92308-cmbg19-2-0-cust814.5-4.cable.virginm.net. [82.24.251.47]) by smtp.gmail.com with ESMTPSA id w144sm3369194wmd.8.2016.02.18.07.01.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Feb 2016 07:01:02 -0800 (PST) From: Leif Lindholm To: grub-devel@gnu.org Subject: [PATCH] efidisk: Respect block_io_protocol buffer alignment Date: Thu, 18 Feb 2016 15:00:59 +0000 Message-Id: <1455807659-21474-1-git-send-email-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.1.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22b X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: grub-devel-bounces+patch=linaro.org@gnu.org Returned from the OpenProtocol operation, the grub_efi_block_io_media structure contains the io_align field, specifying the minimum alignment required for buffers used in any data transfers with the device. Make grub_efidisk_readwrite() allocate a temporary buffer, aligned to this boundary, if the buffer passed to it does not already meet the requirements. Also sanity check the io_align field in grub_efidisk_open() for power-of-two-ness and bail if invalid. Reported-by: Jeremy Linton --- v3 - Fixed up types and added a check on the io_align field, as per Andrei's comments. grub-core/disk/efi/efidisk.c | 53 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) -- 2.1.4 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c index 1c00e3e..c1afbe4 100644 --- a/grub-core/disk/efi/efidisk.c +++ b/grub-core/disk/efi/efidisk.c @@ -463,6 +463,7 @@ grub_efidisk_open (const char *name, struct grub_disk *disk) int num; struct grub_efidisk_data *d = 0; grub_efi_block_io_media_t *m; + unsigned long i = 0; grub_dprintf ("efidisk", "opening %s\n", name); @@ -491,10 +492,21 @@ grub_efidisk_open (const char *name, struct grub_disk *disk) disk->id = ((num << GRUB_CHAR_BIT) | name[0]); m = d->block_io->media; + /* Ensure required buffer alignment is a power of two (or is zero). */ + do + { + if ((m->io_align & (1UL << i)) && (m->io_align & ~(1UL << i))) + return grub_error (GRUB_ERR_IO, "invalid buffer alignment %d", + m->io_align); + } + while (++i < (sizeof (grub_addr_t) * 8)); + /* FIXME: Probably it is better to store the block size in the disk, and total sectors should be replaced with total blocks. */ - grub_dprintf ("efidisk", "m = %p, last block = %llx, block size = %x\n", - m, (unsigned long long) m->last_block, m->block_size); + grub_dprintf ("efidisk", + "m = %p, last block = %llx, block size = %x, io align = %x\n", + m, (unsigned long long) m->last_block, m->block_size, + m->io_align); disk->total_sectors = m->last_block + 1; /* Don't increase this value due to bug in some EFI. */ disk->max_agglomerate = 0xa0000 >> (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS); @@ -524,15 +536,42 @@ grub_efidisk_readwrite (struct grub_disk *disk, grub_disk_addr_t sector, { struct grub_efidisk_data *d; grub_efi_block_io_t *bio; + grub_efi_status_t status; + grub_size_t io_align, num_bytes; + char *aligned_buf; d = disk->data; bio = d->block_io; - return efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio, - bio->media->media_id, - (grub_efi_uint64_t) sector, - (grub_efi_uintn_t) size << disk->log_sector_size, - buf); + /* Set alignment to 1 if 0 specified */ + io_align = bio->media->io_align ? bio->media->io_align : 1; + num_bytes = size << disk->log_sector_size; + + if ((grub_addr_t) buf & (io_align - 1)) + { + aligned_buf = grub_memalign (io_align, num_bytes); + if (! aligned_buf) + return GRUB_EFI_OUT_OF_RESOURCES; + if (wr) + grub_memcpy (aligned_buf, buf, num_bytes); + } + else + { + aligned_buf = buf; + } + + status = efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio, + bio->media->media_id, (grub_efi_uint64_t) sector, + (grub_efi_uintn_t) num_bytes, aligned_buf); + + if ((grub_addr_t) buf & (io_align - 1)) + { + if (!wr) + grub_memcpy (buf, aligned_buf, num_bytes); + grub_free (aligned_buf); + } + + return status; } static grub_err_t