DOSFS 67.9 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*> c.DOSFS <*/
/*-------------------------------------------------------------------------*/
17
/* DOSFS (image filing system module)           Copyright (c) 1990 JGSmith */
Neil Turton's avatar
Neil Turton committed
18 19 20 21 22
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
23
#include <stdbool.h>
Neil Turton's avatar
Neil Turton committed
24 25 26
#include "kernel.h"
#include "swis.h"
#include "ctype.h"
27
#include "debuglib/debuglib.h"
Neil Turton's avatar
Neil Turton committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#undef DOSFS_DiscFormat
#undef DOSFS_LayoutStructure

#include "h.ASCII"
#include "h.DOSFS"
#include "h.DOSFSctl"
#include "h.debug"
#include "h.DOSFShdr"
#include "h.ADFSshape"
#include "h.DOSshape"
#include "h.FSerrors"
#include "h.DOSmapping"
#include "h.DOSnaming"
#include "h.DOSFSops"
#include "h.MultiFS"
#include "h.wcompare"
#include "h.BOOTBLOCK"  /* for the default BOOT BLOCK structure */
#include "h.TIMEconv"   /* for format time-stamping */
47
#include "h.MsgTrans"
48
#include "h.Statics"
Neil Turton's avatar
Neil Turton committed
49 50 51

/*-------------------------------------------------------------------------*/

52
#define imagetype (0xFC8) /* as allocated by Acorn : "DOSDisc" */
Neil Turton's avatar
Neil Turton committed
53 54 55 56

/*-------------------------------------------------------------------------*/
/* global (static) variables used within the module */

57 58 59
FILEhand    *FILE_list = NULL ;           /* Open file handle list */
mapentry    *maplist = NULL ;             /* DOS/RISC OS filetype mapping chain */
int         discopswi = FileCore_DiscOp;  /* Choice of whether we attempt a DiscOp64 or just DiscOp */
Neil Turton's avatar
Neil Turton committed
60
#if 1 /* SMC_TRUNCATE */
61
int         module_flags = 0 ;            /* Global flags */
Neil Turton's avatar
Neil Turton committed
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
#endif

/* The following are used for parameter returns to the RISC OS world. They
 * are provided as static structures to ensure they are not de-allocated
 * when we leave the C world (since normal variables are allocated from the
 * stack).
 */
FS_open_block fblock ;
FS_datestamp  tstamp ;
FS_cat_entry  fcat ;
FS_dir_block  dblock ;
FS_free_space fspace ;
char          tline[MaxString] = {0} ; /* static filename return area */

/* These are used to create the MessageTrans tokens for the format menu
 * and help text.
 */
#define FORMAT_FMT     "FMT%d"
#define HELP_FMT       "FMTH%d"

/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* Register our filing system with RISC OS */

87
static _kernel_oserror *declare_FS(void *privateword)
Neil Turton's avatar
Neil Turton committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
 _kernel_swi_regs rset ;
#if 1 /* SMC_TRUNCATE */
 _kernel_oserror *err;
#endif
 word             infoblock[9] ; /* nasty constant */

#if 1 /* SMC_TRUNCATE */
 /* Read the CMOS RAM truncate bit state */
#define FileSwitchCMOS 0x1C
#define FileSwitchTruncateNamesCMOSBit 0x01
 rset.r[0] = 161;    /* Reason code "ReadCMOS" */
 rset.r[1] = FileSwitchCMOS;
 if ((err = _kernel_swi(OS_Byte, &rset, &rset)) != NULL)
  return err;
 if (rset.r[2] & FileSwitchTruncateNamesCMOSBit) {
104
  dprintf(("","File names will be truncated.\n"));
Neil Turton's avatar
Neil Turton committed
105 106
  module_flags |= TRUNCATE_NAMES;
 } else {
107
  dprintf(("","Long file names will generate an error.\n"));
Neil Turton's avatar
Neil Turton committed
108 109 110 111
  module_flags &= ~TRUNCATE_NAMES;
 }
#endif

112
 /* register our module as an image filing system */
Neil Turton's avatar
Neil Turton committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126
 /* "OS_FSControl" reason 35 */
 infoblock[0] = (word)0x00000000 ; /* bit27 is only significant flag */
 infoblock[1] = (word)imagetype ;
 infoblock[2] = ((word)DOSFS_Open     - (word)moduleBase) ;
 infoblock[3] = ((word)DOSFS_GetBytes - (word)moduleBase) ;
 infoblock[4] = ((word)DOSFS_PutBytes - (word)moduleBase) ;
 infoblock[5] = ((word)DOSFS_Args     - (word)moduleBase) ;
 infoblock[6] = ((word)DOSFS_Close    - (word)moduleBase) ;
 infoblock[7] = ((word)DOSFS_File     - (word)moduleBase) ;
 infoblock[8] = ((word)DOSFS_Func     - (word)moduleBase) ;

 rset.r[0] = 35 ; /* reason code "FSControl_AddImage" */
 rset.r[1] = (word)moduleBase ;
 rset.r[2] = ((word)infoblock - (word)moduleBase) ;
127
 rset.r[3] = (word)privateword ;
Neil Turton's avatar
Neil Turton committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

 return(_kernel_swi(OS_FSControl,&rset,&rset)) ;
}

/*-------------------------------------------------------------------------*/
/* select_FS:
 * Select "DOSFS" as the current filing system.
 */
static _kernel_oserror *select_FS(void)
{
 _kernel_swi_regs rset ;

 rset.r[0] = FSControl_SelectFS ;               /* operation */
 rset.r[1] = (unsigned int)moduleName() ;       /* filing system name */
 return(_kernel_swi(OS_FSControl,&rset,&rset)) ;
}

/*-------------------------------------------------------------------------*/

147
static _kernel_oserror *FSSWI_DiscFormat(_kernel_swi_regs *rset,void *privateword)
Neil Turton's avatar
Neil Turton committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
{
 /* in:  r0 = pointer to disc format structure to be filled in
  *      r1 = vetting SWI (normally "ADFS_VetFormat")
  *      r2 = r1 parameter to the vetting SWI
  *      r3 = format specifier
  * out: r0 = pointer to disc format specification structure (updated)
  */
 _kernel_swi_regs  urset ;
 _kernel_oserror  *rerror = NULL ;
 format_spec      *fspec ;

 tracef4("DOSFS: FSSWI_DiscFormat: r0 = &%08X, r1 = &%08X, r2 = &%08X, r3 = &%08X\n",rset->r[0],rset->r[1],rset->r[2],rset->r[3]) ;

 /* see "format_spec" structure in "h.MultiFS" */
 /* place the desired disk format specification into the referenced structure */
 fspec = &(DOS_formatinfo[DOS_formats[rset->r[3]].findex]) ;
164
 dprintf(("","DOSFS: FSSWI_DiscFormat: found format_spec &%08X\n",(word)fspec)) ;
Neil Turton's avatar
Neil Turton committed
165 166
 (void)memmove((void *)rset->r[0],fspec,sizeof(format_spec)) ;

167 168 169 170 171 172
 dprintf(("","DOSFS: FSSWI_DiscFormat: fspec->secsize  = %d\n",fspec->secsize)) ;
 dprintf(("","DOSFS: FSSWI_DiscFormat: fspec->secstrk  = %d\n",fspec->secstrk)) ;
 dprintf(("","DOSFS: FSSWI_DiscFormat: fspec->density  = %d\n",fspec->density)) ;
 dprintf(("","DOSFS: FSSWI_DiscFormat: fspec->options  = &%02X\n",fspec->options)) ;
 dprintf(("","DOSFS: FSSWI_DiscFormat: fspec->startsec = %d\n",fspec->startsec)) ;
 dprintf(("","DOSFS: FSSWI_DiscFormat: fspec->tracks   = %d\n",fspec->tracks)) ;
Neil Turton's avatar
Neil Turton committed
173 174 175 176 177 178 179 180

 /* "VetFormat" SWI call:
  * in:  r0 = pointer to required format structure
  *      r1 = VetFormat parameter (r2 passed to "DiscFormat")
  * out: registers preserved (but structure possibly upgraded)
  */
 urset.r[0] = rset->r[0] ; /* copy of disc format structure */
 urset.r[1] = rset->r[2] ; /* parameter for the VetFormat call */
Kevin Bracey's avatar
Kevin Bracey committed
181
 if ((rerror = _kernel_swi(rset->r[1],&urset,&urset)) == NULL)
Neil Turton's avatar
Neil Turton committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  {
   /* Re-check that the structure is still acceptable to us:
    *  Fields that cannot change:
    *     fspec->secsize  fspec->secstrk  fspec->density  fspec->options  fspec->startsec  fspec->tracks
    *
    * Fields that can possibly be changed:
    *     fspec->gap1side0  fspec->gap1side1  fspec->gap3
    * Some research is needed here to discover what values are acceptable
    * to standard MS-DOS machines.
    *
    * Fields that are acceptable to change:
    *     fspec->secileave  fspec->sideskew  fspec->trackskew  fspec->fillvalue
    */
   format_spec *rfspec = (format_spec *)urset.r[0] ;
   if ((rfspec->secsize != fspec->secsize) || (rfspec->secstrk != fspec->secstrk) || (rfspec->density != fspec->density) || (rfspec->options != fspec->options) || (rfspec->startsec != fspec->startsec) || (rfspec->tracks != fspec->tracks))
    rerror = global_error(err_badformat) ; /* changes unacceptable to us */
  }

 return(rerror) ;
201
 UNUSED(privateword) ;
Neil Turton's avatar
Neil Turton committed
202 203
}

