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