From owner-svn-src-stable@freebsd.org Thu Nov 29 13:37:45 2018 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 70B3E113C1BC; Thu, 29 Nov 2018 13:37:45 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 179D36B913; Thu, 29 Nov 2018 13:37:45 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id ECEDD1A364; Thu, 29 Nov 2018 13:37:44 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id wATDbimM036654; Thu, 29 Nov 2018 13:37:44 GMT (envelope-from tsoome@FreeBSD.org) Received: (from tsoome@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id wATDbi0k036650; Thu, 29 Nov 2018 13:37:44 GMT (envelope-from tsoome@FreeBSD.org) Message-Id: <201811291337.wATDbi0k036650@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tsoome set sender to tsoome@FreeBSD.org using -f From: Toomas Soome Date: Thu, 29 Nov 2018 13:37:44 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r341226 - in stable/12/stand: common i386/libi386 X-SVN-Group: stable-12 X-SVN-Commit-Author: tsoome X-SVN-Commit-Paths: in stable/12/stand: common i386/libi386 X-SVN-Commit-Revision: 341226 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 179D36B913 X-Spamd-Result: default: False [0.37 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_SHORT(-0.17)[-0.171,0]; NEURAL_SPAM_LONG(0.29)[0.292,0]; NEURAL_SPAM_MEDIUM(0.25)[0.254,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-Rspamd-Server: mx1.freebsd.org X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Nov 2018 13:37:45 -0000 Author: tsoome Date: Thu Nov 29 13:37:44 2018 New Revision: 341226 URL: https://svnweb.freebsd.org/changeset/base/341226 Log: MFC r340215: oader: always set media size from partition. The disk access is validated by using partition table definitions, therefore we have no need for if statements, just set the disk size. Of course the partition table itself may be incorrect/inconsistent, but if so, we are in trouble anyhow. Differential Revision: https://reviews.freebsd.org/D17822 Modified: stable/12/stand/common/disk.c stable/12/stand/common/part.c stable/12/stand/i386/libi386/biosdisk.c Directory Properties: stable/12/ (props changed) Modified: stable/12/stand/common/disk.c ============================================================================== --- stable/12/stand/common/disk.c Thu Nov 29 13:01:21 2018 (r341225) +++ stable/12/stand/common/disk.c Thu Nov 29 13:37:44 2018 (r341226) @@ -265,9 +265,7 @@ disk_open(struct disk_devdesc *dev, uint64_t mediasize rc = ENXIO; goto out; } - if (mediasize > od->mediasize) { - od->mediasize = mediasize; - } + od->mediasize = mediasize; if (ptable_gettype(od->table) == PTABLE_BSD && partition >= 0) { Modified: stable/12/stand/common/part.c ============================================================================== --- stable/12/stand/common/part.c Thu Nov 29 13:01:21 2018 (r341225) +++ stable/12/stand/common/part.c Thu Nov 29 13:37:44 2018 (r341226) @@ -323,8 +323,7 @@ ptable_gptread(struct ptable *table, void *dev, diskre * Note, this is still not a foolproof way to get disk's size. For * example, an image file can be truncated when copied to smaller media. */ - if (hdr.hdr_lba_alt + 1 > table->sectors) - table->sectors = hdr.hdr_lba_alt + 1; + table->sectors = hdr.hdr_lba_alt + 1; for (i = 0; i < size / hdr.hdr_entsz; i++) { ent = (struct gpt_ent *)(tbl + i * hdr.hdr_entsz); Modified: stable/12/stand/i386/libi386/biosdisk.c ============================================================================== --- stable/12/stand/i386/libi386/biosdisk.c Thu Nov 29 13:01:21 2018 (r341225) +++ stable/12/stand/i386/libi386/biosdisk.c Thu Nov 29 13:37:44 2018 (r341226) @@ -325,6 +325,33 @@ bd_print(int verbose) } /* + * Read disk size from partition. + * This is needed to work around buggy BIOS systems returning + * wrong (truncated) disk media size. + * During bd_probe() we tested if the multiplication of bd_sectors + * would overflow so it should be safe to perform here. + */ +static uint64_t +bd_disk_get_sectors(struct disk_devdesc *dev) +{ + struct disk_devdesc disk; + uint64_t size; + + disk.dd.d_dev = dev->dd.d_dev; + disk.dd.d_unit = dev->dd.d_unit; + disk.d_slice = -1; + disk.d_partition = -1; + disk.d_offset = 0; + + size = BD(dev).bd_sectors * BD(dev).bd_sectorsize; + if (disk_open(&disk, size, BD(dev).bd_sectorsize) == 0) { + (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size); + disk_close(&disk); + } + return (size / BD(dev).bd_sectorsize); +} + +/* * Attempt to open the disk described by (dev) for use by (f). * * Note that the philosophy here is "give them exactly what @@ -338,9 +365,7 @@ static int bd_open(struct open_file *f, ...) { struct disk_devdesc *dev; - struct disk_devdesc disk; va_list ap; - uint64_t size; int rc; va_start(ap, f); @@ -349,33 +374,12 @@ bd_open(struct open_file *f, ...) if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo) return (EIO); - BD(dev).bd_open++; if (BD(dev).bd_bcache == NULL) BD(dev).bd_bcache = bcache_allocate(); - /* - * Read disk size from partition. - * This is needed to work around buggy BIOS systems returning - * wrong (truncated) disk media size. - * During bd_probe() we tested if the mulitplication of bd_sectors - * would overflow so it should be safe to perform here. - */ - disk.dd.d_dev = dev->dd.d_dev; - disk.dd.d_unit = dev->dd.d_unit; - disk.d_slice = -1; - disk.d_partition = -1; - disk.d_offset = 0; - - if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize, - BD(dev).bd_sectorsize) == 0) { - - if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) { - size /= BD(dev).bd_sectorsize; - if (size > BD(dev).bd_sectors) - BD(dev).bd_sectors = size; - } - disk_close(&disk); - } + if (BD(dev).bd_open == 0) + BD(dev).bd_sectors = bd_disk_get_sectors(dev); + BD(dev).bd_open++; rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize, BD(dev).bd_sectorsize);