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