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>
34253923Smarcel#include <err.h>
35253923Smarcel#include <errno.h>
36253923Smarcel#include <stdint.h>
37263537Smarcel#include <stdlib.h>
38263466Smarcel#include <string.h>
39253923Smarcel#include <unistd.h>
40253923Smarcel
41268161Smarcel#include "image.h"
42263382Smarcel#include "mkimg.h"
43253923Smarcel#include "scheme.h"
44253923Smarcel
45263487Smarcelstatic struct {
46263487Smarcel	const char *name;
47263487Smarcel	enum alias alias;
48263487Smarcel} scheme_alias[] = {
49263672Smarcel	{ "ebr", ALIAS_EBR },
50263487Smarcel	{ "efi", ALIAS_EFI },
51263672Smarcel	{ "fat32", ALIAS_FAT32 },
52263487Smarcel	{ "freebsd", ALIAS_FREEBSD },
53263487Smarcel	{ "freebsd-boot", ALIAS_FREEBSD_BOOT },
54263487Smarcel	{ "freebsd-nandfs", ALIAS_FREEBSD_NANDFS },
55263487Smarcel	{ "freebsd-swap", ALIAS_FREEBSD_SWAP },
56263487Smarcel	{ "freebsd-ufs", ALIAS_FREEBSD_UFS },
57263487Smarcel	{ "freebsd-vinum", ALIAS_FREEBSD_VINUM },
58263487Smarcel	{ "freebsd-zfs", ALIAS_FREEBSD_ZFS },
59263487Smarcel	{ "mbr", ALIAS_MBR },
60263487Smarcel	{ NULL, ALIAS_NONE }		/* Keep last! */
61263487Smarcel};
62263487Smarcel
63263409Smarcelstatic struct mkimg_scheme *scheme;
64263537Smarcelstatic void *bootcode;
65253923Smarcel
66263487Smarcelstatic enum alias
67263487Smarcelscheme_parse_alias(const char *name)
68263487Smarcel{
69263487Smarcel	u_int idx;
70263487Smarcel
71263487Smarcel	idx = 0;
72263487Smarcel	while (scheme_alias[idx].name != NULL) {
73263487Smarcel		if (strcasecmp(scheme_alias[idx].name, name) == 0)
74263487Smarcel			return (scheme_alias[idx].alias);
75263487Smarcel		idx++;
76263487Smarcel	}
77263487Smarcel	return (ALIAS_NONE);
78263487Smarcel}
79263487Smarcel
80253923Smarcelint
81253923Smarcelscheme_select(const char *spec)
82253923Smarcel{
83263409Smarcel	struct mkimg_scheme *s, **iter;
84253923Smarcel
85263409Smarcel	SET_FOREACH(iter, schemes) {
86263409Smarcel		s = *iter;
87263409Smarcel		if (strcasecmp(spec, s->name) == 0) {
88263409Smarcel			scheme = s;
89253923Smarcel			return (0);
90253923Smarcel		}
91253923Smarcel	}
92253923Smarcel	return (EINVAL);
93253923Smarcel}
94253923Smarcel
95263409Smarcelstruct mkimg_scheme *
96253923Smarcelscheme_selected(void)
97253923Smarcel{
98253923Smarcel
99253923Smarcel	return (scheme);
100253923Smarcel}
101253923Smarcel
102253923Smarcelint
103263537Smarcelscheme_bootcode(int fd)
104263537Smarcel{
105263537Smarcel	struct stat sb;
106263537Smarcel
107263537Smarcel	if (scheme->bootcode == 0)
108263537Smarcel		return (ENXIO);
109263537Smarcel
110268161Smarcel	if (fstat(fd, &sb) == -1)
111268161Smarcel		return (errno);
112263537Smarcel	if (sb.st_size > scheme->bootcode)
113263537Smarcel		return (EFBIG);
114263537Smarcel
115263537Smarcel	bootcode = malloc(scheme->bootcode);
116263537Smarcel	if (bootcode == NULL)
117263537Smarcel		return (ENOMEM);
118263537Smarcel	memset(bootcode, 0, scheme->bootcode);
119263537Smarcel	if (read(fd, bootcode, sb.st_size) != sb.st_size) {
120263537Smarcel		free(bootcode);
121263537Smarcel		bootcode = NULL;
122263537Smarcel		return (errno);
123263537Smarcel	}
124263537Smarcel	return (0);
125263537Smarcel}
126263537Smarcel
127263537Smarcelint
128263409Smarcelscheme_check_part(struct part *p)
129253923Smarcel{
130263487Smarcel	struct mkimg_alias *iter;
131263487Smarcel	enum alias alias;
132253923Smarcel
133263414Smarcel	/* Check the partition type alias */
134263487Smarcel	alias = scheme_parse_alias(p->alias);
135263487Smarcel	if (alias == ALIAS_NONE)
136263487Smarcel		return (EINVAL);
137263487Smarcel
138263414Smarcel	iter = scheme->aliases;
139263487Smarcel	while (iter->alias != ALIAS_NONE) {
140263487Smarcel		if (alias == iter->alias)
141263414Smarcel			break;
142263414Smarcel		iter++;
143263414Smarcel	}
144263487Smarcel	if (iter->alias == ALIAS_NONE)
145263414Smarcel		return (EINVAL);
146263461Smarcel	p->type = iter->type;
147263466Smarcel
148263466Smarcel	/* Validate the optional label. */
149263466Smarcel	if (p->label != NULL) {
150263466Smarcel		if (strlen(p->label) > scheme->labellen)
151263487Smarcel			return (EINVAL);
152263466Smarcel	}
153263466Smarcel
154253923Smarcel	return (0);
155253923Smarcel}
156253923Smarcel
157253923Smarcelu_int
158253923Smarcelscheme_max_parts(void)
159253923Smarcel{
160253923Smarcel
161263409Smarcel	return (scheme->nparts);
162253923Smarcel}
163253923Smarcel
164263829Smarcelu_int
165263829Smarcelscheme_max_secsz(void)
166263829Smarcel{
167263829Smarcel
168263829Smarcel	return (scheme->maxsecsz);
169263829Smarcel}
170263829Smarcel
171263653Smarcellba_t
172263844Smarcelscheme_metadata(u_int where, lba_t start)
173263442Smarcel{
174263442Smarcel
175272030Smarcel	return (scheme->metadata(where, start));
176263442Smarcel}
177263442Smarcel
178263442Smarcelint
179268161Smarcelscheme_write(lba_t end)
180253923Smarcel{
181263442Smarcel	int error;
182253923Smarcel
183268161Smarcel	end = image_get_size();
184268161Smarcel	error = scheme->write(end, bootcode);
185263442Smarcel	return (error);
186253923Smarcel}
187