55 */
66
77/*
8- * This file builds a disk-image from two different files:
8+ * This file builds a disk-image from three different files:
99 *
1010 * - setup: 8086 machine code, sets up system parm
1111 * - system: 80386 code for actual system
12+ * - zoffset.h: header with ZO_* defines
1213 *
13- * It does some checking that all files are of the correct type, and
14- * just writes the result to stdout , removing headers and padding to
15- * the right amount. It also writes some system data to stderr .
14+ * It does some checking that all files are of the correct type, and writes
15+ * the result to the specified destination , removing headers and padding to
16+ * the right amount. It also writes some system data to stdout .
1617 */
1718
1819/*
@@ -136,7 +137,7 @@ static void die(const char * str, ...)
136137
137138static void usage (void )
138139{
139- die ("Usage: build setup system [ zoffset.h] [> image] " );
140+ die ("Usage: build setup system zoffset.h image" );
140141}
141142
142143#ifdef CONFIG_EFI_STUB
@@ -265,7 +266,7 @@ int main(int argc, char ** argv)
265266 int c ;
266267 u32 sys_size ;
267268 struct stat sb ;
268- FILE * file ;
269+ FILE * file , * dest ;
269270 int fd ;
270271 void * kernel ;
271272 u32 crc = 0xffffffffUL ;
@@ -280,10 +281,13 @@ int main(int argc, char ** argv)
280281 startup_64 = 0x200 ;
281282#endif
282283
283- if (argc == 4 )
284- parse_zoffset (argv [3 ]);
285- else if (argc != 3 )
284+ if (argc != 5 )
286285 usage ();
286+ parse_zoffset (argv [3 ]);
287+
288+ dest = fopen (argv [4 ], "w" );
289+ if (!dest )
290+ die ("Unable to write `%s': %m" , argv [4 ]);
287291
288292 /* Copy the setup code */
289293 file = fopen (argv [1 ], "r" );
@@ -318,7 +322,7 @@ int main(int argc, char ** argv)
318322 /* Set the default root device */
319323 put_unaligned_le16 (DEFAULT_ROOT_DEV , & buf [508 ]);
320324
321- fprintf ( stderr , "Setup is %d bytes (padded to %d bytes).\n" , c , i );
325+ printf ( "Setup is %d bytes (padded to %d bytes).\n" , c , i );
322326
323327 /* Open and stat the kernel file */
324328 fd = open (argv [2 ], O_RDONLY );
@@ -327,7 +331,7 @@ int main(int argc, char ** argv)
327331 if (fstat (fd , & sb ))
328332 die ("Unable to stat `%s': %m" , argv [2 ]);
329333 sz = sb .st_size ;
330- fprintf ( stderr , "System is %d kB\n" , (sz + 1023 )/1024 );
334+ printf ( "System is %d kB\n" , (sz + 1023 )/1024 );
331335 kernel = mmap (NULL , sz , PROT_READ , MAP_SHARED , fd , 0 );
332336 if (kernel == MAP_FAILED )
333337 die ("Unable to mmap '%s': %m" , argv [2 ]);
@@ -348,27 +352,31 @@ int main(int argc, char ** argv)
348352#endif
349353
350354 crc = partial_crc32 (buf , i , crc );
351- if (fwrite (buf , 1 , i , stdout ) != i )
355+ if (fwrite (buf , 1 , i , dest ) != i )
352356 die ("Writing setup failed" );
353357
354358 /* Copy the kernel code */
355359 crc = partial_crc32 (kernel , sz , crc );
356- if (fwrite (kernel , 1 , sz , stdout ) != sz )
360+ if (fwrite (kernel , 1 , sz , dest ) != sz )
357361 die ("Writing kernel failed" );
358362
359363 /* Add padding leaving 4 bytes for the checksum */
360364 while (sz ++ < (sys_size * 16 ) - 4 ) {
361365 crc = partial_crc32_one ('\0' , crc );
362- if (fwrite ("\0" , 1 , 1 , stdout ) != 1 )
366+ if (fwrite ("\0" , 1 , 1 , dest ) != 1 )
363367 die ("Writing padding failed" );
364368 }
365369
366370 /* Write the CRC */
367- fprintf ( stderr , "CRC %x\n" , crc );
371+ printf ( "CRC %x\n" , crc );
368372 put_unaligned_le32 (crc , buf );
369- if (fwrite (buf , 1 , 4 , stdout ) != 4 )
373+ if (fwrite (buf , 1 , 4 , dest ) != 4 )
370374 die ("Writing CRC failed" );
371375
376+ /* Catch any delayed write failures */
377+ if (fclose (dest ))
378+ die ("Writing image failed" );
379+
372380 close (fd );
373381
374382 /* Everything is OK */
0 commit comments