1/*- 2 * Copyright (c) 2013,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 --- 35 unchanged lines hidden (view full) --- 44#include "mkimg.h" 45#include "scheme.h" 46 47#define BUFFER_SIZE (1024*1024) 48 49struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist); 50u_int nparts = 0; 51 |
52u_int secsz = 512; 53 |
54static int bcfd = -1; 55static int outfd = 0; 56static int tmpfd = -1; 57 58static char tmpfname[] = "/tmp/mkimg-XXXXXX"; 59 60static void 61cleanup(void) --- 156 unchanged lines hidden (view full) --- 218 wrsz = write(dst, buffer, rdsz); 219 if (wrsz < 0) 220 break; 221 } 222 free(buffer); 223 return (errno); 224} 225 |
226int 227mkimg_seek(int fd, lba_t blk) 228{ 229 off_t off; 230 231 off = blk * secsz; 232 if (lseek(fd, off, SEEK_SET) != off) 233 return (errno); 234 return (0); 235} 236 |
237static void 238mkimg(int bfd) 239{ 240 FILE *fp; 241 struct part *part; |
242 lba_t block; 243 off_t bytesize; |
244 int error, fd; 245 246 if (nparts > scheme_max_parts()) 247 errc(EX_DATAERR, ENOSPC, "only %d partitions are supported", 248 scheme_max_parts()); 249 250 error = scheme_bootcode(bfd); 251 if (error) 252 errc(EX_DATAERR, error, "boot code"); 253 254 /* First check partition information */ 255 STAILQ_FOREACH(part, &partlist, link) { 256 error = scheme_check_part(part); 257 if (error) 258 errc(EX_DATAERR, error, "partition %d", part->index+1); 259 } 260 |
261 block = scheme_first_block(); |
262 STAILQ_FOREACH(part, &partlist, link) { |
263 part->block = block; 264 error = mkimg_seek(tmpfd, block); |
265 switch (part->kind) { 266 case PART_KIND_SIZE: |
267 if (expand_number(part->contents, &bytesize) == -1) |
268 error = errno; 269 break; 270 case PART_KIND_FILE: 271 fd = open(part->contents, O_RDONLY, 0); 272 if (fd != -1) { |
273 error = fdcopy(fd, tmpfd, &bytesize); |
274 close(fd); 275 } else 276 error = errno; 277 break; 278 case PART_KIND_PIPE: 279 fp = popen(part->contents, "r"); 280 if (fp != NULL) { |
281 error = fdcopy(fileno(fp), tmpfd, &bytesize); |
282 pclose(fp); 283 } else 284 error = errno; 285 break; 286 } 287 if (error) 288 errc(EX_IOERR, error, "partition %d", part->index+1); |
289 part->size = (bytesize + secsz - 1) / secsz; 290 block = scheme_next_block(part->block, part->size); |
291 } 292 |
293 error = (scheme_write(tmpfd, block)); |
294} 295 296int 297main(int argc, char *argv[]) 298{ 299 int c, error; 300 301 while ((c = getopt(argc, argv, "b:h:o:p:s:t:z")) != -1) { --- 67 unchanged lines hidden --- |