openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

makerom.c (2958B)


      1 /* $Id: //depot/blt/netboot/makerom.c#4 $
      2 **
      3 ** Copyright 1998 Brian J. Swetland
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #include <stdio.h>
     30 #include <fcntl.h>
     31 #include <unistd.h>
     32 
     33 #define ROMSIZE 0x4000
     34 
     35 unsigned char rom[ROMSIZE];
     36 unsigned int sum;
     37 
     38 void makehex(char *n)
     39 {
     40     FILE *fp;
     41     int chk,chk2;
     42     int l,i;
     43     
     44     if(fp = fopen(n,"w")){
     45         for(l=0;l<ROMSIZE;l+=32) {
     46             fprintf(fp,":%02X%04X00",32,l);
     47 
     48             chk = 32;
     49 
     50             chk += ((l & 0xFF00) >> 8);
     51             chk += ((l & 0x00FF));
     52             
     53             for(i=0;i<32;i++){
     54                 fprintf(fp,"%02X",rom[l+i]);
     55                 chk += rom[l+i];
     56             }
     57             chk &= 0xFF;
     58             chk2 = 0x100 - chk;
     59             
     60             fprintf(fp,"%02X\r\n",(chk2 & 0xFF));
     61         }
     62         fprintf(fp,":00000001FF\r\n");
     63         
     64         fclose(fp);
     65     }
     66 }
     67 
     68 
     69 int main(int argc, char *argv[])
     70 {
     71 	int i, fd;
     72 	if (argc < 2) {
     73 		fprintf(stderr,"usage: %s rom-file hex-file\n",argv[0]);
     74 		exit(2);
     75 	}
     76 	if ((fd = open(argv[1], O_RDWR)) < 0) {
     77 		perror("unable to open file");
     78 		return 1;
     79 	}
     80 	bzero(rom, ROMSIZE);
     81 	
     82 	if (read(fd, rom, ROMSIZE) < 0) {
     83 		perror("read error");
     84 		return 1;
     85 	}
     86 
     87 	/* store size in ROM header */	
     88 	rom[2] = ROMSIZE / 512;
     89 
     90 	/* store size in PCI ROM header */
     91 	rom[0x18 + 0x10 + 4] = ROMSIZE / 512;
     92 
     93 	rom[5] = 0;
     94 	for (i=0,sum=0; i<ROMSIZE; i++) sum += rom[i];
     95 	rom[5] = -sum;
     96 	
     97 	for (i=0,sum=0; i<ROMSIZE; i++) sum += rom[i];
     98 	if (sum & 0x00FF) printf("checksum fails.\n");
     99 	
    100 	close(fd);
    101 	
    102 	makehex(argv[2]);
    103         
    104 	return 0;
    105 }