]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.bin/mkimg/scheme.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.bin / mkimg / scheme.c
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$");
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
47 static struct {
48         const char *name;
49         enum alias alias;
50 } scheme_alias[] = {
51         { "ebr", ALIAS_EBR },
52         { "efi", ALIAS_EFI },
53         { "fat32", ALIAS_FAT32 },
54         { "freebsd", ALIAS_FREEBSD },
55         { "freebsd-boot", ALIAS_FREEBSD_BOOT },
56         { "freebsd-nandfs", ALIAS_FREEBSD_NANDFS },
57         { "freebsd-swap", ALIAS_FREEBSD_SWAP },
58         { "freebsd-ufs", ALIAS_FREEBSD_UFS },
59         { "freebsd-vinum", ALIAS_FREEBSD_VINUM },
60         { "freebsd-zfs", ALIAS_FREEBSD_ZFS },
61         { "mbr", ALIAS_MBR },
62         { NULL, ALIAS_NONE }            /* Keep last! */
63 };
64
65 static struct mkimg_scheme *scheme;
66 static void *bootcode;
67
68 static enum alias
69 scheme_parse_alias(const char *name)
70 {
71         u_int idx;
72
73         idx = 0;
74         while (scheme_alias[idx].name != NULL) {
75                 if (strcasecmp(scheme_alias[idx].name, name) == 0)
76                         return (scheme_alias[idx].alias);
77                 idx++;
78         }
79         return (ALIAS_NONE);
80 }
81
82 int
83 scheme_select(const char *spec)
84 {
85         struct mkimg_scheme *s, **iter;
86
87         SET_FOREACH(iter, schemes) {
88                 s = *iter;
89                 if (strcasecmp(spec, s->name) == 0) {
90                         scheme = s;
91                         return (0);
92                 }
93         }
94         return (EINVAL);
95 }
96
97 struct mkimg_scheme *
98 scheme_selected(void)
99 {
100
101         return (scheme);
102 }
103
104 int
105 scheme_bootcode(int fd)
106 {
107         struct stat sb;
108
109         if (scheme == NULL || scheme->bootcode == 0)
110                 return (ENXIO);
111
112         if (fstat(fd, &sb) == -1)
113                 return (errno);
114         if (sb.st_size > scheme->bootcode)
115                 return (EFBIG);
116
117         bootcode = malloc(scheme->bootcode);
118         if (bootcode == NULL)
119                 return (ENOMEM);
120         memset(bootcode, 0, scheme->bootcode);
121         if (read(fd, bootcode, sb.st_size) != sb.st_size) {
122                 free(bootcode);
123                 bootcode = NULL;
124                 return (errno);
125         }
126         return (0);
127 }
128
129 int
130 scheme_check_part(struct part *p)
131 {
132         struct mkimg_alias *iter;
133         enum alias alias;
134
135         assert(scheme != NULL);
136
137         /* Check the partition type alias */
138         alias = scheme_parse_alias(p->alias);
139         if (alias == ALIAS_NONE)
140                 return (EINVAL);
141
142         iter = scheme->aliases;
143         while (iter->alias != ALIAS_NONE) {
144                 if (alias == iter->alias)
145                         break;
146                 iter++;
147         }
148         if (iter->alias == ALIAS_NONE)
149                 return (EINVAL);
150         p->type = iter->type;
151
152         /* Validate the optional label. */
153         if (p->label != NULL) {
154                 if (strlen(p->label) > scheme->labellen)
155                         return (EINVAL);
156         }
157
158         return (0);
159 }
160
161 u_int
162 scheme_max_parts(void)
163 {
164
165         return ((scheme == NULL) ? 0 : scheme->nparts);
166 }
167
168 u_int
169 scheme_max_secsz(void)
170 {
171
172         return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz);
173 }
174
175 lba_t
176 scheme_metadata(u_int where, lba_t start)
177 {
178
179         return ((scheme == NULL) ? start : scheme->metadata(where, start));
180 }
181
182 int
183 scheme_write(lba_t end)
184 {
185
186         return ((scheme == NULL) ? 0 : scheme->write(end, bootcode));
187 }