vtoc8.c revision 263845
1/*-
2 * Copyright (c) 2014 Juniper Networks, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: user/marcel/mkimg/vtoc8.c 263845 2014-03-27 22:48:48Z marcel $");
29
30#include <sys/types.h>
31#include <sys/endian.h>
32#include <sys/errno.h>
33#include <sys/vtoc.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include "mkimg.h"
40#include "scheme.h"
41
42static struct mkimg_alias vtoc8_aliases[] = {
43    {	ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) },
44    {	ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_SWAP) },
45    {	ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_UFS) },
46    {	ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_VINUM) },
47    {	ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) },
48    {	ALIAS_NONE, 0 }
49};
50
51static u_int
52vtoc8_metadata(u_int where)
53{
54	u_int secs;
55
56	secs = (where == SCHEME_META_IMG_START) ? 1 : 0;
57	return (secs);
58}
59
60static int
61vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused)
62{
63	struct vtoc8 vtoc8;
64	struct part *part;
65	u_char *p;
66	int error;
67	uint16_t ofs, sum;
68
69	memset(&vtoc8, 0, sizeof(vtoc8));
70	sprintf(vtoc8.ascii, "FreeBSD%lldM", (long long)(imgsz / 2048));
71	be32enc(&vtoc8.version, VTOC_VERSION);
72	be16enc(&vtoc8.nparts, VTOC8_NPARTS);
73	be32enc(&vtoc8.sanity, VTOC_SANITY);
74	be16enc(&vtoc8.rpm, 3600);
75	be16enc(&vtoc8.physcyls, ncyls);
76	be16enc(&vtoc8.ncyls, ncyls);
77	be16enc(&vtoc8.altcyls, 0);
78	be16enc(&vtoc8.nheads, nheads);
79	be16enc(&vtoc8.nsecs, nsecs);
80	be16enc(&vtoc8.magic, VTOC_MAGIC);
81
82	ftruncate(fd, ncyls * nheads * nsecs *secsz);
83
84	STAILQ_FOREACH(part, &partlist, link) {
85		be16enc(&vtoc8.part[part->index].tag,
86		    ALIAS_TYPE2INT(part->type));
87		be32enc(&vtoc8.map[part->index].cyl,
88		    part->block / (nsecs * nheads));
89		be32enc(&vtoc8.map[part->index].nblks,
90		    part->size);
91	}
92
93	/* Calculate checksum. */
94	sum = 0;
95	p = (void *)&vtoc8;
96	for (ofs = 0; ofs < sizeof(vtoc8) - 2; ofs += 2)
97		sum ^= be16dec(p + ofs);
98	be16enc(&vtoc8.cksum, sum);
99
100	error = mkimg_seek(fd, 0);
101	if (error == 0) {
102		if (write(fd, &vtoc8, sizeof(vtoc8)) != sizeof(vtoc8))
103			error = errno;
104	}
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,
115	.maxsecsz = 512
116};
117
118SCHEME_DEFINE(vtoc8_scheme);
119