2 * Copyright (c) 2013,2014 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
48 { "fat16b", ALIAS_FAT16B },
49 { "fat32", ALIAS_FAT32 },
50 { "freebsd", ALIAS_FREEBSD },
51 { "freebsd-boot", ALIAS_FREEBSD_BOOT },
52 { "freebsd-nandfs", ALIAS_FREEBSD_NANDFS },
53 { "freebsd-swap", ALIAS_FREEBSD_SWAP },
54 { "freebsd-ufs", ALIAS_FREEBSD_UFS },
55 { "freebsd-vinum", ALIAS_FREEBSD_VINUM },
56 { "freebsd-zfs", ALIAS_FREEBSD_ZFS },
58 { "ntfs", ALIAS_NTFS },
59 { "prepboot", ALIAS_PPCBOOT },
60 { NULL, ALIAS_NONE } /* Keep last! */
63 static struct mkimg_scheme *first;
64 static struct mkimg_scheme *scheme;
65 static void *bootcode;
68 scheme_parse_alias(const char *name)
73 while (scheme_alias[idx].name != NULL) {
74 if (strcasecmp(scheme_alias[idx].name, name) == 0)
75 return (scheme_alias[idx].alias);
82 scheme_iterate(struct mkimg_scheme *s)
85 return ((s == NULL) ? first : s->next);
89 scheme_register(struct mkimg_scheme *s)
96 scheme_select(const char *spec)
98 struct mkimg_scheme *s;
101 while ((s = scheme_iterate(s)) != NULL) {
102 if (strcasecmp(spec, s->name) == 0) {
110 struct mkimg_scheme *
111 scheme_selected(void)
118 scheme_bootcode(int fd)
122 if (scheme == NULL || scheme->bootcode == 0)
125 if (fstat(fd, &sb) == -1)
127 if (sb.st_size > scheme->bootcode)
130 bootcode = malloc(scheme->bootcode);
131 if (bootcode == NULL)
133 memset(bootcode, 0, scheme->bootcode);
134 if (read(fd, bootcode, sb.st_size) != sb.st_size) {
143 scheme_check_part(struct part *p)
145 struct mkimg_alias *iter;
148 assert(scheme != NULL);
150 /* Check the partition type alias */
151 alias = scheme_parse_alias(p->alias);
152 if (alias == ALIAS_NONE)
155 iter = scheme->aliases;
156 while (iter->alias != ALIAS_NONE) {
157 if (alias == iter->alias)
161 if (iter->alias == ALIAS_NONE)
163 p->type = iter->type;
165 /* Validate the optional label. */
166 if (p->label != NULL) {
167 if (strlen(p->label) > scheme->labellen)
175 scheme_max_parts(void)
178 return ((scheme == NULL) ? 0 : scheme->nparts);
182 scheme_max_secsz(void)
185 return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz);
189 scheme_metadata(u_int where, lba_t start)
192 return ((scheme == NULL) ? start : scheme->metadata(where, start));
196 scheme_write(lba_t end)
199 return ((scheme == NULL) ? 0 : scheme->write(end, bootcode));