Subversion Repositories SvarDOS

Rev

Rev 1953 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
28 mv_fox 1
/*
190 mateuszvis 2
 * SVARDOS INSTALL PROGRAM
206 mateuszvis 3
 *
190 mateuszvis 4
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
42 mv_fox 5
 *
1624 mateusz.vi 6
 * COPYRIGHT (C) 2016-2024 MATEUSZ VISTE, ALL RIGHTS RESERVED.
94 mv_fox 7
 *
190 mateuszvis 8
 * Permission is hereby granted, free of charge, to any person obtaining a
9
 * copy of this software and associated documentation files (the "Software"),
10
 * to deal in the Software without restriction, including without limitation
11
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
 * and/or sell copies of the Software, and to permit persons to whom the
13
 * Software is furnished to do so, subject to the following conditions:
94 mv_fox 14
 *
190 mateuszvis 15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
94 mv_fox 17
 *
190 mateuszvis 18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
 * DEALINGS IN THE SOFTWARE.
94 mv_fox 25
 *
868 mateusz.vi 26
 * http://svardos.org
28 mv_fox 27
 */
28
 
29
#include <dos.h>
30 mv_fox 30
#include <direct.h>  /* mkdir() */
28 mv_fox 31
#include <stdio.h>   /* printf() and friends */
32
#include <stdlib.h>  /* system() */
33
#include <string.h>  /* memcpy() */
34
#include <unistd.h>
42 mv_fox 35
 
1662 mateusz.vi 36
#include "mdr\cout.h"
1661 mateusz.vi 37
#include "mdr\dos.h"
624 mateuszvis 38
#include "svarlang.lib\svarlang.h"
42 mv_fox 39
 
67 mv_fox 40
/* keyboard layouts and locales */
41
#include "keylay.h"
42
#include "keyoff.h"
42 mv_fox 43
 
908 mateusz.vi 44
 
1664 mateusz.vi 45
/* color scheme (preset for color) */
46
static unsigned char COLOR_TITLEBAR  = 0x70;
1671 mateusz.vi 47
static unsigned char COLOR_TITLEVER  = 0x78;
1664 mateusz.vi 48
static unsigned char COLOR_BODY      = 0x17;
1673 mateusz.vi 49
static unsigned char COLOR_BODYWARN  = 0x1F;
1664 mateusz.vi 50
static unsigned char COLOR_SELECT    = 0x70;
51
static unsigned char COLOR_SELECTCUR = 0x1F;
28 mv_fox 52
 
1671 mateusz.vi 53
/* build release string, populated at startup by reading floppy's label */
54
static char BUILDSTRING[13];
28 mv_fox 55
 
190 mateuszvis 56
/* how much disk space does SvarDOS require (in MiB) */
1928 mateusz.vi 57
#define SVARDOS_DISK_REQ 4
28 mv_fox 58
 
73 mv_fox 59
/* menu screens can output only one of these: */
60
#define MENUNEXT 0
61
#define MENUPREV -1
62
#define MENUQUIT -2
63
 
67 mv_fox 64
/* a convenience 'function' used for debugging */
65
#define DBG(x) { video_putstringfix(24, 0, 0x4F00u, x, 80); }
47 mv_fox 66
 
67 mv_fox 67
struct slocales {
68
  char lang[4];
624 mateuszvis 69
  const char *keybcode;
1908 mateusz.vi 70
  unsigned short codepage;
71
  unsigned char egafile;
72
  unsigned char keybfile;
73
  short keyboff;
74
  short keyblen;
75
  unsigned short keybid;
76
  unsigned short countryid; /* 1=USA, 33=FR, 48=PL, etc */
67 mv_fox 77
};
78
 
79
 
1918 mateusz.vi 80
/* install a dummy int24h handler that always fails. this is to avoid the
81
 * annoying "abort, retry, fail... DOS messages. */
82
static void install_int24(void) {
83
  static unsigned char handler[] = { /* contains machine code instructions */
84
    0xB0, 0x03,  /* mov al, 3   ; tell DOS the action has to FAIL   */
85
    0xCF};       /* ret         ; return from the interrupt handler */
86
  /* install the handler */
87
  _asm {
88
    push dx
89
    mov ax, 0x2524          /* set INT vector 0x24 (to DS:DX)   */
90
    mov dx, offset handler  /* DS:DX points at my dummy handler */
91
    int 0x21
92
    pop dx
93
  }
94
}
95
 
96
 
97
static void exec(const char *s) {
98
  system(s);
99
  install_int24(); /* reinstall my int24 handler, apparently system() reverts
100
                      the original (DOS) one */
101
}
102
 
103
 
1908 mateusz.vi 104
/* put a string on screen and fill it until w chars with white space */
1662 mateusz.vi 105
static void video_putstringfix(unsigned char y, unsigned char x, unsigned char attr, const char *s, unsigned char w) {
106
  unsigned char i;
107
 
108
  /* print the string up to w characters */
109
  i = mdr_cout_str(y, x, s, attr, w);
110
 
111
  /* fill in left space (if any) with blanks */
112
  mdr_cout_char_rep(y, x + i, ' ', attr, w - i);
113
}
114
 
115
 
28 mv_fox 116
/* reboot the computer */
117
static void reboot(void) {
118
  void ((far *bootroutine)()) = (void (far *)()) 0xFFFF0000L;
119
  int far *rstaddr = (int far *)0x00400072L; /* BIOS boot flag is at 0040:0072 */
120
  *rstaddr = 0x1234; /* 0x1234 = warm boot, 0 = cold boot */
121
  (*bootroutine)(); /* jump to the BIOS reboot routine at FFFF:0000 */
122
}
123
 
42 mv_fox 124
 
1669 mateusz.vi 125
/* returns 1 if file exists, zero otherwise */
126
static int fileexists(const char *fname) {
127
  FILE *fd;
128
  fd = fopen(fname, "rb");
129
  if (fd == NULL) return(0);
130
  fclose(fd);
131
  return(1);
132
}
133
 
134
 
56 mv_fox 135
/* outputs a string to screen with taking care of word wrapping. returns amount of lines. */
1662 mateusz.vi 136
static unsigned char putstringwrap(unsigned char y, unsigned char x, unsigned char attr, const char *s) {
137
  unsigned char linew, lincount;
138
  linew = 80 - (x << 1);
56 mv_fox 139
 
140
  for (lincount = 1; y+lincount < 25; lincount++) {
141
    int i, len = linew;
142
    for (i = 0; i <= linew; i++) {
143
      if (s[i] == ' ') len = i;
144
      if (s[i] == '\n') {
145
        len = i;
146
        break;
147
      }
148
      if (s[i] == 0) {
149
        len = i;
150
        break;
151
      }
152
    }
1662 mateusz.vi 153
    mdr_cout_str(y++, x, s, attr, len);
56 mv_fox 154
    s += len;
155
    if (*s == 0) break;
156
    s += 1; /* skip the whitespace char */
157
  }
158
  return(lincount);
159
}
160
 
161
 
162
/* an NLS wrapper around video_putstring(), also performs line wrapping when
163
 * needed. returns the amount of lines that were output */
1662 mateusz.vi 164
static unsigned char putstringnls(unsigned char y, unsigned char x, unsigned char attr, unsigned char nlsmaj, unsigned char nlsmin) {
624 mateuszvis 165
  const char *s = svarlang_str(nlsmaj, nlsmin);
166
  if (s == NULL) s = "";
56 mv_fox 167
  return(putstringwrap(y, x, attr, s));
42 mv_fox 168
}
169
 
170
 
280 mateuszvis 171
/* copy file f1 to f2 using buff as a buffer of buffsz bytes. f2 will be overwritten if it
172
 * exists already! returns 0 on success. */
173
static int fcopy(const char *f2, const char *f1, void *buff, size_t buffsz) {
174
  FILE *fd1, *fd2;
175
  size_t sz;
176
  int res = -1; /* assume failure */
177
 
178
  /* open files */
179
  fd1 = fopen(f1, "rb");
180
  fd2 = fopen(f2, "wb");
181
  if ((fd1 == NULL) || (fd2 == NULL)) goto QUIT;
182
 
183
  /* copy data */
184
  for (;;) {
185
    sz = fread(buff, 1, buffsz, fd1);
186
    if (sz == 0) {
187
      if (feof(fd1) != 0) break;
188
      goto QUIT;
189
    }
190
    if (fwrite(buff, 1, sz, fd2) != sz) goto QUIT;
191
  }
192
 
193
  res = 0; /* success */
194
 
195
  QUIT:
196
  if (fd1 != NULL) fclose(fd1);
197
  if (fd2 != NULL) fclose(fd2);
198
  return(res);
199
}
200
 
201
 
1666 mateusz.vi 202
/* display a menu with items and return user's choice.
203
 * ypos: starting line where the menu is drawn
1948 mateusz.vi 204
 * height: max number of items to display inside the menu
1666 mateusz.vi 205
 * list: NULL-terminated list of items
206
 * maxlistlen: limit list to this many items tops */
