mkimg.c (253923) | mkimg.c (263382) |
---|---|
1/*- 2 * Copyright (c) 2013 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 --- 26 unchanged lines hidden (view full) --- 35#include <fcntl.h> 36#include <libutil.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <sysexits.h> 41#include <unistd.h> 42 | 1/*- 2 * Copyright (c) 2013 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 --- 26 unchanged lines hidden (view full) --- 35#include <fcntl.h> 36#include <libutil.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <sysexits.h> 41#include <unistd.h> 42 |
43#include "mkimg.h" |
|
43#include "scheme.h" 44 45#define BUFFER_SIZE (1024*1024) 46 | 44#include "scheme.h" 45 46#define BUFFER_SIZE (1024*1024) 47 |
47struct part { 48 STAILQ_ENTRY(part) link; 49 char *type; /* Partition type. */ 50 char *contents; /* Contents/size specification. */ 51 u_int kind; /* Content kind. */ 52#define PART_UNDEF 0 53#define PART_KIND_FILE 1 54#define PART_KIND_PIPE 2 55#define PART_KIND_SIZE 3 56 u_int index; /* Partition index (0-based). */ 57 off_t offset; /* Byte-offset of partition in image. */ 58 off_t size; /* Size in bytes of partition. */ 59}; | 48struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist); 49u_int nparts = 0; |
60 | 50 |
61static STAILQ_HEAD(, part) parts = STAILQ_HEAD_INITIALIZER(parts); 62static u_int nparts = 0; 63 | |
64static int bcfd = 0; 65static int outfd = 0; 66static int tmpfd = -1; 67 68static char tmpfname[] = "/tmp/mkimg-XXXXXX"; 69 70static void 71cleanup(void) --- 78 unchanged lines hidden (view full) --- 150 151 part->contents = strdup(spec); 152 if (part->contents == NULL) { 153 error = ENOMEM; 154 goto errout; 155 } 156 157 part->index = nparts; | 51static int bcfd = 0; 52static int outfd = 0; 53static int tmpfd = -1; 54 55static char tmpfname[] = "/tmp/mkimg-XXXXXX"; 56 57static void 58cleanup(void) --- 78 unchanged lines hidden (view full) --- 137 138 part->contents = strdup(spec); 139 if (part->contents == NULL) { 140 error = ENOMEM; 141 goto errout; 142 } 143 144 part->index = nparts; |
158 STAILQ_INSERT_TAIL(&parts, part, link); | 145 STAILQ_INSERT_TAIL(&partlist, part, link); |
159 nparts++; 160 return (0); 161 162 errout: 163 if (part->type != NULL) 164 free(part->type); 165 free(part); 166 return (error); --- 36 unchanged lines hidden (view full) --- 203 uint64_t size; 204 int error, fd; 205 206 if (nparts > scheme_max_parts()) 207 errc(EX_DATAERR, ENOSPC, "only %d partitions are supported", 208 scheme_max_parts()); 209 210 offset = scheme_first_offset(nparts); | 146 nparts++; 147 return (0); 148 149 errout: 150 if (part->type != NULL) 151 free(part->type); 152 free(part); 153 return (error); --- 36 unchanged lines hidden (view full) --- 190 uint64_t size; 191 int error, fd; 192 193 if (nparts > scheme_max_parts()) 194 errc(EX_DATAERR, ENOSPC, "only %d partitions are supported", 195 scheme_max_parts()); 196 197 offset = scheme_first_offset(nparts); |
211 STAILQ_FOREACH(part, &parts, link) { | 198 STAILQ_FOREACH(part, &partlist, link) { |
212 part->offset = offset; 213 lseek(tmpfd, offset, SEEK_SET); 214 /* XXX check error */ 215 216 error = 0; 217 switch (part->kind) { 218 case PART_KIND_SIZE: 219 if (expand_number(part->contents, &size) == -1) --- 12 unchanged lines hidden (view full) --- 232 if (fp != NULL) { 233 error = fdcopy(fileno(fp), tmpfd, &size); 234 pclose(fp); 235 } else 236 error = errno; 237 break; 238 } 239 part->size = size; | 199 part->offset = offset; 200 lseek(tmpfd, offset, SEEK_SET); 201 /* XXX check error */ 202 203 error = 0; 204 switch (part->kind) { 205 case PART_KIND_SIZE: 206 if (expand_number(part->contents, &size) == -1) --- 12 unchanged lines hidden (view full) --- 219 if (fp != NULL) { 220 error = fdcopy(fileno(fp), tmpfd, &size); 221 pclose(fp); 222 } else 223 error = errno; 224 break; 225 } 226 part->size = size; |
240 scheme_add_part(part->index, part->type, part->offset, 241 part->size); | 227 scheme_check_part(part); |
242 offset = scheme_next_offset(offset, size); 243 } 244 245 scheme_write(tmpfd, offset); 246} 247 248int 249main(int argc, char *argv[]) --- 71 unchanged lines hidden --- | 228 offset = scheme_next_offset(offset, size); 229 } 230 231 scheme_write(tmpfd, offset); 232} 233 234int 235main(int argc, char *argv[]) --- 71 unchanged lines hidden --- |