xref: /freebsd/usr.bin/mkimg/apm.c (revision 95d45410b5100e07f6f98450bcd841a8945d4726)
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/apm.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 APM_ENT_TYPE_APPLE_BOOT
43 #define	APM_ENT_TYPE_APPLE_BOOT		"Apple_Bootstrap"
44 #endif
45 #ifndef APM_ENT_TYPE_FREEBSD_NANDFS
46 #define	APM_ENT_TYPE_FREEBSD_NANDFS	"FreeBSD-nandfs"
47 #endif
48 
49 static struct mkimg_alias apm_aliases[] = {
50     {	ALIAS_FREEBSD, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD) },
51     {	ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(APM_ENT_TYPE_APPLE_BOOT) },
52     {	ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_NANDFS) },
53     {	ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_SWAP) },
54     {	ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_UFS) },
55     {	ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_VINUM) },
56     {	ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_ZFS) },
57     {	ALIAS_NONE, 0 }
58 };
59 
60 static u_int
61 apm_metadata(u_int where)
62 {
63 	u_int secs;
64 
65 	secs = (where == SCHEME_META_IMG_START) ? nparts + 2 : 0;
66 	return (secs);
67 }
68 
69 static int
70 apm_write(lba_t imgsz, void *bootcode __unused)
71 {
72 	u_char *buf;
73 	struct apm_ddr *ddr;
74 	struct apm_ent *ent;
75 	struct part *part;
76 	int error;
77 
78 	buf = calloc(nparts + 2, secsz);
79 	if (buf == NULL)
80 		return (ENOMEM);
81 	ddr = (void *)buf;
82 	be16enc(&ddr->ddr_sig, APM_DDR_SIG);
83 	be16enc(&ddr->ddr_blksize, secsz);
84 	be32enc(&ddr->ddr_blkcount, imgsz);
85 
86 	/* partition entry for the partition table itself. */
87 	ent = (void *)(buf + secsz);
88 	be16enc(&ent->ent_sig, APM_ENT_SIG);
89 	be32enc(&ent->ent_pmblkcnt, nparts + 1);
90 	be32enc(&ent->ent_start, 1);
91 	be32enc(&ent->ent_size, nparts + 1);
92 	strncpy(ent->ent_type, APM_ENT_TYPE_SELF, sizeof(ent->ent_type));
93 	strncpy(ent->ent_name, "Apple", sizeof(ent->ent_name));
94 
95 	STAILQ_FOREACH(part, &partlist, link) {
96 		ent = (void *)(buf + (part->index + 2) * secsz);
97 		be16enc(&ent->ent_sig, APM_ENT_SIG);
98 		be32enc(&ent->ent_pmblkcnt, nparts + 1);
99 		be32enc(&ent->ent_start, part->block);
100 		be32enc(&ent->ent_size, part->size);
101 		strncpy(ent->ent_type, ALIAS_TYPE2PTR(part->type),
102 		    sizeof(ent->ent_type));
103 		if (part->label != NULL)
104 			strncpy(ent->ent_name, part->label,
105 			    sizeof(ent->ent_name));
106 	}
107 
108 	error = image_write(0, buf, nparts + 2);
109 	free(buf);
110 	return (error);
111 }
112 
113 static struct mkimg_scheme apm_scheme = {
114 	.name = "apm",
115 	.description = "Apple Partition Map",
116 	.aliases = apm_aliases,
117 	.metadata = apm_metadata,
118 	.write = apm_write,
119 	.nparts = 4096,
120 	.labellen = APM_ENT_NAMELEN - 1,
121 	.maxsecsz = 4096
122 };
123 
124 SCHEME_DEFINE(apm_scheme);
125