1665 mateusz.vi 207
static int menuselect(unsigned char ypos, unsigned char height, const char **list, int maxlistlen) {
208
  int i, offset = 0, res = 0, count;
209
  unsigned char y, xpos, width = 0;
1662 mateusz.vi 210
 
1666 mateusz.vi 211
  /* count how many positions there are, and check their width */
1665 mateusz.vi 212
  for (count = 0; (list[count] != NULL) && (count != maxlistlen); count++) {
28 mv_fox 213
    int len = strlen(list[count]);
214
    if (len > width) width = len;
215
  }
1666 mateusz.vi 216
  width++; /* it's nice to have a small margin to the right of the widest item */
28 mv_fox 217
 
1948 mateusz.vi 218
  /* adjust height if there is less items than max height */
219
  if (count < height) height = count;
220
 
28 mv_fox 221
  /* if xpos negative, means 'center out' */
1665 mateusz.vi 222
  xpos = 39 - (width >> 1);
28 mv_fox 223
 
1666 mateusz.vi 224
  mdr_cout_char_rep(ypos, xpos, 0xC4, COLOR_SELECT, width + 2);  /* top line */
1664 mateusz.vi 225
  mdr_cout_char(ypos, xpos+width+2, 0xBF, COLOR_SELECT);         /*       \ */
226
  mdr_cout_char(ypos, xpos-1, 0xDA, COLOR_SELECT);               /*  /      */
1666 mateusz.vi 227
  ypos++; /* from now on ypos relates to the position of the content */
228
  mdr_cout_char(ypos+height, xpos-1, 0xC0, COLOR_SELECT);      /*  \      */
229
  mdr_cout_char(ypos+height, xpos+width+2, 0xD9, COLOR_SELECT);/*      /  */
230
  mdr_cout_char_rep(ypos+height, xpos, 0xC4, COLOR_SELECT, width + 2);
28 mv_fox 231
 
232
  for (;;) {
233
    int key;
1666 mateusz.vi 234
 
235
    /* draw side borders of the menu + the cursor */
236
    if (count <= height) { /* no need for a cursor, all fits on one page */
237
      i = 255;
238
    } else {
239
      i = offset * (height - 1) / (count - height);
240
    }
241
 
242
    for (y = ypos; y < (ypos + height); y++) {
243
      mdr_cout_char(y, xpos-1, 0xB3, COLOR_SELECT); /* left side */
244
      if (y - ypos == i) {
1667 mateusz.vi 245
        mdr_cout_char(y, xpos+width+2, '=', COLOR_SELECT); /* cursor */
1666 mateusz.vi 246
      } else {
247
        mdr_cout_char(y, xpos+width+2, 0xB3, COLOR_SELECT); /* right side */
248
      }
249
    }
250
 
28 mv_fox 251
    /* list of selectable items */
1666 mateusz.vi 252
    for (i = 0; i < height; i++) {
28 mv_fox 253
      if (i + offset == res) {
1666 mateusz.vi 254
        mdr_cout_char(ypos + i, xpos, 16, COLOR_SELECTCUR);
255
        mdr_cout_char(ypos + i, xpos+width+1, 17, COLOR_SELECTCUR);
256
        mdr_cout_locate(ypos + i, xpos);
257
        video_putstringfix(ypos + i, xpos+1, COLOR_SELECTCUR, list[i + offset], width);
28 mv_fox 258
      } else if (i + offset < count) {
1666 mateusz.vi 259
        mdr_cout_char(ypos + i, xpos, ' ', COLOR_SELECT);
260
        mdr_cout_char(ypos + i, xpos+width+1, ' ', COLOR_SELECT);
261
        video_putstringfix(ypos + i, xpos+1, COLOR_SELECT, list[i + offset], width);
28 mv_fox 262
      } else {
1666 mateusz.vi 263
        mdr_cout_char_rep(ypos + i, xpos, ' ', COLOR_SELECT, width+2);
28 mv_fox 264
      }
265
    }
1661 mateusz.vi 266
    key = mdr_dos_getkey();
28 mv_fox 267
    if (key == 0x0D) { /* ENTER */
268
      return(res);
269
    } else if (key == 0x148) { /* up */
1948 mateusz.vi 270
      UP_AGAIN:
33 mv_fox 271
      if (res > 0) {
272
        res--;
273
        if (res < offset) offset = res;
1948 mateusz.vi 274
        if (list[res][0] == 0) goto UP_AGAIN;
33 mv_fox 275
      }
28 mv_fox 276
    } else if (key == 0x150) { /* down */
1948 mateusz.vi 277
      DOWN_AGAIN:
33 mv_fox 278
      if (res+1 < count) {
279
        res++;
1666 mateusz.vi 280
        if (res > offset + height - 1) offset = res - (height - 1);
1948 mateusz.vi 281
        if (list[res][0] == 0) goto DOWN_AGAIN;
33 mv_fox 282
      }
283
    } else if (key == 0x147) { /* home */
284
      res = 0;
285
      offset = 0;
286
    } else if (key == 0x14F) { /* end */
287
      res = count - 1;
1666 mateusz.vi 288
      if (res > offset + height - 1) offset = res - (height - 1);
28 mv_fox 289
    } else if (key == 0x1B) {  /* ESC */
290
      return(-1);
78 mv_fox 291
    }/* else {
33 mv_fox 292
      char buf[8];
55 mv_fox 293
      snprintf(buf, sizeof(buf), "0x%02X ", key);
1664 mateusz.vi 294
      video_putstring(1, 0, COLOR_BODY, buf, -1);
78 mv_fox 295
    }*/
28 mv_fox 296
  }
297
}
298
 
1946 mateusz.vi 299
 
1662 mateusz.vi 300
static void newscreen(unsigned char statusbartype) {
624 mateuszvis 301
  const char *msg;
1664 mateusz.vi 302
  mdr_cout_cls(COLOR_BODY);
624 mateuszvis 303
  msg = svarlang_strid(0x00); /* "SVARDOS INSTALLATION" */
1664 mateusz.vi 304
  mdr_cout_char_rep(0, 0, ' ', COLOR_TITLEBAR, 80);
305
  mdr_cout_str(0, 40 - (strlen(msg) >> 1), msg, COLOR_TITLEBAR, 80);
1671 mateusz.vi 306
  mdr_cout_str(0, 80 - strlen(BUILDSTRING), BUILDSTRING, COLOR_TITLEVER, 12);
307
 
79 mv_fox 308
  switch (statusbartype) {
309
    case 1:
624 mateuszvis 310
      msg = svarlang_strid(0x000B); /* "Up/Down = Select entry | Enter = Validate your choice | ESC = Quit to DOS" */
79 mv_fox 311
      break;
312
    case 2:
624 mateuszvis 313
      msg = svarlang_strid(0x0005); /* "Press any key..." */
79 mv_fox 314
      break;
315
    case 3:
316
      msg = "";
317
      break;
318
    default:
624 mateuszvis 319
      msg = svarlang_strid(0x000A); /* "Up/Down = Select entry | Enter = Validate your choice | ESC = Previous screen" */
79 mv_fox 320
      break;
321
  }
1664 mateusz.vi 322
  mdr_cout_char(24, 0, ' ', COLOR_TITLEBAR);
323
  video_putstringfix(24, 1, COLOR_TITLEBAR, msg, 79);
1662 mateusz.vi 324
  mdr_cout_locate(25,0);
28 mv_fox 325
}
326
 
1908 mateusz.vi 327
 
96 mv_fox 328
/* fills a slocales struct accordingly to the value of its keyboff member */
329
static void kblay2slocal(struct slocales *locales) {
624 mateuszvis 330
  const char *m;
96 mv_fox 331
  for (m = kblayouts[locales->keyboff]; *m != 0; m++); /* skip layout name */
332
  m++;
333
  /* skip keyb code and copy it to locales.keybcode */
334
  locales->keybcode = m;
335
  for (; *m != 0; m++);
336
  /* */
337
  locales->codepage = ((unsigned short)m[1] << 8) | m[2];
338
  locales->egafile = m[3];
339
  locales->keybfile = m[4];
340
  locales->keybid = ((unsigned short)m[5] << 8) | m[6];
1908 mateusz.vi 341
  locales->countryid = ((unsigned short)m[7] << 8) | m[8];
96 mv_fox 342
}
343
 
1908 mateusz.vi 344
 
