2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2007, Marcus Overhagen. All Rights Reserved.
4 * Distributed under the terms of the MIT License.
18 #define EXIT_FAILURE 1
22 print_usage(bool error
)
25 printf("create_image\n");
27 printf("usage: create_image -i <imagesize> [-c] [-f] <file>\n");
28 printf(" -i, --imagesize size of raw partition image file\n");
29 printf(" -f, --file the raw partition image file\n");
30 printf(" -c, --clear-image set the image content to zero\n");
31 exit(error
? EXIT_FAILURE
: 0);
36 main(int argc
, char *argv
[])
39 const char *file
= NULL
;
40 bool clearImage
= false;
44 static struct option long_options
[] = {
45 {"file", required_argument
, 0, 'f'},
46 {"clear-image", no_argument
, 0, 'c'},
47 {"help", no_argument
, 0, 'h'},
48 {"imagesize", required_argument
, 0, 'i'},
52 opterr
= 0; /* don't print errors */
53 c
= getopt_long(argc
, argv
, "+hi:cf:", long_options
, NULL
);
63 imageSize
= strtoull(optarg
, NULL
, 10);
64 if (strchr(optarg
, 'G') || strchr(optarg
, 'g'))
65 imageSize
*= 1024 * 1024 * 1024;
66 else if (strchr(optarg
, 'M') || strchr(optarg
, 'm'))
67 imageSize
*= 1024 * 1024;
68 else if (strchr(optarg
, 'K') || strchr(optarg
, 'k'))
85 if (file
== NULL
&& optind
== argc
- 1)
88 if (!imageSize
|| !file
)
92 fprintf(stderr
, "Error: invalid image size\n");
96 if (imageSize
% 512) {
97 fprintf(stderr
, "Error: image size must be a multiple of 512 bytes\n");
101 int fd
= open(file
, O_RDWR
| O_CREAT
, 0666);
103 fprintf(stderr
, "Error: couldn't open file %s (%s)\n", file
,
109 if (fstat(fd
, &st
) < 0) {
110 fprintf(stderr
, "Error: stat()ing file %s failed (%s)\n", file
,
115 if (!S_ISREG(st
.st_mode
) && !S_ISBLK(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) {
116 fprintf(stderr
, "Error: type of file %s not supported\n", file
);
120 if (S_ISREG(st
.st_mode
)) {
121 // regular file -- use ftruncate() to resize it
122 if ((clearImage
&& ftruncate(fd
, 0) != 0)
123 || ftruncate(fd
, imageSize
) != 0) {
124 fprintf(stderr
, "Error: resizing file %s failed (%s)\n", file
,
129 // some kind of device -- clear it manually, if we have to
131 char buffer
[1024 * 1024];
132 memset(buffer
, 0, sizeof(buffer
));
134 off_t totalWritten
= 0;
136 while ((written
= write(fd
, buffer
, sizeof(buffer
))) > 0)
137 totalWritten
+= written
;
139 // Only fail, if an error occurs and we haven't written anything at
141 // TODO: We should probably first determine the size of the device
142 // and try to write only that much.
143 if (totalWritten
== 0 && written
< 0) {
144 fprintf(stderr
, "Error: writing to device file %s failed "
145 "(%s)\n", file
, strerror(errno
));