File: load_dir.c

package info (click to toggle)
kcemu 0.5.1%2Bdfsg-5
  • links: PTS
  • area: contrib
  • in suites: wheezy
  • size: 14,552 kB
  • sloc: cpp: 48,501; ansic: 26,963; sh: 4,875; makefile: 1,760; asm: 376; yacc: 291; xml: 64; sed: 16
file content (776 lines) | stat: -rw-r--r-- 19,145 bytes parent folder | download | duplicates (5)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*
 *  KCemu -- The emulator for the KC85 homecomputer series and much more.
 *  Copyright (C) 1997-2010 Torsten Paul
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <ctype.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "libdisk/libdiskP.h"

// CP/M record size is 128
#define RECORD_SIZE    (128)                                      

// size of one directory entry
#define DIR_ENTRY_SIZE (32)                                       

#define SIZE_TO_REC(x) (((x) + (RECORD_SIZE - 1)) / RECORD_SIZE)

// directory size in bytes
#define DIR_SIZE(dpb)              (DIR_ENTRY_SIZE * ((dpb)->drm + 1))

// disk size in bytes
#define DISK_SIZE(dpb)             (RECORD_SIZE * (((dpb)->dsm + 1) << ((dpb)->bsh)))

// sector size in bytes
#define SECTOR_SIZE(dpb)           (RECORD_SIZE << ((dpb)->bsh))

// size of allocation unit
#define AU_SIZE(dpb)               (RECORD_SIZE << (dpb)->bsh)

#define SIZE_TO_AU(dpb,x)          (((x) + (AU_SIZE(dpb) - 1)) / AU_SIZE(dpb))

// FIXME: uses hardcoded disk geometry
#define CHS_TO_SECT(dpb,c,h,s)     (((s) - 1) + 5 * (h) + 10 * (c))

// FIXME: still uses hardcoded information that AU is 2 * SECTOR size
#define CHS_TO_AU(dpb,c,h,s)       (CHS_TO_SECT((dpb), (c), (h), (s)) / 2)

// FIXME: still uses hardcoded information that AU is 2 * SECTOR size
#define AU_TO_OFFSET(dpb,au,c,h,s) (CHS_TO_SECT((dpb), (c), (h), (s)) - 2 * (au))

// first data allocation unit (after directory, not counting system tracks)
#define FIRST_AU(dpb)              ((DIR_SIZE(dpb) + (AU_SIZE(dpb) - 1)) / AU_SIZE(dpb))

#define SYSTEM_NAME "@boot.sys"

