The bootmaker now defaults to the endian of the host platform.

Fixed style issues.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2402 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2003-01-10 11:48:38 +00:00
parent d9f08d808d
commit 1ff1332c77

View File

@ -38,6 +38,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef sparc
# define xBIG_ENDIAN 1
@ -128,7 +129,12 @@ struct Elf32_Phdr {
#define LE 0
#define BE 1
// define host endian as default for the target environment
#if xBIG_ENDIAN
static int target_endian = BE;
#else
static int target_endian = LE;
#endif
#define fix(x) ((target_endian == BE) ? HOST_TO_BENDIAN32(x) : HOST_TO_LENDIAN32(x))
@ -136,7 +142,9 @@ static int make_sparcboot = 0;
static int strip_debug = 0;
static char *strip_binary = "strip";
void die(char *s, char *a)
void
die(char *s, char *a)
{
fprintf(stderr,"error: ");
fprintf(stderr,s,a);
@ -144,7 +152,9 @@ void die(char *s, char *a)
exit(1);
}
void *loadfile(char *file, int *size)
void *
loadfile(char *file, int *size)
{
int fd;
char *data;
@ -170,13 +180,14 @@ void *loadfile(char *file, int *size)
return NULL;
}
void *loadstripfile(char *file, int *size)
void *
loadstripfile(char *file, int *size)
{
char temp[256];
char cmd[4096];
void *retval;
if (strip_debug) {
strcpy(temp, "/tmp/mkboot.XXXXXXXX");
mktemp(temp);
@ -193,11 +204,13 @@ void *loadstripfile(char *file, int *size)
return retval;
}
// write a boot block to the head of the dir.
// note: the first 0x20 bytes are removed by the sparc prom
// which makes the whole file off by 0x20 bytes
/*
int writesparcbootblock(int fd, unsigned int blocks)
int
writesparcbootblock(int fd, unsigned int blocks)
{
unsigned char bb[0x200+0x20];
@ -223,12 +236,15 @@ typedef struct _section
struct _nvpair *firstnv;
} section;
void print_sections(section *first)
void
print_sections(section *first)
{
nvpair *p;
while (first) {
printf("\n[%s]\n", first->name);
for (p = first->firstnv; p; p = p->next) {
printf("%s=%s\n", p->name, p->value);
}
@ -236,6 +252,7 @@ void print_sections(section *first)
}
}
#define stNEWLINE 0
#define stSKIPLINE 1
#define stHEADER 2
@ -245,26 +262,26 @@ void print_sections(section *first)
section *first = NULL;
section *last = NULL;
section *load_ini(char *file)
section *
load_ini(char *file)
{
char *data, *end;
int size;
int state = stNEWLINE;
section *cur;
char *lhs, *rhs;
if(!(data = loadfile(file,&size))){
if (!(data = loadfile(file, &size)))
return NULL;
}
end = data+size;
while (data < end) {
switch (state) {
case stSKIPLINE:
if(*data == '\n' || *data == '\r'){
if (*data == '\n' || *data == '\r')
state = stNEWLINE;
}
data++;
break;
@ -273,12 +290,14 @@ section *load_ini(char *file)
data++;
break;
}
if (*data == '[') {
lhs = data+1;
state = stHEADER;
data++;
break;
}
if (*data == '#' || *data <= ' ') {
state = stSKIPLINE;
data++;
@ -306,9 +325,9 @@ section *load_ini(char *file)
data++;
break;
case stLHS:
if(*data == '\n' || *data == '\r'){
if (*data == '\n' || *data == '\r')
state = stNEWLINE;
}
if (*data == '=') {
*data = 0;
rhs = data+1;
@ -331,29 +350,35 @@ section *load_ini(char *file)
}
}
return first;
}
char *getval(section *s, char *name)
char *
getval(section *s, char *name)
{
nvpair *p;
for (p = s->firstnv; p; p = p->next) {
if(!strcmp(p->name,name)) return p->value;
if (!strcmp(p->name, name))
return p->value;
}
return NULL;
}
char *getvaldef(section *s, char *name, char *def)
char *
getvaldef(section *s, char *name, char *def)
{
nvpair *p;
for (p = s->firstnv; p; p = p->next) {
if(!strcmp(p->name,name)) return p->value;
if (!strcmp(p->name, name))
return p->value;
}
return def;
}
Elf32_Addr elf_find_entry(void *buf, int size)
Elf32_Addr
elf_find_entry(void *buf, int size)
{
struct Elf32_Ehdr *header;
struct Elf32_Phdr *pheader;
@ -389,7 +414,8 @@ Elf32_Addr elf_find_entry(void *buf, int size)
#undef SWAPIT
#define centry bdir.bd_entry[c]
void makeboot(section *s, char *outfile)
void
makeboot(section *s, char *outfile)
{
int fd;
void *rawdata[64];
@ -400,8 +426,8 @@ void makeboot(section *s, char *outfile)
int nextpage = 1; /* page rel offset of next loaded object */
memset(fill, 0, 4096);
memset(&bdir, 0, 4096);
for (i = 0; i < 64; i++) {
rawdata[i] = NULL;
rawsize[i] = 0;
@ -424,12 +450,15 @@ void makeboot(section *s, char *outfile)
int size;
struct stat statbuf;
if(!type) die("section %s has no type",s->name);
if (!type)
die("section %s has no type", s->name);
strncpy(centry.be_name,s->name, BOOTDIR_NAMELEN);
centry.be_name[BOOTDIR_NAMELEN - 1] = 0;
if(!file) die("section %s has no file",s->name);
if (!file)
die("section %s has no file", s->name);
rawdata[c] = ((strcmp(type, "elf32")==0) ? loadstripfile : loadfile)(file,&rawsize[c]);
if (!rawdata[c])
die("cannot load \"%s\"",file);
@ -439,8 +468,7 @@ void makeboot(section *s, char *outfile)
vsize = statbuf.st_size;
centry.be_size = rawsize[c] / 4096 + (rawsize[c] % 4096 ? 1 : 0);
centry.be_vsize =
(vsize < centry.be_size) ? centry.be_size : vsize;
centry.be_vsize = (vsize < centry.be_size) ? centry.be_size : vsize;
centry.be_offset = nextpage;
nextpage += centry.be_size;
@ -459,33 +487,31 @@ void makeboot(section *s, char *outfile)
centry.be_code_vaddr = fix(atoi(getvaldef(s, "vaddr", "0")));
centry.be_code_ventr = fix(atoi(getvaldef(s, "ventry", "0")));
}
if(!strcmp(type,"data")){
if (!strcmp(type, "data"))
centry.be_type = fix(BE_TYPE_DATA);
}
if (!strcmp(type, "elf32")) {
centry.be_type = fix(BE_TYPE_ELF32);
centry.be_code_vaddr = 0;
centry.be_code_ventr = fix(elf_find_entry(rawdata[c], rawsize[c]));
}
if(centry.be_type == BE_TYPE_NONE){
if (centry.be_type == BE_TYPE_NONE)
die("unrecognized section type \"%s\"", type);
}
c++;
s = s->next;
if(c == BOOTDIR_MAX_ENTRIES) die("too many sections (>63)",NULL);
if (c == BOOTDIR_MAX_ENTRIES)
die("too many sections (>63)",NULL);
}
if((fd = open(outfile, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
if ((fd = open(outfile, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
die("cannot write to \"%s\"",outfile);
}
/* XXX - Hope this isn't needed :(
if(make_sparcboot) {
if (make_sparcboot)
writesparcbootblock(fd, nextpage + 1);
}
*/
for (i = 0; i < c; i++) {
write(fd, rawdata[i], rawsize[i]);
@ -495,16 +521,38 @@ void makeboot(section *s, char *outfile)
close(fd);
}
int main(int argc, char **argv)
void
usage(char *name)
{
char *programName = strrchr(name, '/');
if (programName == NULL)
programName = name;
else
programName++;
fprintf(stderr,
"usage: %s [--littleendian] [--bigendian ] [ --strip-binary <binary ] [ --strip-debug] [ --sparc | -s ] [ <inifile> ... ] -o <bootfile>\n"
"\tdefaults to "
#if xBIG_ENDIAN
"\"big-endian\""
#else
"\"little-endian\""
#endif
" on this platform\n", programName);
exit(1);
}
int
main(int argc, char **argv)
{
char *file = NULL;
char *programName = argv[0];
section *s;
if(argc < 2){
usage:
fprintf(stderr,"usage: %s [--littleendian (default)] [--bigendian ] [ --strip-binary <binary ] [ --strip-debug] [ --sparc | -s ] [ <inifile> ... ] -o <bootfile>\n",argv[0]);
return 1;
}
if (argc < 2)
usage(programName);
argc--;
argv++;
@ -517,43 +565,38 @@ usage:
} else if (!strcmp(*argv,"-o")) {
argc--;
argv++;
if(argc) {
if (argc)
file = *argv;
} else {
goto usage;
}
else
usage(programName);
} else if (!strcmp(*argv, "--strip-binary")) {
argc--;
argv++;
if(argc) {
if (argc)
strip_binary = *argv;
} else {
goto usage;
}
else
usage(programName);
} else if (!strcmp(*argv, "--strip-debug")) {
strip_debug = 1;
} else {
if(load_ini(*argv) == NULL) {
if (load_ini(*argv) == NULL)
fprintf(stderr, "warning: cannot load '%s'\n", *argv);
}
}
argc--;
argv++;
}
if((argc > 3) && !strcmp(argv[3],"-sparc")){
if ((argc > 3) && !strcmp(argv[3], "-sparc"))
make_sparcboot = 1;
}
if (!file){
fprintf(stderr,"error: no output specified\n");
goto usage;
usage(programName);
}
if (!first){
fprintf(stderr, "error: no data to write?!\n");
goto usage;
usage(programName);
}
makeboot(first, file);