204
static int testforbpb(ADFS_drecord *dr,char *sector,bool floppy,
205 206
                      int *numFATs,int *FATsize,int *magic,int *ROOTsize,
                      int *Atari,int *dblstep,int *numRESVD,word *volumeid)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
{
 DOS_bootsector  *bblock = (DOS_bootsector *)sector;
 int discSize, noSides;

 /* Can only make sketchy test for a valid boot block, later test should fail */
 /* if the boot block is not really valid */
#ifdef PCMCIATRUE
 if (sector_size(bblock) == DOSsecsize)
#else
 if (sector_size(bblock) == DOSsecsize || bblock->BOOT_num_fats == 2)
#endif
    {
    /* Get number of sectors and number of sectors per track from BOOT block. */
    int bbsize = DOSFS_max_sect(bblock);
    int notracks = (bblock->BOOT_secstrackHI << 8) | bblock->BOOT_secstrack;

    if (notracks == 0) return 1;

    notracks = bbsize / notracks; /* Calculate number of tracks from BOOT block. */
    bbsize = bbsize * DOSsecsize; /* Calculate disc size from BOOT block. */

    /* Calculate disc size too using disc record info. */
    discSize = (secspertrk(dr) * bytespersec(dr) * notracks) ;

231 232
    dprintf(("","DOSFS: testforbpb: discSize = &%08X\n",discSize)) ;
    dprintf(("","DOSFS: testforbpb: check size = &%08X\n",bbsize)) ;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

#ifdef PCMCIATRUE
    /* Now have to cope with PCMCIA cards with bits of first track "missing" */
    /* but if it's not a floppy we can't be sure of the real size */
    if (((bbsize < (discSize + (secspertrk(dr)*bytespersec(dr)))) && (bbsize >= discSize)) || !floppy)
#else
    /* Validate the calculated size matches the discsize filecore has guessed */
    /* but if it's not a floppy we can't be sure of the real size*/
    if ((bbsize == discSize) || !floppy)
#endif
       {
       noSides = bblock->BOOT_heads;
       if (noSides > 0)
          {
          /* It's a DOS disc so claim it as one of ours and set up the disc record info. */
          *magic = bblock->BOOT_magic;
          *ROOTsize = ((bblock->BOOT_root_dir | (bblock->BOOT_root_dirHI << 8)) * sizeof(DOS_direntry)) ;
          *FATsize = (bblock->BOOT_FAT_size | (bblock->BOOT_FAT_sizeHI << 8)) * DOSsecsize;
          *numFATs = bblock->BOOT_num_fats;
          *Atari = (bblock->BOOT_JMP[0] == 0); /* No JMP means Atari */
          *numRESVD = (bblock->BOOT_reserved | (bblock->BOOT_reservedHI << 8)) ;
          *dblstep = ((notracks / noSides) == 40);
255
          if (bblock->BOOT_extra.fat12.sig_rec == 0x29)
256 257
             {
             /* There's an extended boot record,note the volume id */
258 259
             *volumeid = (bblock->BOOT_extra.fat12.volid | (bblock->BOOT_extra.fat12.volid1 << 8) |
                         (bblock->BOOT_extra.fat12.volid2 << 16) | (bblock->BOOT_extra.fat12.volid3 << 24));
260
             }
261 262
          discsize(dr) = bbsize; /* Use bbsize incase FileCore was guessing discSize */
          heads(dr) = noSides;
263
          if (!floppy) secspertrk(dr) = (bblock->BOOT_secstrackHI << 8) | bblock->BOOT_secstrack;
264 265 266 267 268
          return 0;
          }
       }
    }

269
 dprintf(("","DOSFS: testforbpb: FileCore and BOOT BLOCK disc sizes differ\n"));
270 271 272 273
 return 1;
}

static int testforpartition(ADFS_drecord *dr,char *sector,_kernel_swi_regs *rset,word *winioffset,
274 275
                            int *numFATs,int *FATsize,int *magic,int *ROOTsize,
                            int *Atari,int *dblstep,int *numRESVD,word *volumeid)
