1 /*- 2 * Copyright (c) 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 "format.h" 43 #include "mkimg.h" 44 45 static struct mkimg_format *format; 46 47 int 48 format_resize(lba_t end) 49 { 50 51 if (format == NULL) 52 return (ENOSYS); 53 return (format->resize(end)); 54 } 55 56 int 57 format_select(const char *spec) 58 { 59 struct mkimg_format *f, **iter; 60 61 SET_FOREACH(iter, formats) { 62 f = *iter; 63 if (strcasecmp(spec, f->name) == 0) { 64 format = f; 65 return (0); 66 } 67 } 68 return (EINVAL); 69 } 70 71 struct mkimg_format * 72 format_selected(void) 73 { 74 75 return (format); 76 } 77 78 int 79 format_write(int fd) 80 { 81 lba_t size; 82 int error; 83 84 if (format == NULL) 85 return (ENOSYS); 86 size = image_get_size(); 87 error = format->resize(size); 88 if (!error) 89 error = format->write(fd); 90 return (error); 91 } 92