[1] | 1 | #include <stdio.h>
|
---|
| 2 | #include <stdlib.h>
|
---|
| 3 | #include <string.h>
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | unsigned char fill=0x00;
|
---|
| 7 |
|
---|
| 8 | unsigned char block[0x100];
|
---|
| 9 | int blockwhere;
|
---|
| 10 |
|
---|
| 11 | int blocktype;
|
---|
| 12 | char checksum;
|
---|
| 13 |
|
---|
| 14 | FILE *outFILE;
|
---|
| 15 |
|
---|
| 16 | static int mode_mo5=0;
|
---|
| 17 |
|
---|
| 18 | char *mo_fname="DEFAULT\0BIN";
|
---|
| 19 |
|
---|
| 20 | void
|
---|
| 21 | create_block(int type) {
|
---|
| 22 | int i;
|
---|
| 23 | for (i=0;i<16;i++) {
|
---|
| 24 | fputc(0x01,outFILE);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | fputc(0x3C,outFILE);
|
---|
| 28 | fputc(0x5A,outFILE);
|
---|
| 29 | checksum=0;
|
---|
| 30 | blockwhere=0;
|
---|
| 31 | blocktype=type;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | void
|
---|
| 35 | put_block(unsigned char c){
|
---|
| 36 | checksum+=c;
|
---|
| 37 | checksum&=0xFF;
|
---|
| 38 | block[blockwhere]=c;
|
---|
| 39 | blockwhere++;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | void
|
---|
| 43 | end_block() {
|
---|
| 44 | int i;
|
---|
| 45 | switch (blocktype) {
|
---|
| 46 | case 0x00:
|
---|
| 47 | case 0x01:
|
---|
| 48 | fputc(blocktype,outFILE);
|
---|
| 49 | fputc((blockwhere+2)&0xff,outFILE);
|
---|
| 50 | fprintf(stderr,"output a %d block type %d\n",(blockwhere+3)&0xFF,blocktype);
|
---|
| 51 | for (i=0;i<blockwhere;i++)
|
---|
| 52 | fputc(block[i],outFILE);
|
---|
| 53 | fputc((0-checksum)&0xFF,outFILE);
|
---|
| 54 | checksum+=(0-checksum)&0xFF;
|
---|
| 55 | fprintf(stderr,"sum=%d\n",checksum);
|
---|
| 56 | break;
|
---|
| 57 | case 0xff:
|
---|
| 58 | fputc(0xff,outFILE);
|
---|
| 59 | fputc(0x02,outFILE);
|
---|
| 60 | fputc(0x00,outFILE);
|
---|
| 61 | break;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | static int
|
---|
| 67 | LoadBinFile(char *fname,char *k7Fname,int start,int exec)
|
---|
| 68 | {
|
---|
| 69 | unsigned char mem[0x10000+6];
|
---|
| 70 |
|
---|
| 71 | int address;
|
---|
| 72 | int minaddr=0xffff;
|
---|
| 73 | int maxaddr=0x0;
|
---|
| 74 |
|
---|
| 75 | int offset;
|
---|
| 76 | int type;
|
---|
| 77 |
|
---|
| 78 | int i,j;
|
---|
| 79 | int sizedata;
|
---|
| 80 | int size;
|
---|
| 81 | unsigned char *data;
|
---|
| 82 | FILE *f;
|
---|
| 83 |
|
---|
| 84 | // clears mem
|
---|
| 85 | for (i=0;i<0x10000;i++) mem[i]=fill;
|
---|
| 86 |
|
---|
| 87 | f=fopen(fname,"rb");
|
---|
| 88 | if (f==NULL) {
|
---|
| 89 | fprintf(stderr,"cannot open %s",fname);
|
---|
| 90 | exit(-1);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | // un fichier est composé de plusieurs blocs...
|
---|
| 94 | while (!feof(f)) {
|
---|
| 95 |
|
---|
| 96 | type=fgetc(f);
|
---|
| 97 |
|
---|
| 98 | if (type==0) {
|
---|
| 99 | size=fgetc(f);
|
---|
| 100 | size=(size<<8)|fgetc(f);
|
---|
| 101 |
|
---|
| 102 | address=fgetc(f);
|
---|
| 103 | address=(address<<8)|fgetc(f);
|
---|
| 104 | if (minaddr>address) minaddr=address;
|
---|
| 105 |
|
---|
| 106 | fprintf(stderr,"found block type %d len %x address %x\n",
|
---|
| 107 | type,size,address);
|
---|
| 108 |
|
---|
| 109 | fread(&mem[address],1,size,f);
|
---|
| 110 | if (maxaddr<(address+size)) maxaddr=address+size;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if (type==0xff) {
|
---|
| 114 | size=fgetc(f);
|
---|
| 115 | size=(size<<8)|fgetc(f);
|
---|
| 116 |
|
---|
| 117 | if (exec==-1) {
|
---|
| 118 | exec=fgetc(f);
|
---|
| 119 | exec=(exec<<8)|fgetc(f);
|
---|
| 120 | } else {fgetc(f);fgetc(f);}
|
---|
| 121 | fprintf(stderr,"found block type %d exec %x\n",type,exec);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | if (start==-1)
|
---|
| 128 | offset=minaddr;
|
---|
| 129 | else
|
---|
| 130 | offset=start;
|
---|
| 131 |
|
---|
| 132 | data=&mem[offset];
|
---|
| 133 | sizedata=1+maxaddr-offset;
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | fprintf(stderr,"Size : 0x%x start:0x%x end:0x%x exec:0x%x\n",sizedata,offset,maxaddr,exec);
|
---|
| 137 | fclose(f);
|
---|
| 138 |
|
---|
| 139 | // we now have bin in mem from low to high...
|
---|
| 140 | outFILE=fopen(k7Fname,"wb");
|
---|
| 141 |
|
---|
| 142 | // create a header
|
---|
| 143 | create_block(0x00);
|
---|
| 144 | {
|
---|
| 145 | int i;
|
---|
| 146 | for (i=0;i<11;i++) {
|
---|
| 147 | put_block(mo_fname[i]);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | put_block(0x02); // binary
|
---|
| 152 | put_block(0x00); // binary
|
---|
| 153 | put_block(0x00); // dummy
|
---|
| 154 |
|
---|
| 155 | end_block();
|
---|
| 156 |
|
---|
| 157 | j=0;
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | create_block(0x01);
|
---|
| 161 | // type de bloc logique
|
---|
| 162 | put_block(0x00); // dummy
|
---|
| 163 | put_block((sizedata&0xFF00)>>8);
|
---|
| 164 | put_block(sizedata&0xFF);
|
---|
| 165 | // adresse de chargement
|
---|
| 166 | address=offset+i;
|
---|
| 167 | put_block((address&0xFF00)>>8);
|
---|
| 168 | put_block(address&0xFF);
|
---|
| 169 | // data
|
---|
| 170 |
|
---|
| 171 | // on ajoute le bloc de fin
|
---|
| 172 | data[sizedata++]=0xff;
|
---|
| 173 | data[sizedata++]=0x0;
|
---|
| 174 | data[sizedata++]=0x0;
|
---|
| 175 | data[sizedata++]=(exec&0xFF00)>>8;
|
---|
| 176 | data[sizedata++]=(exec&0xFF);
|
---|
| 177 |
|
---|
| 178 | while (sizedata>0) {
|
---|
| 179 | while((blockwhere<254) && (sizedata>0)) {
|
---|
| 180 | put_block(data[j++]);
|
---|
| 181 | sizedata--;
|
---|
| 182 | }
|
---|
| 183 | end_block();
|
---|
| 184 | if (sizedata>0) {
|
---|
| 185 | create_block(0x01);
|
---|
| 186 | // type de bloc logique
|
---|
| 187 | put_block(0x01);
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | // special end block
|
---|
| 193 | create_block(0xFF);
|
---|
| 194 | end_block();
|
---|
| 195 |
|
---|
| 196 | fclose(outFILE);
|
---|
| 197 | return 0;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | int
|
---|
| 201 | main(int argc, char *argv[]) {
|
---|
| 202 | char *file_in=NULL;
|
---|
| 203 | char *file_out=NULL;
|
---|
| 204 |
|
---|
| 205 | int start=-1;
|
---|
| 206 | int exec=-1;
|
---|
| 207 |
|
---|
| 208 | int argnum;
|
---|
| 209 |
|
---|
| 210 | for (argnum=1;argnum<argc;argnum++) {
|
---|
| 211 | if (strncmp(argv[argnum],"-o",2)==0) file_out=&argv[argnum][2];
|
---|
| 212 | if (strncmp(argv[argnum],"-i",2)==0) file_in=&argv[argnum][2];
|
---|
| 213 | if (strncmp(argv[argnum],"-start",6)==0)
|
---|
| 214 | sscanf(&argv[argnum][6],"%x",&start);
|
---|
| 215 | if (strncmp(argv[argnum],"-exec",5)==0)
|
---|
| 216 | sscanf(&argv[argnum][5],"%x",&exec);
|
---|
| 217 | if (strncmp(argv[argnum],"-mo5",4)==0) {
|
---|
| 218 | mode_mo5=1;
|
---|
| 219 | }
|
---|
| 220 | if (strncmp(argv[argnum],"-fname",6)==0) {
|
---|
| 221 | int i;
|
---|
| 222 | mo_fname=&argv[argnum][6];
|
---|
| 223 | /* normalize name (space converted to \0)*/
|
---|
| 224 | for (i=7;i>0;i--) {
|
---|
| 225 | if (mo_fname[i]!=' ') break;
|
---|
| 226 | if (mo_fname[i]==' ') mo_fname[i]='\0';
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | if (file_in==NULL) { fprintf(stderr,"No input file");exit(-1);};
|
---|
| 232 | if (file_out==NULL) { fprintf(stderr,"No output file");exit(-1);};
|
---|
| 233 |
|
---|
| 234 | LoadBinFile(file_in,file_out,start,exec);
|
---|
| 235 | return 0;
|
---|
| 236 |
|
---|
| 237 | }
|
---|