1253923Smarcel/*-
2263409Smarcel * Copyright (c) 2013,2014 Juniper Networks, Inc.
3253923Smarcel * All rights reserved.
4253923Smarcel *
5253923Smarcel * Redistribution and use in source and binary forms, with or without
6253923Smarcel * modification, are permitted provided that the following conditions
7253923Smarcel * are met:
8253923Smarcel * 1. Redistributions of source code must retain the above copyright
9253923Smarcel *    notice, this list of conditions and the following disclaimer.
10253923Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11253923Smarcel *    notice, this list of conditions and the following disclaimer in the
12253923Smarcel *    documentation and/or other materials provided with the distribution.
13253923Smarcel *
14253923Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15253923Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16253923Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17253923Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18253923Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19253923Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20253923Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21253923Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22253923Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23253923Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24253923Smarcel * SUCH DAMAGE.
25253923Smarcel */
26253923Smarcel
27253923Smarcel#include <sys/cdefs.h>
28253923Smarcel__FBSDID("$FreeBSD$");
29253923Smarcel
30253923Smarcel#include <sys/types.h>
31263409Smarcel#include <sys/linker_set.h>
32263382Smarcel#include <sys/queue.h>
33263537Smarcel#include <sys/stat.h>
34284523Smarcel#include <assert.h>
35253923Smarcel#include <err.h>
36253923Smarcel#include <errno.h>
37284523Smarcel#include <limits.h>
38253923Smarcel#include <stdint.h>
39263537Smarcel#include <stdlib.h>
40263466Smarcel#include <string.h>
41253923Smarcel#include <unistd.h>
42253923Smarcel
43268161Smarcel#include "image.h"
44263382Smarcel#include "mkimg.h"
45253923Smarcel#include "scheme.h"
46253923Smarcel
47263487Smarcelstatic struct {
48263487Smarcel	const char *name;
49263487Smarcel	enum alias alias;
50263487Smarcel} scheme_alias[] = {
51263672Smarcel	{ "ebr", ALIAS_EBR },
52263487Smarcel	{ "efi", ALIAS_EFI },
53292341Semaste	{ "fat16b", ALIAS_FAT16B },
54263672Smarcel	{ "fat32", ALIAS_FAT32 },
55263487Smarcel	{ "freebsd", ALIAS_FREEBSD },
56263487Smarcel	{ "freebsd-boot", ALIAS_FREEBSD_BOOT },
57263487Smarcel	{ "freebsd-nandfs", ALIAS_FREEBSD_NANDFS },
58263487Smarcel	{ "freebsd-swap", ALIAS_FREEBSD_SWAP },
59263487Smarcel	{ "freebsd-ufs", ALIAS_FREEBSD_UFS },
60263487Smarcel	{ "freebsd-vinum", ALIAS_FREEBSD_VINUM },
61263487Smarcel	{ "freebsd-zfs", ALIAS_FREEBSD_ZFS },
62263487Smarcel	{ "mbr", ALIAS_MBR },
63287122Smarcel	{ "ntfs", ALIAS_NTFS },
64263487Smarcel	{ NULL, ALIAS_NONE }		/* Keep last! */
65263487Smarcel};
66263487Smarcel
67263409Smarcelstatic struct mkimg_scheme *scheme;
68263537Smarcelstatic void *bootcode;
69253923Smarcel
70263487Smarcelstatic enum alias
71263487Smarcelscheme_parse_alias(const char *name)
72263487Smarcel{
73263487Smarcel	u_int idx;
74263487Smarcel
75263487Smarcel	idx = 0;
76263487Smarcel	while (scheme_alias[idx].name != NULL) {
77263487Smarcel		if (strcasecmp(scheme_alias[idx].name, name) == 0)
78263487Smarcel			return (scheme_alias[idx].alias);
79263487Smarcel		idx++;
80263487Smarcel	}
81263487Smarcel	return (ALIAS_NONE);
82263487Smarcel}
83263487Smarcel
84253923Smarcelint
85253923Smarcelscheme_select(const char *spec)
86253923Smarcel{
87263409Smarcel	struct mkimg_scheme *s, **iter;
88253923Smarcel
89263409Smarcel	SET_FOREACH(iter, schemes) {
90263409Smarcel		s = *iter;
91263409Smarcel		if (strcasecmp(spec, s->name) == 0) {
92263409Smarcel			scheme = s;
93253923Smarcel			return (0);
94253923Smarcel		}
95253923Smarcel	}
96253923Smarcel	return (EINVAL);
97253923Smarcel}
98253923Smarcel
99263409Smarcelstruct mkimg_scheme *
100253923Smarcelscheme_selected(void)
101253923Smarcel{
102253923Smarcel
103253923Smarcel	return (scheme);
104253923Smarcel}
105253923Smarcel
106253923Smarcelint
107263537Smarcelscheme_bootcode(int fd)
108263537Smarcel{
109263537Smarcel	struct stat sb;
110263537Smarcel
111284523Smarcel	if (scheme == NULL || scheme->bootcode == 0)
112263537Smarcel		return (ENXIO);
113263537Smarcel
114268161Smarcel	if (fstat(fd, &sb) == -1)
115268161Smarcel		return (errno);
116263537Smarcel	if (sb.st_size > scheme->bootcode)
117263537Smarcel		return (EFBIG);
118263537Smarcel
119263537Smarcel	bootcode = malloc(scheme->bootcode);
120263537Smarcel	if (bootcode == NULL)
121263537Smarcel		return (ENOMEM);
122263537Smarcel	memset(bootcode, 0, scheme->bootcode);
123263537Smarcel	if (read(fd, bootcode, sb.st_size) != sb.st_size) {
124263537Smarcel		free(bootcode);
125263537Smarcel		bootcode = NULL;
126263537Smarcel		return (errno);
127263537Smarcel	}
128263537Smarcel	return (0);
129263537Smarcel}
130263537Smarcel
131263537Smarcelint
132263409Smarcelscheme_check_part(struct part *p)
133253923Smarcel{
134263487Smarcel	struct mkimg_alias *iter;
135263487Smarcel	enum alias alias;
136253923Smarcel
137284523Smarcel	assert(scheme != NULL);
138284523Smarcel
139263414Smarcel	/* Check the partition type alias */
140263487Smarcel	alias = scheme_parse_alias(p->alias);
141263487Smarcel	if (alias == ALIAS_NONE)
142263487Smarcel		return (EINVAL);
143263487Smarcel
144263414Smarcel	iter = scheme->aliases;
145263487Smarcel	while (iter->alias != ALIAS_NONE) {
146263487Smarcel		if (alias == iter->alias)
147263414Smarcel			break;
148263414Smarcel		iter++;
149263414Smarcel	}
150263487Smarcel	if (iter->alias == ALIAS_NONE)
151263414Smarcel		return (EINVAL);
152263461Smarcel	p->type = iter->type;
153263466Smarcel
154263466Smarcel	/* Validate the optional label. */
155263466Smarcel	if (p->label != NULL) {
156263466Smarcel		if (strlen(p->label) > scheme->labellen)
157263487Smarcel			return (EINVAL);
158263466Smarcel	}
159263466Smarcel
160253923Smarcel	return (0);
161253923Smarcel}
162253923Smarcel
163253923Smarcelu_int
164253923Smarcelscheme_max_parts(void)
165253923Smarcel{
166253923Smarcel
167284523Smarcel	return ((scheme == NULL) ? 0 : scheme->nparts);
168253923Smarcel}
169253923Smarcel
170263829Smarcelu_int
171263829Smarcelscheme_max_secsz(void)
172263829Smarcel{
173263829Smarcel
174284523Smarcel	return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz);
175263829Smarcel}
176263829Smarcel
177263653Smarcellba_t
178263844Smarcelscheme_metadata(u_int where, lba_t start)
179263442Smarcel{
180263442Smarcel
181284523Smarcel	return ((scheme == NULL) ? start : scheme->metadata(where, start));
182263442Smarcel}
183263442Smarcel
184263442Smarcelint
185268161Smarcelscheme_write(lba_t end)
186253923Smarcel{
187253923Smarcel
188284523Smarcel	return ((scheme == NULL) ? 0 : scheme->write(end, bootcode));
189253923Smarcel}
190