xref: /freebsd/usr.bin/mkimg/scheme.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*-
2  * Copyright (c) 2013,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/stat.h>
32 #include <assert.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "image.h"
42 #include "mkimg.h"
43 #include "scheme.h"
44 
45 static struct {
46 	const char *name;
47 	enum alias alias;
48 } scheme_alias[] = {
49 	{ "ebr", ALIAS_EBR },
50 	{ "efi", ALIAS_EFI },
51 	{ "fat16b", ALIAS_FAT16B },
52 	{ "fat32", ALIAS_FAT32 },
53 	{ "freebsd", ALIAS_FREEBSD },
54 	{ "freebsd-boot", ALIAS_FREEBSD_BOOT },
55 	{ "freebsd-nandfs", ALIAS_FREEBSD_NANDFS },
56 	{ "freebsd-swap", ALIAS_FREEBSD_SWAP },
57 	{ "freebsd-ufs", ALIAS_FREEBSD_UFS },
58 	{ "freebsd-vinum", ALIAS_FREEBSD_VINUM },
59 	{ "freebsd-zfs", ALIAS_FREEBSD_ZFS },
60 	{ "mbr", ALIAS_MBR },
61 	{ "ntfs", ALIAS_NTFS },
62 	{ "prepboot", ALIAS_PPCBOOT },
63 	{ NULL, ALIAS_NONE }		/* Keep last! */
64 };
65 
66 static struct mkimg_scheme *first;
67 static struct mkimg_scheme *scheme;
68 static void *bootcode;
69 
70 static enum alias
71 scheme_parse_alias(const char *name)
72 {
73 	u_int idx;
74 
75 	idx = 0;
76 	while (scheme_alias[idx].name != NULL) {
77 		if (strcasecmp(scheme_alias[idx].name, name) == 0)
78 			return (scheme_alias[idx].alias);
79 		idx++;
80 	}
81 	return (ALIAS_NONE);
82 }
83 
84 struct mkimg_scheme *
85 scheme_iterate(struct mkimg_scheme *s)
86 {
87 
88 	return ((s == NULL) ? first : s->next);
89 }
90 
91 void
92 scheme_register(struct mkimg_scheme *s)
93 {
94 	s->next = first;
95 	first = s;
96 }
97 
98 int
99 scheme_select(const char *spec)
100 {
101 	struct mkimg_scheme *s;
102 
103 	s = NULL;
104 	while ((s = scheme_iterate(s)) != NULL) {
105 		if (strcasecmp(spec, s->name) == 0) {
106 			scheme = s;
107 			return (0);
108 		}
109 	}
110 	return (EINVAL);
111 }
112 
113 struct mkimg_scheme *
114 scheme_selected(void)
115 {
116 
117 	return (scheme);
118 }
119 
120 int
121 scheme_bootcode(int fd)
122 {
123 	struct stat sb;
124 
125 	if (scheme == NULL || scheme->bootcode == 0)
126 		return (ENXIO);
127 
128 	if (fstat(fd, &sb) == -1)
129 		return (errno);
130 	if (sb.st_size > scheme->bootcode)
131 		return (EFBIG);
132 
133 	bootcode = malloc(scheme->bootcode);
134 	if (bootcode == NULL)
135 		return (ENOMEM);
136 	memset(bootcode, 0, scheme->bootcode);
137 	if (read(fd, bootcode, sb.st_size) != sb.st_size) {
138 		free(bootcode);
139 		bootcode = NULL;
140 		return (errno);
141 	}
142 	return (0);
143 }
144 
145 int
146 scheme_check_part(struct part *p)
147 {
148 	struct mkimg_alias *iter;
149 	enum alias alias;
150 
151 	assert(scheme != NULL);
152 
153 	/* Check the partition type alias */
154 	alias = scheme_parse_alias(p->alias);
155 	if (alias == ALIAS_NONE)
156 		return (EINVAL);
157 
158 	iter = scheme->aliases;
159 	while (iter->alias != ALIAS_NONE) {
160 		if (alias == iter->alias)
161 			break;
162 		iter++;
163 	}
164 	if (iter->alias == ALIAS_NONE)
165 		return (EINVAL);
166 	p->type = iter->type;
167 
168 	/* Validate the optional label. */
169 	if (p->label != NULL) {
170 		if (strlen(p->label) > scheme->labellen)
171 			return (EINVAL);
172 	}
173 
174 	return (0);
175 }
176 
177 u_int
178 scheme_max_parts(void)
179 {
180 
181 	return ((scheme == NULL) ? 0 : scheme->nparts);
182 }
183 
184 u_int
185 scheme_max_secsz(void)
186 {
187 
188 	return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz);
189 }
190 
191 lba_t
192 scheme_metadata(u_int where, lba_t start)
193 {
194 
195 	return ((scheme == NULL) ? start : scheme->metadata(where, start));
196 }
197 
198 int
199 scheme_write(lba_t end)
200 {
201 
202 	return ((scheme == NULL) ? 0 : scheme->write(end, bootcode));
203 }
204