image.c revision 265574
1265574Smarcel/*- 2265574Smarcel * Copyright (c) 2014 Juniper Networks, Inc. 3265574Smarcel * All rights reserved. 4265574Smarcel * 5265574Smarcel * Redistribution and use in source and binary forms, with or without 6265574Smarcel * modification, are permitted provided that the following conditions 7265574Smarcel * are met: 8265574Smarcel * 1. Redistributions of source code must retain the above copyright 9265574Smarcel * notice, this list of conditions and the following disclaimer. 10265574Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11265574Smarcel * notice, this list of conditions and the following disclaimer in the 12265574Smarcel * documentation and/or other materials provided with the distribution. 13265574Smarcel * 14265574Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15265574Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16265574Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17265574Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18265574Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19265574Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20265574Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21265574Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22265574Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23265574Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24265574Smarcel * SUCH DAMAGE. 25265574Smarcel */ 26265574Smarcel 27265574Smarcel#include <sys/cdefs.h> 28265574Smarcel__FBSDID("$FreeBSD: user/marcel/mkimg/image.c 265574 2014-05-07 17:12:15Z marcel $"); 29265574Smarcel 30265574Smarcel#include <sys/types.h> 31265574Smarcel#include <assert.h> 32265574Smarcel#include <errno.h> 33265574Smarcel#include <stdlib.h> 34265574Smarcel#include <unistd.h> 35265574Smarcel 36265574Smarcel#include "mkimg.h" 37265574Smarcel#include "image.h" 38265574Smarcel 39265574Smarcel#define BUFFER_SIZE (1024*1024) 40265574Smarcel 41265574Smarcelint 42265574Smarcelimage_copyin(lba_t blk, int fd, uint64_t *sizep) 43265574Smarcel{ 44265574Smarcel char *buffer; 45265574Smarcel uint64_t bytesize; 46265574Smarcel ssize_t bcnt, rdsz; 47265574Smarcel int error, partial; 48265574Smarcel 49265574Smarcel assert(BUFFER_SIZE % secsz == 0); 50265574Smarcel 51265574Smarcel buffer = malloc(BUFFER_SIZE); 52265574Smarcel if (buffer == NULL) 53265574Smarcel return (ENOMEM); 54265574Smarcel bytesize = 0; 55265574Smarcel partial = 0; 56265574Smarcel while (1) { 57265574Smarcel rdsz = read(fd, buffer, BUFFER_SIZE); 58265574Smarcel if (rdsz <= 0) { 59265574Smarcel error = (rdsz < 0) ? errno : 0; 60265574Smarcel break; 61265574Smarcel } 62265574Smarcel if (partial) 63265574Smarcel abort(); 64265574Smarcel bytesize += rdsz; 65265574Smarcel bcnt = (rdsz + secsz - 1) / secsz; 66265574Smarcel error = image_write(blk, buffer, bcnt); 67265574Smarcel if (error) 68265574Smarcel break; 69265574Smarcel blk += bcnt; 70265574Smarcel partial = (bcnt * secsz != rdsz) ? 1 : 0; 71265574Smarcel } 72265574Smarcel free(buffer); 73265574Smarcel if (sizep != NULL) 74265574Smarcel *sizep = bytesize; 75265574Smarcel return (error); 76265574Smarcel} 77265574Smarcel 78265574Smarcelint 79265574Smarcelimage_set_size(lba_t blk __unused) 80265574Smarcel{ 81265574Smarcel 82265574Smarcel /* TODO */ 83265574Smarcel return (0); 84265574Smarcel} 85265574Smarcel 86265574Smarcelint 87265574Smarcelimage_write(lba_t blk __unused, void *buf __unused, ssize_t len __unused) 88265574Smarcel{ 89265574Smarcel 90265574Smarcel /* TODO */ 91265574Smarcel return (0); 92265574Smarcel} 93