2 * Copyright (c) 2014 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/errno.h>
42 static struct mkimg_alias bsd_aliases[] = {
43 { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) },
44 { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) },
45 { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(FS_BSDFFS) },
46 { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(FS_VINUM) },
47 { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(FS_ZFS) },
52 bsd_metadata(u_int where, lba_t blk)
55 if (where == SCHEME_META_IMG_START)
56 blk += BSD_BOOTBLOCK_SIZE / secsz;
57 else if (where == SCHEME_META_IMG_END)
58 blk = round_cylinder(blk);
60 blk = round_block(blk);
65 bsd_write(lba_t imgsz, void *bootcode)
71 int bsdparts, error, n;
74 buf = malloc(BSD_BOOTBLOCK_SIZE);
77 if (bootcode != NULL) {
78 memcpy(buf, bootcode, BSD_BOOTBLOCK_SIZE);
79 memset(buf + secsz, 0, sizeof(struct disklabel));
81 memset(buf, 0, BSD_BOOTBLOCK_SIZE);
83 bsdparts = nparts + 1; /* Account for c partition */
84 if (bsdparts < BSD_NPARTS_MIN)
85 bsdparts = BSD_NPARTS_MIN;
87 d = (void *)(buf + secsz);
88 le32enc(&d->d_magic, BSD_MAGIC);
89 le32enc(&d->d_secsize, secsz);
90 le32enc(&d->d_nsectors, nsecs);
91 le32enc(&d->d_ntracks, nheads);
92 le32enc(&d->d_ncylinders, ncyls);
93 le32enc(&d->d_secpercyl, nsecs * nheads);
94 le32enc(&d->d_secperunit, imgsz);
95 le16enc(&d->d_rpm, 3600);
96 le32enc(&d->d_magic2, BSD_MAGIC);
97 le16enc(&d->d_npartitions, bsdparts);
98 le32enc(&d->d_bbsize, BSD_BOOTBLOCK_SIZE);
100 dp = &d->d_partitions[BSD_PART_RAW];
101 le32enc(&dp->p_size, imgsz);
102 TAILQ_FOREACH(part, &partlist, link) {
103 n = part->index + ((part->index >= BSD_PART_RAW) ? 1 : 0);
104 dp = &d->d_partitions[n];
105 le32enc(&dp->p_size, part->size);
106 le32enc(&dp->p_offset, part->block);
107 le32enc(&dp->p_fsize, 0);
108 dp->p_fstype = ALIAS_TYPE2INT(part->type);
110 le16enc(&dp->p_cpg, 0);
113 dp = &d->d_partitions[bsdparts];
115 for (p = (void *)d; p < (u_char *)dp; p += 2)
116 checksum ^= le16dec(p);
117 le16enc(&d->d_checksum, checksum);
119 error = image_write(0, buf, BSD_BOOTBLOCK_SIZE / secsz);
124 static struct mkimg_scheme bsd_scheme = {
126 .description = "BSD disk label",
127 .aliases = bsd_aliases,
128 .metadata = bsd_metadata,
130 .nparts = BSD_NPARTS_MAX - 1,
131 .bootcode = BSD_BOOTBLOCK_SIZE,
135 SCHEME_DEFINE(bsd_scheme);