From owner-svn-src-all@FreeBSD.ORG Fri Sep 19 23:16:05 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 04ECE629; Fri, 19 Sep 2014 23:16:05 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E306C94A; Fri, 19 Sep 2014 23:16:04 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id s8JNG4Bw002527; Fri, 19 Sep 2014 23:16:04 GMT (envelope-from marcel@FreeBSD.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id s8JNG2oe002514; Fri, 19 Sep 2014 23:16:02 GMT (envelope-from marcel@FreeBSD.org) Message-Id: <201409192316.s8JNG2oe002514@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: marcel set sender to marcel@FreeBSD.org using -f From: Marcel Moolenaar Date: Fri, 19 Sep 2014 23:16:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r271881 - head/usr.bin/mkimg X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Sep 2014 23:16:05 -0000 Author: marcel Date: Fri Sep 19 23:16:02 2014 New Revision: 271881 URL: http://svnweb.freebsd.org/changeset/base/271881 Log: Fix partition alignment and image rounding when any of -P (block size), -T (track size) or -H (number of heads) is given: o scheme_metadata() always rounded to the block size. This is not always valid (e.g. vtoc8 that must have partitions start at cylinder boundaries). o The bsd and vtoc8 schemes "resized" the image to make it match the geometry, but since the geometry is an approximation and the size of the image computed from cylinders * heads * sectors is always smaller than the original image size, the partition information ran out of bounds. The fix is to have scheme_metadata() simply pass it's arguments to the per-scheme metadata callback, so that schemes not only know where the metadata is to go, but also what the current block address is. It's now up to the per-scheme callback to reserve room for metadata and to make sure alignment and rounding is applied. The BSD scheme now has the most elaborate alignment and rounding. Just to make the point: partitions are aligned on block boundaries, but the image is rounded to the next cyclinder boundary. vtoc8 now properly has all partitions aligned (and rounded) to the cyclinder boundary. Obtained from: Juniper Networks, Inc. MFC after: 3 days Modified: head/usr.bin/mkimg/apm.c head/usr.bin/mkimg/bsd.c head/usr.bin/mkimg/ebr.c head/usr.bin/mkimg/gpt.c head/usr.bin/mkimg/mbr.c head/usr.bin/mkimg/mkimg.h head/usr.bin/mkimg/pc98.c head/usr.bin/mkimg/scheme.c head/usr.bin/mkimg/scheme.h head/usr.bin/mkimg/vtoc8.c Modified: head/usr.bin/mkimg/apm.c ============================================================================== --- head/usr.bin/mkimg/apm.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/apm.c Fri Sep 19 23:16:02 2014 (r271881) @@ -57,13 +57,12 @@ static struct mkimg_alias apm_aliases[] { ALIAS_NONE, 0 } }; -static u_int -apm_metadata(u_int where) +static lba_t +apm_metadata(u_int where, lba_t blk) { - u_int secs; - secs = (where == SCHEME_META_IMG_START) ? nparts + 2 : 0; - return (secs); + blk += (where == SCHEME_META_IMG_START) ? nparts + 2 : 0; + return (round_block(blk)); } static int Modified: head/usr.bin/mkimg/bsd.c ============================================================================== --- head/usr.bin/mkimg/bsd.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/bsd.c Fri Sep 19 23:16:02 2014 (r271881) @@ -52,13 +52,17 @@ static struct mkimg_alias bsd_aliases[] { ALIAS_NONE, 0 } }; -static u_int -bsd_metadata(u_int where) +static lba_t +bsd_metadata(u_int where, lba_t blk) { - u_int secs; - secs = BBSIZE / secsz; - return ((where == SCHEME_META_IMG_START) ? secs : 0); + if (where == SCHEME_META_IMG_START) + blk += BBSIZE / secsz; + else if (where == SCHEME_META_IMG_END) + blk = round_cylinder(blk); + else + blk = round_block(blk); + return (blk); } static int @@ -83,12 +87,6 @@ bsd_write(lba_t imgsz, void *bootcode) bsdparts = nparts + 1; /* Account for c partition */ if (bsdparts < MAXPARTITIONS) bsdparts = MAXPARTITIONS; - imgsz = (lba_t)ncyls * nheads * nsecs; - error = image_set_size(imgsz); - if (error) { - free(buf); - return (error); - } d = (void *)(buf + secsz); le32enc(&d->d_magic, DISKMAGIC); Modified: head/usr.bin/mkimg/ebr.c ============================================================================== --- head/usr.bin/mkimg/ebr.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/ebr.c Fri Sep 19 23:16:02 2014 (r271881) @@ -49,13 +49,12 @@ static struct mkimg_alias ebr_aliases[] { ALIAS_NONE, 0 } }; -static u_int -ebr_metadata(u_int where) +static lba_t +ebr_metadata(u_int where, lba_t blk) { - u_int secs; - secs = (where == SCHEME_META_PART_BEFORE) ? nsecs : 0; - return (secs); + blk += (where == SCHEME_META_PART_BEFORE) ? 1 : 0; + return (round_track(blk)); } static void Modified: head/usr.bin/mkimg/gpt.c ============================================================================== --- head/usr.bin/mkimg/gpt.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/gpt.c Fri Sep 19 23:16:02 2014 (r271881) @@ -153,17 +153,15 @@ gpt_tblsz(void) return ((nparts + ents - 1) / ents); } -static u_int -gpt_metadata(u_int where) +static lba_t +gpt_metadata(u_int where, lba_t blk) { - u_int secs; - if (where != SCHEME_META_IMG_START && where != SCHEME_META_IMG_END) - return (0); - - secs = gpt_tblsz(); - secs += (where == SCHEME_META_IMG_START) ? 2 : 1; - return (secs); + if (where == SCHEME_META_IMG_START || where == SCHEME_META_IMG_END) { + blk += gpt_tblsz(); + blk += (where == SCHEME_META_IMG_START) ? 2 : 1; + } + return (round_block(blk)); } static int Modified: head/usr.bin/mkimg/mbr.c ============================================================================== --- head/usr.bin/mkimg/mbr.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/mbr.c Fri Sep 19 23:16:02 2014 (r271881) @@ -50,13 +50,12 @@ static struct mkimg_alias mbr_aliases[] { ALIAS_NONE, 0 } /* Keep last! */ }; -static u_int -mbr_metadata(u_int where) +static lba_t +mbr_metadata(u_int where, lba_t blk) { - u_int secs; - secs = (where == SCHEME_META_IMG_START) ? nsecs : 0; - return (secs); + blk += (where == SCHEME_META_IMG_START) ? 1 : 0; + return (round_track(blk)); } static void Modified: head/usr.bin/mkimg/mkimg.h ============================================================================== --- head/usr.bin/mkimg/mkimg.h Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/mkimg.h Fri Sep 19 23:16:02 2014 (r271881) @@ -66,6 +66,21 @@ round_block(lba_t n) return ((n + b - 1) & ~(b - 1)); } +static inline lba_t +round_cylinder(lba_t n) +{ + u_int cyl = nsecs * nheads; + u_int r = n % cyl; + return ((r == 0) ? n : n + cyl - r); +} + +static inline lba_t +round_track(lba_t n) +{ + u_int r = n % nsecs; + return ((r == 0) ? n : n + nsecs - r); +} + #if !defined(SPARSE_WRITE) #define sparse_write write #else Modified: head/usr.bin/mkimg/pc98.c ============================================================================== --- head/usr.bin/mkimg/pc98.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/pc98.c Fri Sep 19 23:16:02 2014 (r271881) @@ -59,13 +59,12 @@ static struct mkimg_alias pc98_aliases[] { ALIAS_NONE, 0 } }; -static u_int -pc98_metadata(u_int where) +static lba_t +pc98_metadata(u_int where, lba_t blk) { - u_int secs; - - secs = PC98_BOOTCODESZ / secsz; - return ((where == SCHEME_META_IMG_START) ? secs : 0); + if (where == SCHEME_META_IMG_START) + blk += PC98_BOOTCODESZ / secsz; + return (round_track(blk)); } static void Modified: head/usr.bin/mkimg/scheme.c ============================================================================== --- head/usr.bin/mkimg/scheme.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/scheme.c Fri Sep 19 23:16:02 2014 (r271881) @@ -171,10 +171,8 @@ scheme_max_secsz(void) lba_t scheme_metadata(u_int where, lba_t start) { - lba_t secs; - secs = scheme->metadata(where); - return (round_block(start + secs)); + return (scheme->metadata(where, start)); } int Modified: head/usr.bin/mkimg/scheme.h ============================================================================== --- head/usr.bin/mkimg/scheme.h Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/scheme.h Fri Sep 19 23:16:02 2014 (r271881) @@ -62,7 +62,7 @@ struct mkimg_scheme { const char *name; const char *description; struct mkimg_alias *aliases; - u_int (*metadata)(u_int); + lba_t (*metadata)(u_int, lba_t); #define SCHEME_META_IMG_START 1 #define SCHEME_META_IMG_END 2 #define SCHEME_META_PART_BEFORE 3 Modified: head/usr.bin/mkimg/vtoc8.c ============================================================================== --- head/usr.bin/mkimg/vtoc8.c Fri Sep 19 21:30:45 2014 (r271880) +++ head/usr.bin/mkimg/vtoc8.c Fri Sep 19 23:16:02 2014 (r271881) @@ -53,13 +53,12 @@ static struct mkimg_alias vtoc8_aliases[ { ALIAS_NONE, 0 } }; -static u_int -vtoc8_metadata(u_int where) +static lba_t +vtoc8_metadata(u_int where, lba_t blk) { - u_int secs; - secs = (where == SCHEME_META_IMG_START) ? nsecs * nheads : 0; - return (secs); + blk += (where == SCHEME_META_IMG_START) ? 1 : 0; + return (round_cylinder(blk)); } static int @@ -87,10 +86,6 @@ vtoc8_write(lba_t imgsz, void *bootcode be16enc(&vtoc8.nsecs, nsecs); be16enc(&vtoc8.magic, VTOC_MAGIC); - error = image_set_size(imgsz); - if (error) - return (error); - be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz); STAILQ_FOREACH(part, &partlist, link) { n = part->index + ((part->index >= VTOC_RAW_PART) ? 1 : 0);