276 277 278 279 280 281 282 283 284 285
{
 DOS_bootsector  *bblock = (DOS_bootsector *)sector;
 _kernel_swi_regs urset ;
 FS_discop64      opblock;
 _kernel_oserror *rerror = NULL ;
 byte  *pentry ;         /* partition description pointer */
 word   winisize = 0 ;   /* media size */
 int    index;

 /* This may be a partition on a fixed disc though, so let's have a look */
286
 if (((bblock->BOOT_extra.fat12.BOOT_signature) | (bblock->BOOT_extra.fat12.BOOT_signatureHI << 8)) != DOS_PARTITION_signature)
287 288 289 290 291
    return 1;

 /* If there's a bootable partition,take that in preference to any other */
 for (index=0; index < 4; index++)
     {
292
     pentry = (byte *)&(bblock->BOOT_extra.fat12.partitions[index*sizeof(partition_entry)]);
293 294 295 296 297 298 299
     if (pentry[0] == bootable) break;
     }
 if (index == 4)
    {
    /* Try non bootable partitions whose start sector != 0 */
    for (index=0; index < 4; index++)
        {
300
        pentry = (byte *)&(bblock->BOOT_extra.fat12.partitions[index*sizeof(partition_entry)]);
301 302 303 304 305 306 307 308 309
        if (pentry[2] != 0) break;
        }
    }

 if (index == 4) return 1;

 /* Got a vaguely valid partition,go with it */
 *winioffset = ((pentry[8] | (pentry[9] << 8) | (pentry[10] << 16) | (pentry[11] << 24)) * DOSsecsize);
 winisize = (pentry[12] | (pentry[13] << 8) | (pentry[14] << 16) | (pentry[15] << 24));
310
 dprintf(("","DOSFS: testforpartition: Found partition at winioffset = &%08X\n",*winioffset));
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

 urset.r[3] = (word)bblock ;                        /* BOOT block buffer */
 urset.r[4] = sizeof(DOS_bootsector) ;              /* size of the buffer */
 urset.r[6] = rset->r[6] ;                          /* entry sector cache handle */
 urset.r[8] = rset->r[8] ;                          /* entry FileCore private word pointer */
 if (discopswi==FileCore_DiscOp)
    {
    urset.r[1] = (9 | (4 << 4) | (((int)dr >> 2) << 8)) ;
    urset.r[2] = (dr->dr_rootSIN & 0xE0000000) + *winioffset;      /* disc address */
    }
 else
    {
    opblock.drivenumber   = (dr->dr_rootSIN & 0xE0000000) >> 29;
    opblock.byteaddresslo = *winioffset;
    opblock.byteaddresshi = 0;
    urset.r[1] = (9 | (4 << 4));
    urset.r[2] = (int)&opblock;
    urset.r[5] = (int)dr;
    }
330
 dprintf(("","DOSFS: testforpartition: reading sector from &%08X\n",dr->dr_rootSIN + *winioffset));
331 332 333 334 335
 if ((rerror = _kernel_swi(discopswi,&urset,&urset)) == NULL)
    {
    int bbsize = DOSFS_max_sect(bblock);
    int notracks = (bblock->BOOT_secstrackHI << 8) | bblock->BOOT_secstrack;

336
    dprintf(("","DOSFS: testforpartition: read sector OK\n"));
337 338 339 340 341 342 343 344 345 346 347
    bbsize = bbsize * DOSsecsize; /* Calculate disc size from BOOT block. */
    if (bbsize > (winisize*DOSsecsize)) return 1;

    /* It's a DOS disc so claim it as one of ours and set up the disc record info. */
    *magic = bblock->BOOT_magic;
    *ROOTsize = ((bblock->BOOT_root_dir | (bblock->BOOT_root_dirHI << 8)) * sizeof(DOS_direntry)) ;
    *FATsize = (bblock->BOOT_FAT_size | (bblock->BOOT_FAT_sizeHI << 8)) * DOSsecsize;
    *numFATs = bblock->BOOT_num_fats;
    *Atari = 0; /* Never Atari */
    *numRESVD = (bblock->BOOT_reserved | (bblock->BOOT_reservedHI << 8)) ;
    *dblstep = 0; /* Never double step */
348
    if (bblock->BOOT_extra.fat12.sig_rec == 0x29)
349 350
       {
       /* There's an extended boot record,note the volume id */
351 352
       *volumeid = (bblock->BOOT_extra.fat12.volid | (bblock->BOOT_extra.fat12.volid1 << 8) |
                   (bblock->BOOT_extra.fat12.volid2 << 16) | (bblock->BOOT_extra.fat12.volid3 << 24));
353
       }
354 355
    discsize(dr) = bbsize; /* Use bbsize incase FileCore was guessing discSize */
    heads(dr) = 0; /* It's a harddisc */
356
    secspertrk(dr) = (bblock->BOOT_secstrackHI << 8) | bblock->BOOT_secstrack;
357 358 359 360 361 362 363 364 365 366 367
    UNUSED(notracks);

    tracef6("DOSFS: testforpartition %d %x %x %d %d %x\n",
             (int)*magic,(int)*ROOTsize,(int)*FATsize,(int)*numFATs,(int)*numRESVD,bbsize);
    return 0;
    }

 return 1;
}

static int testfor160360k(ADFS_drecord *dr,char *sector,bool floppy,
368 369
                          int *numFATs,int *FATsize,int *magic,int *ROOTsize,
                          int *Atari,int *dblstep,int *numRESVD)
370 371 372 373 374 375 376
{
 /* If we have not claimed the disc then it could still be valid only if it is a 320K or
  * 160K format, both of which have 8 sectors per track (these old formats need not have a
  * valid boot block)
  */
 if (secspertrk(dr) != 8)
    {
377
    dprintf(("","DOSFS: testfor160360k: not 320K or 160K format 8 sectors per track\n"));
378 379 380 381 382 383 384 385 386 387 388 389
    return 1;
    }
 /* Defaults for 320K and 160K formats */
 *magic = 0;
 /* ROOTsize could be 112 or 64 entries,we'll find out later by inspecting the FAT */
 *FATsize = DOSsecsize;
 *numFATs = 2;
 *Atari = 0;
 *numRESVD = 1;
 *dblstep = 1;
 /* discsize(dr) = guessed from FAT entry later */
 /* heads(dr) = guessed from FAT entry later */
390
 /* secspertrk() = no BPB so don't know,but it must be a floppy so don't care either */
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

 UNUSED(floppy);
 UNUSED(sector);
 UNUSED(ROOTsize);
 return 0;
}