67 mv_fox 345
static int selectlang(struct slocales *locales) {
346
  int choice, x;
624 mateuszvis 347
  const char *msg;
348
  const char *langlist[] = {
67 mv_fox 349
    "English",
1177 mateusz.vi 350
    "Brazilian",
67 mv_fox 351
    "French",
133 mv_fox 352
    "German",
119 mv_fox 353
    "Italian",
67 mv_fox 354
    "Polish",
116 mv_fox 355
    "Russian",
123 mv_fox 356
    "Slovene",
128 mv_fox 357
    "Swedish",
67 mv_fox 358
    "Turkish",
28 mv_fox 359
    NULL
360
  };
361
 
79 mv_fox 362
  newscreen(1);
624 mateuszvis 363
  msg = svarlang_strid(0x0100); /* "Welcome to SvarDOS" */
42 mv_fox 364
  x = 40 - (strlen(msg) >> 1);
1664 mateusz.vi 365
  mdr_cout_str(4, x, msg, COLOR_BODY, 80);
366
  mdr_cout_char_rep(5, x, '=', COLOR_BODY, strlen(msg));
1662 mateusz.vi 367
 
368
  /* center out the string "Please select your language..." */
369
  msg = svarlang_str(1, 1); /* "Please select your language from the list below:" */
370
  if (strlen(msg) > 74) {
1664 mateusz.vi 371
    putstringwrap(8, 1, COLOR_BODY, msg);
1662 mateusz.vi 372
  } else {
1664 mateusz.vi 373
    mdr_cout_str(8, 40 - (strlen(msg) / 2), msg, COLOR_BODY, 80);
1662 mateusz.vi 374
  }
375
 
1666 mateusz.vi 376
  choice = menuselect(11, 9, langlist, -1);
73 mv_fox 377
  if (choice < 0) return(MENUPREV);
1669 mateusz.vi 378
 
67 mv_fox 379
  /* populate locales with default values */
380
  memset(locales, 0, sizeof(struct slocales));
381
  switch (choice) {
382
    case 1:
1179 mateusz.vi 383
      strcpy(locales->lang, "BR");
384
      locales->keyboff = OFFLOC_BR;
385
      locales->keyblen = OFFLEN_BR;
386
      break;
387
    case 2:
67 mv_fox 388
      strcpy(locales->lang, "FR");
389
      locales->keyboff = OFFLOC_FR;
390
      locales->keyblen = OFFLEN_FR;
391
      break;
1177 mateusz.vi 392
    case 3:
133 mv_fox 393
      strcpy(locales->lang, "DE");
394
      locales->keyboff = OFFLOC_DE;
395
      locales->keyblen = OFFLEN_DE;
396
      break;
1177 mateusz.vi 397
    case 4:
119 mv_fox 398
      strcpy(locales->lang, "IT");
399
      locales->keyboff = OFFLOC_IT;
400
      locales->keyblen = OFFLEN_IT;
401
      break;
1177 mateusz.vi 402
    case 5:
67 mv_fox 403
      strcpy(locales->lang, "PL");
404
      locales->keyboff = OFFLOC_PL;
405
      locales->keyblen = OFFLEN_PL;
406
      break;
1177 mateusz.vi 407
    case 6:
116 mv_fox 408
      strcpy(locales->lang, "RU");
409
      locales->keyboff = OFFLOC_RU;
410
      locales->keyblen = OFFLEN_RU;
411
      break;
1177 mateusz.vi 412
    case 7:
123 mv_fox 413
      strcpy(locales->lang, "SI");
414
      locales->keyboff = OFFLOC_SI;
415
      locales->keyblen = OFFLEN_SI;
416
      break;
1177 mateusz.vi 417
    case 8:
128 mv_fox 418
      strcpy(locales->lang, "SV");
419
      locales->keyboff = OFFLOC_SV;
420
      locales->keyblen = OFFLEN_SV;
421
      break;
1177 mateusz.vi 422
    case 9:
67 mv_fox 423
      strcpy(locales->lang, "TR");
424
      locales->keyboff = OFFLOC_TR;
425
      locales->keyblen = OFFLEN_TR;
426
      break;
427
    default:
428
      strcpy(locales->lang, "EN");
429
      locales->keyboff = 0;
430
      locales->keyblen = OFFCOUNT;
431
      break;
432
  }
96 mv_fox 433
  /* populate the slocales struct accordingly to the keyboff member */
434
  kblay2slocal(locales);
67 mv_fox 435
  /* */
73 mv_fox 436
  return(MENUNEXT);
28 mv_fox 437
}
438
 
439
 
67 mv_fox 440
static int selectkeyb(struct slocales *locales) {
96 mv_fox 441
  int menuheight, choice;
442
  if (locales->keyblen == 1) return(MENUNEXT); /* do not ask for keyboard layout if only one is available for given language */
79 mv_fox 443
  newscreen(0);
1664 mateusz.vi 444
  putstringnls(5, 1, COLOR_BODY, 1, 5); /* "SvarDOS supports different keyboard layouts */
1666 mateusz.vi 445
  menuheight = locales->keyblen;
446
  if (menuheight > 11) menuheight = 11;
1665 mateusz.vi 447
  choice = menuselect(10, menuheight, &(kblayouts[locales->keyboff]), locales->keyblen);
96 mv_fox 448
  if (choice < 0) return(MENUPREV);
449
  /* (re)load the keyboard layout & codepage setup */
450
  locales->keyboff += choice;
451
  kblay2slocal(locales);
73 mv_fox 452
  return(MENUNEXT);
67 mv_fox 453
}
454
 
455
 
28 mv_fox 456
/* returns 0 if installation must proceed, non-zero otherwise */
457
static int welcomescreen(void) {
73 mv_fox 458
  int c;
624 mateuszvis 459
  const char *choice[3];
460
  choice[0] = svarlang_strid(0x0001);
461
  choice[1] = svarlang_strid(0x0002);
462
  choice[2] = NULL;
79 mv_fox 463
  newscreen(0);
1664 mateusz.vi 464
  putstringnls(4, 1, COLOR_BODY, 2, 0); /* "You are about to install SvarDOS */
1666 mateusz.vi 465
  c = menuselect(13, 2, choice, -1);
73 mv_fox 466
  if (c < 0) return(MENUPREV);
467
  if (c == 0) return(MENUNEXT);
468
  return(MENUQUIT);
28 mv_fox 469
}
470
 
471
 
1908 mateusz.vi 472
/* returns total disk space of drive drv (in MiB, max 2048, A=1 B=2 etc), or -1 if drive invalid */
1911 mateusz.vi 473
static int disksize(unsigned char drv) {
474
  unsigned short sec_per_cluster = 0;
475
  unsigned short tot_clusters = 0;
476
  unsigned short bytes_per_sec = 0;
477
  long res;
478
  _asm {
479
    push ax
480
    push bx
481
    push cx
482
    push dx
35 mv_fox 483
 
1911 mateusz.vi 484
    mov ah, 0x36
485
    mov dl, drv
486
    int 0x21
487
    /* AX=sec_per_cluster DX=tot_clusters BX=free_clusters CX=bytes_per_sec */
488
    mov sec_per_cluster, ax
489
    mov bytes_per_sec, cx
490
    mov tot_clusters, dx
35 mv_fox 491
 
1911 mateusz.vi 492
    pop dx
493
    pop cx
494
    pop bx
495
    pop ax
496
  }
497
 
498
  if (sec_per_cluster == 0xffff) return(-1);
499
  res = sec_per_cluster;
500
  res *= tot_clusters;
501
  res *= bytes_per_sec;
502
  res >>= 20;
503
  return((int)res);
504
}
505
 
506
 
35 mv_fox 507
/* returns 0 if disk is empty, non-zero otherwise */
1942 mateusz.vi 508
static int diskempty(char drv) {
35 mv_fox 509
  unsigned int rc;
510
  int res;
511
  char buff[8];
512
  struct find_t fileinfo;
1942 mateusz.vi 513
  snprintf(buff, sizeof(buff), "%c:\\*.*", drv);
35 mv_fox 514
  rc = _dos_findfirst(buff, _A_NORMAL | _A_SUBDIR | _A_HIDDEN | _A_SYSTEM, &fileinfo);
515
  if (rc == 0) {
516
    res = 1; /* call successfull means disk is not empty */
28 mv_fox 517
  } else {
35 mv_fox 518
    res = 0;
28 mv_fox 519
  }
35 mv_fox 520
  /* _dos_findclose(&fileinfo); */ /* apparently required only on OS/2 */
28 mv_fox 521
  return(res);
522
}
523
 
200 mateuszvis 524
 
1930 mateusz.vi 525
/* replace all occurences of char a by char b in s */
526
static void strtr(char *s, char a, char b) {
527
  for (;*s != 0; s++) {
528
    if (*s == a) *s = b;
529
  }
530
}
531
 
532
 
898 mateusz.vi 533
/* tries to write an empty file to drive.
534
 * asks DOS to inhibit the int 24h handler for this job, so erros are
535
 * gracefully reported - unfortunately this does not work under FreeDOS because
536
 * the DOS-C kernel does not implement the required flag.
537
 * returns 0 on success (ie. "disk exists and is writeable"). */
1908 mateusz.vi 538
static unsigned short test_drive_write(char drive) {
898 mateusz.vi 539
  unsigned short result = 0;
1908 mateusz.vi 540
  char *buff = "@:\\SVWRTEST.123";
541
  buff[0] = drive;
898 mateusz.vi 542
  _asm {
543
    push ax
544
    push bx
545
    push cx
546
    push dx
547
 
548
    mov ah, 0x6c   /* extended open/create file */
549
    mov bx, 0x2001 /* open for write + inhibit int 24h handler */
550
    xor cx, cx     /* file attributes on the created file */
551
    mov dx, 0x0010 /* create file if does not exist, fail if it exists */
552
    mov si, buff   /* filename to create */
553
    int 0x21
554
    jc FAILURE
555
    /* close the file (handle in AX) */
556
    mov bx, ax
557
    mov ah, 0x3e /* close file */
558
    int 0x21
559
    jc FAILURE
560
    /* delete the file */
561
    mov ah, 0x41 /* delete file pointed out by DS:DX */
562
    mov dx, buff
563
    int 0x21
564
    jnc DONE
565
 
566
    FAILURE:
567
    mov result, ax
568
    DONE:
569
 
570
    pop dx
571
    pop cx
572
    pop bx
573
    pop ax
574
  }
575
  return(result);
576
}
577
 
578
 
