Guest User

Untitled

a guest
Sep 19th, 2010
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.39 KB | None | 0 0
  1. #include "boot.h"
  2.  
  3. #include "bootblock.h"
  4.  
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. static int make_floppy = 0;
  12.  
  13. void die(char *s, char *a)
  14. {
  15.     fprintf(stderr,"error: ");
  16.     fprintf(stderr,s,a);
  17.     fprintf(stderr,"\n");
  18.     exit(1);
  19. }
  20.  
  21. void *loadfile(char *file, int *size)
  22. {
  23.     printf("Loading file ...");
  24.     FILE *fp;
  25.     long lSize;
  26.     char *buffer;
  27.  
  28.     fp = fopen ( file , "rb" );
  29.     if( !fp ) perror(file),exit(1);
  30.  
  31.     fseek( fp , 0L , SEEK_END);
  32.     lSize = ftell( fp );
  33.     rewind( fp );
  34.  
  35.     /* allocate memory for entire content */
  36.     buffer = calloc( 1, lSize+1 );
  37.     if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);
  38.  
  39.     /* copy the file into the buffer */
  40.     if( 1!=fread( buffer , lSize, 1 , fp) )
  41.       fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);
  42.  
  43.     /* do your work here, buffer is a string contains the whole text */
  44.     size = (int *)lSize;
  45.     fclose(fp);
  46.     printf("Done ;-) \n");
  47.     return buffer;
  48. }
  49.  
  50. /* at location 2 is a uint16, set to blocks * 8 */
  51. int writebootblock(FILE *fp, unsigned int blocks)
  52. {
  53.     unsigned char bb[512];
  54.    
  55.     blocks *= 8;
  56.    
  57.     memcpy(bb,bootblock,512);
  58.    
  59.     bb[2] = (blocks & 0x00FF);
  60.     bb[3] = (blocks & 0xFF00) >> 8;
  61.    
  62.     fwrite(bb,512,1,fp);
  63. }
  64.  
  65. typedef struct _nvpair
  66. {
  67.     struct _nvpair *next;
  68.     char *name;
  69.     char *value;
  70. } nvpair;
  71.  
  72.  
  73. typedef struct _section
  74. {
  75.     struct _section *next;
  76.     char *name;
  77.     struct _nvpair *firstnv;
  78. } section;
  79.  
  80. void print_sections(section *first)
  81. {
  82.     nvpair *p;
  83.    
  84.     while(first){
  85.         printf("\n[%s]\n",first->name);
  86.         for(p = first->firstnv; p; p = p->next){
  87.             printf("%s=%s\n",p->name,p->value);
  88.         }
  89.         first = first->next;
  90.     }
  91. }
  92.  
  93. #ifdef xBIG_ENDIAN
  94. unsigned int fix(unsigned int x)
  95. {
  96.     int r;
  97.     unsigned char *a = (unsigned char *) &x;
  98.     unsigned char b[4];
  99.  
  100.     b[0] = a[3];
  101.     b[1] = a[2];
  102.     b[2] = a[1];
  103.     b[3] = a[0];
  104.  
  105.     r = *((unsigned int *)b);
  106.     return r;
  107.    
  108. }
  109. #else
  110. #define fix(x) (x)
  111. #endif
  112.  
  113. #define stNEWLINE 0
  114. #define stSKIPLINE 1
  115. #define stHEADER 2
  116. #define stLHS 3
  117. #define stRHS 4
  118.  
  119. section *load_ini(char *file)
  120. {
  121.     char *data,*end;
  122.     int size;
  123.     int state = stNEWLINE;
  124.     section *first, *last, *cur;
  125.     char *lhs,*rhs;
  126.    
  127.     first = last = NULL;
  128.     printf("Going to load data.\n");
  129.     if(!(data = loadfile(file,&size))){
  130.         return NULL;
  131.     }
  132.     printf("Data loaded form INI file.\n");
  133.     end = data+size;
  134.     printf("Starting to interpret.\n");
  135.     while(data < end){
  136.         printf(".");
  137.         switch(state){
  138.         case stSKIPLINE:
  139.             if(*data == '\n'){
  140.                 state = stNEWLINE;
  141.             }
  142.             data++;
  143.             break;
  144.            
  145.         case stNEWLINE:
  146.             if(*data == '\n'){
  147.                 data++;
  148.                 break;
  149.             }
  150.             if(*data == '['){
  151.                 lhs = data+1;
  152.                 state = stHEADER;
  153.                 data++;
  154.                 break;
  155.             }
  156.             if(*data == '#' || *data <= ' '){
  157.                 state = stSKIPLINE;
  158.                 data++;
  159.                 break;
  160.             }
  161.             lhs = data;
  162.             data++;
  163.             state = stLHS;
  164.             break;
  165.         case stHEADER:        
  166.             if(*data == ']'){                
  167.                 cur = (section *) malloc(sizeof(section));
  168.                 cur->name = lhs;
  169.                 cur->firstnv = NULL;
  170.                 cur->next = NULL;
  171.                 if(last){
  172.                     last->next = cur;
  173.                     last = cur;
  174.                 } else {
  175.                     last = first = cur;
  176.                 }
  177.                 *data = 0;
  178.                 state = stSKIPLINE;
  179.             }
  180.             data++;
  181.             break;
  182.         case stLHS:
  183.             if(*data == '\n'){
  184.                 state = stNEWLINE;
  185.             }
  186.             if(*data == '='){
  187.                 *data = 0;
  188.                 rhs = data+1;
  189.                 state = stRHS;
  190.             }
  191.             data++;
  192.             continue;
  193.         case stRHS:
  194.             if(*data == '\n'){
  195.                 nvpair *p = (nvpair *) malloc(sizeof(nvpair));
  196.                 p->name = lhs;
  197.                 p->value = rhs;
  198.                 *data = 0;
  199.                 p->next = cur->firstnv;
  200.                 cur->firstnv = p;
  201.                 state = stNEWLINE;
  202.             }
  203.             data++;
  204.             break;
  205.         }
  206.     }
  207.     printf("Data interpreted.\n");
  208.     return first;
  209.    
  210. }
  211.  
  212.  
  213. char *getval(section *s, char *name)
  214. {
  215.     nvpair *p;
  216.     for(p = s->firstnv; p; p = p->next){
  217.         if(!strcmp(p->name,name)) return p->value;
  218.     }
  219.     return NULL;
  220. }
  221.  
  222. char *getvaldef(section *s, char *name, char *def)
  223. {
  224.     nvpair *p;
  225.     for(p = s->firstnv; p; p = p->next){
  226.         if(!strcmp(p->name,name)) return p->value;
  227.     }
  228.     return def;
  229. }
  230.  
  231. #define centry bdir.bd_entry[c]
  232. void makeboot(section *s, char *outfile)
  233. {
  234.     printf("Starting to make boot.\n");
  235.     FILE *fp;
  236.     void *rawdata[64];
  237.     int rawsize[64];
  238.     char fill[4096];
  239.     boot_dir bdir;
  240.     int i,c;
  241.     int nextpage = 1; /* page rel offset of next loaded object */
  242.  
  243.     memset(fill,0,4096);
  244.    
  245.     memset(&bdir, 0, 4096);
  246.     for(i=0;i<64;i++){
  247.         rawdata[i] = NULL;
  248.         rawsize[i] = 0;
  249.     }
  250.  
  251.     c = 1;
  252.     printf("Creating DIR.\n");
  253.     bdir.bd_entry[0].be_type = fix(BE_TYPE_DIRECTORY);
  254.     bdir.bd_entry[0].be_size = fix(1);
  255.     bdir.bd_entry[0].be_vsize = fix(1);
  256.     rawdata[0] = (void *) &bdir;
  257.     rawsize[0] = 4096;
  258.    
  259.     strcpy(bdir.bd_entry[0].be_name,"SBBB/Directory");
  260.     printf("Dir created.\n");
  261.     while(s){
  262.         char *type =  getvaldef(s,"type","NONE");
  263.         char *file =   getval(s,"file");
  264.         int vsize = atoi(getvaldef(s,"vsize","0"));
  265.         int size;
  266.        
  267.         if(!type) die("section %s has no type",s->name);
  268.  
  269.         strncpy(centry.be_name,s->name,32);
  270.         centry.be_name[31] = 0;
  271.  
  272.         if(!file) die("section %s has no file",s->name);
  273.         if(!(rawdata[c] = loadfile(file,&rawsize[c])))
  274.            die("cannot load \"%s\"",file);
  275.        
  276.         centry.be_size = rawsize[c] / 4096 + (rawsize[c] % 4096 ? 1 : 0);
  277.         centry.be_vsize =
  278.             (vsize < centry.be_size) ? centry.be_size : vsize;
  279.  
  280.         centry.be_offset = nextpage;
  281.         nextpage += centry.be_size;
  282.  
  283.         centry.be_size = fix(centry.be_size);
  284.         centry.be_vsize = fix(centry.be_vsize);
  285.         centry.be_offset = fix(centry.be_offset);
  286.        
  287.         if(!strcmp(type,"boot")){
  288.             centry.be_type = fix(BE_TYPE_BOOTSTRAP);
  289.             centry.be_code_vaddr = fix(atoi(getvaldef(s,"vaddr","0")));
  290.             centry.be_code_ventr = fix(atoi(getvaldef(s,"ventry","0")));            
  291.         }
  292.         if(!strcmp(type,"code")){
  293.             centry.be_type = fix(BE_TYPE_CODE);
  294.             centry.be_code_vaddr = fix(atoi(getvaldef(s,"vaddr","0")));
  295.             centry.be_code_ventr = fix(atoi(getvaldef(s,"ventry","0")));
  296.         }
  297.         if(!strcmp(type,"data")){
  298.             centry.be_type = fix(BE_TYPE_DATA);
  299.         }
  300.         if(!strcmp(type,"elf32")){
  301.             centry.be_type = fix(BE_TYPE_ELF32);
  302.         }
  303.  
  304.         if(centry.be_type == BE_TYPE_NONE){
  305.             die("unrecognized section type \"%s\"",type);
  306.         }
  307.  
  308.         c++;
  309.         s = s->next;
  310.        
  311.         if(c==64) die("too many sections (>63)",NULL);
  312.     }
  313.  
  314.     if(!(fp = fopen(outfile,"w"))){
  315.         die("cannot write to \"%s\"",outfile);
  316.     }
  317.  
  318.     if(make_floppy) {
  319.         fprintf(stderr,"whoohah!");
  320.        
  321.         writebootblock(fp, nextpage+1);
  322.     }
  323.    
  324.     for(i=0;i<c;i++){
  325.         fwrite(rawdata[i],rawsize[i],1,fp);
  326.         if(rawsize[i]%4096) fwrite(fill,4096 - (rawsize[i]%4096),1,fp);
  327.     }
  328.     fclose(fp);
  329.    
  330.    
  331. }
  332.  
  333. int main(int argc, char *argv[])
  334. {
  335.     section *s;
  336.    
  337.     if(argc < 3){
  338.         fprintf(stderr,"usage: %s <inifile> <bootfile>\n",argv[0]);
  339.         return 1;
  340.     }
  341.  
  342.     if((argc > 3) && !strcmp(argv[3],"-floppy")){
  343.         make_floppy = 1;
  344.     }
  345.    
  346.     if (s = load_ini(argv[1])) {
  347.         makeboot(s,argv[2]);
  348.  
  349.     } else {
  350.         printf("Could not interpret file.\n");
  351.  
  352.     }
  353.        
  354.    
  355.     return 0;
  356.    
  357. }
Advertisement
Add Comment
Please, Sign In to add comment