static void validatedosformat(ADFS_drecord *dr,_kernel_swi_regs *rset)
{
 /* In response to Service_IdentifyDisc this will have a few goes at working */
 /* out if this will pass as a DOS disc image. Note that exactly which test  */
 /* is performed depends on whether it's a harddisc or a floppy disc         */

 _kernel_swi_regs urset ;
 _kernel_oserror *rerror = NULL ;
406
 char *sector,*sectorblock;
407 408 409 410 411 412 413 414 415
 byte *name;
 DOS_bootsector  *bblock;
 FS_discop64      opblock;
 int   dblstep, numFATs, FATsize, numRESVD, ROOTsize;
 int   magic = 0;
 int   Atari = 0;
 int   discID = 0;
 char *buf = 0;
 int   bufsz = 0;
416
 word  winioffset = 0, volumeid = 0;
417
 bool  floppy = !((log2secsize(dr) == 0) || (dr->dr_floppy_density == 0) || (secspertrk(dr) == 0) || (heads(dr) == 0));
418
 int   loop, index, step;
419

420
 dprintf(("","DOSFS: validatedosformat: treating as a %s disc\n",floppy ? "floppy" : "fixed"));
421 422 423
 /* If it is a floppy then insist on a few floppyesque parameters */
 if (floppy && ((log2secsize(dr) != log2DOSsecsize) || (tracklow(dr) != 1) || (trackskew(dr) != 0)))
    {
424
    dprintf(("","DOSFS: validatedosformat: floppy fails even basic DOS parameters\n"));
425 426 427 428 429 430 431
    return;
    }

 /* Get some memory to load a sector */
 sector = (char *)malloc(DOSsecsize) ;
 if (sector == NULL)
    {
432
    dprintf(("","DOSFS: validatedosformat: failed to get memory\n"));
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    return;
    }
 bblock = (DOS_bootsector *)sector;

 /* Sector 1, track 0 should always be readable regardless of the specific DOS format */
 /* Use FileCore_DiscOp 9 (option bits 2_01x0) and the alternative disc record we have */
 /* FileCore_DiscOp{64} */
 /*      in:     r1 = bits0-3    reason code 9 */
 /*                   bits4-7    option bits */
 /*                   bits8-31   bits2-25 of pointer to alternative disc record */
 /*                  {bits 8-31 reserved} */
 /*              r2 = disc address {pointer to block containing the 64 bit disc address} */
 /*              r3 = pointer to buffer */
 /*              r4 = length (bytes) */
 /*             {r5 = address of alternate disc record} */
 /*              r6 = sector cache handle */
 /*              r8 = pointer to FileCore instance private word */
 /*      out:    r1 = preserved */
 /*              r2 = disc address of next byte to be transferred */
 /*              r3 = pointer to next buffer location to be transferred */
 /*              r4 = number of bytes NOT transferred */
 /*              r6 = new sector cache handle */
 /*              r8 = preserved */

 urset.r[3] = (word)bblock ;                        /* BOOT block buffer */
 urset.r[4] = sizeof(DOS_bootsector) ;              /* size of the buffer */
 urset.r[6] = rset->r[6] ;                          /* entry sector cache handle */
 urset.r[8] = rset->r[8] ;                          /* entry FileCore private word pointer */
 if (discopswi==FileCore_DiscOp)
    {
463 464
    urset.r[1] = (9 | (4 << 4) | (((int)dr >> 2) << 8)) ; /* read cached here */
    urset.r[2] = dr->dr_rootSIN & 0xE0000000 ;            /* disc address */
465 466 467 468 469 470 471 472 473 474
    }
 else
    {
    opblock.drivenumber   = (dr->dr_rootSIN & 0xE0000000) >> 29;
    opblock.byteaddresslo = 0;
    opblock.byteaddresshi = 0;
    urset.r[1] = (9 | (4 << 4));
    urset.r[2] = (int)&opblock;
    urset.r[5] = (int)dr;
    }
475
 dprintf(("","DOSFS: validatedosformat: reading sector from &%08X\n",dr->dr_rootSIN));
476 477 478 479
 /* Try a read of the disc */
 if ((rerror = _kernel_swi(discopswi,&urset,&urset)) != NULL)
    {
    free(sector);
480
    dprintf(("","DOSFS: validatedosformat: failed to read 0th sector\n"));
481 482 483
    return;
    }

484
 dprintf(("","DOSFS: validatedosformat: sector read OK\n"));
485
 /* See if the sector read contains a BPB */
486 487
 if (testforbpb(dr,sector,floppy,&numFATs,&FATsize,&magic,&ROOTsize,
                &Atari,&dblstep,&numRESVD,&volumeid))
488 489
    {
    /* Crusty 160 & 360k floppies don't need one */
490 491
    if (!floppy || testfor160360k(dr,sector,floppy,&numFATs,&FATsize,&magic,&ROOTsize,
                                  &Atari,&dblstep,&numRESVD))
492 493
       {
       /* Last ditch attempt scanning for partitions on harddiscs */
494 495
       if (floppy || testforpartition(dr,sector,rset,&winioffset,&numFATs,&FATsize,&magic,&ROOTsize,
                                      &Atari,&dblstep,&numRESVD,&volumeid))
496 497
          {
          free(sector);
498
          dprintf(("","DOSFS: validatedosformat: shame,not a DOS disc\n"));
499 500 501 502 503 504 505 506 507 508 509 510
          return;
          }
       }
    }

 /* Update the disc record with what we know now */
 put_doublestep(dr, dblstep);
 put_sequence(dr, 0);

 /* Read the FAT.  This will be used to identify a 320K or 160K format if necessary and
  * also to calculate a disc ID to pass back to FileCore.
  */
511
 dprintf(("","DOSFS: validatedosformat: reading FAT\n"));
512 513 514 515 516 517 518 519 520 521 522 523
 sectorblock = realloc(sector,FATsize);
 if (sectorblock == NULL)
    {
    step = DOSsecsize; /* Low on memory,so FAT is read one sector at a time */
    }
 else
    {
    sector = sectorblock;
    step = FATsize;    /* Enough memory to load the whole FAT at once */
    }

 for (index = 0; index < FATsize; index += step)
524 525
     {
     urset.r[3] = (word)sector ;
526
     urset.r[4] = step ;
527 528 529 530
     /* sector cache handle and FileCore private word pointer preserved from above call */

     if (discopswi==FileCore_DiscOp)
        {
531
        urset.r[1] = ((floppy ? 9 : 1) | (4 << 4) | (((int)dr >> 2) << 8)) ;
532 533 534 535
        urset.r[2] = (index + DOSsecsize + winioffset) | (dr->dr_rootSIN & 0xE0000000); /* disc address */
        }
     else
        {
536
        /* rely on r2 being preserved,and the drive number in the block */
537 538
        opblock.byteaddresslo = index + DOSsecsize + winioffset;
        opblock.byteaddresshi = 0;
539
        urset.r[1] = ((floppy ? 9 : 1) | (4 << 4)); /* Read uncached for fixed discs */
540 541 542 543 544
        urset.r[5] = (int)dr;
        }
     if ((rerror = _kernel_swi(discopswi,&urset,&urset)) == NULL)
        {
        /* Check the first byte of the FAT for the 320K or 160K media type. */
545
        if ((*sector == 0xFE) && (magic==0) && (index==0))
546 547 548 549 550 551
           {
           magic = 0xFE;
           ROOTsize = 64 * sizeof(DOS_direntry);
           discsize(dr) = 0x28000;
           heads(dr) = 1;
           }
552
        if ((*sector == 0xFF) && (magic==0) && (index==0))
553 554 555 556 557 558 559
           {
           magic = 0xFF;
           ROOTsize = 112 * sizeof(DOS_direntry);
           discsize(dr) = 0x50000;
           heads(dr) = 2;
           }
        }
560
     /* We're certain it's a DOS disc so calculate a disc ID from the FAT contents */
561 562
     for (loop = 0; loop < step; loop++)
         discID += sector[loop] ;
563 564 565 566 567 568
     }

 /* In all circumstances we'd have deduced the magic number by now */
 if (magic == 0)
    {
    free(sector);
569
    dprintf(("","DOSFS: validatedosformat: don't know the magic number by any method\n"));
570 571 572 573 574
    return;
    }

 /* Claim the service and fill in the disc record.    */
 /* Get the default disc name from the Messages file. */
575 576
 dprintf(("","DOSFS: validatedosformat: accepted disc type &%02X\n",magic));
 dprintf(("","DOSFS: validatedosformat: cycleID was 0x%x\n", discID));
577 578 579 580 581 582 583 584 585 586 587

 /* Lookup default disc name. */
 if ((rerror = msgtrans_lookup("DEFDNM", &buf, &bufsz, 0, 0, 0, 0)) != NULL)
    {
    buf = rerror->errmess;
    bufsz = strlen(buf);
    }
 if (bufsz > 9) bufsz = 9;
 strncpy((char *)(dr->dr_discname), buf, bufsz);
 dr->dr_discname[bufsz] = '\0';

588 589 590
 /* A volume id is better than the default */
 if (volumeid != 0) sprintf((char*)(dr->dr_discname),"%04X-%04X\0",(volumeid >> 16) & 0xFFFF,volumeid & 0xFFFF);

591 592 593 594 595 596 597 598
 /* Try to find a volume label on the disc. */
 for (loop = 0; loop < ROOTsize; loop+= DOSsecsize)
     {
     urset.r[3] = (word)sector ;
     urset.r[4] = DOSsecsize ;
     if (discopswi==FileCore_DiscOp)
        {
        /* sector cache handle and FileCore private word pointer preserved from above call */
599
        urset.r[1] = ((floppy ? 9 : 1) | (4 << 4) | (((int)dr >> 2) << 8)) ;
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
        urset.r[2] = ((numFATs * FATsize) + loop + DOSsecsize + winioffset) | (dr->dr_rootSIN & 0xE0000000) ; /* disc address */
        }
     else
        {
        /* rely on r1 and r2 being preserved,and the drive number in the block*/
        opblock.byteaddresslo = (numFATs * FATsize) + loop + DOSsecsize + winioffset;
        opblock.byteaddresshi = 0;
        urset.r[5] = (int)dr;
        }
     if ((rerror = _kernel_swi(discopswi,&urset,&urset)) == NULL)
        {
        DOS_direntry *dentry;

        index = 0;
        if ((dentry = findDIRtype((byte)FILE_win95,(byte)FILE_volume,(DOS_direntry *)sector,DOSsecsize,&index)) != NULL)
           {
616
           dprintf(("","DOSFS: validatedosformat: volume label found\n") );
617 618
           memmove((void *)&(dr->dr_discname[0]),(void *)&(dentry->FILE_status),(discnamesize - 1)) ;
           /* Replace invalid characters in disc name */
619 620 621 622 623 624 625 626 627 628 629 630 631 632
           dprintf(("","DOSFS: dr->dr_discname = %p\n",dr->dr_discname));
           dprintf(("","DOSFS: dr->dr_discname+9 = %p\n",dr->dr_discname+9));
           for (name=dr->dr_discname+9; name>=dr->dr_discname; name--)
           {
            dprintf(("","DOSFS: in loop: *name = %c\n",*name));
            if(isalnum(*name))
            {
             name++;
             *name = NULL;
             name = dr->dr_discname;
             dprintf(("","DOSFS: ok\n"));
            }
           }
           dprintf(("","DOSFS: dr->dr_discname = %p\n",dr->dr_discname));
633 634 635 636
           for (name=dr->dr_discname; *name; name++)
               {
               *name=mapchar(*name,DOSmapping,ROmapping);
               }
637
           dprintf(("","DOSFS: validatedosformat: volume \"%s\"\n",(char *)dr->dr_discname)) ;
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
           break; /* End search */
           }
        }
     }

#ifndef PRE218
 /* Write format name into buffer. */
 if (rset->r[2] != 0)
    {
    char token[8];
    for (loop = 0; DOS_formats[loop].magic_ID != 0; loop++)
        {
        if (magic == DOS_formats[loop].magic_ID)
           {
           /* If type is &F9 then we need more tests. */
           if (magic == 0xF9)
              {
              if (Atari && strcmp(DOS_formats[loop].idtext, "Atari/M"))
                 continue;
              if (FATsize != DOS_formats[loop].secsFAT * DOSsecsize)
                 continue;
              }
           break;
           }
        }
    sprintf(token, FORMAT_FMT, loop + 1);
    buf = (char *)rset->r[2];
    bufsz = rset->r[3];
    if ((rerror = msgtrans_lookup(token, &buf, &bufsz, 0, 0, 0, 0)) != NULL)
       {
       bufsz = strlen(rerror->errmess);
       strncpy(buf, rerror->errmess, bufsz);
       }
    buf[bufsz] = '\0';
672
    dprintf(("","DOSFS: validatedosformat: format name = \"%s\"\n", buf));
673 674 675 676 677 678 679 680 681 682 683
    }
#endif

 /* Claim the service and fill in the disc record. */
 rset->r[1] = 0x00 ;         /* Service_Serviced (claimed) */
 put_discID(dr, discID);     /* Still pointed to by R5 */
 rset->r[2] = imagetype ;    /* filetype to be associated with the disc image */
 rset->r[6] = urset.r[6] ;   /* sector cache handle returned from "FileCore_DiscOp{64}" */
 return;
}

