]> pd.if.org Git - mmurtl/blob - mscode16/bootcode/makeimg.c
autocommit for file dated 1994-12-31 09:58:38
[mmurtl] / mscode16 / bootcode / makeimg.c
1 /* MakeImg.c\r
2    This turns a MMURTL operating system run file\r
3    into an image just as it will appear in memory\r
4    after it is loaded. The MMURTL OS loads at address 0\r
5    and is assembled with that as the virtual address\r
6    so no fixups are required. The data segment is first,\r
7    then there will be padding up to the code segment\r
8    which will begin at address 64K Linear (65536 or FFF:0\r
9    segmented).\r
10 \r
11    THIS MUST BE COMPILED WITH THE LARGE MODEL (Borland or Turbo C)\r
12    (e.g., BCC -ml MAKEIMG.C)\r
13 */\r
14 \r
15 #define U32 unsigned long\r
16 #define S32 long\r
17 #define U16 unsigned int\r
18 #define S16 int\r
19 #define U8 unsigned char\r
20 #define S8 char\r
21 \r
22 #define TRUE 1\r
23 #define FALSE 1\r
24 \r
25 #include <ctype.h>\r
26 #include <stdio.h>\r
27 #include <string.h>\r
28 #include <stdlib.h>\r
29 #include <dos.h>\r
30 #include "runfile.h"\r
31 \r
32 U32 fEOF = 0;\r
33 \r
34 long CSegSize = 0;\r
35 long DSegSize = 0;\r
36 \r
37 char *ptest;\r
38 \r
39 long    sStack = 0;\r
40 long    sCode = 0;\r
41 long    sData = 0;\r
42 \r
43 long    offCode = 0;\r
44 long    offData = 0;\r
45 long    pStart = 0;\r
46 \r
47 struct tagtype tag;\r
48 \r
49 char osname[50] = {"MMURTL.RUN"};       /* default OS name */\r
50 \r
51 FILE *run_fh = 0;\r
52 FILE *out_fh = 0;\r
53 \r
54 char far *pDataBuf1;\r
55 char far *pDataBuf2;\r
56 \r
57 char far *pCodeBuf1;\r
58 char far *pCodeBuf2;\r
59 char far *pCodeBuf3;\r
60 \r
61 /*********************************************************\r
62   This reads the MMURTL run file and writes it to disk\r
63   as an image file as it would have been loaded by\r
64   the MMURTL loader. This is used so we can place this\r
65   on a disk to make it bootable.\r
66   This is a tag/length/value file is described in the\r
67   DASM documentation (in great detail).\r
68 *********************************************************/\r
69 \r
70 void MakeImageFile(void)\r
71 {\r
72 int ii;\r
73 long i, dret, nobj, nread;\r
74 char filetype, junk, *pin;\r
75 \r
76         while (1) {\r
77                 tag.id = 0;\r
78                 nobj = fread (&tag, 1, 5, run_fh);\r
79                 if (nobj) {\r
80                         ii = tag.id;\r
81                 }\r
82                 else {\r
83                         printf("Invalid OS Run file. No End Tag Found.\r\n");\r
84                         fclose(run_fh);\r
85                         fclose(out_fh);\r
86                         exit(1);\r
87                 }\r
88 \r
89                 switch (tag.id) {\r
90                         case IDTAG:\r
91                                 nobj = fread (&filetype, 1, 1, run_fh);\r
92                                 if (filetype == 1)\r
93                                         break;\r
94                                 else {\r
95                                         printf("File MUST be a run file\r\n");\r
96                                         exit(1);\r
97                                 }\r
98                                 break;\r
99                         case SEGTAG:\r
100                                 nobj = fread (&sStack, 1, 4, run_fh);\r
101                                 if (nobj) nobj = fread (&sCode, 1, 4, run_fh);\r
102                                 if (nobj) nobj = fread (&sData, 1, 4, run_fh);\r
103                                 printf("  Code    %ld\r\n", sCode);\r
104                                 printf("  Data    %ld\r\n", sData);\r
105                                 break;\r
106                         case DOFFTAG:\r
107                                 nobj = fread (&offData, 1, 4, run_fh);\r
108                                 break;\r
109                         case COFFTAG:\r
110                                 nobj = fread (&offCode, 1, 4, run_fh);\r
111                                 break;\r
112                         case STRTTAG:\r
113                                 nobj = fread (&pStart, 1, 4, run_fh);\r
114                                 break;\r
115                         case CODETAG:\r
116                                 printf("Reading code:  %ld bytes\r\n", tag.len);\r
117                 CSegSize = tag.len;\r
118 \r
119                                 if (tag.len >= 32768)\r
120                                         nread = 32768;\r
121                                 else\r
122                                         nread = tag.len;\r
123                                 tag.len -= nread;\r
124 \r
125                                 pin = pCodeBuf1;\r
126                                 while (nread--) {\r
127                                         nobj = fread (pin++, 1, 1, run_fh);\r
128                                 }\r
129 \r
130                                 if (tag.len >= 32768)\r
131                                         nread = 32768;\r
132                                 else\r
133                                         nread = tag.len;\r
134                                 tag.len -= nread;\r
135 \r
136                                 pin = pCodeBuf2;\r
137                                 while (nread--) {\r
138                                         nobj = fread (pin++, 1, 1, run_fh);\r
139                                 }\r
140 \r
141                                 if (tag.len >= 32768)\r
142                                         nread = 32768;\r
143                                 else\r
144                                         nread = tag.len;\r
145                                 tag.len -= nread;\r
146 \r
147                                 pin = pCodeBuf3;\r
148                                 while (nread--) {\r
149                                         nobj = fread (pin++, 1, 1, run_fh);\r
150                                 }\r
151                                 break;\r
152 \r
153                         case DATATAG:\r
154                                 printf("Reading data:  %ld bytes\r\n", tag.len);\r
155                 DSegSize = tag.len;\r
156                                 if (tag.len >= 32768)\r
157                                         nread = 32768;\r
158                                 else\r
159                                         nread = tag.len;\r
160                                 tag.len -= nread;\r
161 \r
162                                 pin = pDataBuf1;\r
163                                 while (nread--) {\r
164                                         nobj = fread (pin++, 1, 1, run_fh);\r
165                                 }\r
166 \r
167                                 if (tag.len >= 32768)\r
168                                         nread = 32768;\r
169                                 else\r
170                                         nread = tag.len;\r
171                                 tag.len -= nread;\r
172 \r
173                                 pin = pDataBuf2;\r
174                                 while (nread--) {\r
175                                         nobj = fread (pin++, 1, 1, run_fh);\r
176                                 }\r
177                                 break;\r
178                         case CDFIXTAG:\r
179                         case CCFIXTAG:\r
180                         case DDFIXTAG:\r
181                         case DCFIXTAG:\r
182                                 while ((tag.len) && (nobj)) {\r
183                                         nobj = fread (&i, 1, 4, run_fh);\r
184                                         tag.len-=4;\r
185                                 }\r
186                                 break;\r
187                         case ENDTAG:\r
188                                 nobj = fread (&i, 1, 4, run_fh);        /* Eat the end tag */\r
189                                 fclose(run_fh);\r
190                                 return;\r
191                         default:\r
192                                 while ((tag.len--) && (nobj))\r
193                                         nobj = fread (&junk, 1, 1, run_fh);\r
194                                 break;\r
195 \r
196                 } /* end of switch */\r
197         }\r
198 }\r
199 \r
200 /***************************\r
201 * Main program -- MMLoader\r
202 ****************************/\r
203 \r
204 void main(S16 argc, S8   *argv[])\r
205 {\r
206 S8   *ptr;\r
207 S16  i;\r
208 char fName;\r
209 \r
210         out_fh = fopen("MMURTL.IMG", "wb");\r
211         fName = 0;\r
212 \r
213         for(i=1; i < argc; ++i) {\r
214                 ptr = argv[i];\r
215                 if (*ptr == '/') {\r
216                   ptr++;\r
217                   switch(*ptr) {\r
218                         case 'U' :\r
219                         case 'u' :\r
220                                 break;\r
221                         case 'D' :\r
222                         case 'd' :\r
223                                 break;\r
224                         case 'O' :\r
225                         case 'o' :\r
226                                 fName = 1;\r
227                                 break;\r
228                         default:\r
229                                 printf("Invalid option/swtich \n");\r
230                                 if (run_fh)\r
231                                         fclose(run_fh);\r
232                                 exit(1);\r
233                                 break;\r
234                   }\r
235                 }\r
236                 else {\r
237                         if (fName)\r
238                                 strncpy(osname, argv[i], 39);\r
239                 }\r
240         }\r
241 \r
242         printf("MMURTL Image File Maker. Ver 1.0 \r\n");\r
243 \r
244         printf("OS Filename specified: %s \r\n",  osname);\r
245         run_fh = fopen(osname, "rb");\r
246         if (!run_fh) {\r
247                 printf("Can't open: %s \r\n",  osname);\r
248                 exit(1);\r
249         }\r
250 \r
251         pDataBuf1 = malloc(32768);      /*  32K */\r
252         pDataBuf2 = malloc(32768);      /*  32K */\r
253         pCodeBuf1 = malloc(32768);      /*  32K */\r
254         pCodeBuf2 = malloc(32768);      /*  32K */\r
255         pCodeBuf3 = malloc(32768);      /*  32K */\r
256 \r
257         MakeImageFile();\r
258 \r
259         fwrite(pDataBuf1, 1, 32768, out_fh);  /* Write 32K of the Image */\r
260         fwrite(pDataBuf2, 1, 32768, out_fh);  /* Write 32K of the Image */\r
261 \r
262         fwrite(pCodeBuf1, 1, 32768, out_fh);  /* Write 32K of the Image */\r
263         fwrite(pCodeBuf2, 1, 32768, out_fh);  /* Write 32K of the Image */\r
264         fwrite(pCodeBuf3, 1, 32768, out_fh);  /* Write 32K of the Image */\r
265 \r
266         fclose(out_fh);\r
267 \r
268         printf("\r\nDone.\r\n");\r
269 \r
270 \r
271 }\r
272 \r
273 /*********************** End of Module *****************/\r