summaryrefslogtreecommitdiff
path: root/v4.0/src/CMD/FDISK/INT13.C
blob: ded7ff09b915ad69125f141889bcba2dff49aa07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "dos.h"                                                        /* AN000 */
#include "fdisk.h"                                                      /* AN000 */
#include "extern.h"                                                     /* AN000 */
#include "subtype.h"                                                    /* AN000 */
#include "fdiskmsg.h"                                                   /* AN000 */

#include "string.h"
#include "stdio.h"
#include "stdlib.h"

/* int     printf(char *, ...); */

/*  */
char get_disk_info()

BEGIN

unsigned char   i;

        /* Initialize values */
        number_of_drives = uc(0);                                       /* AC000 */
        for (i=uc(0); i < uc(2); i++)                                   /* AC000 */
           BEGIN
            total_disk[i] = u(0);                                       /* AC000 */
            total_mbytes[i] = f(0);                                     /* AC000 */
            max_sector[i] = uc(0);                                      /* AC000 */
            max_head[0] = u(0);                                         /* AC004 */
           END

        /* See how many drives there are */
        if (get_drive_parameters(uc(0x80)))                             /* AC000 */

           BEGIN
            /* Get the drive parameters for all drives */
            for (i = uc(0); i < number_of_drives;i++)                   /* AC000 */

               BEGIN
                if (get_drive_parameters(uc(0x80)+i))                   /* AC000 */

                   BEGIN
                    /* Save drive parameters */
                    max_sector[i] = ((unsigned char)(regs.h.cl & 0x3F));
                    max_head[i] = ((unsigned)(regs.h.dh +1));          /* AC004 */
#if IBMCOPYRIGHT
                    total_disk[i] = ((((unsigned)(regs.h.cl & 0xC0 )) & 0x00C0) << 2)+ ((unsigned)regs.h.ch) +1;
#else
	                 total_disk[i] = ((((unsigned)(regs.h.cl & 0xc0)) << 2) | regs.h.ch) + 1;
#endif
                    total_mbytes[i] = cylinders_to_mbytes(total_disk[i],i);  /* AN004 */
                   END
                else
                   BEGIN
                    good_disk[i] = FALSE;
                    return(FALSE);
                   END
               END
            return(TRUE);
           END
        else
            /* No drives present */
            BEGIN
             no_fatal_error = FALSE;
             return(FALSE);
            END
END



/*  */
char write_boot_record(cylinder,which_disk)

unsigned    cylinder;
unsigned char   which_disk;

BEGIN

char i;
char j;
char far *buffer_pointer = boot_record;

        /* Setup read, always on a cylinder boundary */
        regs.h.ah = uc(WRITE_DISK);                                     /* AC000 */
        regs.h.al = uc(1);                                              /* AC000 */
        regs.h.dh = uc(0);                                              /* AC000 */
        regs.h.cl = uc(0x01);                                           /* AC000 */

        /* Specify the disk */
        regs.h.dl = which_disk + 0x80;

        /* Need to scramble CX so that sectors and cyl's are in INT 13 format */

        if (cylinder > u(255))                                          /* AC000 */
           BEGIN
            regs.h.cl = regs.h.cl | ((unsigned char)((cylinder /256) << 6));
           END
        regs.h.ch = (unsigned char)(cylinder & 0xFF);

        /* Point at the place to read the boot record */
        regs.x.bx = FP_OFF(buffer_pointer);
        segregs.es = FP_SEG(buffer_pointer);

        /* write the boot record */
        DiskIo(&regs,&regs,&segregs);                         /* AC000 */

        /* Check for error reading it */
        if ((regs.x.cflag & 1) != u(1))                                 /* AC000 */
           BEGIN
            return(TRUE);
           END
        else
           BEGIN
            /* Tell user there was an error */
            clear_screen(u(0),u(0),u(24),u(79));                        /* AC000 */
            display(error_2);
            no_fatal_error = FALSE;
            return(FALSE);
           END
END

/*  */
unsigned verify_tracks(pointer,type)

char pointer;
char type;

BEGIN
unsigned        i;
unsigned        location;
unsigned        sectors_per_fat;
unsigned        cur_cyl;
unsigned        verify_cyl;
unsigned        num_tracks;
unsigned long   total_sectors;
char   golden_tracks;
char   retry;
unsigned        char   cur_head;                                        /* AC004 */