Neil Turton's avatar
Neil Turton committed
684
/*-------------------------------------------------------------------------*/
685 686 687
/* NOTE: This code currently only deals with floppy structures and         */
/* optionally PCMCIA (arbitrary sector-multiple size structures) through   */
/* the addition of r4 as a parameter                                       */
Neil Turton's avatar
Neil Turton committed
688

689
static _kernel_oserror *FSSWI_LayoutStructure(_kernel_swi_regs *rset,void *privateword)
Neil Turton's avatar
Neil Turton committed
690 691 692 693 694
{
 /* in:  r0 = disc structure identifier (r5 from "Service_IdentifyFormat")
  *      r1 = pointer to -1 terminated list of bad blocks
  *      r2 = pointer to NULL terminated disc name
  *      r3 = file handle of system image
695
  *      r4 = size of partiton if r0 = PCMCIA
Neil Turton's avatar
Neil Turton committed
696 697 698 699 700 701 702 703
  * out:
  */
 _kernel_swi_regs  reglist ;
 _kernel_oserror  *rerror = NULL ;
  char *sector = NULL;

 tracef4("DOSFS: FSSWI_LayoutStructure: r0 = &%08X, r1 = &%08X, r2 = &%08X, r3 = &%08X\n",rset->r[0],rset->r[1],rset->r[2],rset->r[3]) ;

704
#ifdef PCMCIATRUE
705
 if(((rset->r[4] % DOSsecsize) != 0) && (rset->r[0] == 9)) { /* Should be a multiple of sectors big for PCMCIA */
Neil Turton's avatar
Neil Turton committed
706 707 708
     rerror = global_error(err_badformat);
     return(rerror);    /* Quit! */
 }
709
#endif
Neil Turton's avatar
Neil Turton committed
710 711 712 713 714 715 716 717

 /* Place default BOOT BLOCK (shape defined by r0 on entry) */
 if ((sector = (char *)malloc(DOSsecsize)) != NULL)
  {
   DOS_bootsector *dbsector = (DOS_bootsector *)sector; /* BOOT BLOCK for image */
   int findex = DOS_formats[rset->r[0]].findex ;
   int maxsect = (DOS_formatinfo[findex].secstrk * DOS_formatinfo[findex].tracks) ;

718
   dprintf(("","Findex = %d\n", findex));
Neil Turton's avatar
Neil Turton committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

   *dbsector = *(default_dbsector) ; /* copy the structure across */
   /* write format specific information (dictated by entry r0 parameter) */
   dbsector->BOOT_secalloc = DOS_formats[rset->r[0]].secsclus;
   dbsector->BOOT_root_dir = DOS_formats[rset->r[0]].rootsize;
   /* dbsector->BOOT_root_dirHI = 0x00; */
   dbsector->BOOT_max_sect = ((maxsect >> 0) & 0xFF) ;
   dbsector->BOOT_max_sectHI = ((maxsect >> 8) & 0xFF) ;
   dbsector->BOOT_magic = DOS_formats[rset->r[0]].magic_ID ;
   dbsector->BOOT_secstrack = ((DOS_formatinfo[findex].secstrk >> 0) & 0xFF) ;
   dbsector->BOOT_secstrackHI = ((DOS_formatinfo[findex].secstrk >> 8) & 0xFF) ;
   dbsector->BOOT_FAT_size = DOS_formats[rset->r[0]].secsFAT;
   /* dbsector->BOOT_FAT_sizeHI = 0x00; */
   switch (DOS_formatinfo[findex].options & sideinfomask) {
    case o_alternate:
    case o_sequence:
     dbsector->BOOT_heads = 2;
     break;
    default:
     dbsector->BOOT_heads = 1;
   }

741
#ifdef PCMCIATRUE
Neil Turton's avatar
Neil Turton committed
742 743 744 745 746 747 748 749 750 751 752 753 754
   /* Special code for PCMCIA format */
   if((strcmp(DOS_formats[rset->r[0]].idtext,"PCMCIA") == 0)) {
       /* We need to change the max_sect and secsFAT settings */
        maxsect = rset->r[4] / DOSsecsize; /* Should be an integer result */
        dbsector->BOOT_max_sect = ((maxsect >> 0) & 0xFF) ;
        dbsector->BOOT_max_sectHI = ((maxsect >> 8) & 0xFF) ;
        dbsector->BOOT_FAT_size = ((maxsect * 2) + (DOSsecsize / 2))/ DOSsecsize; /* Rounded up */
   }
#endif

   /* Special code for Atari formats */
   if ((strcmp(DOS_formats[rset->r[0]].idtext,"Atari/M") == 0) || (strcmp(DOS_formats[rset->r[0]].idtext,"Atari/N") == 0))
    {
755 756 757
     /* Atari formats have no jmp instruction in the boot block,and a 24bit volume serial number in the OEM id */
     dbsector->BOOT_JMP[0] =  dbsector->BOOT_JMP[1] = dbsector->BOOT_JMP[2] = 0;
     dbsector->BOOT_OEM[5] = (rand() & 0xFF) ;
Neil Turton's avatar
Neil Turton committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
     dbsector->BOOT_OEM[6] = (rand() & 0xFF) ;
     dbsector->BOOT_OEM[7] = (rand() & 0xFF) ;
    }

   /* write the BOOT BLOCK into the image */
   reglist.r[0] = osgbpb_wptr ;            /* write operation */
   reglist.r[1] = rset->r[3] ;             /* FileSwitch handle */
   reglist.r[2] = (word)dbsector ;         /* data address */
   reglist.r[3] = DOSsecsize ;             /* amount of data */
   reglist.r[4] = 0x00000000 ;             /* destination image offset */
   reglist.r[5] = NULL ;
   reglist.r[6] = NULL ;
   if ((rerror = _kernel_swi(OS_GBPB,&reglist,&reglist)) == NULL)
    {
     int   FATsize = (dbsector->BOOT_FAT_size|(dbsector->BOOT_FAT_sizeHI<<8));
     int   numFATs = dbsector->BOOT_num_fats;
     word  ROOTsize = ((dbsector->BOOT_root_dir | (dbsector->BOOT_root_dirHI << 8)) * sizeof(DOS_direntry)) / DOSsecsize;
     word offset = ((DOS_FAT_sector - 1) * DOSsecsize) ;
     int  loop ; /* general index counter */

     for (loop = 0; loop < DOSsecsize; loop++)
      sector[loop] = '\0' ; /* zero sector buffer */
     /* Can no longer take data from dbsector. */

     /* Create FATs */
     while (numFATs--) {
      sector[0] = DOS_formats[rset->r[0]].magic_ID ;
      sector[1] = 0xFF ;
      sector[2] = 0xFF ;
      for (loop = 0; loop < FATsize; loop++) {
       reglist.r[0] = osgbpb_wptr ;            /* write operation */
       reglist.r[1] = rset->r[3] ;             /* FileSwitch handle */
       reglist.r[2] = (word)sector ;           /* data address */
       reglist.r[3] = DOSsecsize ;             /* amount of data */
       reglist.r[4] = offset ;                 /* destination image offset */
       reglist.r[5] = NULL ;
       reglist.r[6] = NULL ;
       if ((rerror = _kernel_swi(OS_GBPB,&reglist,&reglist)) != NULL)
        break ; /* out of the FAT writing loop */
       offset += DOSsecsize ;
       if (loop)
        continue;
       sector[0] = sector[1] = sector[2] = 0;
      }
     }

     if (rerror == NULL) {
      DOS_direntry *dentry = (DOS_direntry *)sector ;
      time5byte     formTIME ;

      localTIME(&formTIME) ;

      /* place the default disc name into the first slot */
811 812
      put_FILE_time(dentry->FILE_time,dentry->FILE_timeHI,RISCOStoTIME(&formTIME)) ;
      put_FILE_date(dentry->FILE_date,dentry->FILE_dateHI,RISCOStoDATE(&formTIME)) ;
Neil Turton's avatar
Neil Turton committed
813

814 815 816 817
      /* Copy title into block,pad with spaces (not nulls),and mark as a volume title */

      memset((char *)&(dentry->FILE_status),32,namsize+extsize);
      memcpy((char *)&(dentry->FILE_status),(char *)rset->r[2],strlen((char *)rset->r[2]));
Neil Turton's avatar
Neil Turton committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
      dentry->FILE_attribute = (FILE_volume | FILE_archive) ;

      /* Write the root directory. */
      for (loop = 0; loop < ROOTsize; loop++) {
       int i;
       reglist.r[0] = osgbpb_wptr ;   /* write operation */
       reglist.r[1] = rset->r[3] ;    /* FileSwitch handle */
       reglist.r[2] = (word)sector ;  /* data address */
       reglist.r[3] = DOSsecsize ;    /* amount of data */
       reglist.r[4] = offset ;        /* destination image offset */
       reglist.r[5] = NULL ;
       reglist.r[6] = NULL ;
       if ((rerror = _kernel_swi(OS_GBPB,&reglist,&reglist)) != NULL)
        break;
       offset += DOSsecsize;
       if (loop)
        continue;
       for (i = 0; i < sizeof(DOS_direntry); i++)
        sector[i] = 0;
      }
     }
    }
   free(sector) ;
  }
 else
  rerror = global_errorT(err_heapexhausted, tok_heapexhausted, 0, 0) ;

 return(rerror) ;
846
 UNUSED(privateword) ;
Neil Turton's avatar
Neil Turton committed
847 848 849 850 851 852
}