1944 mateusz.vi 579
#define MBR_WRITE 3
580
#define MBR_READ 2
581
/* read (action=2) or write (action=3) MBR of driveid (0x80, 0x81..) into buff */
582
static int READ_OR_WRITE_MBR(unsigned char action, char *buff, unsigned char driveid);
583
#pragma aux READ_OR_WRITE_MBR = \
1939 mateusz.vi 584
"push es" \
1944 mateusz.vi 585
"mov al, 1"       /* read 1 sector into memory */ \
1940 mateusz.vi 586
"mov cx, 0x0001"  /* cylinder 0, sector 1 */ \
587
"xor dh, dh"      /* head 0 */ \
1939 mateusz.vi 588
"push ds" \
589
"pop es" \
590
"int 0x13" \
591
"mov ax, 0" /* do not do "xor ax, ax", CF needs to be preserved */ \
592
"jnc DONE" \
593
"inc ax" \
594
"DONE:" \
595
"pop es" \
1944 mateusz.vi 596
parm [ah] [bx] [dl] \
1940 mateusz.vi 597
modify [cx dx] \
1939 mateusz.vi 598
value [ax]
599
 
600
 
1944 mateusz.vi 601
 
1939 mateusz.vi 602
/*
603
 * MBR structure:
604
 *
605
 * Boot signature (word 0xAA55) is at 0x01FE
606
 *
607
 * partition entry 1 is at 0x01BE
608
 *                 2    at 0x01CE
609
 *                 3    at 0x01DE
610
 *                 4    at 0x01EE
611
 *
612
 * a partition entry is:
613
 * 0x00  status byte (active is bit 0x80 set)
614
 * 0x01  CHS addr of first sector (here: HEAD)
615
 * 0x02  CHS addr of first sector (CCSSSSSS) sector in 6 low bits, cylinder high bits in CC
616
 * 0x03  CHS addr of furst sector (here: low 8 bits of cylinder)
617
 * 0x04  Partition type:
618
        0x06/0x08 for CHS FAT16/32
619
        0x0E/0x0C for LBA FAT16/32
620
 * 0x05  CHS addr of last sector (same format as for 1st sector)
621
 * 0x06  CHS addr of last sector (same format as for 1st sector)
622
 * 0x07  CHS addr of last sector (same format as for 1st sector)
623
 * 0x08  LBA addr of first sector (4 bytes) - used sometimes instead of CHS
624
 * 0x0C  number of sectors in partition (4 bytes)
625
 */
626
 
627
struct drivelist {
1940 mateusz.vi 628
  unsigned long start_lba;
1943 mateusz.vi 629
  unsigned long tot_sect;  /* size (of partition), in sectors */
1940 mateusz.vi 630
  unsigned short tot_size; /* size in MiB */
1943 mateusz.vi 631
  unsigned char partid;    /* position of the partition in the MBR (0,1,2,3) */
1944 mateusz.vi 632
  unsigned char hd;        /* 0-3 */
633
  unsigned char dosid;     /* DOS drive id (A=0, B=1...)*/
1939 mateusz.vi 634
};
635
 
1940 mateusz.vi 636
 
637
/* a (very partial) struct mimicking what EDR-DOS uses in int 2Fh,AX=0803h */
638
#pragma pack (push)
639
#pragma pack (0) /* disable the automatic alignment of struct members */
640
struct dos_udsc {
1944 mateusz.vi 641
  unsigned short next_off; /* offset FFFFh if last */
1940 mateusz.vi 642
  unsigned short next_seg;
1944 mateusz.vi 643
  unsigned char hd;        /* physical unit (as in int 13h) */
644
  unsigned char letter;    /* DOS letter drive (A=0, B=1, C=2, ...) */
1940 mateusz.vi 645
  unsigned short sectsize; /* bytes per sector */
646
  unsigned char unknown[15];
647
  unsigned long start_lba; /* LBA address of the partition (only for primary partitions) */
648
};
649
#pragma pack (pop)
650
 
651
 
652
/* get the DOS drive data table list */
653
static struct dos_udsc far *get_dos_udsc(void)  {
654
  unsigned short udsc_seg = 0, udsc_off = 0;
655
  _asm {
656
    push ds
657
    push di
658
 
659
    mov ax, 0x0803
660
    int 0x2f
661
    /* drive data table list is in DS:DI now */
662
    mov udsc_seg, ds
663
    mov udsc_off, di
664
 
665
    pop di
666
    pop ds
667
  }
668
  if (udsc_off == 0xffff) return(NULL);
669
  return(MK_FP(udsc_seg, udsc_off));
670
}
671
 
672
 
673
/* reads the MBR and matches its entries to DOS drives
1943 mateusz.vi 674
 * fills the drives array with up to 16 drives (4 partitions * 4 disks)
1940 mateusz.vi 675
 * see https://github.com/SvarDOS/bugz/issues/89 */
1943 mateusz.vi 676
static int get_drives_list(char *buff, struct drivelist *drives) {
1939 mateusz.vi 677
  int i;
1943 mateusz.vi 678
  int listlen = 0;
679
  int drv;
1940 mateusz.vi 680
  struct dos_udsc far *udsc_root = get_dos_udsc();
681
  struct dos_udsc far *udsc_node;
1939 mateusz.vi 682
 
1943 mateusz.vi 683
  for (drv = 0x80; drv < 0x84; drv++) {
1940 mateusz.vi 684
 
1944 mateusz.vi 685
    if (READ_OR_WRITE_MBR(MBR_READ, buff, drv) != 0) continue;
1939 mateusz.vi 686
 
1943 mateusz.vi 687
    /* check boot signature at 0x01fe */
688
    if (*(unsigned short *)(buff + 0x01fe) != 0xAA55) continue;
1939 mateusz.vi 689
 
1943 mateusz.vi 690
    /* iterate over the 4 partitions in the MBR */
691
    for (i = 0; i < 4; i++) {
692
      unsigned char *entry;
1939 mateusz.vi 693
 
1943 mateusz.vi 694
      entry = buff + 0x01BE + (i * 16);
1939 mateusz.vi 695
 
1943 mateusz.vi 696
      /* ignore partition if fs is unknown (non-FAT) */
697
      if ((entry[4] != 0x0E) && (entry[4] != 0x0C)  /* LBA FAT */
698
       && (entry[4] != 0x01) && (entry[4] != 0x06) && (entry[4] != 0x08)) { /* CHS FAT */
699
        continue;
1939 mateusz.vi 700
      }
1943 mateusz.vi 701
      bzero(&(drives[listlen]), sizeof(struct drivelist));
1944 mateusz.vi 702
      drives[listlen].hd = drv & 3;
1943 mateusz.vi 703
      drives[listlen].partid = i;
704
      drives[listlen].start_lba = ((unsigned long *)entry)[2];
705
      drives[listlen].tot_sect = ((unsigned long *)entry)[3];
1939 mateusz.vi 706
 
1943 mateusz.vi 707
      /* now iterate over DOS drives and try to match ont to this MBR entry */
708
      udsc_node = udsc_root;
709
      while (udsc_node != NULL) {
710
        if (udsc_node->hd != drv) goto NEXT;
711
        if (udsc_node->start_lba != drives[listlen].start_lba) goto NEXT;
1939 mateusz.vi 712
 
1943 mateusz.vi 713
        /* FOUND! */
1944 mateusz.vi 714
        drives[listlen].dosid = udsc_node->letter;
1943 mateusz.vi 715
        {
716
          unsigned long sz = (drives[listlen].tot_sect * udsc_node->sectsize) >> 20;
717
          drives[listlen].tot_size = (unsigned short)sz;
718
        }
719
        listlen++;
720
        break;
721
 
722
        NEXT:
723
        if (udsc_node->next_off == 0xffff) break;
724
        udsc_node = MK_FP(udsc_node->next_seg, udsc_node->next_off);
725
      } /* iterate over UDSC nodes */
726
    } /* iterate over partition entries of the MBR */
727
  } /* iterate over BIOS disks (0x80, 0x81, ...) */
728
 
729
  return(listlen);
1939 mateusz.vi 730
}
731
 
732
 
1942 mateusz.vi 733
/* displays a list of drives and asks user to choose one for installation
1944 mateusz.vi 734
 * returns a word with bits HHPPLLLLLLLL where:
735
 * HH = hard drive id (0..3)
736
 * PP = position of the partition in MBR (0..3)
737
 * LL... = drive's DOS id (A=0, B=1, C=2, ...) */
