@@ -29,28 +29,29 @@ static void *load_file(const char *fn, unsigned *_sz)
29
29
{
30
30
char * data ;
31
31
int sz ;
32
- int fd ;
32
+ FILE * fd ;
33
33
34
34
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 ;
37
37
38
- sz = lseek (fd , 0 , SEEK_END );
38
+ if (fseek (fd , 0 , SEEK_END ) != 0 ) goto oops ;
39
+ sz = ftell (fd );
39
40
if (sz < 0 ) goto oops ;
40
41
41
- if (lseek (fd , 0 , SEEK_SET ) != 0 ) goto oops ;
42
+ if (fseek (fd , 0 , SEEK_SET ) != 0 ) goto oops ;
42
43
43
44
data = (char * ) malloc (sz );
44
45
if (data == 0 ) goto oops ;
45
46
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 );
48
49
49
50
if (_sz ) * _sz = sz ;
50
51
return data ;
51
52
52
53
oops :
53
- close (fd );
54
+ fclose (fd );
54
55
if (data != 0 ) free (data );
55
56
return 0 ;
56
57
}
@@ -74,7 +75,7 @@ int usage(void)
74
75
75
76
static unsigned char padding [16384 ] = { 0 , };
76
77
77
- int write_padding (int fd , unsigned pagesize , unsigned itemsize )
78
+ int write_padding (FILE * fd , unsigned pagesize , unsigned itemsize )
78
79
{
79
80
unsigned pagemask = pagesize - 1 ;
80
81
unsigned count ;
@@ -85,7 +86,7 @@ int write_padding(int fd, unsigned pagesize, unsigned itemsize)
85
86
86
87
count = pagesize - (itemsize & pagemask );
87
88
88
- if (write ( fd , padding , count ) != count ) {
89
+ if (fwrite ( padding , 1 , count , fd ) != count ) {
89
90
return -1 ;
90
91
} else {
91
92
return 0 ;
@@ -106,7 +107,7 @@ int main(int argc, char **argv)
106
107
char * bootimg = 0 ;
107
108
char * board = "" ;
108
109
unsigned pagesize = 2048 ;
109
- int fd ;
110
+ FILE * fd ;
110
111
SHA_CTX ctx ;
111
112
uint8_t * sha ;
112
113
unsigned base = 0x10000000 ;
@@ -246,31 +247,31 @@ int main(int argc, char **argv)
246
247
memcpy (hdr .id , sha ,
247
248
SHA_DIGEST_SIZE > sizeof (hdr .id ) ? sizeof (hdr .id ) : SHA_DIGEST_SIZE );
248
249
249
- fd = open (bootimg , O_CREAT | O_TRUNC | O_WRONLY , 0644 );
250
- if (fd < 0 ) {
250
+ fd = fopen (bootimg , "wb" );
251
+ if (fd == 0 ) {
251
252
fprintf (stderr ,"error: could not create '%s'\n" , bootimg );
252
253
return 1 ;
253
254
}
254
255
255
- if (write ( fd , & hdr , sizeof (hdr )) != sizeof (hdr )) goto fail ;
256
+ if (fwrite ( & hdr , 1 , sizeof (hdr ), fd ) != sizeof (hdr )) goto fail ;
256
257
if (write_padding (fd , pagesize , sizeof (hdr ))) goto fail ;
257
258
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 ;
259
260
if (write_padding (fd , pagesize , hdr .kernel_size )) goto fail ;
260
261
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 ;
262
263
if (write_padding (fd , pagesize , hdr .ramdisk_size )) goto fail ;
263
264
264
265
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 ;
266
267
if (write_padding (fd , pagesize , hdr .ramdisk_size )) goto fail ;
267
268
}
268
269
269
270
return 0 ;
270
271
271
272
fail :
272
273
unlink (bootimg );
273
- close (fd );
274
+ fclose (fd );
274
275
fprintf (stderr ,"error: failed writing '%s': %s\n" , bootimg ,
275
276
strerror (errno ));
276
277
return 1 ;
0 commit comments