/*
 *
 *  CP/M 2.2 disc formats
 *  (see http://www.seasip.demon.co.uk/Cpm/format22.html)
 *  
 *  CP/M 2.2 works with a much larger range of discs than CP/M 1.4. The
 *  disc statistics are stored in a parameter block (the DPB), which
 *  contains the following information:
 *  
 *  	DEFW	spt	;Number of 128-byte records per track
 *  	DEFB	bsh	;Block shift. 3 => 1k, 4 => 2k, 5 => 4k....
 *  	DEFB	blm	;Block mask. 7 => 1k, 0Fh => 2k, 1Fh => 4k...
 *  	DEFB	exm	;Extent mask, see later
 *  	DEFW	dsm	;(no. of blocks on the disc)-1
 *  	DEFW	drm	;(no. of directory entries)-1
 *  	DEFB	al0	;Directory allocation bitmap, first byte
 *  	DEFB	al1	;Directory allocation bitmap, second byte
 *  	DEFW	cks	;Checksum vector size, 0 for a fixed disc
 *  			;No. directory entries/4, rounded up.
 *  	DEFW	ofs	;Offset, number of reserved tracks
 *  
 *  The directory allocation bitmap is interpreted as:
 *  
 *         al0              al1
 *  b7b6b5b4b3b2b1b0 b7b6b5b4b3b2b1b0
 *   1 1 1 1 0 0 0 0  0 0 0 0 0 0 0 0
 *  
 *   - ie, in this example, the first 4 blocks of the disc contain the
 *   directory.
 *  
 *  
 *  The DPB is not stored on disc. It is either hardwired into the BIOS or
 *  generated on the fly when a disc is logged in.
 *  
 *  The reserved tracks will contain an image of CP/M 2.2, used when the
 *  system is rebooted. Discs can be formatted as data only discs, in
 *  which case they have no system tracks and cannot be used to reboot the
 *  system.
 *  
 *  CP/M 2.2 directory
 *  
 *  The CP/M 2.2 directory has only one type of entry:
 *  
 *  UU F1 F2 F3 F4 F5 F6 F7 F8 T1 T2 T3 EX S1 S2 RC   .FILENAMETYP....
 *  AL AL AL AL AL AL AL AL AL AL AL AL AL AL AL AL   ................
 *  
 *  UU = User number. 0-15 (on some systems, 0-31). The user number
 *       allows multiple files of the same name to coexist on the disc. 
 *       User number = 0E5h => File deleted
 *  Fn - filename
 *  Tn - filetype. The characters used for these are 7-bit ASCII.
 *       The top bit of T1 (often referred to as T1') is set if the
 *       file is read-only.
 *       T2' is set if the file is a system file (this corresponds
 *       to "hidden" on other systems). 
 *  EX = Extent counter, low byte - takes values from 0-31
 *  S2 = Extent counter, high byte.
 *  
 *       An extent is the portion of a file controlled by one directory
 *       entry.  If a file takes up more blocks than can be listed in one
 *       directory entry, it is given multiple entries, distinguished by
 *       their EX and S2 bytes. The formula is: Entry number =
 *       ((32*S2)+EX) / (exm+1) where exm is the extent mask value from
 *       the Disc Parameter Block.
 *  
 *  S1 - reserved, set to 0.
 *  RC - Number of records (1 record=128 bytes) used in this extent, low
 *       byte. The total number of records used in this extent is
 *  
 *       (EX & exm) * 128 + RC
 *  
 *       If RC is 80h, this extent is full and there may be another one on
 *       the disc.  File lengths are only saved to the nearest 128 bytes.
 *  
 *  AL - Allocation. Each AL is the number of a block on the disc. If an
 *       AL number is zero, that section of the file has no storage
 *       allocated to it (ie it does not exist). For example, a 3k file
 *       might have allocation 5,6,8,0,0.... - the first 1k is in block 5,
 *       the second in block 6, the third in block 8.
 *      
 *       AL numbers can either be 8-bit (if there are fewer than 256
 *       blocks on the disc) or 16-bit (stored low byte first).
 *
 *************************************************************************
 *
 *  $a = 2 * $au;
 *  $s = ($a % 5) + 1;
 *  $h = ($a % 10) / 5;
 *  $c = ($a / 10);
 *
 *  ($s - 1) + $h * 5 + $c * 10
 *
 *  Used disk layout: 2 sides, 80 tracks with 5 sectors of 1024 bytes each
 *  (This is the default disk format for the KC)
 */

static const char * loader_dir_get_name(void);
static int loader_dir_open(libdisk_prop_t *prop, const char *path);
static void loader_dir_close(libdisk_prop_t *prop);
static int loader_dir_read_sector(libdisk_prop_t *prop, unsigned char *buf, int len);
static int loader_dir_write_sector(libdisk_prop_t *prop, unsigned char *buf, int len);

static libdisk_loader_t _loader = {
  loader_dir_get_name,
  loader_dir_open,
  loader_dir_close,
  loader_dir_read_sector,
  loader_dir_write_sector
};

#define __DRM (127)                               // FIXME: hack, change to use dpb
#define __DIR_SIZE (DIR_ENTRY_SIZE * (__DRM + 1)) // FIXME: hack, change to use dpb
typedef struct {
  char *path;
  libdisk_dpb_t *dpb;
  char *filename[__DRM + 1];
  long size[__DRM + 1];
  long au[__DRM + 1];
  unsigned char dir[__DIR_SIZE];
} dir_data_t;

typedef struct sector_data_struct {
  int c;
  int h;
  int s;
  int au;
  int len;
  unsigned char *data;
  struct sector_data_struct *next;
} sector_data_t;

/*
 *  DISK PARAMETER BLOCK (for disk format 5*1024*80*2 OFF 2)
 */
static libdisk_dpb_t _dpb = {
  // logical disc descriptor

  80,   // spt: number of 128-byte records per track
  4,    // bsh: block shift. 3 => 1k, 4 => 2k, 5 => 4k....
  15,   // blm: block mask. 7 => 1k, 0Fh => 2k, 1Fh => 4k...
  0,    // exm: extent mask
  389,  // dsm: (no. of blocks on the disc) - 1
  127,  // drm: (no. of directory entries) - 1
  192,  // al0: directory allocation bitmap, first byte
  0,    // al1: directory allocation bitmap, second byte
  16,   // cks: checksum vector size, No. directory entries/4, rounded up.
  2,    // ofs: offset, number of reserved tracks
  
  // physical disc descriptor

  3,    // psh: physical block shift
  7,    // phm: physical block mask
  5,    // eot: number of last sector
  15,   // gap: sector gap
  80,   // ntr: number of tracks
};

