xref: /freebsd/usr.bin/mkimg/scheme.c (revision dab8138e13dea539a387c458979403980a137bf2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013,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/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 	{ "hifive-bbl", ALIAS_HIFIVE_BBL },
62 	{ "xbootldr", ALIAS_XBOOTLDR },
63 	{ "hifive-fsbl", ALIAS_HIFIVE_FSBL },
64 	{ NULL, ALIAS_NONE }		/* Keep last! */
65 };
66 
67 static struct mkimg_scheme *first;
68 static struct mkimg_scheme *scheme;
69 static void *bootcode;
70 
71 static enum alias
scheme_parse_alias(const char * name)72 scheme_parse_alias(const char *name)
73 {
74 	u_int idx;
75 
76 	idx = 0;
77 	while (scheme_alias[idx].name != NULL) {
78 		if (strcasecmp(scheme_alias[idx].name, name) == 0)
79 			return (scheme_alias[idx].alias);
80 		idx++;
81 	}
82 	return (ALIAS_NONE);
83 }
84 
85 struct mkimg_scheme *
scheme_iterate(struct mkimg_scheme * s)86 scheme_iterate(struct mkimg_scheme *s)
87 {
88 
89 	return ((s == NULL) ? first : s->next);
90 }
91 
92 void
scheme_register(struct mkimg_scheme * s)93 scheme_register(struct mkimg_scheme *s)
94 {
95 	s->next = first;
96 	first = s;
97 }
98 
99 int
scheme_select(const char * spec)100 scheme_select(const char *spec)
101 {
102 	struct mkimg_scheme *s;
103 
104 	s = NULL;
105 	while ((s = scheme_iterate(s)) != NULL) {
106 		if (strcasecmp(spec, s->name) == 0) {
107 			scheme = s;
108 			return (0);
109 		}
110 	}
111 	return (EINVAL);
112 }
113 
114 struct mkimg_scheme *
scheme_selected(void)115 scheme_selected(void)
116 {
117 
118 	return (scheme);
119 }
120 
121 int
scheme_bootcode(int fd)122 scheme_bootcode(int fd)
123 {
124 	struct stat sb;
125 
126 	if (scheme == NULL || scheme->bootcode == 0)
127 		return (ENXIO);
128 
129 	if (fstat(fd, &sb) == -1)
130 		return (errno);
131 	if (sb.st_size > scheme->bootcode)
132 		return (EFBIG);
133 
134 	bootcode = malloc(scheme->bootcode);
135 	if (bootcode == NULL)
136 		return (ENOMEM);
137 	memset(bootcode, 0, scheme->bootcode);
138 	if (read(fd, bootcode, sb.st_size) != sb.st_size) {
139 		free(bootcode);
140 		bootcode = NULL;
141 		return (errno);
142 	}
143 	return (0);
144 }
145 
146 int
scheme_check_part(struct part * p)147 scheme_check_part(struct part *p)
148 {
149 	struct mkimg_alias *iter;
150 	enum alias alias;
151 
152 	assert(scheme != NULL);
153 
154 	/* Check the partition type alias */
155 	alias = scheme_parse_alias(p->alias);
156 	if (alias == ALIAS_NONE)
157 		return (EINVAL);
158 
159 	iter = scheme->aliases;
160 	while (iter->alias != ALIAS_NONE) {
161 		if (alias == iter->alias)
162 			break;
163 		iter++;
164 	}
165 	if (iter->alias == ALIAS_NONE)
166 		return (EINVAL);
167 	p->type = iter->type;
168 
169 	/* Validate the optional label. */
170 	if (p->label != NULL) {
171 		if (strlen(p->label) > scheme->labellen)
172 			return (EINVAL);
173 	}
174 
175 	return (0);
176 }
177 
178 u_int
scheme_max_parts(void)179 scheme_max_parts(void)
180 {
181 
182 	return ((scheme == NULL) ? 0 : scheme->nparts);
183 }
184 
185 u_int
scheme_max_secsz(void)186 scheme_max_secsz(void)
187 {
188 
189 	return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz);
190 }
191 
192 lba_t
scheme_metadata(u_int where,lba_t start)193 scheme_metadata(u_int where, lba_t start)
194 {
195 
196 	return ((scheme == NULL) ? start : scheme->metadata(where, start));
197 }
198 
199 int
scheme_write(lba_t end)200 scheme_write(lba_t end)
201 {
202 
203 	return ((scheme == NULL) ? 0 : scheme->write(end, bootcode));
204 }
205