From owner-svn-src-user@FreeBSD.ORG Sat Jun 28 04:30:41 2014 Return-Path: Delivered-To: svn-src-user@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 2E50E455; Sat, 28 Jun 2014 04:30:41 +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 01DEF2120; Sat, 28 Jun 2014 04:30:40 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s5S4Uemh016687; Sat, 28 Jun 2014 04:30:40 GMT (envelope-from marcel@svn.freebsd.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s5S4UekT016686; Sat, 28 Jun 2014 04:30:40 GMT (envelope-from marcel@svn.freebsd.org) Message-Id: <201406280430.s5S4UekT016686@svn.freebsd.org> From: Marcel Moolenaar Date: Sat, 28 Jun 2014 04:30:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r267998 - user/marcel/mkimg X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Jun 2014 04:30:41 -0000 Author: marcel Date: Sat Jun 28 04:30:40 2014 New Revision: 267998 URL: http://svnweb.freebsd.org/changeset/base/267998 Log: Add vhd_geometry() to fill in the geometry in the footer. We respect the command line arguments if possible, but otherwise implement the algorithm from the specification. Modified: user/marcel/mkimg/vhd.c Modified: user/marcel/mkimg/vhd.c ============================================================================== --- user/marcel/mkimg/vhd.c Sat Jun 28 04:27:00 2014 (r267997) +++ user/marcel/mkimg/vhd.c Sat Jun 28 04:30:40 2014 (r267998) @@ -164,6 +164,49 @@ vhd_uuid_enc(void *buf, const uuid_t *uu p[10 + i] = uuid->node[i]; } +static void +vhd_geometry(struct vhd_footer *footer) +{ + lba_t imgsz; + long cth; + + /* Respect command line options if possible. */ + if (nheads > 1 && nheads < 256 && + nsecs > 1 && nsecs < 256 && + ncyls < 65536) { + be16enc(&footer->cylinders, ncyls); + footer->heads = nheads; + footer->sectors = nsecs; + return; + } + + imgsz = (image_get_size() * secsz) / VHD_SECTOR_SIZE; + if (imgsz > 65536 * 16 * 255) + imgsz = 65536 * 16 * 255; + if (imgsz >= 65535 * 16 * 63) { + be16enc(&footer->cylinders, imgsz / (16 * 255)); + footer->heads = 16; + footer->sectors = 255; + return; + } + footer->sectors = 17; + cth = imgsz / 17; + footer->heads = (cth + 1023) / 1024; + if (footer->heads < 4) + footer->heads = 4; + if (cth >= (footer->heads * 1024) || footer->heads > 16) { + footer->heads = 16; + footer->sectors = 31; + cth = imgsz / 31; + } + if (cth >= (footer->heads * 1024)) { + footer->heads = 16; + footer->sectors = 63; + cth = imgsz / 63; + } + be16enc(&footer->cylinders, cth / footer->heads); +} + static int vhd_write(int fd) { @@ -191,7 +234,7 @@ vhd_write(int fd) be32enc(&footer.creator_os, VHD_CREATOR_OS); be64enc(&footer.original_size, imgsz); be64enc(&footer.current_size, imgsz); - /* XXX Geometry */ + vhd_geometry(&footer); be32enc(&footer.disk_type, VHD_DISK_TYPE_DYNAMIC); mkimg_uuid(&id); vhd_uuid_enc(&footer.id, &id);