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
|
#ifdef FSYS_AFFS
#include "shared.h"
#include "filesys.h"
/******************************** RDB definitions */
#define RDB_LOCATION_LIMIT 16
#define IDNAME_RIGIDDISK 0x5244534B /* 'RDSK' */
struct RigidDiskBlock
{
unsigned long rdb_ID;
unsigned long rdb_SummedLongs;
long rdb_ChkSum;
unsigned long rdb_HostID;
unsigned long rdb_BlockBytes;
unsigned long rdb_Flags;
unsigned long rdb_BadBlockList;
unsigned long rdb_PartitionList;
unsigned long rdb_FileSysHeaderList;
unsigned long rdb_DriveInit;
unsigned long rdb_Reserved1[6];
unsigned long rdb_Cylinders;
unsigned long rdb_Sectors;
unsigned long rdb_Heads;
unsigned long rdb_Interleave;
unsigned long rdb_Park;
unsigned long rdb_Reserved2[3];
unsigned long rdb_WritePreComp;
unsigned long rdb_ReducedWrite;
unsigned long rdb_StepRate;
unsigned long rdb_Reserved3[5];
unsigned long rdb_RDBBlocksLo;
unsigned long rdb_RDBBlocksHi;
unsigned long rdb_LoCylinder;
unsigned long rdb_HiCylinder;
unsigned long rdb_CylBlocks;
unsigned long rdb_AutoParkSeconds;
unsigned long rdb_HighRDSKBlock;
unsigned long rdb_Reserved4;
char rdb_DiskVendor[8];
char rdb_DiskProduct[16];
char rdb_DiskRevision[4];
char rdb_ControllerVendor[8];
char rdb_ControllerProduct[16];
char rdb_ControllerRevision[4];
char rdb_DriveInitName[40];
};
struct PartitionBlock
{
unsigned long pb_ID;
unsigned long pb_SummedLongs;
long pb_ChkSum;
unsigned long pb_HostID;
unsigned long pb_Next;
unsigned long pb_Flags;
unsigned long pb_Reserved1[2];
unsigned long pb_DevFlags;
char pb_DriveName[32];
unsigned long pb_Reserved2[15];
unsigned long pb_Environment[20];
unsigned long pb_EReserved[12];
};
#define DE_TABLESIZE 0
#define DE_SIZEBLOCK 1
#define DE_BLOCKSIZE 2
#define DE_NUMHEADS 3
#define DE_SECSPERBLOCK 4
#define DE_BLKSPERTRACK 5
#define DE_RESERVEDBLKS 6
#define DE_PREFAC 7
#define DE_INTERLEAVE 8
#define DE_LOWCYL 9
#define DE_HIGHCYL 10
#define DE_UPPERCYL DE_HIGHCYL
#define DE_NUMBUFFERS 11
#define DE_BUFMEMTYPE 12
#define DE_MEMBUFTYPE DE_BUFMEMTYPE
#define DE_MAXTRANSFER 13
#define DE_MASK 14
#define DE_BOOTPRI 15
#define DE_DOSTYPE 16
#define DE_BAUD 17
#define DE_CONTROL 18
#define DE_BOOTBLOCKS 19
/******************************** AFFS definitions */
#define T_SHORT 2
#define T_LIST 16
#define ST_FILE -3
#define ST_ROOT 1
#define ST_USERDIR 2
struct BootBlock{
int id;
int chksum;
int rootblock;
int data[127];
};
struct RootBlock{
int p_type; //0
int n1[2]; //1-2
int hashtable_size; //3
int n2; //4
int checksum; //5
int hashtable[72]; //6-77
int bitmap_valid_flag; //78
int bitmap_ptrs[25]; //79-103
int bitmap_extension; //104
int root_days; //105
int root_mins; //106
int root_ticks; //107;
char diskname[32]; //108-115
int n3[2]; //116-117
int volume_days; //118
int volume_mins; //119
int volume_ticks; //120
int creation_days; //121
int creation_mins; //122
int creation_ticks; //123
int n4[3]; //124-126
int s_type; //127
};
struct DirHeader {
int p_type; //0
int own_key; //1
int n1[3]; //2-4
int checksum; //5
int hashtable[72]; //6-77
int n2; //78
int owner; //79
int protection; //80
int n3; //81
char comment[92]; //82-104
int days; //105
int mins; //106
int ticks; //107
char name[32]; //108-115
int n4[2]; //116-117
int linkchain; //118
int n5[5]; //119-123
int hashchain; //124
int parent; //125
int n6; //126
int s_type; //127
};
struct FileHeader {
int p_type; //0
int own_key; //1
int n1[3]; //2-4
int checksum; //5
int filekey_table[72]; //6-77
int n2; //78
int owner; //79
int protection; //80
int bytesize; //81
char comment[92]; //82-104
int days; //105
int mins; //106
int ticks; //107
char name[32]; //108-115
int n3[2]; //116-117
int linkchain; //118
int n4[5]; //119-123
int hashchain; //124
int parent; //125
int extension; //126
int s_type; //127
};
struct FileKeyExtension{
int p_type; //0
int own_key; //1
int table_size; //2
int n1[2]; //3-4
int checksum; //5
int filekey_table[72]; //6-77
int info[46]; //78-123
int n2; //124
int parent; //125
int extension; //126
int s_type; //127
};
struct Position {
unsigned int block;
short filekey;
unsigned short byte;
unsigned int offset;
};
struct ReadData {
unsigned int header_block;
struct Position current;
unsigned int filesize;
};
//#warning "Big vs. little endian for configure needed"
#define AROS_BE2LONG(l) \
( \
((((unsigned long)(l)) >> 24) & 0x000000FFUL) | \
((((unsigned long)(l)) >> 8) & 0x0000FF00UL) | \
((((unsigned long)(l)) << 8) & 0x00FF0000UL) | \
((((unsigned long)(l)) << 24) & 0xFF000000UL) \
)
struct CacheBlock {
int blocknum;
unsigned short flags;
unsigned short access_count;
unsigned int blockbuffer[128];
};
#define LockBuffer(x) (((struct CacheBlock *)(x))->flags |= 0x0001)
#define UnLockBuffer(x) (((struct CacheBlock *)(x))->flags &= ~0x0001)
#define MAX_CACHE_BLOCKS 10
struct FSysBuffer {
struct ReadData file;
struct CacheBlock blocks[MAX_CACHE_BLOCKS];
};
#define bootBlock(x) ((struct BootBlock *)(x)->blockbuffer)
#define rootBlock(x) ((struct RootBlock *)(x)->blockbuffer)
#define dirHeader(x) ((struct DirHeader *)(x)->blockbuffer)
#define fileHeader(x) ((struct FileHeader *)(x)->blockbuffer)
#define extensionBlock(x) ((struct FileKeyExtension *)(x)->blockbuffer)
#define rdsk(x) ((struct RigidDiskBlock *)(x)->blockbuffer)
#define part(x) ((struct PartitionBlock *)(x)->blockbuffer)
static struct FSysBuffer *fsysb;
static int blockoffset; /* offset if there is an embedded RDB partition */
static int rootb; /* block number of root block */
static int rdbb; /* block number of rdb block */
static void initCache(void)
{
int i;
for (i=0;i<MAX_CACHE_BLOCKS;i++)
{
fsysb->blocks[i].blocknum = -1;
fsysb->blocks[i].flags = 0;
fsysb->blocks[i].access_count = 0;
}
}
static struct CacheBlock *getBlock(unsigned int block)
{
struct CacheBlock *freeblock;
int i;
/* get first unlocked block */
i = 0;
do
{
freeblock = &fsysb->blocks[i++];
} while (freeblock->flags & 0x0001);
/* search through list if block is already loaded in */
for (i=0;i<MAX_CACHE_BLOCKS;i++)
{
if (fsysb->blocks[i].blocknum == block)
{
fsysb->blocks[i].access_count++;
return &fsysb->blocks[i];
}
if (!(fsysb->blocks[i].flags & 0x0001))
if (freeblock->access_count>fsysb->blocks[i].access_count)
freeblock = &fsysb->blocks[i];
}
freeblock->blocknum = block;
devread(block+blockoffset, 0, 512, (char *)freeblock->blockbuffer);
return freeblock;
}
static unsigned int calcChkSum(unsigned short SizeBlock, unsigned int *buffer)
{
unsigned int sum=0,count=0;
for (count=0;count<SizeBlock;count++)
sum += AROS_BE2LONG(buffer[count]);
return sum;
}
int affs_mount(void) {
struct CacheBlock *cblock;
int i;
if (
(current_drive & 0x80) &&
(current_partition != 0xFFFFFF) &&
(current_slice != 0x30)
)
return 0;
fsysb = (struct FSysBuffer *)FSYS_BUF;
blockoffset = 0;
initCache();
/* check for rdb partitiontable */
for (i=0;i<RDB_LOCATION_LIMIT;i++)
{
cblock = getBlock(i);
if (
(
((AROS_BE2LONG(bootBlock(cblock)->id) & 0xFFFFFF00)==0x444F5300) &&
((AROS_BE2LONG(bootBlock(cblock)->id) & 0xFF)>0)
) ||
(AROS_BE2LONG(cblock->blockbuffer[0]) == IDNAME_RIGIDDISK)
)
break;
}
if (i == RDB_LOCATION_LIMIT)
return 0;
if (AROS_BE2LONG(cblock->blockbuffer[0]) == IDNAME_RIGIDDISK)
{
/* we have an RDB partition table within a MBR-Partition */
rdbb = i;
}
else if (i<2)
{
/* partition type is 0x30 = AROS and AFFS formatted */
rdbb = RDB_LOCATION_LIMIT;
rootb = (part_length-1+2)/2;
cblock = getBlock(rootb);
if (
(AROS_BE2LONG(rootBlock(cblock)->p_type) != T_SHORT) ||
(AROS_BE2LONG(rootBlock(cblock)->s_type) != ST_ROOT) ||
calcChkSum(128, cblock->blockbuffer)
)
return 0;
}
else
return 0;
return 1;
}
static int seek(unsigned long offset)
{
struct CacheBlock *cblock;
unsigned long block;
unsigned long togo;
block = fsysb->file.header_block;
togo = offset / 512;
fsysb->file.current.filekey = 71-(togo % 72);
togo /= 72;
fsysb->file.current.byte = offset % 512;
fsysb->file.current.offset = offset;
while ((togo) && (block))
{
disk_read_func = disk_read_hook;
cblock = getBlock(block);
disk_read_func = NULL;
block = AROS_BE2LONG(extensionBlock(cblock)->extension);
togo--;
}
if (togo)
return 1;
fsysb->file.current.block = block;
return 0;
}
int affs_read(char *buf, int len) {
struct CacheBlock *cblock;
unsigned short size;
unsigned int readbytes = 0;
if (fsysb->file.current.offset != filepos)
{
if (seek(filepos))
return ERR_FILELENGTH;
}
if (fsysb->file.current.block == 0)
return 0;
if (len>(fsysb->file.filesize-fsysb->file.current.offset))
len=fsysb->file.filesize-fsysb->file.current.offset;
disk_read_func = disk_read_hook;
cblock = getBlock(fsysb->file.current.block);
disk_read_func = NULL;
while (len)
{
disk_read_func = disk_read_hook;
if (fsysb->file.current.filekey<0)
{
fsysb->file.current.filekey = 71;
fsysb->file.current.block = AROS_BE2LONG(extensionBlock(cblock)->extension);
if (fsysb->file.current.block)
{
cblock = getBlock(fsysb->file.current.block);
}
//#warning "else shouldn't occour"
}
size = 512;
size -= fsysb->file.current.byte;
if (size>len)
{
size = len;
devread
(
AROS_BE2LONG
(
extensionBlock(cblock)->filekey_table
[fsysb->file.current.filekey]
)+blockoffset,
fsysb->file.current.byte, size, (char *)((long)buf+readbytes)
);
fsysb->file.current.byte += size;
}
else
{
devread
(
AROS_BE2LONG
(
extensionBlock(cblock)->filekey_table
[fsysb->file.current.filekey]
)+blockoffset,
fsysb->file.current.byte, size, (char *)((long)buf+readbytes)
);
fsysb->file.current.byte = 0;
fsysb->file.current.filekey--;
}
disk_read_func = NULL;
len -= size;
readbytes += size;
}
fsysb->file.current.offset += readbytes;
filepos = fsysb->file.current.offset;
return readbytes;
}
static unsigned char capitalch(unsigned char ch, unsigned char flags)
{
if ((flags==0) || (flags==1))
return (unsigned char)((ch>='a') && (ch<='z') ? ch-('a'-'A') : ch);
else // DOS\(>=2)
return (unsigned char)(((ch>=224) && (ch<=254) && (ch!=247)) ||
((ch>='a') && (ch<='z')) ? ch-('a'-'A') : ch);
}
// str2 is a BCPL string
static int noCaseStrCmp(char *str1, char *str2, unsigned char flags)
{
unsigned char length;
length=str2++[0];
do {
if ((*str1==0) && (length==0))
return 0;
length--;
// if ((*str1==0) && (*str2==0)) return 1;
} while (capitalch(*str1++,flags)==capitalch(*str2++,flags));
str1--;
return (*str1) ? 1 : -1;
}
static unsigned int getHashKey(char *name,unsigned int tablesize, unsigned char flags)
{
unsigned int length;
length=0;
while (name[length] != 0)
length++;
while (*name!=0)
length=(length * 13 +capitalch(*name++,flags)) & 0x7FF;
return length%tablesize;
}
static grub_error_t getHeaderBlock(char *name, struct CacheBlock **dirh)
{
int key;
key = getHashKey(name, 72, 1);
if (!dirHeader(*dirh)->hashtable[key])
return ERR_FILE_NOT_FOUND;
*dirh = getBlock(AROS_BE2LONG(dirHeader(*dirh)->hashtable[key]));
if (calcChkSum(128, (*dirh)->blockbuffer))
{
#ifdef DEBUG_AFFS
printf("ghb: %d\n", (*dirh)->blocknum);
#endif
return ERR_FSYS_CORRUPT;
}
if (AROS_BE2LONG(dirHeader(*dirh)->p_type) != T_SHORT)
return ERR_BAD_FILETYPE;
while (noCaseStrCmp(name,dirHeader(*dirh)->name,1) != 0)
{
if (!dirHeader(*dirh)->hashchain)
return ERR_FILE_NOT_FOUND;
*dirh = getBlock(AROS_BE2LONG(dirHeader(*dirh)->hashchain));
if (calcChkSum(128, (*dirh)->blockbuffer))
{
#ifdef DEBUG_AFFS
printf("ghb2: %d\n", (*dirh)->blocknum);
#endif
return ERR_FSYS_CORRUPT;
}
if (AROS_BE2LONG(dirHeader(*dirh)->p_type) != T_SHORT)
return ERR_BAD_FILETYPE;
}
return 0;
}
static char *copyPart(char *src, char *dst)
{
while ((*src != '/') && (*src))
*dst++ = *src++;
if (*src == '/')
src++;
*dst-- = 0;
/* cut off spaces at the end */
while (*dst == ' ')
*dst-- = 0;
return src;
}
static grub_error_t findBlock(char *name, struct CacheBlock **dirh)
{
char dname[32];
int block;
name++; /* skip "/" */
/* partition table part */
if (rdbb < RDB_LOCATION_LIMIT)
{
int bpc;
blockoffset = 0;
*dirh = getBlock(rdbb);
if (*name==0)
return 0;
name = copyPart(name, dname);
bpc = AROS_BE2LONG(rdsk(*dirh)->rdb_Sectors)*AROS_BE2LONG(rdsk(*dirh)->rdb_Heads);
block = AROS_BE2LONG(rdsk(*dirh)->rdb_PartitionList);
while (block != -1)
{
*dirh = getBlock(block);
if (noCaseStrCmp(dname, part(*dirh)->pb_DriveName, 1) == 0)
break;
block = AROS_BE2LONG(part(*dirh)->pb_Next);
}
if (block == -1)
return ERR_FILE_NOT_FOUND;
if (
((AROS_BE2LONG(part(*dirh)->pb_Environment[DE_DOSTYPE]) & 0xFFFFFF00)!=0x444F5300) ||
((AROS_BE2LONG(part(*dirh)->pb_Environment[DE_DOSTYPE]) & 0xFF)==0)
)
return ERR_BAD_FILETYPE;
blockoffset = AROS_BE2LONG(part(*dirh)->pb_Environment[DE_LOWCYL]);
rootb = AROS_BE2LONG(part(*dirh)->pb_Environment[DE_HIGHCYL]);
rootb = rootb-blockoffset+1; /* highcyl-lowcyl+1 */
rootb *= bpc;
rootb = rootb-1+AROS_BE2LONG(part(*dirh)->pb_Environment[DE_RESERVEDBLKS]);
rootb /= 2;
blockoffset *= bpc;
}
/* filesystem part */
*dirh = getBlock(rootb);
while (*name)
{
if (
(AROS_BE2LONG(dirHeader(*dirh)->s_type) != ST_ROOT) &&
(AROS_BE2LONG(dirHeader(*dirh)->s_type) != ST_USERDIR)
)
return ERR_BAD_FILETYPE;
name = copyPart(name, dname);
errnum = getHeaderBlock(dname, dirh);
if (errnum)
return errnum;
}
return 0;
}
#ifndef STAGE1_5
static void checkPossibility(char *filename, char *bstr)
{
char cstr[32];
if (noCaseStrCmp(filename, bstr, 1)<=0)
{
if (print_possibilities>0)
print_possibilities = -print_possibilities;
memcpy(cstr, bstr+1, bstr[0]);
cstr[bstr[0]]=0;
print_a_completion(cstr);
}
}
#else
#define checkPossibility(a, b) do { } while(0)
#endif
int affs_dir(char *dirname)
{
struct CacheBlock *buffer1;
struct CacheBlock *buffer2;
char *current = dirname;
char filename[128];
char *fname = filename;
int i,block;
if (print_possibilities)
{
while (*current)
current++;
while (*current != '/')
current--;
current++;
while (*current)
{
*fname++ = *current;
*current++ = 0;
}
*fname=0;
errnum = findBlock(dirname, &buffer1);
if (errnum)
return 0;
if (AROS_BE2LONG(dirHeader(buffer1)->p_type) == IDNAME_RIGIDDISK)
{
block = AROS_BE2LONG(rdsk(buffer1)->rdb_PartitionList);
while (block != -1)
{
buffer1 = getBlock(block);
checkPossibility(filename, part(buffer1)->pb_DriveName);
block = AROS_BE2LONG(part(buffer1)->pb_Next);
}
#ifndef STAGE1_5
if (*filename == 0)
if (print_possibilities>0)
print_possibilities = -print_possibilities;
#endif
}
else if (AROS_BE2LONG(dirHeader(buffer1)->p_type) == T_SHORT)
{
LockBuffer(buffer1);
for (i=0;i<72;i++)
{
block = dirHeader(buffer1)->hashtable[i];
while (block)
{
buffer2 = getBlock(AROS_BE2LONG(block));
if (calcChkSum(128, buffer2->blockbuffer))
{
errnum = ERR_FSYS_CORRUPT;
return 0;
}
if (AROS_BE2LONG(dirHeader(buffer2)->p_type) != T_SHORT)
{
errnum = ERR_BAD_FILETYPE;
return 0;
}
checkPossibility(filename, dirHeader(buffer2)->name);
block = dirHeader(buffer2)->hashchain;
}
}
UnLockBuffer(buffer1);
#ifndef STAGE1_5
if (*filename == 0)
if (print_possibilities>0)
print_possibilities = -print_possibilities;
#endif
}
else
{
errnum = ERR_BAD_FILETYPE;
return 0;
}
while (*current != '/')
current--;
current++;
fname = filename;
while (*fname)
*current++ = *fname++;
//#warning "TODO: add some more chars until possibilities differ"
if (print_possibilities>0)
errnum = ERR_FILE_NOT_FOUND;
return (print_possibilities<0);
}
else
{
while (*current && !isspace(*current))
*fname++ = *current++;
*fname = 0;
errnum = findBlock(filename, &buffer2);
if (errnum)
return 0;
if (AROS_BE2LONG(fileHeader(buffer2)->s_type)!=ST_FILE)
{
errnum = ERR_BAD_FILETYPE;
return 0;
}
fsysb->file.header_block = AROS_BE2LONG(fileHeader(buffer2)->own_key);
fsysb->file.current.block = AROS_BE2LONG(fileHeader(buffer2)->own_key);
fsysb->file.current.filekey = 71;
fsysb->file.current.byte = 0;
fsysb->file.current.offset = 0;
fsysb->file.filesize = AROS_BE2LONG(fileHeader(buffer2)->bytesize);
filepos = 0;
filemax = fsysb->file.filesize;
return 1;
}
}
#endif
|