xref: /freebsd/usr.bin/mkimg/bsd.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
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/types.h>
31 #include <sys/disklabel.h>
32 #include <sys/endian.h>
33 #include <sys/errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include "image.h"
39 #include "mkimg.h"
40 #include "scheme.h"
41 
42 #ifndef FS_NANDFS
43 #define	FS_NANDFS	30
44 #endif
45 
46 static struct mkimg_alias bsd_aliases[] = {
47     {	ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) },
48     {	ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) },
49     {	ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(FS_BSDFFS) },
50     {	ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(FS_VINUM) },
51     {	ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(FS_ZFS) },
52     {	ALIAS_NONE, 0 }
53 };
54 
55 static lba_t
56 bsd_metadata(u_int where, lba_t blk)
57 {
58 
59 	if (where == SCHEME_META_IMG_START)
60 		blk += BBSIZE / secsz;
61 	else if (where == SCHEME_META_IMG_END)
62 		blk = round_cylinder(blk);
63 	else
64 		blk = round_block(blk);
65 	return (blk);
66 }
67 
68 static int
69 bsd_write(lba_t imgsz, void *bootcode)
70 {
71 	u_char *buf, *p;
72 	struct disklabel *d;
73 	struct partition *dp;
74 	struct part *part;
75 	int bsdparts, error, n;
76 	uint16_t checksum;
77 
78 	buf = malloc(BBSIZE);
79 	if (buf == NULL)
80 		return (ENOMEM);
81 	if (bootcode != NULL) {
82 		memcpy(buf, bootcode, BBSIZE);
83 		memset(buf + secsz, 0, sizeof(struct disklabel));
84 	} else
85 		memset(buf, 0, BBSIZE);
86 
87 	bsdparts = nparts + 1;	/* Account for c partition */
88 	if (bsdparts < MAXPARTITIONS)
89 		bsdparts = MAXPARTITIONS;
90 
91 	d = (void *)(buf + secsz);
92 	le32enc(&d->d_magic, DISKMAGIC);
93 	le32enc(&d->d_secsize, secsz);
94 	le32enc(&d->d_nsectors, nsecs);
95 	le32enc(&d->d_ntracks, nheads);
96 	le32enc(&d->d_ncylinders, ncyls);
97 	le32enc(&d->d_secpercyl, nsecs * nheads);
98 	le32enc(&d->d_secperunit, imgsz);
99 	le16enc(&d->d_rpm, 3600);
100 	le32enc(&d->d_magic2, DISKMAGIC);
101 	le16enc(&d->d_npartitions, bsdparts);
102 	le32enc(&d->d_bbsize, BBSIZE);
103 
104 	dp = &d->d_partitions[RAW_PART];
105 	le32enc(&dp->p_size, imgsz);
106 	STAILQ_FOREACH(part, &partlist, link) {
107 		n = part->index + ((part->index >= RAW_PART) ? 1 : 0);
108 		dp = &d->d_partitions[n];
109 		le32enc(&dp->p_size, part->size);
110 		le32enc(&dp->p_offset, part->block);
111 		le32enc(&dp->p_fsize, 0);
112 		dp->p_fstype = ALIAS_TYPE2INT(part->type);
113 		dp->p_frag = 0;
114 		le16enc(&dp->p_cpg, 0);
115 	}
116 
117 	dp = &d->d_partitions[bsdparts];
118 	checksum = 0;
119 	for (p = (void *)d; p < (u_char *)dp; p += 2)
120 		checksum ^= le16dec(p);
121 	le16enc(&d->d_checksum, checksum);
122 
123 	error = image_write(0, buf, BBSIZE / secsz);
124 	free(buf);
125 	return (error);
126 }
127 
128 static struct mkimg_scheme bsd_scheme = {
129 	.name = "bsd",
130 	.description = "BSD disk label",
131 	.aliases = bsd_aliases,
132 	.metadata = bsd_metadata,
133 	.write = bsd_write,
134 	.nparts = 19,
135 	.bootcode = BBSIZE,
136 	.maxsecsz = 512
137 };
138 
139 SCHEME_DEFINE(bsd_scheme);
140