1942 mateusz.vi 738
static int selectdrive(void) {
1935 mateusz.vi 739
  char buff[512];
1943 mateusz.vi 740
  struct drivelist drives[16];
741
  char drvlist[16][32]; /* up to 16 drives (4 partitions on 4 disks) */
742
  int i, drvlistlen;
1942 mateusz.vi 743
  const char *menulist[16];
744
  unsigned char driveid = 1; /* fdisk runs on first drive (unless USB boot) */
1935 mateusz.vi 745
 
1940 mateusz.vi 746
  /* read MBR of first HDD */
1943 mateusz.vi 747
  drvlistlen = get_drives_list(buff, drives);
1939 mateusz.vi 748
 
1942 mateusz.vi 749
  /* if no drive found - disk not partitioned? */
750
  if (drvlistlen == 0) {
751
    const char *list[4];
752
    newscreen(0);
753
    list[0] = svarlang_str(0, 3); /* Create a partition automatically */
754
    list[1] = svarlang_str(0, 4); /* Run the FDISK tool */
755
    list[2] = svarlang_str(0, 2); /* Quit to DOS */
756
    list[3] = NULL;
757
    snprintf(buff, sizeof(buff), svarlang_strid(0x0300), SVARDOS_DISK_REQ); /* "ERROR: No drive could be found. Note, that SvarDOS requires at least %d MiB of available disk space */
758
    switch (menuselect(6 + putstringwrap(4, 1, COLOR_BODY, buff), 3, list, -1)) {
759
      case 0:
760
        sprintf(buff, "FDISK /PRI:MAX %u", driveid);
761
        exec(buff);
762
        break;
763
      case 1:
764
        mdr_cout_cls(0x07);
765
        mdr_cout_locate(0, 0);
766
        sprintf(buff, "FDISK %u", driveid);
767
        exec(buff);
768
        break;
769
      case 2:
770
        return(MENUQUIT);
771
      default:
772
        return(-1);
1908 mateusz.vi 773
    }
1942 mateusz.vi 774
    /* write a temporary MBR which only skips the drive (in case BIOS would
775
     * try to boot off the not-yet-ready C: disk) */
776
    sprintf(buff, "FDISK /LOADIPL %u", driveid);
777
    exec(buff); /* writes BOOT.MBR into actual MBR */
778
    newscreen(2);
779
    putstringnls(10, 10, COLOR_BODY, 3, 1); /* "Your computer will reboot now." */
780
    putstringnls(12, 10, COLOR_BODY, 0, 5); /* "Press any key..." */
781
    mdr_dos_getkey();
782
    reboot();
783
    return(MENUQUIT);
784
  }
1908 mateusz.vi 785
 
1943 mateusz.vi 786
  /* build a menu with all drives */
787
  for (i = 0; i < drvlistlen; i++) {
1944 mateusz.vi 788
    snprintf(drvlist[i], sizeof(drvlist[0]), "%c: [%u MiB, hd%c%u]", 'A' + drives[i].dosid, drives[i].tot_size, 'a' + drives[i].hd, drives[i].partid);
1943 mateusz.vi 789
    menulist[i] = drvlist[i];
790
  }
1948 mateusz.vi 791
  menulist[i++] = "";
1942 mateusz.vi 792
  menulist[i++] = svarlang_str(0, 2); /* Quit to DOS */
793
  menulist[i] = NULL;
1943 mateusz.vi 794
 
1942 mateusz.vi 795
  newscreen(0);
1948 mateusz.vi 796
 
1949 mateusz.vi 797
  snprintf(buff, sizeof(buff), "%s", svarlang_strid(0x0307));
1948 mateusz.vi 798
  i = menuselect(7 + putstringwrap(4, 1, COLOR_BODY, buff) /*ypos*/, 10 /*max-height*/, menulist, -1);
1942 mateusz.vi 799
  if (i < 0) {
800
    return(MENUPREV);
801
  } else if (i < drvlistlen) {
1944 mateusz.vi 802
    /* return a bitfield HHPPLLLLLLLL
803
     * HH = hard drive id (0..3)
804
     * PP = position of the partition in MBR (0..3)
805
     * LL... = drive's DOS id (A=0, B=1, C=2, ...) */
806
    return((drives[i].partid << 8) | (drives[i].hd << 10) | drives[i].dosid);
1942 mateusz.vi 807
  }
1935 mateusz.vi 808
 
1942 mateusz.vi 809
  return(MENUQUIT);
810
}
1935 mateusz.vi 811
 
1944 mateusz.vi 812
/* hd_drv is a bitfield HHPPLLLLLLLL
813
 * HH = hard drive id (0..3)
814
 * PP = position of the partition in MBR (0..3)
815
 * LL... = drive's DOS id (A=0, B=1, C=2, ...) */
1942 mateusz.vi 816
static int preparedrive(int hd_drv) {
817
  unsigned char selecteddrive;
1944 mateusz.vi 818
  unsigned char driveid, partid;
1942 mateusz.vi 819
  char cselecteddrive;
820
  int choice;
821
  char buff[512];
556 mateuszvis 822
 
1942 mateusz.vi 823
  /* decode hd and drive id from hd_drv */
1944 mateusz.vi 824
  selecteddrive = hd_drv & 0xff; /* DOS drive letter (A=0. B=1, C=2 etc) */
825
  partid = (hd_drv >> 8) & 3;    /* position of partition in MBR (0..3) */
826
  driveid = (hd_drv >> 10);      /* HDD identifier (0..3) */
556 mateuszvis 827
 
1944 mateusz.vi 828
  cselecteddrive = 'A' + selecteddrive;
1935 mateusz.vi 829
 
1942 mateusz.vi 830
  TRY_AGAIN:
1935 mateusz.vi 831
 
1942 mateusz.vi 832
  /* if not formatted, propose to format it right away (try to create a directory) */
833
  if (test_drive_write(cselecteddrive) != 0) {
834
    const char *list[3];
79 mv_fox 835
    newscreen(0);
1942 mateusz.vi 836
    snprintf(buff, sizeof(buff), svarlang_str(3, 3), cselecteddrive); /* "ERROR: Drive %c: seems to be unformated. Do you wish to format it?") */
837
    mdr_cout_str(7, 1, buff, COLOR_BODY, 80);
556 mateuszvis 838
 
1942 mateusz.vi 839
    snprintf(buff, sizeof(buff), svarlang_strid(0x0007), cselecteddrive); /* "Format drive %c:" */
840
    list[0] = buff;
841
    list[1] = svarlang_strid(0x0002); /* "Quit to DOS" */
842
    list[2] = NULL;
556 mateuszvis 843
 
1942 mateusz.vi 844
    choice = menuselect(12, 2, list, -1);
845
    if (choice < 0) return(MENUPREV);
846
    if (choice == 1) return(MENUQUIT);
847
    mdr_cout_cls(0x07);
848
    mdr_cout_locate(0, 0);
849
    snprintf(buff, sizeof(buff), "FORMAT %c: /Q /U /Z:seriously /V:SVARDOS", cselecteddrive);
850
    exec(buff);
851
    goto TRY_AGAIN;
28 mv_fox 852
  }
1942 mateusz.vi 853
 
854
  /* check total disk space */
1944 mateusz.vi 855
  if (disksize(selecteddrive+1) < SVARDOS_DISK_REQ) {
1942 mateusz.vi 856
    int y = 9;
857
    newscreen(2);
858
    snprintf(buff, sizeof(buff), svarlang_strid(0x0304), cselecteddrive, SVARDOS_DISK_REQ); /* "ERROR: Drive %c: is not big enough! SvarDOS requires a disk of at least %d MiB." */
859
    y += putstringwrap(y, 1, COLOR_BODY, buff);
860
    putstringnls(++y, 1, COLOR_BODY, 0, 5); /* "Press any key..." */
861
    mdr_dos_getkey();
862
    return(MENUPREV);
863
  }
864
 
865
  /* is the disk empty? */
866
  newscreen(0);
867
  if (diskempty(cselecteddrive) != 0) {
868
    const char *list[3];
869
    int y = 6;
870
    snprintf(buff, sizeof(buff), svarlang_strid(0x0305), cselecteddrive); /* "ERROR: Drive %c: not empty" */
871
    y += putstringwrap(y, 1, COLOR_BODY, buff);
872
 
873
    snprintf(buff, sizeof(buff), svarlang_strid(0x0007), cselecteddrive); /* "Format drive %c:" */
874
    list[0] = buff;
875
    list[1] = svarlang_strid(0x0002); /* "Quit to DOS" */
876
    list[2] = NULL;
877
 
878
    choice = menuselect(++y, 2, list, -1);
879
    if (choice < 0) return(MENUPREV);
880
    if (choice == 1) return(MENUQUIT);
881
    mdr_cout_cls(0x07);
882
    mdr_cout_locate(0, 0);
883
    snprintf(buff, sizeof(buff), "FORMAT %c: /Q /U /Z:seriously /V:SVARDOS", cselecteddrive);
884
    exec(buff);
885
    goto TRY_AGAIN;
886
  } else {
887
    /* final confirmation */
888
    const char *list[3];
889
    list[0] = svarlang_strid(0x0001); /* Install SvarDOS */
890
    list[1] = svarlang_strid(0x0002); /* Quit to DOS */
891
    list[2] = NULL;
892
    snprintf(buff, sizeof(buff), svarlang_strid(0x0306), cselecteddrive); /* "The installation of SvarDOS to %c: is about to begin." */
893
    mdr_cout_str(7, 40 - (strlen(buff) / 2), buff, COLOR_BODY, 80);
894
    choice = menuselect(10, 2, list, -1);
895
    if (choice < 0) return(MENUPREV);
896
    if (choice == 1) return(MENUQUIT);
1944 mateusz.vi 897
 
898
    /* update the disk's MBR, transfer the boot system, make sure the partition
899
     * is ACTIVE and create a TEMP directory to copy SVP files over */
1942 mateusz.vi 900
    snprintf(buff, sizeof(buff), "SYS %c: > NUL", cselecteddrive);
901
    exec(buff);
1944 mateusz.vi 902
    sprintf(buff, "FDISK /MBR %u", driveid + 1);
1942 mateusz.vi 903
    exec(buff);
1944 mateusz.vi 904
 
905
    if (READ_OR_WRITE_MBR(MBR_READ, buff, driveid | 0x80) == 0) {
906
      /* active flags for part 0,1,2,3 are at offsets 0x01BE, 0x01CE, 0x01DE, 0x01EE */
907
      unsigned char i, flag_before, changed = 0;
908
      unsigned short memoffset = 0x01BE; /* first partition flag is here */
909
      for (i = 0; i < 4; i++) {
910
        flag_before = buff[memoffset];
911
        buff[memoffset] &= 127;
912
        if (i == partid) buff[memoffset] |= 0x80;
913
        if (flag_before != buff[memoffset]) changed++;
914
        memoffset += 16; /* jump to next partition entry */
915
      }
916
      /* do I need to update the MBR? */
917
      if (changed != 0) READ_OR_WRITE_MBR(MBR_WRITE, buff, driveid | 0x80);
918
    }
919
 
920
    snprintf(buff, sizeof(buff), "%c:\\TEMP", cselecteddrive);
1942 mateusz.vi 921
    mkdir(buff);
922
    return(0);
923
  }
28 mv_fox 924
}
925
 
