Subversion Repositories SvarDOS

Rev

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