This page was generated from a source code file. Click here to download the file this page was generated from.
| 1 | #include <stdio.h> | 
| 2 | #include <stdlib.h> | 
| 3 | #include <string.h> | 
| 4 | int main( int argc, char* argv ) { | 
| 5 | int retval = 0; | 
| 6 | FILE* bios_in = NULL; | 
| 7 | FILE* bios_out = NULL; | 
| 8 | size_t pg_sz = 0; | 
| 9 | size_t rom_sz = 0; | 
| 10 | size_t in_sz = 0; | 
| 11 | size_t pg_writ_sz = 0; | 
| 12 | size_t iter_r_sz = 0; | 
| 13 | size_t iter_w_sz = 0; | 
| 14 | size_t file_writ_sz = 0; | 
| 15 | char* bios_in_c = argv1; | 
| 16 | char buffer1024; | 
Make sure command line makes sense.
| 17 | if( 5 != argc ) { | 
| 18 | printf( "usage: pad <bios_in> <bios_out> <pg_sz_kb> <rom_sz_kb>\n" ); | 
| 19 | retval = 1; | 
| 20 | goto cleanup; | 
| 21 | } | 
Open input and output ROM files.
| 22 | bios_in = fopen( bios_in_c, "rb" ); | 
| 23 | bios_out = fopen( argv2, "wb" ); | 
Get page and output ROM size from the command line, converting from KB to bytes.
| 24 | pg_sz = atoi( argv3 ) * 1024; | 
| 25 | rom_sz = atoi( argv4 ) * 1024; | 
Get the input ROM size from the input ROM file, in bytes.
| 26 | fseek( bios_in, 0, SEEK_END ); | 
| 27 | in_sz = ftell( bios_in ); | 
| 28 | fseek( bios_in, 0, SEEK_SET ); | 
Status update.
| 29 | printf( "bios_in \"%s\" sz: %lu\n", bios_in_c, in_sz ); | 
| 30 | if( in_sz < pg_sz ) { | 
| 31 | printf( "pages will be padded to %lu\n", pg_sz ); | 
| 32 | } | 
Iterate pages until the output ROM file matches the size specified on the command line.
| 33 | while( file_writ_sz < rom_sz ) { | 
Iterate bytes from the input ROM and then zero-padding until the current position in the output ROM matches a page size.
| 34 | pg_writ_sz = 0; | 
| 35 | while( pg_writ_sz < pg_sz ) { | 
Determine if there are still bytes to read from the input ROM, or if we should switch to zero-padding.
| 36 | if( pg_writ_sz < in_sz ) { | 
| 37 | iter_r_sz = fread( buffer, 1, 1024, bios_in ); | 
| 38 | printf( "copying %lu bytes to page...\n", iter_r_sz ); | 
| 39 | } else { | 
| 40 | iter_r_sz = 1024; | 
| 41 | printf( "padding %lu bytes to page...\n", iter_r_sz ); | 
| 42 | memset( buffer, '\0', iter_r_sz ); | 
| 43 | } | 
Write read bytes or zero padding to output ROM file.
| 44 | iter_w_sz = fwrite( buffer, 1, iter_r_sz, bios_out ); | 
| 45 | if( iter_w_sz < iter_r_sz ) { | 
| 46 | printf( "could not write buffer!\n" ); | 
| 47 | retval = 2; | 
| 48 | goto cleanup; | 
| 49 | } | 
| 50 | pg_writ_sz += iter_w_sz; | 
| 51 | } | 
Bump up file written size by this page's written size.
| 52 | file_writ_sz += pg_writ_sz; | 
| 53 | fseek( bios_in, 0, SEEK_SET ); | 
| 54 | printf( "wrote %lu bytes to file...\n", file_writ_sz ); | 
| 55 | } | 
Close remaining open ROM files.
| 56 | cleanup: | 
| 57 | if( NULL != bios_in ) { | 
| 58 | fclose( bios_in ); | 
| 59 | } | 
| 60 | if( NULL != bios_out ) { | 
| 61 | fclose( bios_out ); | 
| 62 | } | 
| 63 | return retval; | 
| 64 | } |