1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2014 Juniper Networks, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/errno.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <apm.h>
36
37 #include "endian.h"
38 #include "image.h"
39 #include "mkimg.h"
40 #include "scheme.h"
41
42 static struct mkimg_alias apm_aliases[] = {
43 { ALIAS_FREEBSD, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD) },
44 { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(APM_ENT_TYPE_APPLE_BOOT) },
45 { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_NANDFS) },
46 { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_SWAP) },
47 { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_UFS) },
48 { ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_VINUM) },
49 { ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_ZFS) },
50 { ALIAS_NONE, 0 }
51 };
52
53 static lba_t
apm_metadata(u_int where,lba_t blk)54 apm_metadata(u_int where, lba_t blk)
55 {
56
57 blk += (where == SCHEME_META_IMG_START) ? nparts + 2 : 0;
58 return (round_block(blk));
59 }
60
61 static int
apm_write(lba_t imgsz,void * bootcode __unused)62 apm_write(lba_t imgsz, void *bootcode __unused)
63 {
64 u_char *buf;
65 struct apm_ddr *ddr;
66 struct apm_ent *ent;
67 struct part *part;
68 int error;
69
70 buf = calloc(nparts + 2, secsz);
71 if (buf == NULL)
72 return (ENOMEM);
73 ddr = (void *)buf;
74 be16enc(&ddr->ddr_sig, APM_DDR_SIG);
75 be16enc(&ddr->ddr_blksize, secsz);
76 be32enc(&ddr->ddr_blkcount, imgsz);
77
78 /* partition entry for the partition table itself. */
79 ent = (void *)(buf + secsz);
80 be16enc(&ent->ent_sig, APM_ENT_SIG);
81 be32enc(&ent->ent_pmblkcnt, nparts + 1);
82 be32enc(&ent->ent_start, 1);
83 be32enc(&ent->ent_size, nparts + 1);
84 strncpy(ent->ent_type, APM_ENT_TYPE_SELF, sizeof(ent->ent_type));
85 strncpy(ent->ent_name, "Apple", sizeof(ent->ent_name));
86
87 TAILQ_FOREACH(part, &partlist, link) {
88 ent = (void *)(buf + (part->index + 2) * secsz);
89 be16enc(&ent->ent_sig, APM_ENT_SIG);
90 be32enc(&ent->ent_pmblkcnt, nparts + 1);
91 be32enc(&ent->ent_start, part->block);
92 be32enc(&ent->ent_size, part->size);
93 strncpy(ent->ent_type, ALIAS_TYPE2PTR(part->type),
94 sizeof(ent->ent_type));
95 if (part->label != NULL)
96 strncpy(ent->ent_name, part->label,
97 sizeof(ent->ent_name));
98 }
99
100 error = image_write(0, buf, nparts + 2);
101 free(buf);
102 return (error);
103 }
104
105 static struct mkimg_scheme apm_scheme = {
106 .name = "apm",
107 .description = "Apple Partition Map",
108 .aliases = apm_aliases,
109 .metadata = apm_metadata,
110 .write = apm_write,
111 .nparts = 4096,
112 .labellen = APM_ENT_NAMELEN - 1,
113 .maxsecsz = 4096
114 };
115
116 SCHEME_DEFINE(apm_scheme);
117