/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* Vector through to the relevant SWI handler */
853
_kernel_oserror *fs_swi(int swi_no,_kernel_swi_regs *rset,void *privateword)
Neil Turton's avatar
Neil Turton committed
854 855 856 857
{
 switch(swi_no + DOSFS_00)
  {
   case DOSFS_DiscFormat :
858
             return(FSSWI_DiscFormat(rset,privateword)) ;
Neil Turton's avatar
Neil Turton committed
859 860 861
             break ;

   case DOSFS_LayoutStructure :
862
             return(FSSWI_LayoutStructure(rset,privateword)) ;
Neil Turton's avatar
Neil Turton committed
863 864 865
             break ;

   default : /* unknown */
866
             dprintf(("","DOSFS: return unknown SWI (%d) error\n",swi_no) );
Neil Turton's avatar
Neil Turton committed
867 868 869 870 871 872 873 874 875 876
             return global_errorT(err_badswi, tok_badswi, "DOSFS", 0);
             break ;
  }

 return(NULL) ;
}

/*-------------------------------------------------------------------------*/
/* Provide handlers for the services we intercept */

877
void fs_service(int service_number,_kernel_swi_regs *rset,void *privateword)
Neil Turton's avatar
Neil Turton committed
878 879 880 881 882 883 884 885
{
 /* Note: We should NOT perform any IO from these functions */
 switch (service_number)
  {
   case 0x11 : /* Service_Memory */
               /* if (rset->r[2] == module base addr) then claim this call */
               if (rset->r[2] == (int)moduleBase)
                {
886
                 dprintf(("","DOSFS: Service_Memory (0x11): matched\n")) ;
Neil Turton's avatar
Neil Turton committed
887 888 889 890 891 892 893 894 895
                 rset->r[1] = 0x00000000 ; /* claim this service */
                }
               break ;

   case 0x12 : /* Service_StartUpFS */
               select_FS() ;
               break ;

   case 0x27 : /* Service_Reset */
896
               dprintf(("","DOSFS: Service_Reset: called\n")) ;
Neil Turton's avatar
Neil Turton committed
897 898 899
               break ;

   case 0x40 : /* Service_FSRedeclare */
900
               declare_FS(privateword) ;
Neil Turton's avatar
Neil Turton committed
901 902 903 904 905 906
               break ;

   case 0x42 : /* Service_LookupFileType */
               break ;

   case 0x5C : /* Service_WimpSaveDesktop */
907 908 909 910
               dprintf(("","DOSFS: Service_WimpSaveDesktop: called\n")) ;
               dprintf(("","DOSFS: WimpSaveDesktop: r0 = &%08X\n",rset->r[0])) ;
               dprintf(("","DOSFS: WimpSaveDesktop: r1 = &%08X\n",rset->r[1])) ;
               dprintf(("","DOSFS: WimpSaveDesktop: r2 = &%08X\n",rset->r[2])) ;
Neil Turton's avatar
Neil Turton committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
               /* r0 = 0x00 */
               /* r1 = 0x5C - Service_WimpSaveDesktop */
               /* r2 = FileSwitch file handle */
               {
                _kernel_oserror  *rerror = NULL ;
                _kernel_swi_regs  reglist ;
                mapentry         *cptr = maplist ;
                char             *tbuff ;

                tbuff = (char *)malloc(strlen("DOSMap DOS FFF\n") + 1) ;
                /* we do not do anything if we failed to alloc memory */
                if (tbuff != NULL)
                 {
                  for (; (cptr != NULL); cptr = cptr->next)
                   {
                    int loop ;
                    sprintf(tbuff,"DOSMap ") ;
928
                    for (loop = 0; (loop < 7); loop++)
Neil Turton's avatar
Neil Turton committed
929
                     {
930
                      char cchr = (*(char*)(cptr->dosext + (loop ))) ;
Neil Turton's avatar
Neil Turton committed
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
                      cchr = (((cchr > ' ') && (cchr != 0x7F)) ? cchr : ' ') ;
                      sprintf(&tbuff[strlen(tbuff)],"%c",cchr) ;
                     }
                    sprintf(&tbuff[strlen(tbuff)]," %03X\n",cptr->ROtype) ;
                    reglist.r[0] = osgbpb_write ; /* write operation */
                    reglist.r[1] = rset->r[2] ; /* FileSwitch handle */
                    reglist.r[2] = (word)tbuff ; /* data address */
                    reglist.r[3] = strlen(tbuff) ; /* amount of data */
                    reglist.r[4] = NULL ; /* destination image offset */
                    reglist.r[5] = NULL ;
                    reglist.r[6] = NULL ;
                    if ((rerror = _kernel_swi(OS_GBPB,&reglist,&reglist)) != NULL)
                     {
                      rset->r[0] = (word)rerror ; /* pointer to error block */
                      rset->r[1] = 0x00 ; /* Service_Claimed */
                      break ; /* out of for loop */
                     }
                   }
                  free(tbuff) ;
                 }
               }
               break ;

   case 0x68 : /* Service_CloseFile */
               break ;

   case 0x69 : /* Service_IdentifyDisc */
               /* in:   r1 = Service_IdentifyDisc */
               /*       r2 = Pointer to buffer for format name */
               /*       r3 = Length of format name buffer */
               /*       r5 = pointer to disc record */
               /*       r6 = sector cache handle */
               /*       r8 = pointer to filecore instance private word */
               /* out:  r1 = Service_Serviced (NULL) */
               /*       r2 = filetype number for given disc format */
               /*       r5 = pointer to disc record (possibly updated) */
               /*       r6 = new sector cache handle */
               /*       r8 = preserved */
               tracef3("DOSFS: Service_IdentifyDisc: r5 = &%08X, r6 = &%08X, r8 = &%08X\n",rset->r[5],rset->r[6],rset->r[8]) ;
               {
971
                ADFS_drecord *dr = (ADFS_drecord *)rset->r[5] ;
Neil Turton's avatar
Neil Turton committed
972

973 974 975 976 977 978 979 980 981 982
                dprintf(("","DOSFS: Service_IdentifyDisc: log2secsize(dr) = %d\n",log2secsize(dr))) ;
                dprintf(("","DOSFS: Service_IdentifyDisc: secspertrk(dr) = %d\n",secspertrk(dr))) ;
                dprintf(("","DOSFS: Service_IdentifyDisc: heads(dr) = %d\n",heads(dr)));
                dprintf(("","DOSFS: Service_IdentifyDisc: dr->dr_floppy_density = %d\n",dr->dr_floppy_density)) ;
                dprintf(("","DOSFS: Service_IdentifyDisc: discsize(dr) = &%X\n",discsize(dr)));
                dprintf(("","DOSFS: Service_IdentifyDisc: doublestep(dr) = %d\n",doublestep(dr)));
                dprintf(("","DOSFS: Service_IdentifyDisc: sequence(dr) = %d\n",sequence(dr)));
                dprintf(("","DOSFS: Service_IdentifyDisc: tracklow(dr) = %d\n",tracklow(dr)) );
                dprintf(("","DOSFS: Service_IdentifyDisc: trackskew(dr) = %d\n",trackskew(dr)) );
                dprintf(("","DOSFS: Service_IdentifyDisc: dr->dr_drive = &%08X\n",dr->dr_drive));
Neil Turton's avatar
Neil Turton committed
983

984
                validatedosformat(dr, rset);
985
                dprintf(("","DOSFS: Service_IdentifyDisc: exit with r1 = &%X\n",rset->r[1]));
Neil Turton's avatar
Neil Turton committed
986 987 988 989 990 991 992 993 994 995 996 997
               }
               break ;

   case 0x6A : /* Service_EnumerateFormats */
               /* in:   r0 = 0 */
               /*       r1 = Service_EnumerateFormats */
               /* out:  r0 = pointer to linked list of format specifications (as RMA blocks) */
               /*       r1 = preserved */
               {
                format_info **next ;
                int          loop ;

998
                dprintf(("","DOSFS: Service_EnumerateFormats: called\n") );
Neil Turton's avatar
Neil Turton committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

                /* set next to point to the next place to store a format_info pointer */
                next = (format_info **)&(rset->r[2]);
                while (*next != NULL)
                 next = &((*next)->link);

                /* see "format_info" structure in "h.MultiFS" */
                for (loop = 0; (DOS_formats[loop].magic_ID != 0x00); loop++)
                 if (DOS_formats[loop].idtext != NULL && DOS_formats[loop].in_menu) /* we support this format */
                  {
                   format_info *finfo = (format_info *)_kernel_RMAalloc(sizeof(format_info)) ;
                   char        *menu_text = 0;
                   char        *help_text = 0;
                   int         menusz;
                   int         helpsz;
                   _kernel_oserror *err;
                   char        token[8];

                   if (finfo == NULL)
                     continue;

                   /* get the text from the Messages file - to be safe do the copying here in case */
                   /* message trans uses the same buffer for both calls */
                   sprintf(token, FORMAT_FMT, loop + 1);
                   if ((err = msgtrans_lookup(token, &menu_text, &menusz, 0, 0, 0, 0)) != NULL) {
                     menu_text = err->errmess;
                     menusz = strlen(menu_text);
                   }
                   finfo->menu_text = _kernel_RMAalloc(menusz + 1) ;
                   if (finfo->menu_text != NULL) {
                     strncpy(finfo->menu_text, menu_text, menusz);
                     ((char *)(finfo->menu_text))[menusz] = '\0';
                     sprintf(token, HELP_FMT, loop + 1);
                     if ((err = msgtrans_lookup(token, &help_text, &helpsz, 0, 0, 0, 0)) != NULL) {
                       help_text = err->errmess;
                       helpsz = strlen(help_text);
                     }
                     finfo->help_text = _kernel_RMAalloc(helpsz + 1) ;
                     if (finfo->help_text != NULL) {
                       strncpy(finfo->help_text, help_text, helpsz);
                       ((char *)(finfo->help_text))[helpsz] = '\0';
                       finfo->link = NULL ; /* no next block at the moment */
                       finfo->format_SWI = DOSFS_DiscFormat ;
                       finfo->format_r0 = loop ;
                       finfo->layout_SWI = DOSFS_LayoutStructure ;
                       finfo->layout_r0 = loop ;
                       finfo->flags = EnumFormats_HasFormatParam;
                       finfo->format_desc = DOS_formats[loop].idtext;

                       /* add this description onto the end of the list */
                       *next = finfo;
                       next = &(finfo->link);
                     } else {
                       free(finfo->menu_text);
                       free(finfo);
                     }
                   } else {
                    free(finfo);
                   }
                  }
               }
               break ;

   case 0x6B : /* Service_IdentifyFormat */
               /* in:   r0 = ptr to NULL terminated ASCII format identifier
                *       r1 = Service_IdentifyFormat
                * out:  r0 = preserved
                *       r1 = Service_Serviced (NULL)
                *       r2 = SWI number to call to obtain "DOSFS_DiscFormat"
                *       r3 = r3 parameter to be used for "DOSFS_DiscFormat" SWI
                *       r4 = SWI number to call to obtain "DOSFS_LayoutStructure"
                *       r5 = r0 parameter to be used for "DOSFS_LayoutStructure" SWI
                */
               {
                int loop ;

1075
                dprintf(("","DOSFS: Service_IdentifyFormat: r0 = \"%s\"\n",(char *)(rset->r[0]))) ;
Neil Turton's avatar
Neil Turton committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

                /* I use my wild-card compare function since it automatically deals with
                 * case equality, where "strcmp" does not.
                 */
                for (loop = 0; (DOS_formats[loop].magic_ID != 0x00); loop++)
                 if (DOS_formats[loop].idtext != NULL)
                  if (wild_card_compare((char *)rset->r[0],DOS_formats[loop].idtext,ROwcmult,ROwcsing))
                   break ;

                if (DOS_formats[loop].magic_ID != 0x00)
                 {
1087
                  dprintf(("","DOSFS: Service_IdentifyFormat: %s\n",DOS_formats[loop].idtext)) ;
Neil Turton's avatar
Neil Turton committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

                  rset->r[1] = 0x00 ;                   /* Service_Serviced (claimed) */
                  rset->r[2] = DOSFS_DiscFormat ;       /* our format SWI */
                  rset->r[3] = loop ;                   /* "DOS_formats" table index */
                  rset->r[4] = DOSFS_LayoutStructure ;  /* our layout SWI */
                  rset->r[5] = loop ;                   /* "DOS_formats" table index */
                 }
               }
               break ;

   case 0x6C : /* Service_DisplayFormatHelp */
               /* in:   r0 = &00
                *       r1 = Service_HelpFormat
                * out:  NO ERROR : preserved
                *       ERROR    : r0 = pointer to error block
                *                  r1 = Service_Serviced
                *
                * This should list the formats it will respond to in
                * "Service_IdentifyFormat". The listing should be performed
                * using "OS_WriteC", "OS_Write0" or "OS_WriteS".
                */
1109
               dprintf(("","DOSFS: Service_FormatHelp: called\n") );
Neil Turton's avatar
Neil Turton committed
1110
               {
1111
                int            loop ;
Neil Turton's avatar
Neil Turton committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
                for (loop = 0; (DOS_formats[loop].magic_ID != 0x00); loop++)
                 if (DOS_formats[loop].idtext != NULL)
                  {
                   _kernel_swi_regs  urset ;
                   _kernel_oserror  *rerror ;
                   char        *buf = 0;
                   int         bufsz = 0;
                   char        token[8];

                   /* get the text from the Messages file */
                   sprintf(token, HELP_FMT, loop + 1);
1123 1124 1125 1126 1127
                   if ((rerror = msgtrans_lookup(token, &buf, &bufsz, 0, 0, 0, 0)) != NULL)
                    {
                     buf = rerror->errmess;
                     bufsz = strlen(rerror->errmess);
                    }
Neil Turton's avatar
Neil Turton committed
1128 1129
                   urset.r[0] = (word)buf;
                   urset.r[1] = bufsz;
Kevin Bracey's avatar
Kevin Bracey committed
1130
                   rerror = _kernel_swi(OS_WriteN, &urset, &urset);
Neil Turton's avatar
Neil Turton committed
1131 1132

                   if (rerror == NULL)
Kevin Bracey's avatar
Kevin Bracey committed
1133
                    rerror = _kernel_swi(OS_NewLine,&urset,&urset) ;
Neil Turton's avatar
Neil Turton committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

                   if (rerror != NULL)
                    {
                     rset->r[0] = (word)rerror ; /* pointer to standard error block */
                     rset->r[1] = 0x00 ;         /* Service_Serviced */
                     return ;
                    }
                  }
               }
               break ;

   default   : /* unknown - do nothing */
               break ;
  }

 return ;
}

