wrtvid.c revision 1.3.2.1       1 /*	$NetBSD: wrtvid.c,v 1.3.2.1 2002/01/10 19:46:40 thorpej Exp $	*/
      2 
      3 #include <sys/types.h>
      4 #include <fcntl.h>
      5 #include <unistd.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <strings.h>
      9 #define __DBINTERFACE_PRIVATE
     10 #include <db.h>
     11 #include <machine/disklabel.h>
     12 
     13 #include "loadfile.h"
     14 
     15 static void swabcfg(struct cpu_disklabel *);
     16 static void swabvid(struct cpu_disklabel *);
     17 
     18 int
     19 main(int argc, char **argv)
     20 {
     21 	struct cpu_disklabel *pcpul;
     22 	int tape_vid, tape_exe, fd;
     23 	char *filename;
     24 	char *filebuff;
     25 	char fileext[256];
     26 	u_long marks[MARK_MAX];
     27 	u_long entry;
     28 	size_t len;
     29 
     30 	if (argc == 0)
     31 		filename = "a.out";
     32 	else
     33 		filename = argv[1];
     34 
     35 	marks[MARK_START] = 0;
     36 	if ((fd = loadfile(filename, marks, COUNT_TEXT|COUNT_DATA)) == -1)
     37 		return NULL;
     38 	(void)close(fd);
     39 
     40 	len = (((marks[MARK_END] - marks[MARK_START]) + 511) / 512) * 2;
     41 	len *= 256;
     42 	filebuff = malloc(len);
     43 
     44 	entry = marks[MARK_START];
     45 	marks[MARK_START] = (u_long)(filebuff - entry);
     46 
     47 	if ((fd = loadfile(filename, marks, LOAD_TEXT|LOAD_DATA)) == -1)
     48 		return NULL;
     49 	(void)close(fd);
     50 
     51 	sprintf(fileext, "%c%cboot", filename[4], filename[5]);
     52 	tape_vid = open(fileext, O_WRONLY|O_CREAT|O_TRUNC, 0644);
     53 	sprintf(fileext, "boot%c%c", filename[4], filename[5]);
     54 	tape_exe = open(fileext, O_WRONLY|O_CREAT|O_TRUNC,0644);
     55 
     56 	pcpul = (struct cpu_disklabel *)malloc(sizeof(struct cpu_disklabel));
     57 	memset(pcpul, 0, sizeof(struct cpu_disklabel));
     58 
     59 	strcpy(pcpul->vid_id, "NBSD");
     60 
     61 	if (filename[5] == 't' ) {
     62 		pcpul->vid_oss = 1;
     63 	}else {
     64 		pcpul->vid_oss = 2;
     65 	}
     66 	pcpul->vid_osl = len / 256;
     67 	pcpul->vid_osa_u = entry >> 16;
     68 	pcpul->vid_osa_l = entry & 0xffff;
     69 	pcpul->vid_cas = 1;
     70 	pcpul->vid_cal = 1;
     71 	/* do not want to write past end of structure, not null terminated */
     72 	strncpy(pcpul->vid_mot, "MOTOROLA", 8);
     73 
     74 	if (BYTE_ORDER != BIG_ENDIAN)
     75 		swabvid(pcpul);
     76 
     77 	pcpul->cfg_rec = 0x100;
     78 	pcpul->cfg_psm = 0x200;
     79 
     80 	if (BYTE_ORDER != BIG_ENDIAN)
     81 		swabcfg(pcpul);
     82 
     83 	write(tape_vid, pcpul, sizeof(struct cpu_disklabel));
     84 
     85 	free(pcpul);
     86 
     87 	write(tape_exe, filebuff, len);
     88 	free(filebuff);
     89 
     90 	close(tape_vid);
     91 	close(tape_exe);
     92 	return (0);
     93 }
     94 
     95 static void
     96 swabvid(pcpul)
     97 	struct cpu_disklabel *pcpul;
     98 {
     99 	M_32_SWAP(pcpul->vid_oss);
    100 	M_16_SWAP(pcpul->vid_osl);
    101 	/*
    102 	M_16_SWAP(pcpul->vid_osa_u);
    103 	M_16_SWAP(pcpul->vid_osa_l);
    104 	*/
    105 	M_32_SWAP(pcpul->vid_cas);
    106 }
    107 
    108 static void
    109 swabcfg(pcpul)
    110 	struct cpu_disklabel *pcpul;
    111 {
    112 	M_16_SWAP(pcpul->cfg_atm);
    113 	M_16_SWAP(pcpul->cfg_prm);
    114 	M_16_SWAP(pcpul->cfg_atm);
    115 	M_16_SWAP(pcpul->cfg_rec);
    116 	M_16_SWAP(pcpul->cfg_trk);
    117 	M_16_SWAP(pcpul->cfg_psm);
    118 	M_16_SWAP(pcpul->cfg_shd);
    119 	M_16_SWAP(pcpul->cfg_pcom);
    120 	M_16_SWAP(pcpul->cfg_rwcc);
    121 	M_16_SWAP(pcpul->cfg_ecc);
    122 	M_16_SWAP(pcpul->cfg_eatm);
    123 	M_16_SWAP(pcpul->cfg_eprm);
    124 	M_16_SWAP(pcpul->cfg_eatw);
    125 	M_16_SWAP(pcpul->cfg_rsvc1);
    126 	M_16_SWAP(pcpul->cfg_rsvc2);
    127 }
    128