Deleted Added
full compact
bsd.c (263669) bsd.c (263700)
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>
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/bsd.c 263669 2014-03-23 19:22:13Z marcel $");
28__FBSDID("$FreeBSD: user/marcel/mkimg/bsd.c 263700 2014-03-25 02:32:04Z marcel $");
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 "mkimg.h"
39#include "scheme.h"
40
41static struct mkimg_alias bsd_aliases[] = {
42 { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) },
43 { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) },
44 { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(FS_BSDFFS) },
45 { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(FS_VINUM) },
46 { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(FS_ZFS) },
47 { ALIAS_NONE, 0 }
48};
49
50static u_int
51bsd_metadata(u_int where)
52{
53 u_int secs;
54
55 secs = BBSIZE / secsz;
56 return ((where == SCHEME_META_IMG_START) ? secs : 0);
57}
58
59static int
60bsd_write(int fd, lba_t imgsz, void *bootcode)
61{
62 u_char *buf, *p;
63 struct disklabel *d;
64 struct partition *dp;
65 struct part *part;
66 int error;
67 uint16_t checksum;
68
69 buf = malloc(BBSIZE);
70 if (buf == NULL)
71 return (ENOMEM);
72 if (bootcode != NULL) {
73 memcpy(buf, bootcode, BBSIZE);
74 memset(buf + LABELSECTOR * secsz, 0, secsz);
75 } else
76 memset(buf, 0, BBSIZE);
77
78 d = (void *)(buf + LABELSECTOR * secsz + LABELOFFSET);
79 le32enc(&d->d_magic, DISKMAGIC);
80 le32enc(&d->d_secsize, secsz);
81 le32enc(&d->d_nsectors, 1); /* XXX */
82 le32enc(&d->d_ntracks, 1); /* XXX */
83 le32enc(&d->d_ncylinders, 0); /* XXX */
84 le32enc(&d->d_secpercyl, 1); /* XXX */
85 le32enc(&d->d_secperunit, imgsz);
86 le16enc(&d->d_rpm, 3600);
87 le32enc(&d->d_magic2, DISKMAGIC);
88 le16enc(&d->d_npartitions, (8 > nparts) ? 8 : nparts);
89 le32enc(&d->d_bbsize, BBSIZE);
90
91 STAILQ_FOREACH(part, &partlist, link) {
92 dp = &d->d_partitions[part->index];
93 le32enc(&dp->p_size, part->size);
94 le32enc(&dp->p_offset, part->block);
95 dp->p_fstype = ALIAS_TYPE2INT(part->type);
96 }
97
98 dp = &d->d_partitions[nparts];
99 checksum = 0;
100 for (p = buf; p < (u_char *)dp; p += 2)
101 checksum ^= le16dec(p);
102 le16enc(&d->d_checksum, checksum);
103
104 error = mkimg_seek(fd, 0);
105 if (error == 0) {
106 if (write(fd, buf, BBSIZE) != BBSIZE)
107 error = errno;
108 }
109 free(buf);
110 return (error);
111}
112
113static struct mkimg_scheme bsd_scheme = {
114 .name = "bsd",
115 .description = "BSD disk label",
116 .aliases = bsd_aliases,
117 .metadata = bsd_metadata,
118 .write = bsd_write,
119 .nparts = 20,
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 "mkimg.h"
39#include "scheme.h"
40
41static struct mkimg_alias bsd_aliases[] = {
42 { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) },
43 { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) },
44 { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(FS_BSDFFS) },
45 { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(FS_VINUM) },
46 { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(FS_ZFS) },
47 { ALIAS_NONE, 0 }
48};
49
50static u_int
51bsd_metadata(u_int where)
52{
53 u_int secs;
54
55 secs = BBSIZE / secsz;
56 return ((where == SCHEME_META_IMG_START) ? secs : 0);
57}
58
59static int
60bsd_write(int fd, lba_t imgsz, void *bootcode)
61{
62 u_char *buf, *p;
63 struct disklabel *d;
64 struct partition *dp;
65 struct part *part;
66 int error;
67 uint16_t checksum;
68
69 buf = malloc(BBSIZE);
70 if (buf == NULL)
71 return (ENOMEM);
72 if (bootcode != NULL) {
73 memcpy(buf, bootcode, BBSIZE);
74 memset(buf + LABELSECTOR * secsz, 0, secsz);
75 } else
76 memset(buf, 0, BBSIZE);
77
78 d = (void *)(buf + LABELSECTOR * secsz + LABELOFFSET);
79 le32enc(&d->d_magic, DISKMAGIC);
80 le32enc(&d->d_secsize, secsz);
81 le32enc(&d->d_nsectors, 1); /* XXX */
82 le32enc(&d->d_ntracks, 1); /* XXX */
83 le32enc(&d->d_ncylinders, 0); /* XXX */
84 le32enc(&d->d_secpercyl, 1); /* XXX */
85 le32enc(&d->d_secperunit, imgsz);
86 le16enc(&d->d_rpm, 3600);
87 le32enc(&d->d_magic2, DISKMAGIC);
88 le16enc(&d->d_npartitions, (8 > nparts) ? 8 : nparts);
89 le32enc(&d->d_bbsize, BBSIZE);
90
91 STAILQ_FOREACH(part, &partlist, link) {
92 dp = &d->d_partitions[part->index];
93 le32enc(&dp->p_size, part->size);
94 le32enc(&dp->p_offset, part->block);
95 dp->p_fstype = ALIAS_TYPE2INT(part->type);
96 }
97
98 dp = &d->d_partitions[nparts];
99 checksum = 0;
100 for (p = buf; p < (u_char *)dp; p += 2)
101 checksum ^= le16dec(p);
102 le16enc(&d->d_checksum, checksum);
103
104 error = mkimg_seek(fd, 0);
105 if (error == 0) {
106 if (write(fd, buf, BBSIZE) != BBSIZE)
107 error = errno;
108 }
109 free(buf);
110 return (error);
111}
112
113static struct mkimg_scheme bsd_scheme = {
114 .name = "bsd",
115 .description = "BSD disk label",
116 .aliases = bsd_aliases,
117 .metadata = bsd_metadata,
118 .write = bsd_write,
119 .nparts = 20,
120 .bootcode = BBSIZE
120 .bootcode = BBSIZE,
121 .maxsecsz = 512
121};
122
123SCHEME_DEFINE(bsd_scheme);
122};
123
124SCHEME_DEFINE(bsd_scheme);