]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/mkimg/vtoc8.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / usr.bin / mkimg / vtoc8.c
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$");
29
30 #include <sys/errno.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include <sys/vtoc.h>
38
39 #include "endian.h"
40 #include "image.h"
41 #include "mkimg.h"
42 #include "scheme.h"
43
44 #ifndef VTOC_TAG_FREEBSD_NANDFS
45 #define VTOC_TAG_FREEBSD_NANDFS 0x0905
46 #endif
47
48 static struct mkimg_alias vtoc8_aliases[] = {
49     {   ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) },
50     {   ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_SWAP) },
51     {   ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_UFS) },
52     {   ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_VINUM) },
53     {   ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) },
54     {   ALIAS_NONE, 0 }
55 };
56
57 static lba_t
58 vtoc8_metadata(u_int where, lba_t blk)
59 {
60
61         blk += (where == SCHEME_META_IMG_START) ? 1 : 0;
62         return (round_cylinder(blk));
63 }
64
65 static int
66 vtoc8_write(lba_t imgsz, void *bootcode __unused)
67 {
68         struct vtoc8 vtoc8;
69         struct part *part;
70         u_char *p;
71         int error, n;
72         uint16_t ofs, sum;
73
74         imgsz = (lba_t)ncyls * nheads * nsecs;
75
76         memset(&vtoc8, 0, sizeof(vtoc8));
77         sprintf(vtoc8.ascii, "FreeBSD%lldM",
78             (long long)(imgsz * secsz / 1048576));
79         be32enc(&vtoc8.version, VTOC_VERSION);
80         be16enc(&vtoc8.nparts, VTOC8_NPARTS);
81         be32enc(&vtoc8.sanity, VTOC_SANITY);
82         be16enc(&vtoc8.rpm, 3600);
83         be16enc(&vtoc8.physcyls, ncyls);
84         be16enc(&vtoc8.ncyls, ncyls);
85         be16enc(&vtoc8.altcyls, 0);
86         be16enc(&vtoc8.nheads, nheads);
87         be16enc(&vtoc8.nsecs, nsecs);
88         be16enc(&vtoc8.magic, VTOC_MAGIC);
89
90         be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz);
91         TAILQ_FOREACH(part, &partlist, link) {
92                 n = part->index + ((part->index >= VTOC_RAW_PART) ? 1 : 0);
93                 be16enc(&vtoc8.part[n].tag, ALIAS_TYPE2INT(part->type));
94                 be32enc(&vtoc8.map[n].cyl, part->block / (nsecs * nheads));
95                 be32enc(&vtoc8.map[n].nblks, part->size);
96         }
97
98         /* Calculate checksum. */
99         sum = 0;
100         p = (void *)&vtoc8;
101         for (ofs = 0; ofs < sizeof(vtoc8) - 2; ofs += 2)
102                 sum ^= be16dec(p + ofs);
103         be16enc(&vtoc8.cksum, sum);
104
105         error = image_write(0, &vtoc8, 1);
106         return (error);
107 }
108
109 static struct mkimg_scheme vtoc8_scheme = {
110         .name = "vtoc8",
111         .description = "SMI VTOC8 disk labels",
112         .aliases = vtoc8_aliases,
113         .metadata = vtoc8_metadata,
114         .write = vtoc8_write,
115         .nparts = VTOC8_NPARTS - 1,
116         .maxsecsz = 512
117 };
118
119 SCHEME_DEFINE(vtoc8_scheme);