/*-------------------------------------------------------------------------*/

1154
_kernel_oserror *fs_commands(const char *argv,int argc,int command,void *privateword)
Neil Turton's avatar
Neil Turton committed
1155 1156 1157 1158 1159 1160 1161 1162
{
 _kernel_swi_regs  rset ;
 _kernel_oserror  *rerror = NULL ;

 switch (command)
  {
   case CMD_DOSMap :
             {
Kevin Bracey's avatar
Kevin Bracey committed
1163
              int         loop ;
1164
              char        dosext[8] = {0,0,0,0,0,0,0,0} ; /* DOS extension */
Kevin Bracey's avatar
Kevin Bracey committed
1165 1166 1167
              word        ROtype = 0x000 ;      /* RISC OS filetype */
              mapentry   *cptr ;
              const char *tptr ;
Neil Turton's avatar
Neil Turton committed
1168 1169 1170
              char *buf = NULL;
              int bufsz = 0;

1171
              dprintf(("","fs_command: DOSMap\n") );
Neil Turton's avatar
Neil Turton committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
              /* arg0 (optional) : DOS extension */
              /* arg1 (optional) : RISC OS filetype */
              /* If "arg0" is NOT present then we display the current mappings.
               * If "arg1" is NOT present the mapping for the given DOS
               * extension will be removed.
               */

              if (argc == 0) /* display current mappings if no parameters */
               {
                if ((cptr = maplist) == NULL)
                {
                 if ( (rerror = msgtrans_lookup("NOMAP", &buf, &bufsz, NULL, NULL, NULL, NULL)) != NULL )
                 {
                  buf = rerror->errmess;
                  bufsz = strlen( buf );
                 }
                 fwrite( buf, 1, bufsz, stdout );
                 putchar( '\n' );
                }
                else
                 {
                  int exttab, texttab, typetab, linelen;
                  buf = NULL;
                  if ( (rerror = msgtrans_lookup("DOSExt", &buf, &bufsz, NULL, NULL, NULL, NULL)) != NULL )
                  {
                   buf = rerror->errmess;
                   bufsz = strlen( buf );
                  }
                  fwrite( buf, 1, bufsz, stdout );
                  exttab = (bufsz-3)/2;
                  texttab = bufsz-exttab-1;
                  buf = NULL;
                  if ( (rerror = msgtrans_lookup("ROType", &buf, &bufsz, NULL, NULL, NULL, NULL)) != NULL )
                  {
                   buf = rerror->errmess;
                   bufsz = strlen( buf );
                  }
                  printf( "  " );
                  fwrite( buf, 1, bufsz, stdout );
                  putchar( '\n' );
                  if ( bufsz < 12 ) bufsz = 12;
                  typetab = bufsz-11;
                  linelen = exttab+3+texttab+bufsz;
                  for ( loop=linelen; loop--; ) putchar( '-' );
                  putchar('\n');

                  for (; (cptr != NULL); cptr = cptr->next)
                   {
                    for ( loop=exttab; loop--; ) putchar( ' ' );

1222
                    for (loop = 0; (loop < 7); loop++)
Neil Turton's avatar
Neil Turton committed
1223
                     {
1224
                      char cchr = (*(char*)(cptr->dosext + (loop))) ;
Neil Turton's avatar
Neil Turton committed
1225 1226 1227 1228 1229 1230 1231
                      putchar( (((cchr > ' ') && (cchr != 0x7F)) ? cchr : ' ') );
                     }

                    for ( loop=texttab; loop--; ) putchar( ' ' );

                    rset.r[0] = FSControl_ReadFileType ;
                    rset.r[2] = (word)cptr->ROtype ;
Kevin Bracey's avatar
Kevin Bracey committed
1232
                    if ((rerror = _kernel_swi(OS_FSControl,&rset,&rset)) != NULL)
Neil Turton's avatar
Neil Turton committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
                     {
                      /* Just print spaces if unknown (or error returned) */
                      rset.r[2] = 0x20202020 ;
                      rset.r[3] = 0x20202020 ;
                     }

                    for (loop = 0; (loop < 4); loop++)
                     printf("%c",(char)((rset.r[2] >> (loop * 8)) & 0xFF)) ;
                    for (loop = 0; (loop < 4); loop++)
                     printf("%c",(char)((rset.r[3] >> (loop * 8)) & 0xFF)) ;

                    for ( loop=typetab; loop--; ) putchar( ' ' );
                    printf("%03X\n",cptr->ROtype) ;
                   }
                  for ( loop=linelen; loop--; ) putchar( '-' );
                  putchar( '\n' );
                 }
               }
              else
               {
                /* convert the DOS extension given to upper-case */
                for (loop=0; (argv[loop] && (argv[loop] != ' ')); loop++)
                 {
                  int c = toupper(argv[loop]);
                  if (validchar(valchars,c))
1258
                   dosext[loop] = c  ;
Neil Turton's avatar
Neil Turton committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
                  else
                   {
                    rerror = global_error(err_invalidchar) ;
                    break ; /* from the "for" loop */
                   }
                 }

                if (!rerror) /* no error defined yet */
                 {
                  if (loop > 3)
                   rerror = global_error(err_toolong) ;
                  else
                   {
                    if (argc == 1)
                     {
                      mapentry *last = maplist ;
                      /* release this mapping */
                      for (cptr = last; (cptr != NULL); cptr = last->next)
1277
                       if (strcmp(dosext, cptr->dosext)==0)
Neil Turton's avatar
Neil Turton committed
1278
                        {
1279
                         dprintf(("","DOSMap: entry found\n")) ;
Neil Turton's avatar
Neil Turton committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290