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 <assert.h> 32 #include <errno.h> 33 #include <stdlib.h> 34 #include <unistd.h> 35 36 #include "image.h" 37 #include "mkimg.h" 38 39 #define BUFFER_SIZE (1024*1024) 40 41 static char image_tmpfile[] = "/tmp/mkimg-XXXXXX"; 42 static int image_fd = -1; 43 static lba_t image_size; 44 45 static void 46 cleanup(void) 47 { 48 49 if (image_fd != -1) 50 close(image_fd); 51 unlink(image_tmpfile); 52 } 53 54 int 55 image_copyin(lba_t blk, int fd, uint64_t *sizep) 56 { 57 char *buffer; 58 uint64_t bytesize; 59 ssize_t bcnt, rdsz; 60 int error, partial; 61 62 assert(BUFFER_SIZE % secsz == 0); 63 64 buffer = malloc(BUFFER_SIZE); 65 if (buffer == NULL) 66 return (ENOMEM); 67 bytesize = 0; 68 partial = 0; 69 while (1) { 70 rdsz = read(fd, buffer, BUFFER_SIZE); 71 if (rdsz <= 0) { 72 error = (rdsz < 0) ? errno : 0; 73 break; 74 } 75 if (partial) 76 abort(); 77 bytesize += rdsz; 78 bcnt = (rdsz + secsz - 1) / secsz; 79 error = image_write(blk, buffer, bcnt); 80 if (error) 81 break; 82 blk += bcnt; 83 partial = ((ssize_t)(bcnt * secsz) != rdsz) ? 1 : 0; 84 } 85 free(buffer); 86 if (sizep != NULL) 87 *sizep = bytesize; 88 return (error); 89 } 90 91 int 92 image_copyout(int fd) 93 { 94 char *buffer; 95 off_t ofs; 96 ssize_t rdsz, wrsz; 97 int error; 98 99 ofs = lseek(fd, 0L, SEEK_CUR); 100 101 if (lseek(image_fd, 0, SEEK_SET) != 0) 102 return (errno); 103 buffer = malloc(BUFFER_SIZE); 104 if (buffer == NULL) 105 return (errno); 106 error = 0; 107 while (1) { 108 rdsz = read(image_fd, buffer, BUFFER_SIZE); 109 if (rdsz <= 0) { 110 error = (rdsz < 0) ? errno : 0; 111 break; 112 } 113 wrsz = (ofs == -1) ? 114 write(fd, buffer, rdsz) : 115 sparse_write(fd, buffer, rdsz); 116 if (wrsz < 0) { 117 error = errno; 118 break; 119 } 120 } 121 free(buffer); 122 if (error) 123 return (error); 124 ofs = lseek(fd, 0L, SEEK_CUR); 125 if (ofs == -1) 126 return (errno); 127 error = (ftruncate(fd, ofs) == -1) ? errno : 0; 128 return (error); 129 } 130 131 lba_t 132 image_get_size(void) 133 { 134 135 return (image_size); 136 } 137 138 int 139 image_set_size(lba_t blk) 140 { 141 142 image_size = blk; 143 if (ftruncate(image_fd, blk * secsz) == -1) 144 return (errno); 145 return (0); 146 } 147 148 int 149 image_write(lba_t blk, void *buf, ssize_t len) 150 { 151 152 blk *= secsz; 153 if (lseek(image_fd, blk, SEEK_SET) != blk) 154 return (errno); 155 len *= secsz; 156 if (sparse_write(image_fd, buf, len) != len) 157 return (errno); 158 return (0); 159 } 160 161 int 162 image_init(void) 163 { 164 165 if (atexit(cleanup) == -1) 166 return (errno); 167 image_fd = mkstemp(image_tmpfile); 168 if (image_fd == -1) 169 return (errno); 170 return (0); 171 } 172