static sector_data_t *_sector_data_head = NULL;
static sector_data_t *_sector_data_last = NULL;

static void dump_sector(unsigned char *sector, int length);
static void cache_sector_data(dir_data_t *data, int c, int h, int s, unsigned char *buf, int len);

static void
set_filename(unsigned char *buf, char *filename)
{
  int a;
  char *ptr;
  
  memset(buf, ' ', 11);

  for (a = 0;a < 8;a++)
    {
      if (filename[a] == '\0')
	break;
      if (filename[a] == '.')
	break;
      buf[a] = toupper(filename[a]);
    }

  ptr = strrchr(filename, '.');
  if (ptr == NULL)
    return;
  
  ptr++;
  for (a = 0;a < 3;a++)
    {
      if (ptr[a] == '\0')
	break;
      buf[8 + a] = toupper(ptr[a]);
    }
}

static int
set_directory_entry(dir_data_t *data, int idx, int start_au, long size, char *filename)
{
  int a;
  int au;
  int rec;
  int extend;

  au = 0;
  extend = 0;
  rec = SIZE_TO_REC(size);

  printf("set_directory_entry(): %d: '%s'\n", start_au, filename);

  do
    {
      memset(&data->dir[32 * idx], 0, 32);

      //printf("%d: %s - %d\n", extend, filename, rec);
      set_filename(&data->dir[32 * idx + 1], filename);

      data->dir[32 * idx] = 0; // user
      data->dir[32 * idx + 12] = extend;
      data->dir[32 * idx + 13] = 0;
      data->dir[32 * idx + 14] = 0;
      data->dir[32 * idx + 15] = (rec > 0x80) ? 0x80 : rec;

      for (a = 0;a < 8;a++)
	if (au < SIZE_TO_AU(data->dpb, size))
	  {
	    data->dir[32 * idx + 2 * a + 16] = (start_au + au) & 0xff;
	    data->dir[32 * idx + 2 * a + 17] = (start_au + au) >> 8;
	    au++;
	  }

      rec -= 0x80;
      extend++;
      idx++;
    }
  while (rec > 0);

  return extend;
}

static const char *
loader_dir_get_name(void)
{
  return "disk loader for unix directories";
}

static int
loader_dir_open(libdisk_prop_t *prop, const char *path)
{
  DIR *d;
  int a, idx, ro, au;
  char pwd[4096]; // FIXME: fix getcwd()!
  dir_data_t *data;
  struct stat statbuf;
  struct dirent *dirent;
  
  if (prop == NULL)
    return -1;

  if (getcwd(pwd, 4096) == NULL)
    return -1;

  if (chdir(path) < 0)
    return -1;

  ro = 1;
  d = opendir(".");
  if (d == NULL)
    return -1;

  data = (dir_data_t *)malloc(sizeof(dir_data_t));
  if (data == NULL)
      return -1;

  data->path = strdup(path);
  data->dpb = &_dpb;

  memset(data->dir, 0xe5, DIR_SIZE(data->dpb));
  memset(data->au, 0, (data->dpb->drm + 1) * sizeof(long));
  memset(data->filename, 0, (data->dpb->drm + 1) * sizeof(char *));

  a = 0;
  while (242)
    {
      dirent = readdir(d);
      if (dirent == NULL)
	break;

      if (dirent->d_name[0] == '.')
	continue;

      if (strcmp(dirent->d_name, SYSTEM_NAME) == 0)
	continue;

      if (stat(dirent->d_name, &statbuf) < 0)
	continue;

      if (!S_ISREG(statbuf.st_mode))
	continue;

      if (statbuf.st_size > DISK_SIZE(data->dpb))
	continue;

      data->size[a] = statbuf.st_size;
      data->filename[a] = strdup(dirent->d_name);
      a++;
    }
  
  closedir(d);

  if (chdir(pwd) < 0)
    printf("can't restore working directory!\n"); // FIXME:

  idx = 0;
  au = FIRST_AU(data->dpb);
  for (a = 0;(a <= data->dpb->drm) && (data->filename[a] != NULL);a++)
    {
      data->au[a] = au;
      idx += set_directory_entry(data,
				 idx,
				 au,
				 data->size[a],
				 data->filename[a]);
      
      au += SIZE_TO_AU(data->dpb, data->size[a]);
    }

#if 0
  {
    FILE *f;
    f = fopen("/tmp/dir", "wb");
    fwrite(data->dir, 1, DIR_SIZE, f);
    fclose(f);
  }
#endif
  
  prop->read_only = ro;
  prop->head_count = 2;
  prop->cylinder_count = 80;
  prop->sector_size = 1024;
  prop->sectors_per_cylinder = 5;
  prop->data = data;
  prop->loader = &_loader;

  _sector_data_head = NULL;
  
  return 0;
}

