Esempio n. 1
0
void makeboot(section *s, char *outfile)
{
    FILE *fp;
    void *rawdata[64];
    int rawsize[64];
    char fill[4096];
    boot_dir bdir;
    int i,c;
    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;
    }

    c = 1;

    bdir.bd_entry[0].be_type = fix(BE_TYPE_DIRECTORY);
    bdir.bd_entry[0].be_size = fix(1);
    bdir.bd_entry[0].be_vsize = fix(1);
    rawdata[0] = (void *) &bdir;
    rawsize[0] = 4096;
    
    strcpy(bdir.bd_entry[0].be_name,"SBBB/Directory");

    while(s){
        char *type = getvaldef(s,"type","NONE");
        char *file = getval(s,"file");
        int vsize = atoi(getvaldef(s,"vsize","0"));
        int size;
        
        if(!type) die("section %s has no type",s->name);

        strncpy(centry.be_name,s->name,32);
        centry.be_name[31] = 0;

        if(!file) die("section %s has no file",s->name);
        if(!(rawdata[c] = loadfile(file,&rawsize[c])))
           die("cannot load \"%s\"",file);
        
        centry.be_size = rawsize[c] / 4096 + (rawsize[c] % 4096 ? 1 : 0);
        centry.be_vsize = 
            (vsize < centry.be_size) ? centry.be_size : vsize;

        centry.be_offset = nextpage;
        nextpage += centry.be_size;

        centry.be_size = fix(centry.be_size);
        centry.be_vsize = fix(centry.be_vsize);
        centry.be_offset = fix(centry.be_offset);
        
        if(!strcmp(type,"boot")){
            centry.be_type = fix(BE_TYPE_BOOTSTRAP);
            centry.be_code_vaddr = fix(atoi(getvaldef(s,"vaddr","0")));
            centry.be_code_ventr = fix(atoi(getvaldef(s,"ventry","0")));            
        }
        if(!strcmp(type,"code")){
            centry.be_type = fix(BE_TYPE_CODE);
            centry.be_code_vaddr = fix(atoi(getvaldef(s,"vaddr","0")));
            centry.be_code_ventr = fix(atoi(getvaldef(s,"ventry","0"))); 
        }
        if(!strcmp(type,"data")){
            centry.be_type = fix(BE_TYPE_DATA);
        }
        if(!strcmp(type,"elf32")){
            centry.be_type = fix(BE_TYPE_ELF32);
        }

        if(centry.be_type == BE_TYPE_NONE){
            die("unrecognized section type \"%s\"",type);
        }

        c++;
        s = s->next;
        
        if(c==64) die("too many sections (>63)",NULL);
    }

    if(!(fp = fopen(outfile,"w"))){
        die("cannot write to \"%s\"",outfile);
    }

    if(make_floppy) {
        fprintf(stderr,"whoohah!");
        
        writebootblock(fp, nextpage+1);
    }
    
    for(i=0;i<c;i++){
        fwrite(rawdata[i],rawsize[i],1,fp);
        if(rawsize[i]%4096) fwrite(fill,4096 - (rawsize[i]%4096),1,fp);
    }
    fclose(fp);
    
    
}
Esempio n. 2
0
void makeboot(section *s, char *outfile)
{
    int fd;
    void *rawdata[BOOTDIR_MAX_ENTRIES];
    int rawsize[BOOTDIR_MAX_ENTRIES];
    char fill[4096];
    boot_dir bdir;
    int i,c;
    int nextpage = 0; /* page rel offset of next loaded object */
	long outlen = 0;

    memset(fill,0,4096);

    memset(&bdir, 0, sizeof(bdir));
    for(i=0;i<BOOTDIR_MAX_ENTRIES;i++){
        rawdata[i] = NULL;
        rawsize[i] = 0;
    }

    c = 1;

    bdir.bd_entry[0].be_type = fix(BE_TYPE_DIRECTORY);
    bdir.bd_entry[0].be_size = fix(sizeof(bdir)/4096);
    bdir.bd_entry[0].be_vsize = fix(sizeof(bdir));
    rawdata[0] = (void *) &bdir;
    rawsize[0] = sizeof(bdir);
	nextpage += sizeof(bdir)/4096;

    strcpy(bdir.bd_entry[0].be_name,"SBBB/Directory");
	printf("directory size %d\n", rawsize[0]);

    while(s){
        char *type = getvaldef(s,"type","NONE");
        char *file = getval(s,"file");

        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(!strcmp(type, "elf32") || !strcmp(type, "elf64"))
        	rawdata[c] = loadstripfile(file,&rawsize[c]);
        else if(!strcmp(type, "text"))
        	rawdata[c] = loadtextfile(file,&rawsize[c]);
        else
        	rawdata[c] = loadfile(file,&rawsize[c]);

        if(!rawdata[c])
           die("cannot load \"%s\"",file);

        centry.be_size = rawsize[c] / 4096 + (rawsize[c] % 4096 ? 1 : 0);
		centry.be_vsize = rawsize[c];

        centry.be_offset = nextpage;
        nextpage += centry.be_size;

        centry.be_size = fix(centry.be_size);
        centry.be_vsize = fix(centry.be_vsize);
        centry.be_offset = fix(centry.be_offset);

        if(!strcmp(type,"boot")){
            centry.be_type = fix(BE_TYPE_BOOTSTRAP);
            centry.be_code_vaddr = fix(atoi(getvaldef(s,"vaddr","0")));
            centry.be_code_ventr = fix(atoi(getvaldef(s,"ventry","0")));
        }
        if(!strcmp(type,"code")){
            centry.be_type = fix(BE_TYPE_CODE);
            centry.be_code_vaddr = fix(atoi(getvaldef(s,"vaddr","0")));
            centry.be_code_ventr = fix(atoi(getvaldef(s,"ventry","0")));
        }
        if(!strcmp(type,"data") || !strcmp(type,"text")){
            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(elf32_find_entry(rawdata[c], rawsize[c]));
        }
        if(!strcmp(type,"elf64")){
            centry.be_type = fix(BE_TYPE_ELF64);
            centry.be_code_vaddr = 0;
            centry.be_code_ventr = fix(elf64_find_entry(rawdata[c], rawsize[c]));
        }

        if(centry.be_type == BE_TYPE_NONE){
            die("unrecognized section type \"%s\"",type);
        }

		printf(" %8s %8d %s\n", type, fix(centry.be_vsize), centry.be_name);
		
        c++;
        s = s->next;

        if(c==BOOTDIR_MAX_ENTRIES) die("too many sections (>128)",NULL);
    }

    if((fd = open(outfile, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
        die("cannot write to \"%s\"",outfile);
    }

    if(make_sparcboot) {
        writesparcbootblock(fd, nextpage+1);
    }

    for(i=0;i<c;i++){
        write(fd, rawdata[i], rawsize[i]);
		outlen += rawsize[i];
        if(rawsize[i]%4096) {
            write(fd, fill, 4096 - (rawsize[i]%4096));
			outlen += 4096 - (rawsize[i]%4096);
		}
    }
    close(fd);
	printf("wrote %ld bytes to output file %s\n", outlen, outfile);
}