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