static void
loader_dir_close(libdisk_prop_t *prop)
{
  dir_data_t *data;
  sector_data_t *sector_data;

  if (prop == NULL)
    return;

  if (prop->data == NULL)
    return;

  data = (dir_data_t *)prop->data;

  free(data);

  while (_sector_data_head != NULL)
    {
      sector_data = _sector_data_head;
      _sector_data_head = sector_data->next;
      free(sector_data->data);
      free(sector_data);
    }

  prop->data = NULL;
}

static int
read_block(dir_data_t *data, unsigned char *buf, int idx, long offset, int len)
{
  FILE *f;
  char pwd[4096]; // FIXME: fix getcwd()!

  printf("read_block(): '%s' [%ld]\n", data->filename[idx], offset);

  if (getcwd(pwd, 4096) == NULL)
    return -1;

  if (chdir(data->path) < 0)
    return -1;

  f = fopen(data->filename[idx], "rb");
  if (f == NULL)
    return -1;

  if (fseek(f, offset, SEEK_SET) < 0)
    return -1;

  memset(buf, '\0', len);
  fread(buf, 1, len, f);
  fclose(f);

  if (chdir(pwd) < 0)
    printf("can't restore working directory!\n"); // FIXME:

  return len;
}

static int
write_block(dir_data_t *data, unsigned char *buf, int idx, long offset, int len)
{
  FILE *f;
  char pwd[4096]; // FIXME: fix getcwd()!

  printf("write_block(): '%s' [%ld]\n", data->filename[idx], offset);

  if (getcwd(pwd, 4096) == NULL)
    return -1;

  if (chdir(data->path) < 0)
    return -1;

  f = fopen(data->filename[idx], "r+b");
  if (f == NULL)
    return -1;

  if (fseek(f, offset, SEEK_SET) < 0)
    return -1;

  fwrite(buf, 1, len, f);
  fclose(f);

  if (chdir(pwd) < 0)
    printf("can't restore working directory!\n"); // FIXME:

  return len;
}

static int
read_system_block(char *path, unsigned char *buf, int block, int len)
{
  FILE *f;
  char pwd[4096]; // FIXME: fix getcwd()!

  printf("read_system_block(): block %d\n", block);

  if (getcwd(pwd, 4096) == NULL)
    return -1;

  if (chdir(path) < 0)
    return -1;

  f = fopen(SYSTEM_NAME, "rb");
  if (f == NULL)
    return -1;

  if (fseek(f, block * 1024, SEEK_SET) < 0)
    return -1;

  memset(buf, '\0', len);
  fread(buf, 1, len, f);
  fclose(f);

  if (chdir(pwd) < 0)
    printf("can't restore working directory!\n"); // FIXME:

  return len;
}

static int
write_system_block(char *path, unsigned char *buf, int block, int len)
{
  FILE *f;
  char pwd[4096]; // FIXME: fix getcwd()!

  printf("write_system_block(): block %d\n", block);

  if (getcwd(pwd, 4096) == NULL)
    return -1;

  if (chdir(path) < 0)
    return -1;

  f = fopen(SYSTEM_NAME, "r+b");
  if (f == NULL)
    return -1;

  if (fseek(f, block * 1024, SEEK_SET) < 0)
    return -1;

  fwrite(buf, 1, len, f);
  fclose(f);

  if (chdir(pwd) < 0)
    printf("can't restore working directory!\n"); // FIXME:

  return len;
}

static int
is_system_sector(libdisk_dpb_t *dpb, int c, int h, int s)
{
  return c < dpb->ofs;  
}

static int
is_directory_sector(libdisk_dpb_t *dpb, int c, int h, int s)
{
  /*
   *  Get allocation unit index translated by number of system tracks
   *  so we get index 0 for the first directory sector. The directory
   *  block allocation mask can only store 16 bits, so the maximum
   *  index of a directory allocation unit is 15.
   */
  int dir_al = CHS_TO_AU(dpb, c - dpb->ofs, h, s);
  if (dir_al > 15)
    return 0;

  int dir_al_mask = (dpb->al0 << 8) | dpb->al1;
  return dir_al_mask & (1 << (15 - dir_al));
}

static int
find_index(dir_data_t *data, int c, int h, int s)
{
  int a;
  int au = CHS_TO_AU(data->dpb, c - data->dpb->ofs, h, s);

  for (a = data->dpb->drm;a >= 0;a--)
    {
      if (data->au[a] == 0)
	continue;
      if (data->dir[32 * a] == 0xe5) // skip deleted
	continue;
      if (data->au[a] <= au)
	break;
    }

  return a;
}