926
 
280 mateuszvis 927
/* generates locales-related configurations and writes them to file (this
928
 * is used to compute autoexec.bat content) */
929
static void genlocalesconf(FILE *fd, const struct slocales *locales) {
930
 
931
  fprintf(fd, "SET LANG=%s\r\n", locales->lang);
932
 
933
  if (locales->egafile > 0) {
934
    fprintf(fd, "DISPLAY CON=(EGA,,1)\r\n");
935
    if (locales->egafile == 1) {
936
      fprintf(fd, "MODE CON CP PREPARE=((%u) %%DOSDIR%%\\CPI\\EGA.CPX)\r\n", locales->codepage);
937
    } else {
1908 mateusz.vi 938
      fprintf(fd, "MODE CON CP PREPARE=((%u) %%DOSDIR%%\\CPI\\EGA%u.CPX)\r\n", locales->codepage, locales->egafile);
280 mateuszvis 939
    }
940
    fprintf(fd, "MODE CON CP SELECT=%u\r\n", locales->codepage);
941
  }
942
 
943
  if (locales->keybfile > 0) {
1659 bttr 944
    fprintf(fd, "KEYB %s,%d,%%DOSDIR%%\\", locales->keybcode, locales->codepage);
280 mateuszvis 945
    if (locales->keybfile == 1) {
946
      fprintf(fd, "KEYBOARD.SYS");
947
    } else {
1908 mateusz.vi 948
      fprintf(fd, "KEYBRD%u.SYS", locales->keybfile);
280 mateuszvis 949
    }
950
    if (locales->keybid != 0) fprintf(fd, " /ID:%d", locales->keybid);
951
    fprintf(fd, "\r\n");
952
  }
953
}
954
 
955
 
1946 mateusz.vi 956
/* get the DOS "current drive" (0=A:, 1=B:, etc) */
957
static unsigned char get_cur_drive(void);
958
#pragma aux get_cur_drive = \
959
"mov ah, 0x19" /* DOS 1+ GET CURRENT DEFAULT DRIVE */ \
960
"int 0x21" \
961
modify [ah] \
962
value [al]
963
 
964
 
965
/* generates configuration files on the dest drive, this is run once system
966
 * booted successfully on the dst drive (postinst stage) */
1950 mateusz.vi 967
static void bootfilesgen(void) {
28 mv_fox 968
  char buff[128];
969
  FILE *fd;
1946 mateusz.vi 970
  unsigned char bootdrv = get_cur_drive() + 'A';
1950 mateusz.vi 971
  struct slocales locales;
1930 mateusz.vi 972
 
1950 mateusz.vi 973
  /* load locales from ZLOCALES.DAT */
974
  fd = fopen("ZLOCALES.DAT", "rb");
975
  if (fd == NULL) return;
976
  fread(&locales, sizeof(struct slocales), 1, fd);
977
  fclose(fd);
978
 
979
  svarlang_load("INSTALL.LNG", locales.lang);
980
 
1930 mateusz.vi 981
  /****************
982
   * CONFIG.SYS ***
983
   ****************/
1946 mateusz.vi 984
  snprintf(buff, sizeof(buff), "%c:\\CONFIG.SYS", bootdrv);
53 mv_fox 985
  fd = fopen(buff, "wb");
986
  if (fd == NULL) return;
1751 mateusz.vi 987
  fprintf(fd, "; SvarDOS kernel configuration\r\n"
988
              "\r\n"
989
              "; highest allowed drive letter\r\n"
990
              "LASTDRIVE=Z\r\n"
991
              "\r\n"
992
              "; max. number of files that programs are allowed to open simultaneously\r\n"
993
              "FILES=25\r\n");
994
  fprintf(fd, "\r\n"
995
              "; XMS memory driver\r\n"
1930 mateusz.vi 996
              "DEVICE=%c:\\SVARDOS\\HIMEMX.EXE\r\n", bootdrv);
1751 mateusz.vi 997
  fprintf(fd, "\r\n"
998
              "; try moving DOS to upper memory, then to high memory\r\n"
999
              "DOS=UMB,HIGH\r\n");
1000
  fprintf(fd, "\r\n"
1930 mateusz.vi 1001
              "; command interpreter (shell) location and environment size\r\n"
1002
              "SHELL=%c:\\COMMAND.COM /E:512 /P\r\n", bootdrv);
1751 mateusz.vi 1003
  fprintf(fd, "\r\n"
1908 mateusz.vi 1004
              "; NLS configuration\r\n");
1950 mateusz.vi 1005
  fprintf(fd, "COUNTRY=%03u,%u,%c:\\SVARDOS\\COUNTRY.SYS\r\n", locales.countryid, locales.codepage, bootdrv);
1751 mateusz.vi 1006
  fprintf(fd, "\r\n"
1007
              "; CD-ROM driver initialization\r\n"
1930 mateusz.vi 1008
              ";DEVICE=%c:\\DRIVERS\\VIDECDD\\VIDE-CDD.SYS /D:SVCD0001\r\n", bootdrv);
53 mv_fox 1009
  fclose(fd);
1930 mateusz.vi 1010
 
1011
  /****************
1012
   * AUTOEXEC.BAT *
1013
   ****************/
1014
  snprintf(buff, sizeof(buff), "%c:\\TEMP\\AUTOEXEC.BAT", bootdrv);
28 mv_fox 1015
  fd = fopen(buff, "wb");
1930 mateusz.vi 1016
  if (fd == NULL) {
1017
    return;
1018
  } else {
1019
    char *autoexec_bat1 =
1020
      "@ECHO OFF\r\n"
1935 mateusz.vi 1021
      "SET TEMP=#:\\TEMP\r\n"
1022
      "SET DOSDIR=#:\\SVARDOS\r\n"
1930 mateusz.vi 1023
      "SET NLSPATH=%DOSDIR%\\NLS\r\n"
1024
      "SET DIRCMD=/O/P\r\n"
1946 mateusz.vi 1025
      "SET WATTCP.CFG=%DOSDIR%\r\n"
1930 mateusz.vi 1026
      "PATH %DOSDIR%\r\n"
1027
      "PROMPT $P$G\r\n"
1028
      "\r\n"
1029
      "REM enable CPU power saving\r\n"
1030
      "FDAPM ADV:REG\r\n"
1031
      "\r\n";
1032
    char *autoexec_bat2 =
1033
      "REM Uncomment the line below for CDROM support\r\n"
1034
      "REM SHSUCDX /d:SVCD0001\r\n"
1035
      "\r\n"
1036
      "ECHO.\r\n";
1037
 
1935 mateusz.vi 1038
    /* replace all '#' occurences by bootdrive */
1039
    strtr(autoexec_bat1, '#', bootdrv);
1930 mateusz.vi 1040
 
1041
    /* write all to file */
1042
    fputs(autoexec_bat1, fd);
1950 mateusz.vi 1043
    genlocalesconf(fd, &locales);
1930 mateusz.vi 1044
    fputs(autoexec_bat2, fd);
1045
 
1046
    fprintf(fd, "ECHO %s\r\n", svarlang_strid(0x0600)); /* "Welcome to SvarDOS!" */
1047
    fclose(fd);
1048
  }
1049
 
200 mateuszvis 1050
  /*** CREATE DIRECTORY FOR CONFIGURATION FILES ***/
1930 mateusz.vi 1051
  snprintf(buff, sizeof(buff), "%c:\\SVARDOS", bootdrv);
200 mateuszvis 1052
  mkdir(buff);
1930 mateusz.vi 1053
 
1054
  /****************
1055
   * PKG.CFG      *
1056
   ****************/
1935 mateusz.vi 1057
  snprintf(buff, sizeof(buff), "%c:\\SVARDOS\\PKG.CFG", bootdrv);
277 mateuszvis 1058
  fd = fopen(buff, "wb");
1930 mateusz.vi 1059
  if (fd == NULL) {
1060
    return;
1061
  } else {
1062
    char *pkg_cfg =
1063
      "# pkg config file - specifies locations where packages should be installed\r\n"
1064
      "\r\n"
1065
      "# System boot drive\r\n"
1935 mateusz.vi 1066
      "BOOTDRIVE @\r\n"
1930 mateusz.vi 1067
      "\r\n"
1068
      "# DOS core binaries\r\n"
1069
      "DIR BIN @:\\SVARDOS\r\n"
1070
      "\r\n"
1071
      "# Programs\r\n"
1072
      "DIR PROGS @:\\\r\n"
1073
      "\r\n"
1074
      "# Games \r\n"
1075
      "DIR GAMES @:\\\r\n"
1076
      "\r\n"
1077
      "# Drivers\r\n"
1078
      "DIR DRIVERS @:\\DRIVERS\r\n"
1079
      "\r\n"
1080
      "# Development tools\r\n"
1081
      "DIR DEVEL @:\\DEVEL\r\n";
1082
 
1083
    /* replace all @ by the actual boot drive */
1084
    strtr(pkg_cfg, '@', bootdrv);
1085
 
1086
    /* write to file */
1087
    fputs(pkg_cfg, fd);
1088
    fclose(fd);
1089
  }
1090
 
1946 mateusz.vi 1091
  /****************
1092
   * PICOTCP      *
1093
   ****************/
1930 mateusz.vi 1094
  /* TODO (or not? maybe not that useful) */
1095
 
1946 mateusz.vi 1096
  /****************
1097
   * WATTCP.CFG   *
1098
   ****************/
1099
  snprintf(buff, sizeof(buff), "%c:\\SVARDOS\\WATTCP.CFG", bootdrv);
303 mateuszvis 1100
  fd = fopen(buff, "wb");
1101
  if (fd == NULL) return;
1102
  fprintf(fd, "my_ip = dhcp\r\n"
1103
              "#my_ip = 192.168.0.7\r\n"
1104
              "#netmask = 255.255.255.0\r\n"
1105
              "#nameserver = 192.168.0.1\r\n"
1106
              "#nameserver = 192.168.0.2\r\n"
554 mateuszvis 1107
              "#gateway = 192.168.0.1\r\n");
303 mateuszvis 1108
  fclose(fd);
1946 mateusz.vi 1109
 
1110
  /****************
1111
   * POSTINST.BAT *
1112
   ****************/
1113
  /* create the postinst.bat file for actual installation of packages */
1950 mateusz.vi 1114
  fd = fopen("POSTINST.BAT", "wb");
1115
  if (fd == NULL) return;
1946 mateusz.vi 1116
  fprintf(fd,
1117
    "@ECHO OFF\r\n"
1118
    "SET DOSDIR=%c:\\SVARDOS\r\n"
1119
    "PATH %%DOSDIR%%\r\n"
1120
    "ECHO INSTALLING PACKAGES\r\n"
1121
    "COPY \\COMMAND.COM \\CMD.COM\r\n" /* move COMMAND.COM so it does not clashes with the installation of the SVARCOM package */
1122
    "SET COMSPEC=%c:\\CMD.COM\r\n"
1123
    "DEL \\AUTOEXEC.BAT\r\n"
1124
    "COPY AUTOEXEC.BAT \\\r\n"
1950 mateusz.vi 1125
    "DEL AUTOEXEC.BAT\r\n"
1126
    "DEL ZLOCALES.DAT\r\n"
1946 mateusz.vi 1127
    "DEL \\COMMAND.COM\r\n"
1128
    "DEL \\KERNEL.SYS\r\n" /* KERNEL.SYS will be installed from the package in a moment */
1982 mateusz.vi 1129
    "FOR %%%%P IN (*.SVP) DO PKG INOWARN %%%%P\r\n" /* install packages (with warnings muted) */
1946 mateusz.vi 1130
    "DEL *.SVP\r\n", bootdrv, bootdrv);
1131
 
1132
  /* restore COMSPEC and do some cleanup */
1133
  fprintf(fd, "DEL pkg.exe\r\n"
1134
              "DEL install.com\r\n"
1135
              "DEL install.lng\r\n"
1136
              "SET COMSPEC=\\COMMAND.COM\r\n"
1137
              "DEL \\CMD.COM\r\n");
1138
  /* print out the "installation over" message (load codepage first, now that MODE is installed) */
1950 mateusz.vi 1139
  genlocalesconf(fd, &locales);
1946 mateusz.vi 1140
  fprintf(fd, "ECHO.\r\n"
1141
              "ECHO ");
1142
  fprintf(fd, svarlang_strid(0x0502)); /* "SvarDOS installation is over. Please restart your computer now" */
1143
  fprintf(fd, "\r\n"
1144
              "ECHO.\r\n");
1145
  fclose(fd);
1146
 
28 mv_fox 1147
}
1148
 
