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