vtoc8.c revision 302408
1120441Sbms/*- 2120441Sbms * Copyright (c) 2014 Juniper Networks, Inc. 3120441Sbms * All rights reserved. 4120441Sbms * 5120441Sbms * Redistribution and use in source and binary forms, with or without 6120441Sbms * modification, are permitted provided that the following conditions 7120441Sbms * are met: 8120441Sbms * 1. Redistributions of source code must retain the above copyright 9120441Sbms * notice, this list of conditions and the following disclaimer. 10120441Sbms * 2. Redistributions in binary form must reproduce the above copyright 11120441Sbms * notice, this list of conditions and the following disclaimer in the 12120441Sbms * documentation and/or other materials provided with the distribution. 13120441Sbms * 14120441Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15120441Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16120441Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17120441Sbms * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18120441Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19120441Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20120441Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21120441Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22120441Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23120441Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24120441Sbms * SUCH DAMAGE. 25120441Sbms */ 26120441Sbms 27120441Sbms#include <sys/cdefs.h> 28120441Sbms__FBSDID("$FreeBSD: stable/11/usr.bin/mkimg/vtoc8.c 271881 2014-09-19 23:16:02Z marcel $"); 29120441Sbms 30131681Sru#include <sys/types.h> 31120441Sbms#include <sys/endian.h> 32120441Sbms#include <sys/errno.h> 33120441Sbms#include <sys/vtoc.h> 34120441Sbms#include <stdio.h> 35120441Sbms#include <stdlib.h> 36120441Sbms#include <string.h> 37120441Sbms#include <unistd.h> 38120441Sbms 39120441Sbms#include "image.h" 40120441Sbms#include "mkimg.h" 41120441Sbms#include "scheme.h" 42120441Sbms 43120441Sbms#ifndef VTOC_TAG_FREEBSD_NANDFS 44120441Sbms#define VTOC_TAG_FREEBSD_NANDFS 0x0905 45120441Sbms#endif 46120441Sbms 47120441Sbmsstatic struct mkimg_alias vtoc8_aliases[] = { 48131642Sru { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) }, 49120441Sbms { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_SWAP) }, 50120441Sbms { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_UFS) }, 51120441Sbms { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_VINUM) }, 52120441Sbms { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) }, 53120441Sbms { ALIAS_NONE, 0 } 54120441Sbms}; 55120441Sbms 56120441Sbmsstatic lba_t 57131642Sruvtoc8_metadata(u_int where, lba_t blk) 58120441Sbms{ 59120441Sbms 60120441Sbms blk += (where == SCHEME_META_IMG_START) ? 1 : 0; 61120441Sbms return (round_cylinder(blk)); 62120441Sbms} 63120441Sbms 64120441Sbmsstatic int 65120441Sbmsvtoc8_write(lba_t imgsz, void *bootcode __unused) 66120441Sbms{ 67120441Sbms struct vtoc8 vtoc8; 68131642Sru struct part *part; 69120441Sbms u_char *p; 70120441Sbms int error, n; 71120441Sbms uint16_t ofs, sum; 72120441Sbms 73120441Sbms imgsz = (lba_t)ncyls * nheads * nsecs; 74267936Sbapt 75 memset(&vtoc8, 0, sizeof(vtoc8)); 76 sprintf(vtoc8.ascii, "FreeBSD%lldM", 77 (long long)(imgsz * secsz / 1048576)); 78 be32enc(&vtoc8.version, VTOC_VERSION); 79 be16enc(&vtoc8.nparts, VTOC8_NPARTS); 80 be32enc(&vtoc8.sanity, VTOC_SANITY); 81 be16enc(&vtoc8.rpm, 3600); 82 be16enc(&vtoc8.physcyls, ncyls); 83 be16enc(&vtoc8.ncyls, ncyls); 84 be16enc(&vtoc8.altcyls, 0); 85 be16enc(&vtoc8.nheads, nheads); 86 be16enc(&vtoc8.nsecs, nsecs); 87 be16enc(&vtoc8.magic, VTOC_MAGIC); 88 89 be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz); 90 STAILQ_FOREACH(part, &partlist, link) { 91 n = part->index + ((part->index >= VTOC_RAW_PART) ? 1 : 0); 92 be16enc(&vtoc8.part[n].tag, ALIAS_TYPE2INT(part->type)); 93 be32enc(&vtoc8.map[n].cyl, part->block / (nsecs * nheads)); 94 be32enc(&vtoc8.map[n].nblks, part->size); 95 } 96 97 /* Calculate checksum. */ 98 sum = 0; 99 p = (void *)&vtoc8; 100 for (ofs = 0; ofs < sizeof(vtoc8) - 2; ofs += 2) 101 sum ^= be16dec(p + ofs); 102 be16enc(&vtoc8.cksum, sum); 103 104 error = image_write(0, &vtoc8, 1); 105 return (error); 106} 107 108static struct mkimg_scheme vtoc8_scheme = { 109 .name = "vtoc8", 110 .description = "SMI VTOC8 disk labels", 111 .aliases = vtoc8_aliases, 112 .metadata = vtoc8_metadata, 113 .write = vtoc8_write, 114 .nparts = VTOC8_NPARTS - 1, 115 .maxsecsz = 512 116}; 117 118SCHEME_DEFINE(vtoc8_scheme); 119