Home | History | Annotate | Line # | Download | only in wrtvid
wrtvid.c revision 1.1
      1 /*	$NetBSD: wrtvid.c,v 1.1 1996/05/17 19:58:55 chuck Exp $	*/
      2 
      3 #include <sys/types.h>
      4 #include <sys/stat.h>
      5 #include <fcntl.h>
      6 #include <unistd.h>
      7 #include <stdio.h>
      8 #define __DBINTERFACE_PRIVATE
      9 #include <db.h>
     10 #include <machine/disklabel.h>
     11 
     12 main(argc, argv)
     13 	int argc;
     14 	char **argv;
     15 {
     16 	struct cpu_disklabel *pcpul;
     17 	struct stat stat;
     18 	int exe_file;
     19 	int tape_vid;
     20 	int tape_exe;
     21 	unsigned int exe_addr;
     22 	unsigned short exe_addr_u;
     23 	unsigned short exe_addr_l;
     24 	char *filename;
     25 	char fileext[256];
     26 	char filebase[256];
     27 
     28 	if (argc == 0)
     29 		filename = "a.out";
     30 	else
     31 		filename = argv[1];
     32 
     33 	exe_file = open(filename, O_RDONLY,0444);
     34 	if (exe_file == -1) {
     35 		perror(filename);
     36 		exit(2);
     37 	}
     38 	sprintf(fileext, "%c%cboot", filename[4], filename[5]);
     39 	tape_vid = open(fileext, O_WRONLY|O_CREAT|O_TRUNC, 0644);
     40 	sprintf(fileext, "boot%c%c", filename[4], filename[5]);
     41 	tape_exe = open(fileext, O_WRONLY|O_CREAT|O_TRUNC,0644);
     42 
     43 	pcpul = (struct cpu_disklabel *)malloc(sizeof(struct cpu_disklabel));
     44 	bzero(pcpul, sizeof(struct cpu_disklabel));
     45 
     46 	strcpy(pcpul->vid_id, "NBSD");
     47 
     48 	fstat(exe_file, &stat);
     49 	/* size in 256 byte blocks round up after a.out header removed */
     50 
     51 	if (filename[5] == 't' ) {
     52 		pcpul->vid_oss = 1;
     53 	}else {
     54 		pcpul->vid_oss = 2;
     55 	}
     56 	pcpul->vid_osl = (((stat.st_size -0x20) +511) / 512) *2;
     57 
     58 	lseek(exe_file, 0x14, SEEK_SET);
     59 	read(exe_file, &exe_addr, 4);
     60 
     61 	/* check this, it may not work in both endian. */
     62 	{
     63 		union {
     64 			struct s {
     65 				unsigned short s1;
     66 				unsigned short s2;
     67 			} s;
     68 			unsigned long l;
     69 		} a;
     70 		a.l = exe_addr;
     71 		pcpul->vid_osa_u = a.s.s1;
     72 		pcpul->vid_osa_l = a.s.s2;
     73 
     74 	}
     75 	pcpul->vid_cas = 1;
     76 	pcpul->vid_cal = 1;
     77 	/* do not want to write past end of structure, not null terminated */
     78 	strncpy(pcpul->vid_mot, "MOTOROLA", 8);
     79 
     80 	if (BYTE_ORDER != BIG_ENDIAN)
     81 		swabvid(pcpul);
     82 
     83 	pcpul->cfg_rec = 0x100;
     84 	pcpul->cfg_psm = 0x200;
     85 
     86 	if (BYTE_ORDER != BIG_ENDIAN)
     87 		swabcfg(pcpul);
     88 
     89 	write(tape_vid, pcpul, sizeof(struct cpu_disklabel));
     90 
     91 	free(pcpul);
     92 
     93 	copy_exe(exe_file, tape_exe);
     94 	close(exe_file);
     95 	close(tape_vid);
     96 	close(tape_exe);
     97 	return (0);
     98 }
     99 
    100 #define BUF_SIZ 512
    101 copy_exe(exe_file, tape_exe)
    102 	int exe_file, tape_exe;
    103 {
    104 	char *buf;
    105 	int cnt = 0;
    106 
    107 	buf = (char *)malloc(BUF_SIZ);
    108 
    109 	lseek (exe_file, 0x20, SEEK_SET);
    110 	while (BUF_SIZ == (cnt = read(exe_file, buf, BUF_SIZ))) {
    111 		write(tape_exe, buf, cnt);
    112 	}
    113 	bzero(&buf[cnt], BUF_SIZ-cnt);
    114 	write(tape_exe, buf, BUF_SIZ);
    115 }
    116 
    117 swabvid(pcpul)
    118 	struct cpu_disklabel *pcpul;
    119 {
    120 	M_32_SWAP(pcpul->vid_oss);
    121 	M_16_SWAP(pcpul->vid_osl);
    122 	/*
    123 	M_16_SWAP(pcpul->vid_osa_u);
    124 	M_16_SWAP(pcpul->vid_osa_l);
    125 	*/
    126 	M_32_SWAP(pcpul->vid_cas);
    127 }
    128 
    129 swabcfg(pcpul)
    130 	struct cpu_disklabel *pcpul;
    131 {
    132 	M_16_SWAP(pcpul->cfg_atm);
    133 	M_16_SWAP(pcpul->cfg_prm);
    134 	M_16_SWAP(pcpul->cfg_atm);
    135 	M_16_SWAP(pcpul->cfg_rec);
    136 	M_16_SWAP(pcpul->cfg_trk);
    137 	M_16_SWAP(pcpul->cfg_psm);
    138 	M_16_SWAP(pcpul->cfg_shd);
    139 	M_16_SWAP(pcpul->cfg_pcom);
    140 	M_16_SWAP(pcpul->cfg_rwcc);
    141 	M_16_SWAP(pcpul->cfg_ecc);
    142 	M_16_SWAP(pcpul->cfg_eatm);
    143 	M_16_SWAP(pcpul->cfg_eprm);
    144 	M_16_SWAP(pcpul->cfg_eatw);
    145 	M_16_SWAP(pcpul->cfg_rsvc1);
    146 	M_16_SWAP(pcpul->cfg_rsvc2);
    147 }
    148