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