1265625Smarcel/*- 2265625Smarcel * Copyright (c) 2014 Juniper Networks, Inc. 3265625Smarcel * All rights reserved. 4265625Smarcel * 5265625Smarcel * Redistribution and use in source and binary forms, with or without 6265625Smarcel * modification, are permitted provided that the following conditions 7265625Smarcel * are met: 8265625Smarcel * 1. Redistributions of source code must retain the above copyright 9265625Smarcel * notice, this list of conditions and the following disclaimer. 10265625Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11265625Smarcel * notice, this list of conditions and the following disclaimer in the 12265625Smarcel * documentation and/or other materials provided with the distribution. 13265625Smarcel * 14265625Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15265625Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16265625Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17265625Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18265625Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19265625Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20265625Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21265625Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22265625Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23265625Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24265625Smarcel * SUCH DAMAGE. 25265625Smarcel */ 26265625Smarcel 27265625Smarcel#include <sys/cdefs.h> 28265625Smarcel__FBSDID("$FreeBSD$"); 29265625Smarcel 30265625Smarcel#include <sys/types.h> 31265625Smarcel#include <sys/linker_set.h> 32265625Smarcel#include <sys/queue.h> 33265625Smarcel#include <sys/stat.h> 34265625Smarcel#include <err.h> 35265625Smarcel#include <errno.h> 36265625Smarcel#include <stdint.h> 37265625Smarcel#include <stdlib.h> 38265625Smarcel#include <string.h> 39265625Smarcel#include <unistd.h> 40265625Smarcel 41266039Smarcel#include "image.h" 42265625Smarcel#include "format.h" 43265625Smarcel#include "mkimg.h" 44265625Smarcel 45265625Smarcelstatic struct mkimg_format *format; 46265625Smarcel 47265625Smarcelint 48266039Smarcelformat_resize(lba_t end) 49266039Smarcel{ 50266039Smarcel 51266100Smarcel if (format == NULL) 52266039Smarcel return (ENOSYS); 53266039Smarcel return (format->resize(end)); 54266039Smarcel} 55266039Smarcel 56266039Smarcelint 57265625Smarcelformat_select(const char *spec) 58265625Smarcel{ 59265625Smarcel struct mkimg_format *f, **iter; 60265625Smarcel 61265625Smarcel SET_FOREACH(iter, formats) { 62265625Smarcel f = *iter; 63265625Smarcel if (strcasecmp(spec, f->name) == 0) { 64265625Smarcel format = f; 65265625Smarcel return (0); 66265625Smarcel } 67265625Smarcel } 68265625Smarcel return (EINVAL); 69265625Smarcel} 70265625Smarcel 71265625Smarcelstruct mkimg_format * 72265625Smarcelformat_selected(void) 73265625Smarcel{ 74265625Smarcel 75265625Smarcel return (format); 76265625Smarcel} 77265718Smarcel 78265718Smarcelint 79265718Smarcelformat_write(int fd) 80265718Smarcel{ 81266100Smarcel lba_t size; 82266100Smarcel int error; 83265718Smarcel 84266100Smarcel if (format == NULL) 85265718Smarcel return (ENOSYS); 86266100Smarcel size = image_get_size(); 87266100Smarcel error = format->resize(size); 88266100Smarcel if (!error) 89266100Smarcel error = format->write(fd); 90266100Smarcel return (error); 91265718Smarcel} 92