1149
 
1942 mateusz.vi 1150
static int copypackages(char drvletter, const struct slocales *locales) {
192 mateuszvis 1151
  char pkglist[512];
30 mv_fox 1152
  int i, pkglistlen;
192 mateuszvis 1153
  size_t pkglistflen;
280 mateuszvis 1154
  char buff[1024]; /* must be *at least* 1 sector big for efficient file copying */
1155
  FILE *fd = NULL;
192 mateuszvis 1156
  char *pkgptr;
1942 mateusz.vi 1157
 
79 mv_fox 1158
  newscreen(3);
1942 mateusz.vi 1159
 
192 mateuszvis 1160
  /* load pkg list */
1161
  fd = fopen("install.lst", "rb");
1162
  if (fd == NULL) {
1664 mateusz.vi 1163
    mdr_cout_str(10, 30, "ERROR: INSTALL.LST NOT FOUND", COLOR_BODY, 80);
1661 mateusz.vi 1164
    mdr_dos_getkey();
192 mateuszvis 1165
    return(-1);
1166
  }
1125 mateusz.vi 1167
  pkglistflen = fread(pkglist, 1, sizeof(pkglist) - 2, fd);
192 mateuszvis 1168
  fclose(fd);
1125 mateusz.vi 1169
  if (pkglistflen == sizeof(pkglist) - 2) {
1664 mateusz.vi 1170
    mdr_cout_str(10, 30, "ERROR: INSTALL.LST TOO LARGE", COLOR_BODY, 80);
1661 mateusz.vi 1171
    mdr_dos_getkey();
192 mateuszvis 1172
    return(-1);
1173
  }
1125 mateusz.vi 1174
  /* mark the end of list */
1175
  pkglist[pkglistflen] = 0;
1176
  pkglist[pkglistflen + 1] = 0xff;
192 mateuszvis 1177
  /* replace all \r and \n chars by 0 bytes, and count the number of packages */
1178
  pkglistlen = 0;
1179
  for (i = 0; i < pkglistflen; i++) {
1180
    switch (pkglist[i]) {
1181
      case '\n':
1182
        pkglistlen++;
1183
        /* FALLTHRU */
1184
      case '\r':
1185
        pkglist[i] = 0;
1186
        break;
1187
    }
1188
  }
280 mateuszvis 1189
 
1930 mateusz.vi 1190
  /* copy pkg.exe, install.com and install.lng to the new drive, along with all packages */
1942 mateusz.vi 1191
  snprintf(buff, sizeof(buff), "%c:\\TEMP\\PKG.EXE", drvletter);
1930 mateusz.vi 1192
  fcopy(buff, buff + 8, buff, sizeof(buff));
1942 mateusz.vi 1193
  snprintf(buff, sizeof(buff), "%c:\\TEMP\\INSTALL.COM", drvletter);
1930 mateusz.vi 1194
  fcopy(buff, buff + 8, buff, sizeof(buff));
1942 mateusz.vi 1195
  snprintf(buff, sizeof(buff), "%c:\\TEMP\\INSTALL.LNG", drvletter);
1930 mateusz.vi 1196
  fcopy(buff, buff + 8, buff, sizeof(buff));
1197
 
280 mateuszvis 1198
  /* copy packages */
192 mateuszvis 1199
  for (i = 0;; i++) {
1125 mateusz.vi 1200
    RETRY_ENTIRE_LIST:
1201
 
192 mateuszvis 1202
    /* move forward to nearest entry or end of list */
1125 mateusz.vi 1203
    for (pkgptr = pkglist; *pkgptr == 0; pkgptr++);
1204
    if (*pkgptr == 0xff) break; /* end of list: means all packages have been processed */
1205
 
1206
    /* is this package present on the floppy disk? */
1207
    TRY_NEXTPKG:
1208
    sprintf(buff, "%s.svp", pkgptr);
1669 mateusz.vi 1209
    if (!fileexists(buff)) {
1125 mateusz.vi 1210
      while (*pkgptr != 0) pkgptr++;
1211
      while (*pkgptr == 0) pkgptr++;
1212
      /* end of list? ask for next floppy, there's nothing interesting left on this one */
1213
      if (*pkgptr == 0xff) {
1664 mateusz.vi 1214
        putstringnls(12, 1, COLOR_BODY, 4, 1); /* "INSERT THE DISK THAT CONTAINS THE REQUIRED FILE AND PRESS ANY KEY" */
1661 mateusz.vi 1215
        mdr_dos_getkey();
1664 mateusz.vi 1216
        video_putstringfix(12, 1, COLOR_BODY, "", 80); /* erase the 'insert disk' message */
1125 mateusz.vi 1217
        goto RETRY_ENTIRE_LIST;
1218
      }
1219
      goto TRY_NEXTPKG;
1220
    }
1221
 
1953 mateusz.vi 1222
    /* copy the package */
1223
    snprintf(buff, sizeof(buff), svarlang_strid(0x0400), i+1, pkglistlen, pkgptr); /* "Copying package %d/%d: %s" */
36 mv_fox 1224
    strcat(buff, "       ");
1664 mateusz.vi 1225
    mdr_cout_str(10, 1, buff, COLOR_BODY, 40);
1125 mateusz.vi 1226
 
1227
    /* proceed with package copy */
1942 mateusz.vi 1228
    sprintf(buff, "%c:\\TEMP\\%s.svp", drvletter, pkgptr);
1930 mateusz.vi 1229
    if (fcopy(buff, buff + 8, buff, sizeof(buff)) != 0) {
1664 mateusz.vi 1230
      mdr_cout_str(10, 30, "READ ERROR", COLOR_BODY, 80);
1661 mateusz.vi 1231
      mdr_dos_getkey();
192 mateuszvis 1232
      return(-1);
55 mv_fox 1233
    }
1125 mateusz.vi 1234
    /* jump to next entry or end of list and zero out the pkg name in the process */
1235
    while ((*pkgptr != 0) && (*pkgptr != 0xff)) {
1236
      *pkgptr = 0;
1237
      pkgptr++;
1238
    }
28 mv_fox 1239
  }
1934 mateusz.vi 1240
 
1950 mateusz.vi 1241
  /* dump locales into ZLOCALES.DAT so the 2nd stage install can get them back */
1242
  snprintf(buff, sizeof(buff), "%c:\\TEMP\\ZLOCALES.DAT", drvletter);
1934 mateusz.vi 1243
  fd = fopen(buff, "wb");
1244
  if (fd == NULL) return(-1);
1950 mateusz.vi 1245
  fwrite(locales, sizeof(struct slocales), 1, fd);
280 mateuszvis 1246
  fclose(fd);
1247
 
1946 mateusz.vi 1248
  /* prepare a dummy autoexec.bat that will exec install and call temp\postinst.bat */
1942 mateusz.vi 1249
  snprintf(buff, sizeof(buff), "%c:\\autoexec.bat", drvletter);
280 mateuszvis 1250
  fd = fopen(buff, "wb");
1251
  if (fd == NULL) return(-1);
1252
  fprintf(fd, "@ECHO OFF\r\n"
1950 mateusz.vi 1253
              "CD TEMP\r\n"
1946 mateusz.vi 1254
              "install\r\n"   /* installer will run in 2nd stage (generating autoexec.bat, pkg.cfg and stuff) */
280 mateuszvis 1255
              "postinst.bat\r\n");
1256
  fclose(fd);
1257
 
192 mateuszvis 1258
  return(0);
28 mv_fox 1259
}
1260
 
