wrtvid.c revision 1.2 1 /* $NetBSD: wrtvid.c,v 1.2 2000/12/04 21:24:34 scw 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 size_t len;
28
29 if (argc == 0)
30 filename = "a.out";
31 else
32 filename = argv[1];
33
34 marks[MARK_START] = 0;
35 if ((fd = loadfile(filename, marks, COUNT_TEXT|COUNT_DATA)) == -1)
36 return NULL;
37 (void)close(fd);
38
39 len = (((marks[MARK_END] - marks[MARK_START]) + 511) / 512) * 2;
40 len *= 256;
41 filebuff = malloc(len);
42
43 marks[MARK_START] = (u_long)(filebuff - marks[MARK_START]);
44
45 if ((fd = loadfile(filename, marks, LOAD_TEXT|LOAD_DATA)) == -1)
46 return NULL;
47 (void)close(fd);
48
49 sprintf(fileext, "%c%cboot", filename[4], filename[5]);
50 tape_vid = open(fileext, O_WRONLY|O_CREAT|O_TRUNC, 0644);
51 sprintf(fileext, "boot%c%c", filename[4], filename[5]);
52 tape_exe = open(fileext, O_WRONLY|O_CREAT|O_TRUNC,0644);
53
54 pcpul = (struct cpu_disklabel *)malloc(sizeof(struct cpu_disklabel));
55 bzero(pcpul, sizeof(struct cpu_disklabel));
56
57 strcpy(pcpul->vid_id, "NBSD");
58
59 if (filename[5] == 't' ) {
60 pcpul->vid_oss = 1;
61 }else {
62 pcpul->vid_oss = 2;
63 }
64 pcpul->vid_osl = len / 256;
65
66 /* check this, it may not work in both endian. */
67 {
68 union {
69 struct s {
70 unsigned short s1;
71 unsigned short s2;
72 } s;
73 unsigned long l;
74 } a;
75 a.l = marks[MARK_ENTRY];
76 pcpul->vid_osa_u = a.s.s1;
77 pcpul->vid_osa_l = a.s.s2;
78
79 }
80 pcpul->vid_cas = 1;
81 pcpul->vid_cal = 1;
82 /* do not want to write past end of structure, not null terminated */
83 strncpy(pcpul->vid_mot, "MOTOROLA", 8);
84
85 if (BYTE_ORDER != BIG_ENDIAN)
86 swabvid(pcpul);
87
88 pcpul->cfg_rec = 0x100;
89 pcpul->cfg_psm = 0x200;
90
91 if (BYTE_ORDER != BIG_ENDIAN)
92 swabcfg(pcpul);
93
94 write(tape_vid, pcpul, sizeof(struct cpu_disklabel));
95
96 free(pcpul);
97
98 write(tape_exe, filebuff, len);
99 free(filebuff);
100
101 close(tape_vid);
102 close(tape_exe);
103 return (0);
104 }
105
106 static void
107 swabvid(pcpul)
108 struct cpu_disklabel *pcpul;
109 {
110 M_32_SWAP(pcpul->vid_oss);
111 M_16_SWAP(pcpul->vid_osl);
112 /*
113 M_16_SWAP(pcpul->vid_osa_u);
114 M_16_SWAP(pcpul->vid_osa_l);
115 */
116 M_32_SWAP(pcpul->vid_cas);
117 }
118
119 static void
120 swabcfg(pcpul)
121 struct cpu_disklabel *pcpul;
122 {
123 M_16_SWAP(pcpul->cfg_atm);
124 M_16_SWAP(pcpul->cfg_prm);
125 M_16_SWAP(pcpul->cfg_atm);
126 M_16_SWAP(pcpul->cfg_rec);
127 M_16_SWAP(pcpul->cfg_trk);
128 M_16_SWAP(pcpul->cfg_psm);
129 M_16_SWAP(pcpul->cfg_shd);
130 M_16_SWAP(pcpul->cfg_pcom);
131 M_16_SWAP(pcpul->cfg_rwcc);
132 M_16_SWAP(pcpul->cfg_ecc);
133 M_16_SWAP(pcpul->cfg_eatm);
134 M_16_SWAP(pcpul->cfg_eprm);
135 M_16_SWAP(pcpul->cfg_eatw);
136 M_16_SWAP(pcpul->cfg_rsvc1);
137 M_16_SWAP(pcpul->cfg_rsvc2);
138 }
139