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: releng/11.0/usr.bin/mkimg/scheme.c 292082 2015-12-11 05:39:42Z imp $"); 29253923Smarcel 30253923Smarcel#include <sys/types.h> 31263409Smarcel#include <sys/linker_set.h> 32263382Smarcel#include <sys/queue.h> 33263537Smarcel#include <sys/stat.h> 34279128Smarcel#include <assert.h> 35253923Smarcel#include <err.h> 36253923Smarcel#include <errno.h> 37279128Smarcel#include <limits.h> 38253923Smarcel#include <stdint.h> 39263537Smarcel#include <stdlib.h> 40263466Smarcel#include <string.h> 41253923Smarcel#include <unistd.h> 42253923Smarcel 43266176Smarcel#include "image.h" 44263382Smarcel#include "mkimg.h" 45253923Smarcel#include "scheme.h" 46253923Smarcel 47263487Smarcelstatic struct { 48263487Smarcel const char *name; 49263487Smarcel enum alias alias; 50263487Smarcel} scheme_alias[] = { 51263672Smarcel { "ebr", ALIAS_EBR }, 52263487Smarcel { "efi", ALIAS_EFI }, 53289349Semaste { "fat16b", ALIAS_FAT16B }, 54263672Smarcel { "fat32", ALIAS_FAT32 }, 55263487Smarcel { "freebsd", ALIAS_FREEBSD }, 56263487Smarcel { "freebsd-boot", ALIAS_FREEBSD_BOOT }, 57263487Smarcel { "freebsd-nandfs", ALIAS_FREEBSD_NANDFS }, 58263487Smarcel { "freebsd-swap", ALIAS_FREEBSD_SWAP }, 59263487Smarcel { "freebsd-ufs", ALIAS_FREEBSD_UFS }, 60263487Smarcel { "freebsd-vinum", ALIAS_FREEBSD_VINUM }, 61263487Smarcel { "freebsd-zfs", ALIAS_FREEBSD_ZFS }, 62263487Smarcel { "mbr", ALIAS_MBR }, 63284883Smarcel { "ntfs", ALIAS_NTFS }, 64292082Simp { "prepboot", ALIAS_PPCBOOT }, 65263487Smarcel { NULL, ALIAS_NONE } /* Keep last! */ 66263487Smarcel}; 67263487Smarcel 68263409Smarcelstatic struct mkimg_scheme *scheme; 69263537Smarcelstatic void *bootcode; 70253923Smarcel 71263487Smarcelstatic enum alias 72263487Smarcelscheme_parse_alias(const char *name) 73263487Smarcel{ 74263487Smarcel u_int idx; 75263487Smarcel 76263487Smarcel idx = 0; 77263487Smarcel while (scheme_alias[idx].name != NULL) { 78263487Smarcel if (strcasecmp(scheme_alias[idx].name, name) == 0) 79263487Smarcel return (scheme_alias[idx].alias); 80263487Smarcel idx++; 81263487Smarcel } 82263487Smarcel return (ALIAS_NONE); 83263487Smarcel} 84263487Smarcel 85253923Smarcelint 86253923Smarcelscheme_select(const char *spec) 87253923Smarcel{ 88263409Smarcel struct mkimg_scheme *s, **iter; 89253923Smarcel 90263409Smarcel SET_FOREACH(iter, schemes) { 91263409Smarcel s = *iter; 92263409Smarcel if (strcasecmp(spec, s->name) == 0) { 93263409Smarcel scheme = s; 94253923Smarcel return (0); 95253923Smarcel } 96253923Smarcel } 97253923Smarcel return (EINVAL); 98253923Smarcel} 99253923Smarcel 100263409Smarcelstruct mkimg_scheme * 101253923Smarcelscheme_selected(void) 102253923Smarcel{ 103253923Smarcel 104253923Smarcel return (scheme); 105253923Smarcel} 106253923Smarcel 107253923Smarcelint 108263537Smarcelscheme_bootcode(int fd) 109263537Smarcel{ 110263537Smarcel struct stat sb; 111263537Smarcel 112279128Smarcel if (scheme == NULL || scheme->bootcode == 0) 113263537Smarcel return (ENXIO); 114263537Smarcel 115266511Smarcel if (fstat(fd, &sb) == -1) 116266511Smarcel return (errno); 117263537Smarcel if (sb.st_size > scheme->bootcode) 118263537Smarcel return (EFBIG); 119263537Smarcel 120263537Smarcel bootcode = malloc(scheme->bootcode); 121263537Smarcel if (bootcode == NULL) 122263537Smarcel return (ENOMEM); 123263537Smarcel memset(bootcode, 0, scheme->bootcode); 124263537Smarcel if (read(fd, bootcode, sb.st_size) != sb.st_size) { 125263537Smarcel free(bootcode); 126263537Smarcel bootcode = NULL; 127263537Smarcel return (errno); 128263537Smarcel } 129263537Smarcel return (0); 130263537Smarcel} 131263537Smarcel 132263537Smarcelint 133263409Smarcelscheme_check_part(struct part *p) 134253923Smarcel{ 135263487Smarcel struct mkimg_alias *iter; 136263487Smarcel enum alias alias; 137253923Smarcel 138279128Smarcel assert(scheme != NULL); 139279128Smarcel 140263414Smarcel /* Check the partition type alias */ 141263487Smarcel alias = scheme_parse_alias(p->alias); 142263487Smarcel if (alias == ALIAS_NONE) 143263487Smarcel return (EINVAL); 144263487Smarcel 145263414Smarcel iter = scheme->aliases; 146263487Smarcel while (iter->alias != ALIAS_NONE) { 147263487Smarcel if (alias == iter->alias) 148263414Smarcel break; 149263414Smarcel iter++; 150263414Smarcel } 151263487Smarcel if (iter->alias == ALIAS_NONE) 152263414Smarcel return (EINVAL); 153263461Smarcel p->type = iter->type; 154263466Smarcel 155263466Smarcel /* Validate the optional label. */ 156263466Smarcel if (p->label != NULL) { 157263466Smarcel if (strlen(p->label) > scheme->labellen) 158263487Smarcel return (EINVAL); 159263466Smarcel } 160263466Smarcel 161253923Smarcel return (0); 162253923Smarcel} 163253923Smarcel 164253923Smarcelu_int 165253923Smarcelscheme_max_parts(void) 166253923Smarcel{ 167253923Smarcel 168279128Smarcel return ((scheme == NULL) ? 0 : scheme->nparts); 169253923Smarcel} 170253923Smarcel 171263829Smarcelu_int 172263829Smarcelscheme_max_secsz(void) 173263829Smarcel{ 174263829Smarcel 175279128Smarcel return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz); 176263829Smarcel} 177263829Smarcel 178263653Smarcellba_t 179263844Smarcelscheme_metadata(u_int where, lba_t start) 180263442Smarcel{ 181263442Smarcel 182279128Smarcel return ((scheme == NULL) ? start : scheme->metadata(where, start)); 183263442Smarcel} 184263442Smarcel 185263442Smarcelint 186266176Smarcelscheme_write(lba_t end) 187253923Smarcel{ 188253923Smarcel 189279128Smarcel return ((scheme == NULL) ? 0 : scheme->write(end, bootcode)); 190253923Smarcel} 191