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