char far *buffer_pointer = boot_record;
char head;



        for (i=u(0); i< u(BYTES_PER_SECTOR);i++)                        /* AC000 */
           BEGIN
            /* Put something other then 0's so that unformatted FAT looks full */

            boot_record[i] = uc(0xF6);                                  /* AC000 */
           END

        /* Get the start cylinder for the sweep */
        cur_cyl = free_space[pointer].start;

        /* Also keep track of what it is */
        verify_cyl = cur_cyl;

        /* Initialize the start head -assume 0*/
        cur_head = uc(0);                                               /* AC004 */
        if ((type == c(PRIMARY)) && (cur_cyl == u(0)))                  /* AC000 */
          BEGIN

           /* It's head 1 - NOTE: This is convience for PC-DOS because it is */
           /* always this way - This may have to be beefed up for IFS  */
           cur_head = uc(1);                                            /* AC004 */
          END

        /* Now go figure out the number of golden sectors needed. Use the */
        /* allocation equation in the fixed disk section of DOS Tech Ref. */
        /*                                                                */
        /*  TS = Free cyl's * sector/track * track/cyl                    */
        /*  RS = 1                                                        */
        /*  D  = 512                                                      */
        /*  BPD = 32                                                      */
        /*  BPS = BYTES_PER_SECTOR                                        */
        /*  CF = 2                                                        */
        /*  SPF = Solve                                                   */
        /*  SPC = 4 or 8                                                  */
        /*  BPC = 1.5 or 2                                                */
        /*                                                                */
        /*  Golden Sectors = RS + 2(SPF) + BPD(D)  + (DOSFILES)           */
        /*                                 ������   if bootable           */
        /*                                  BPS                           */

        total_sectors = ((unsigned long)free_space[pointer].space) * max_head[cur_disk] * max_sector[cur_disk];

        /* Chop off one track if it starts on head 1 */
        if (cur_head == uc(1))                                          /* AC004 */
           BEGIN
            total_sectors = total_sectors - max_sector[cur_disk];
           END

        /* See if 12 or 16 bit fat */
        if (total_sectors > (unsigned long)FAT16_SIZE)
           BEGIN
            /* 16 bit */
            sectors_per_fat = ((unsigned)((total_sectors -33)/(2 + ((BYTES_PER_SECTOR *4)/2))));
           END
        else
           BEGIN
            /* 12 bit */
            sectors_per_fat = ((unsigned)((total_sectors -33)/(2 + ((BYTES_PER_SECTOR *8)/1.5))));
           END

        /* Round up one just to handle any rounding errors */
        sectors_per_fat++;

        /* Now see how many tracks */
        num_tracks = (sectors_per_fat*2) + 33;

        /* If primary and drive 0, add in enough for system files */
        if ((type == c(PRIMARY)) && (cur_disk == c(0)))                 /* AC000 */
           BEGIN
            num_tracks = num_tracks + SYSTEM_FILE_SECTORS;
           END

	#if IBMCOPYRIGHT
         /* Now convert to tracks */
         num_tracks = num_tracks/max_sector[cur_disk];

         /* Handle upward rounding */
         if (num_tracks%max_sector[cur_disk] != u(0))                   /* AC000 */
            BEGIN
             num_tracks++;
            END
	#else
			/* Handle upward rounding */										/* The problem with the IBM code is  				*/
			if (num_tracks%max_sector[cur_disk] != u(0))					/* that if num_tracks is < max_sector[cur_disk] */
				BEGIN																	/* the num_tracks becomes 0 due to the integer  */																
					num_tracks = num_tracks + max_sector[cur_disk];	  	/* division and num_tracks will not be inc'd .  */
				END																	/* This section of code overcomes that.		   */

			/* Now convert to tracks */
			num_tracks = num_tracks/max_sector[cur_disk];
	#endif


         golden_tracks = FALSE;
         while (!golden_tracks)
            BEGIN
             for (i = u(0);i < num_tracks; i++)                         /* AC000 */
                BEGIN
                 retry = c(0);                                          /* AC000 */
                 do
                    BEGIN
                     retry++;
                     /* Specify the operation */
                     regs.h.ah = uc(WRITE_DISK);                        /* AC000 */

                     /* Specify number of sectors */
                     regs.h.al = ((unsigned char)max_sector[cur_disk]);

                     /* Specify the start sectors */
                     regs.h.cl = uc(1);                                 /* AC000 */

                     /* Need to scramble CX so that sectors and cyl's are in INT 13 format */
                     if (cur_cyl > u(255))                              /* AC000 */
                        BEGIN
                         regs.h.cl = regs.h.cl | ((unsigned char)((cur_cyl/256) << 6));
                        END
                     regs.h.ch = ((unsigned char)cur_cyl) & 0xFF;

                     /* Specify the disk */
                     regs.h.dl = ((unsigned char)cur_disk) + 0x80;

                     /* Specify the head */
                     regs.h.dh = cur_head;                              /* AC004 */

                     /* Point at the place to write */
                     regs.x.bx = FP_OFF(buffer_pointer);
                     segregs.es = FP_SEG(buffer_pointer);

                     /* write the track */
                     DiskIo(&regs,&regs,&segregs);            /* AC000 */

                    END
                    while (((regs.x.cflag & 1) == u(1)) && (retry != c(5))); /* AC000 */

                 /* See if we had a good read */
                 if ((regs.x.cflag & 1) != u(1))                        /* AC000 */
                    BEGIN
                     golden_tracks = TRUE;
                     /* Get the next head */
                     cur_head++;
                     if (cur_head == (uc(max_head[cur_disk] -1)))       /* AC004 */
                        BEGIN
                         /* Up to the next cylinder */
                         cur_head = uc(0);                              /* AC004 */
                         cur_cyl++;

                         /* Check to see if we've reached the end of the free_space*/
                         if (cur_cyl > free_space[pointer].end)
                            BEGIN
                             /* It is, so return with the cyl offset equal to the freespace */
                             return(free_space[pointer].space);
                            END
                        END
                    END
                 else
                    BEGIN
                     /* Get out of the for loop, with a false flag */
                     golden_tracks = FALSE;

                     /* Bump up to the next cylinder boundary */
                     cur_cyl++;
                     cur_head = uc(0);                                  /* AC004 */

                     /* Save the new verify start point */
                     verify_cyl = cur_cyl;

                     break;
                    END
                END  /* for num_tracks */
            END /* while !golden_tracks */

        /* All done, return the offset from original cyl to the new one */
        return(verify_cyl - free_space[pointer].start);