1261
 
42 mv_fox 1262
static void finalreboot(void) {
56 mv_fox 1263
  int y = 9;
79 mv_fox 1264
  newscreen(2);
1673 mateusz.vi 1265
  y += putstringnls(y, 1, COLOR_BODY, 5, 0); /* "Your computer will reboot now." */
1266
  y += putstringnls(y, 1, COLOR_BODYWARN, 5, 1); /* Please remove the installation disk from your drive" */
1664 mateusz.vi 1267
  putstringnls(++y, 1, COLOR_BODY, 0, 5); /* "Press any key..." */
1661 mateusz.vi 1268
  mdr_dos_getkey();
42 mv_fox 1269
  reboot();
1270
}
1271
 
1272
 
192 mateuszvis 1273
static void loadcp(const struct slocales *locales) {
42 mv_fox 1274
  char buff[64];
67 mv_fox 1275
  if (locales->codepage == 437) return;
1662 mateusz.vi 1276
  mdr_cout_locate(1, 0);
67 mv_fox 1277
  if (locales->egafile == 1) {
310 mateuszvis 1278
    snprintf(buff, sizeof(buff), "MODE CON CP PREP=((%u) EGA.CPX) > NUL", locales->codepage);
42 mv_fox 1279
  } else {
310 mateuszvis 1280
    snprintf(buff, sizeof(buff), "MODE CON CP PREP=((%u) EGA%d.CPX) > NUL", locales->codepage, locales->egafile);
42 mv_fox 1281
  }
1918 mateusz.vi 1282
  exec(buff);
67 mv_fox 1283
  snprintf(buff, sizeof(buff), "MODE CON CP SEL=%u > NUL", locales->codepage);
1918 mateusz.vi 1284
  exec(buff);
42 mv_fox 1285
  /* below I re-init the video controller - apparently this is required if
65 mv_fox 1286
   * I want the new glyph symbols to be actually applied, at least some
1287
   * (broken?) BIOSes, like VBox, apply glyphs only at next video mode change */
1908 mateusz.vi 1288
  _asm {
1289
    push bx
1290
    mov ah, 0x0F  /* get current video mode */
1291
    int 0x10      /* al contains the current video mode now */
1292
    or al, 128    /* set high bit of AL to instruct BIOS not to flush VRAM's content (EGA+) */
1293
    xor ah, ah    /* re-set video mode (to whatever is set in AL) */
1294
    int 0x10
1295
    pop bx
42 mv_fox 1296
  }
1297
}
1298
 
200 mateuszvis 1299
 
1671 mateusz.vi 1300
int main(void) {
1950 mateusz.vi 1301
  struct slocales locales;
28 mv_fox 1302
  int targetdrv;
73 mv_fox 1303
  int action;
28 mv_fox 1304
 
1911 mateusz.vi 1305
  /* setup an internal int 24h handler ("always fail") so DOS does not output
1306
   * the ugly "abort, retry, fail" messages */
1918 mateusz.vi 1307
  install_int24();
908 mateusz.vi 1308
 
1930 mateusz.vi 1309
  /* init screen and detect mono adapters */
1310
  if (mdr_cout_init(NULL, NULL) == 0) {
1311
    /* overload color scheme with mono settings */
1312
    COLOR_TITLEBAR = 0x70;
1313
    COLOR_TITLEVER = 0x70;
1314
    COLOR_BODY = 0x07;
1315
    COLOR_BODYWARN = 0x07;
1316
    COLOR_SELECT = 0x70;
1317
    COLOR_SELECTCUR = 0x07;
1318
  }
1319
 
1320
  /* is it stage 2 of the installation? */
1950 mateusz.vi 1321
  if (fileexists("ZLOCALES.DAT")) goto GENCONF;
1930 mateusz.vi 1322
 
1671 mateusz.vi 1323
  /* read the svardos build revision (from floppy label) */
1324
  {
1325
    const char *fspec = "*.*";
1326
    const char *res = (void*)0x9E; /* default DTA is at PSP:80h, field 1Eh of DTA is the ASCIZ file name */
868 mateusz.vi 1327
 
1671 mateusz.vi 1328
    _asm {
1329
      push cx
1330
      push dx
1331
 
1332
      mov ax, 0x4e00  /* findfirst */
1333
      mov cx, 0x08    /* file attr mask, 0x08 = volume label */
1334
      mov dx, fspec
1335
      int 0x21
1336
      jnc good
1672 mateusz.vi 1337
      mov bx, res
1338
      mov [bx], byte ptr 0
1671 mateusz.vi 1339
      good:
1340
 
1341
      pop dx
1342
      pop cx
1343
    }
1344
 
1345
    memcpy(BUILDSTRING, res, 12);
1346
  }
1347
 
73 mv_fox 1348
 SelectLang:
1950 mateusz.vi 1349
  action = selectlang(&locales); /* welcome to svardos, select your language */
1930 mateusz.vi 1350
  if (action != MENUNEXT) goto QUIT;
1950 mateusz.vi 1351
  loadcp(&locales);
1352
  svarlang_load("INSTALL.LNG", locales.lang); /* NLS support */
865 mateusz.vi 1353
 
1950 mateusz.vi 1354
  action = selectkeyb(&locales);  /* what keyb layout should we use? */
1930 mateusz.vi 1355
  if (action == MENUQUIT) goto QUIT;
1908 mateusz.vi 1356
  if (action == MENUPREV) goto SelectLang;
73 mv_fox 1357
 
1358
 WelcomeScreen:
190 mateuszvis 1359
  action = welcomescreen(); /* what svardos is, ask whether to run live dos or install */
1930 mateusz.vi 1360
  if (action == MENUQUIT) goto QUIT;
1950 mateusz.vi 1361
  if (action == MENUPREV) goto SelectLang;
1669 mateusz.vi 1362
 
1942 mateusz.vi 1363
 SelDriveScreen:
1364
  targetdrv = selectdrive();
1930 mateusz.vi 1365
  if (targetdrv == MENUQUIT) goto QUIT;
73 mv_fox 1366
  if (targetdrv == MENUPREV) goto WelcomeScreen;
1942 mateusz.vi 1367
 
1368
  action = preparedrive(targetdrv); /* what drive should we install to? check avail. space */
1369
  if (action == MENUQUIT) goto QUIT;
1370
  if (action == MENUPREV) goto SelDriveScreen;
1944 mateusz.vi 1371
  targetdrv = (targetdrv & 0xff) + 'A'; /* convert the part+hd+drv value to a DOS letter */
1942 mateusz.vi 1372
 
1373
  /* copy packages to dst drive */
1950 mateusz.vi 1374
  if (copypackages(targetdrv, &locales) != 0) goto QUIT;
1930 mateusz.vi 1375
  finalreboot(); /* remove the install medium and reboot */
73 mv_fox 1376
 
1930 mateusz.vi 1377
  goto QUIT;
1378
 
1379
 GENCONF: /* second stage of the installation (run from the destination disk) */
1950 mateusz.vi 1380
  bootfilesgen(); /* generate boot files and other configurations */
1930 mateusz.vi 1381
 
1382
 QUIT:
1662 mateusz.vi 1383
  mdr_cout_locate(0, 0);
1384
  mdr_cout_close();
28 mv_fox 1385
  return(0);
1386
}