diff options
author | Freya Murphy <freya@freyacat.org> | 2025-03-27 11:39:12 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-03-27 11:39:12 -0400 |
commit | 0ff301cda68669c59351e5854ce98f2cf460543f (patch) | |
tree | cfe8f976261962420ada64b821559b9da0a56841 /util/mkblob.c | |
parent | add compile_flags.txt for clangd lsp (diff) | |
download | comus-0ff301cda68669c59351e5854ce98f2cf460543f.tar.gz comus-0ff301cda68669c59351e5854ce98f2cf460543f.tar.bz2 comus-0ff301cda68669c59351e5854ce98f2cf460543f.zip |
pull upstream changes, add auto formatting
Diffstat (limited to '')
-rw-r--r-- | util/mkblob.c | 207 |
1 files changed, 102 insertions, 105 deletions
diff --git a/util/mkblob.c b/util/mkblob.c index 32c67dd..db7bfc4 100644 --- a/util/mkblob.c +++ b/util/mkblob.c @@ -5,16 +5,16 @@ ** ** Create a binary blob from a collection of ELF files. */ -#define _DEFAULT_SOURCE +#define _DEFAULT_SOURCE -#include <stdio.h> +#include <elf.h> +#include <fcntl.h> #include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <sys/stat.h> -#include <fcntl.h> -#include <elf.h> +#include <unistd.h> /* ** Blob file organization @@ -46,21 +46,21 @@ typedef struct header_s { } header_t; // length of the file name field -#define NAMELEN 20 +#define NAMELEN 20 // program descriptor typedef struct prog_s { - char name[NAMELEN]; // truncated name (15 chars) - uint32_t offset; // offset from the beginning of the blob - uint32_t size; // size of this ELF module - uint32_t flags; // miscellaneous flags + char name[NAMELEN]; // truncated name (15 chars) + uint32_t offset; // offset from the beginning of the blob + uint32_t size; // size of this ELF module + uint32_t flags; // miscellaneous flags } prog_t; // modules must be written as multiples of eight bytes -#define FL_ROUNDUP 0x00000001 +#define FL_ROUNDUP 0x00000001 // mask for mod 8 checking -#define FSIZE_MASK 0x00000007 +#define FSIZE_MASK 0x00000007 // program list entry typedef struct node_s { @@ -69,9 +69,9 @@ typedef struct node_s { struct node_s *next; } node_t; -node_t *progs, *last_prog; // list pointers -uint32_t n_progs; // number of files being copied -uint32_t offset; // current file area offset +node_t *progs, *last_prog; // list pointers +uint32_t n_progs; // number of files being copied +uint32_t offset; // current file area offset /** ** Name: process @@ -80,75 +80,74 @@ uint32_t offset; // current file area offset ** ** @param name The name of the file */ -void process( const char *name ) { +void process(const char *name) +{ struct stat info; // check the name length - if( strlen(name) >= NAMELEN ) { - fprintf( stderr, "%s: name exceeds length limit (%d)\n", - name, NAMELEN-1 ); + if (strlen(name) >= NAMELEN) { + fprintf(stderr, "%s: name exceeds length limit (%d)\n", name, + NAMELEN - 1); return; } // does it exist? - if( stat(name,&info) < 0 ) { - perror( name ); + if (stat(name, &info) < 0) { + perror(name); return; } // is it a regular file? - if( !S_ISREG(info.st_mode) ) { - fprintf( stderr, "%s: not a regular file\n", name ); + if (!S_ISREG(info.st_mode)) { + fprintf(stderr, "%s: not a regular file\n", name); return; } // open it and check the file header - int fd = open( name, O_RDONLY ); - if( fd < 0 ) { - perror( name ); + int fd = open(name, O_RDONLY); + if (fd < 0) { + perror(name); return; } // read and check the ELF header Elf32_Ehdr hdr; - int n = read( fd, &hdr, sizeof(Elf32_Ehdr) ); - close( fd ); + int n = read(fd, &hdr, sizeof(Elf32_Ehdr)); + close(fd); - if( n != sizeof(Elf32_Ehdr) ) { - fprintf( stderr, "%s: header read was short - only %d\n", name, n ); + if (n != sizeof(Elf32_Ehdr)) { + fprintf(stderr, "%s: header read was short - only %d\n", name, n); return; } - if( hdr.e_ident[EI_MAG0] != ELFMAG0 || - hdr.e_ident[EI_MAG1] != ELFMAG1 || - hdr.e_ident[EI_MAG2] != ELFMAG2 || - hdr.e_ident[EI_MAG3] != ELFMAG3 ) { - fprintf( stderr, "%s: bad ELF magic number\n", name ); + if (hdr.e_ident[EI_MAG0] != ELFMAG0 || hdr.e_ident[EI_MAG1] != ELFMAG1 || + hdr.e_ident[EI_MAG2] != ELFMAG2 || hdr.e_ident[EI_MAG3] != ELFMAG3) { + fprintf(stderr, "%s: bad ELF magic number\n", name); return; } // ok, it's a valid ELF file - create the prog list entry - prog_t *new = calloc( 1, sizeof(prog_t) ); - if( new == NULL ) { - fprintf( stderr, "%s: calloc prog returned NULL\n", name ); + prog_t *new = calloc(1, sizeof(prog_t)); + if (new == NULL) { + fprintf(stderr, "%s: calloc prog returned NULL\n", name); return; } - node_t *node = calloc( 1, sizeof(node_t) ); - if( node == NULL ) { - free( new ); - fprintf( stderr, "%s: calloc node returned NULL\n", name ); + node_t *node = calloc(1, sizeof(node_t)); + if (node == NULL) { + free(new); + fprintf(stderr, "%s: calloc node returned NULL\n", name); return; } node->data = new; - node->fullname = strdup( name ); + node->fullname = strdup(name); // copy in the name // only want the last component - const char *slash = strrchr( name, '/' ); - if( slash == NULL ) { + const char *slash = strrchr(name, '/'); + if (slash == NULL) { // only the file name slash = name; } else { @@ -156,7 +155,7 @@ void process( const char *name ) { ++slash; } - strncpy( new->name, slash, sizeof(new->name)-1 ); + strncpy(new->name, slash, sizeof(new->name) - 1); new->offset = offset; new->size = info.st_size; @@ -165,24 +164,24 @@ void process( const char *name ) { offset += info.st_size; // make sure it's a multiple of eight bytes long - if( (info.st_size & FSIZE_MASK) != 0 ) { + if ((info.st_size & FSIZE_MASK) != 0) { // nope, so we must round it up when we write it out new->flags |= FL_ROUNDUP; // increases the offset to the next file offset += 8 - (info.st_size & FSIZE_MASK); } - + // add to the list - if( progs == NULL ) { + if (progs == NULL) { // first entry progs = node; } else { // add to the end - if( last_prog == NULL ) { - fprintf( stderr, "%s: progs ! NULL, last_prog is NULL\n", name ); - free( new ); - free( node->fullname ); - free( node ); + if (last_prog == NULL) { + fprintf(stderr, "%s: progs ! NULL, last_prog is NULL\n", name); + free(new); + free(node->fullname); + free(node); return; } last_prog->next = node; @@ -198,14 +197,14 @@ void process( const char *name ) { ** @param ofd The output FILE* to be written ** @param prog Pointer to the program list entry for the file */ -void copy( FILE *ofd, node_t *node ) { - +void copy(FILE *ofd, node_t *node) +{ prog_t *prog = node->data; // open it so we can copy it - int fd = open( node->fullname, O_RDONLY ); - if( fd < 0 ) { - perror( node->fullname ); + int fd = open(node->fullname, O_RDONLY); + if (fd < 0) { + perror(node->fullname); return; } @@ -213,24 +212,22 @@ void copy( FILE *ofd, node_t *node ) { // copy it block-by-block do { - int n = read( fd, buf, 512); + int n = read(fd, buf, 512); // no bytes --> we're done - if( n < 1 ) { + if (n < 1) { break; } // copy it, and verify the copy count - int k = fwrite( buf, 1, n, ofd ); - if( k != n ) { - fprintf( stderr, "%s: write of %d returned %d\n", - prog->name, n, k ); + int k = fwrite(buf, 1, n, ofd); + if (k != n) { + fprintf(stderr, "%s: write of %d returned %d\n", prog->name, n, k); } - } while( 1 ); + } while (1); - printf( "%s: copied %d", prog->name, prog->size ); + printf("%s: copied %d", prog->name, prog->size); // do we need to round up? - if( (prog->flags & FL_ROUNDUP) != 0 ) { - + if ((prog->flags & FL_ROUNDUP) != 0) { // we'll fill with NUL bytes uint64_t filler = 0; @@ -238,87 +235,87 @@ void copy( FILE *ofd, node_t *node ) { int nbytes = 8 - (prog->size & FSIZE_MASK); // do it, and check the transfer count to be sure - int n = fwrite( &filler, 1, nbytes, ofd ); - if( n != nbytes ) { - fprintf( stderr, "%s: fill write of %d returned %d\n", - prog->name, nbytes, n ); + int n = fwrite(&filler, 1, nbytes, ofd); + if (n != nbytes) { + fprintf(stderr, "%s: fill write of %d returned %d\n", prog->name, + nbytes, n); } // report that we added some filler bytes - printf( "(+%d)", n ); + printf("(+%d)", n); } - puts( " bytes" ); + puts(" bytes"); // all done! - close( fd ); + close(fd); } -int main( int argc, char *argv[] ) { - +int main(int argc, char *argv[]) +{ // construct program list - for( int i = 1; i < argc; ++i ) { - process( argv[i] ); + for (int i = 1; i < argc; ++i) { + process(argv[i]); } - if( n_progs < 1 ) { - fputs( "Nothing to do... exiting.", stderr ); - exit( 0 ); + if (n_progs < 1) { + fputs("Nothing to do... exiting.", stderr); + exit(0); } // create the output file FILE *ofd; - ofd = fopen( "user.img", "wb" ); - if( ofd == NULL ) { - perror( "user.img" ); - exit( 1 ); + ofd = fopen("user.img", "wb"); + if (ofd == NULL) { + perror("user.img"); + exit(1); } - printf( "Processing %d ELF files\n", n_progs ); + printf("Processing %d ELF files\n", n_progs); // we need to adjust the offset values so they are relative to the // start of the blob, not relative to the start of the file area. // do this by adding the sum of the file header and program entries // to each offset field. - + uint32_t hlen = sizeof(header_t) + n_progs * sizeof(prog_t); node_t *curr = progs; - while( curr != NULL ) { + while (curr != NULL) { curr->data->offset += hlen; curr = curr->next; } // write out the blob header header_t hdr = { "BLB", n_progs }; - if( fwrite(&hdr,sizeof(header_t),1,ofd) != 1 ) { - perror( "blob header" ); - fclose( ofd ); - exit( 1 ); + if (fwrite(&hdr, sizeof(header_t), 1, ofd) != 1) { + perror("blob header"); + fclose(ofd); + exit(1); } // next, the program entries curr = progs; - while( curr != NULL ) { - if( fwrite(curr->data,sizeof(prog_t),1,ofd) != 1 ) { - perror( "blob prog entry write" ); - fclose( ofd ); - exit( 1 ); + while (curr != NULL) { + if (fwrite(curr->data, sizeof(prog_t), 1, ofd) != 1) { + perror("blob prog entry write"); + fclose(ofd); + exit(1); } curr = curr->next; } // finally, copy the files curr = progs; - while( curr != NULL ) { + while (curr != NULL) { prog_t *prog = curr->data; - copy( ofd, curr ); + copy(ofd, curr); node_t *tmp = curr; curr = curr->next; - free( tmp->data ); - free( tmp->fullname ); - free( tmp ); + free(tmp->data); + free(tmp->fullname); + free(tmp); } - fclose( ofd ); + fclose(ofd); return 0; } |