Skip to content
This repository was archived by the owner on Mar 24, 2018. It is now read-only.

Commit b83f4b2

Browse files
committed
Fix mkbootimg.c for Windows (MinGW) compatibility
* Using open()/read()/write() and friends is a bad idea on Windows due to the default being to treat them as text files.
1 parent 57c28d1 commit b83f4b2

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

mkbootimg/mkbootimg.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,29 @@ static void *load_file(const char *fn, unsigned *_sz)
2929
{
3030
char *data;
3131
int sz;
32-
int fd;
32+
FILE *fd;
3333

3434
data = 0;
35-
fd = open(fn, O_RDONLY);
36-
if(fd < 0) return 0;
35+
fd = fopen(fn, "rb");
36+
if(fd == 0) return 0;
3737

38-
sz = lseek(fd, 0, SEEK_END);
38+
if(fseek(fd, 0, SEEK_END) != 0) goto oops;
39+
sz = ftell(fd);
3940
if(sz < 0) goto oops;
4041

41-
if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
42+
if(fseek(fd, 0, SEEK_SET) != 0) goto oops;
4243

4344
data = (char*) malloc(sz);
4445
if(data == 0) goto oops;
4546

46-
if(read(fd, data, sz) != sz) goto oops;
47-
close(fd);
47+
if(fread(data, 1, sz, fd) != sz) goto oops;
48+
fclose(fd);
4849

4950
if(_sz) *_sz = sz;
5051
return data;
5152

5253
oops:
53-
close(fd);
54+
fclose(fd);
5455
if(data != 0) free(data);
5556
return 0;
5657
}
@@ -74,7 +75,7 @@ int usage(void)
7475

7576
static unsigned char padding[16384] = { 0, };
7677

77-
int write_padding(int fd, unsigned pagesize, unsigned itemsize)
78+
int write_padding(FILE *fd, unsigned pagesize, unsigned itemsize)
7879
{
7980
unsigned pagemask = pagesize - 1;
8081
unsigned count;
@@ -85,7 +86,7 @@ int write_padding(int fd, unsigned pagesize, unsigned itemsize)
8586

8687
count = pagesize - (itemsize & pagemask);
8788

88-
if(write(fd, padding, count) != count) {
89+
if(fwrite(padding, 1, count, fd) != count) {
8990
return -1;
9091
} else {
9192
return 0;
@@ -106,7 +107,7 @@ int main(int argc, char **argv)
106107
char *bootimg = 0;
107108
char *board = "";
108109
unsigned pagesize = 2048;
109-
int fd;
110+
FILE *fd;
110111
SHA_CTX ctx;
111112
uint8_t* sha;
112113
unsigned base = 0x10000000;
@@ -246,31 +247,31 @@ int main(int argc, char **argv)
246247
memcpy(hdr.id, sha,
247248
SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE);
248249

249-
fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
250-
if(fd < 0) {
250+
fd = fopen(bootimg, "wb");
251+
if(fd == 0) {
251252
fprintf(stderr,"error: could not create '%s'\n", bootimg);
252253
return 1;
253254
}
254255

255-
if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
256+
if(fwrite(&hdr, 1, sizeof(hdr), fd) != sizeof(hdr)) goto fail;
256257
if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
257258

258-
if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
259+
if(fwrite(kernel_data, 1, hdr.kernel_size, fd) != hdr.kernel_size) goto fail;
259260
if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
260261

261-
if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
262+
if(fwrite(ramdisk_data, 1, hdr.ramdisk_size, fd) != hdr.ramdisk_size) goto fail;
262263
if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
263264

264265
if(second_data) {
265-
if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
266+
if(fwrite(second_data, 1, hdr.second_size, fd) != hdr.second_size) goto fail;
266267
if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
267268
}
268269

269270
return 0;
270271

271272
fail:
272273
unlink(bootimg);
273-
close(fd);
274+
fclose(fd);
274275
fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
275276
strerror(errno));
276277
return 1;

0 commit comments

Comments
 (0)