static int
loader_dir_read_sector(libdisk_prop_t *prop, unsigned char *buf, int len)
{
  int h, c, s;
  dir_data_t *data;

  if (prop == NULL)
    return -1;

  if (prop->data == NULL)
    return -1;

  h = prop->head;
  c = prop->cylinder;
  s = prop->sector;
  data = (dir_data_t *)prop->data;

  if (is_system_sector(data->dpb, c, h, s))
    {
      int block = CHS_TO_SECT(data->dpb, c, h, s);
      return read_system_block(data->path, buf, block, len);
    }
  
  if (is_directory_sector(data->dpb, c, h, s))
    {
      int block = CHS_TO_SECT(data->dpb, c - data->dpb->ofs, h, s);
      memcpy(buf, &data->dir[block * len], len);
      return len;
    }

  /*
   *  normal data sector
   */      
  int idx = find_index(data, c, h, s);
  if (idx < 0)
    {
      printf("*** allocation unit not found for sector C/H/S %d/%d/%d ***\n", c, h, s);
      //memset(buf, 0xe5, len);
      //int block = CHS_TO_SECT(data->dpb, c, h, s);
      //memcpy(buf, _debug_sector_buffer + len * block, len);
      return len;
    }
  
  int au_ofs = AU_TO_OFFSET(data->dpb, data->au[idx], c - data->dpb->ofs, h, s);
  return read_block(data, buf, idx, len * au_ofs, len);
}

static int
loader_dir_write_sector(libdisk_prop_t *prop, unsigned char *buf, int len)
{
  int h = prop->head;
  int c = prop->cylinder;
  int s = prop->sector;

  dir_data_t *data = (dir_data_t *)prop->data;

  printf("loader_dir_write_sector(): C/H/S = %d/%d/%d, len = %d\n",
	 c, h, s, len);

  dump_sector(buf, len);

  int block = CHS_TO_SECT(data->dpb, c, h, s);

  if (is_system_sector(data->dpb, c, h, s))
    {
      write_system_block(data->path, buf, block, len);
    }
  else if (is_directory_sector(data->dpb, c, h, s))
    {
      int block = CHS_TO_SECT(data->dpb, c - data->dpb->ofs, h, s);
      memcpy(&data->dir[block * len], buf, len);
    }
  else
    {
      int idx = find_index(data, c, h, s);

      if (idx < 0)
	{
	  cache_sector_data(data, c, h, s, buf, len);
	}
      else
	{
	  int au_ofs = AU_TO_OFFSET(data->dpb, data->au[idx], c - data->dpb->ofs, h, s);
	  return write_block(data, buf, idx, len * au_ofs, len);
	}
    }

  return len;
}

static void
cache_sector_data(dir_data_t *data, int c, int h, int s, unsigned char *buf, int len)
{
  sector_data_t *sector_data;

  sector_data = malloc(sizeof(sector_data_t));
  sector_data->c = c;
  sector_data->h = h;
  sector_data->s = s;
  sector_data->au = CHS_TO_AU(data->dpb, c - data->dpb->ofs, h, s);
  sector_data->len = len;
  sector_data->next = NULL;
  sector_data->data = malloc(len);
	  
  memcpy(sector_data->data, buf, len);
  
  if (_sector_data_head == NULL)
    {
      _sector_data_head = _sector_data_last = sector_data;
    }
  else
    {
      _sector_data_last->next = sector_data;
      _sector_data_last = sector_data;
    }
}

// DEBUG

static void
dump_sector(unsigned char *sector, int len)
{
  int a, b, blen;
  unsigned char buf[16];

  a = 0;
  while (a < len)
    {
      for (b = 0;b < 16;b++)
	{
	  if ((a + b) >= len)
	    break;
	  buf[b] = sector[a++];
	  if (a >= len)
	    {
	      printf("\n");
	      return;
	    }
	  blen = b + 1;
	}
      printf("%04x: ", a);
      for (b = 0;b < 16;b++)
	{
	  if (b < blen)
	    {
	      printf("%02x ", buf[b]);
	    }
	  else
	    {
	      printf("   ");
	    }
	  if (b == 7)
	    printf("- ");
	}
      printf(" |  ");
      for (b = 0;b < blen;b++)
	{
	  printf("%c", (isprint(buf[b])) ? buf[b] : '.');
	  if (b == 7)
	    printf(" - ");
	}
      printf("\n");
      if (blen  != 16)
	break;
    }
  printf("\n");
}

void
loader_dir_init(void)
{
  libdisk_register_loader(&_loader);
}