END


/*  */
char get_drive_parameters(drive)

unsigned char   drive;

BEGIN
        /* See how many drives there are */
        regs.h.ah = uc(DISK_INFO);                                      /* AC000 */
        regs.h.dl = drive;
        DiskIo(&regs,&regs,&segregs);                                   /* AC000 */

        /* See if any drives exist */
        if ((regs.h.dl == uc(0)) || ((regs.x.cflag & 1) == u(1)))       /* AC000 */
           BEGIN
            display(error_1);
            return(FALSE);
           END
        else
           BEGIN
            /* Save the number of drives */
            number_of_drives = regs.h.dl;
            return(TRUE);
           END
END

/*  */
char read_boot_record(cylinder,which_disk,which_head,which_sector)      /* AC000 */

unsigned        cylinder;
unsigned char   which_disk;
unsigned char   which_head;                                             /* AN000 */
unsigned char   which_sector;                                           /* AN000 */

BEGIN

char far *buffer_pointer = boot_record;

        /* Setup read, always on a cylinder boundary */
        regs.h.ah = uc(READ_DISK);                                      /* AC000 */
        regs.h.al = uc(1);                                              /* AC000 */
        regs.h.dh = which_head;                                         /* AC000 */
        regs.h.cl = which_sector;                                       /* AC000 */

        /* Specify the disk */
        regs.h.dl = which_disk + 0x80;

        /* Need to scramble CX so that sectors and cyl's are in INT 13 format */

        if (cylinder > u(255))                                          /* AC000 */
           BEGIN
            regs.h.cl = regs.h.cl | ((char)((cylinder /256) << 6));
           END
        regs.h.ch = (unsigned char)(cylinder & 0xFF);

        /* Point at the place to write the boot record */
        regs.x.bx = FP_OFF(buffer_pointer);
        segregs.es = FP_SEG(buffer_pointer);

        /* read in the boot record */
        DiskIo(&regs,&regs,&segregs);                         /* AC000 */
        /* Check for error reading it */
        if ((regs.x.cflag & 1) != u(1))                                 /* AC000 */
           BEGIN
            return(TRUE);
           END
        else
           BEGIN
            /* Tell user there was an error */
            good_disk[which_disk] = FALSE;
            clear_screen(u(0),u(0),u(24),u(79));                        /* AC000 */
            return(FALSE);
           END
END

/*  */
void DiskIo(InRegs,OutRegs,SegRegs)
union   REGS    *InRegs;
union   REGS    *OutRegs;
struct  SREGS   *SegRegs;

BEGIN

        char    *WritePtr;

#ifdef DEBUG

        switch(InRegs->h.ah)
              {
                case 0:
                case 1:
                case 2:
                case 4:
                case 8:
                case 15:
                case 16:
                        int86x((int)DISK,InRegs,OutRegs,SegRegs);                         /* AC000 */
                        break;

                default:
                        WritePtr = getenv("WRITE");
                        if (strcmpi(WritePtr,"ON") != 0)
                               BEGIN
                                printf("\nDisallowing Disk I/O Request\n");
                                printf("AX:%04X BX:%04X CX:%04X DX:%04X ES:%04X\n",
                                        InRegs->x.ax,InRegs->x.bx,InRegs->x.cx,InRegs->x.dx,SegRegs->es);

                                OutRegs->h.ah = (unsigned char) 0;
                                OutRegs->x.cflag = (unsigned) 0;
                                END
                         else int86x((int)DISK,InRegs,OutRegs,SegRegs);

                        break;

                }

#else

        int86x((int)DISK,InRegs,OutRegs,SegRegs);                         /* AC000 */

#endif

        return;

END