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