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