aboutsummaryrefslogtreecommitdiffstats
path: root/ldlinux.c
blob: 73f3ba2d5b6f9e842db0ba1ee8fa4547a1cacf61 (plain)
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
#include "fat_fs.h"
#include "cache.h"
#include "disklab.h"

#include <stdio.h>
#include <malloc.h>
#include <string.h>


/* some generic defines */
#define FILENAME_MAX_LG2 6
#define FILENAME_MAX_    ( 1 << FILENAME_MAX_LG2 )
#define NULLFILE         0
#define NULLOFFSET       0
#define retry_count      16
#define LDLINUX_MAGIC    0x3eb202fe

#define MAX_OPEN_LG2     6
#define MAX_OPEN         ( 1 << MAX_OPEN_LG2 )

#define SECTOR_SHIFT     9
#define SECTOR_SIZE      ( 1 << SECTOR_SHIFT )

#define DIRENT_SHIFT     5
#define DIRENT_SIZE      ( 1 << DIRENT_SHIFT )

#define ROOT_DIR_WORD    0x002f


/* generic information about FAT fs */

__u32 FAT;            /* Location of (first) FAT */
__u32 RootDirArea;    /* Location of root directory area */
__u32 RootDir;        /* Location of root directory proper */
__u32 DataArea;       /* Location of data area */
__u32 TotalSectors;   /* Total number of sectors */
__u32 ClustSize;      /* Bytes/cluster */
__u32 ClustMask;      /* Sector/cluster - 1 */
__u8  CopySuper;      /* Distinguish .bs versus .bss */
__u8  DriveNumber;    /* BIOS drive number */
__u8  ClustShift;     /* Shift count for sectors/cluster */
__u8  ClustByteShift; /* Shift count for bytes/cluster */

/* for SYSLINUX, the block size equal to the sector size */
__u32 blk_size = 1 << SECTOR_SHIFT;


/* file structure. This holds the information for each currently open file */
struct open_file_t {
        __u32 file_sector;    /* sector pointer ( 0 = structure free ) */
        __u32 file_bytesleft; /* number of bytes left */
        __u32 file_left;      /* number of sectors left */
        __u32 pad;            /* padding */
};

struct open_file_t Files[MAX_OPEN];

#define trackbufsize 8192
char trackbuf[trackbufsize];


/* the fat bpb data */
struct fat_bpb fat;




/**
 * allocate_file:
 * 
 * Allocate a file structure
 *
 * @return: if successful return the file pointer, or return NULL
 *
 */
struct open_file_t *allocate_file()
{
        struct open_file_t *file;
        int i = 0;
        
        file = Files;

        for (; i < MAX_OPEN; i ++ ) {
                if ( file->file_sector == 0 ) /* found it */
                        return file;
                file ++;
        }

        return NULL; /* not found */
}


/**
 * alloc_fill_dir:
 *
 * Allocate then fill a file structure for a directory starting in
 * sector SECTOR. if successful, return the pointer of filled file
 * structure, or return NULL.
 *
 */
struct open_file_t* alloc_fill_dir(__u32 sector)
{
        struct open_file_t *file;

        file  = allocat_file();
        if ( !file )
                return NULL;

        file->file_sector = sector; /* current sector */
        file->file_bytesleft = 0;   /* current offset */
        file->file_left = sector;   /* beginning sector */

        return file;
}


/* Deallocates a file structure */
void close_file(struct open_file_t *file)
{
        if ( !file )
                file->file_sector = 0;
}



/* Deallocates a directory structure */
void close_dir(xxx* yy)
{
        if ( !yy )
                *yy = 0;
}




/**
 * getfssec_edx:
 *
 * get multiple sectors from a file
 *
 * This routine makes sure the subransfers do not cross a 64K boundary
 * and will correct the situation if it does, UNLESS *sectos* cross
 * 64K boundaries.
 *
 * @param: buf
 * @param: curr_sector
 * @param: sectors
 *
 */
void getfssec_edx(char *buf, __u32 curr_sector, __u32 sectors)
{
        
}






/**
 * getfssec:
 *
 * get multiple sectors from a file 
 *
 *
 * @param: buf
 * @param: file
 * @param: sectors
 *
 * @return: number of bytes read
 *
 */
void getfssec(char *buf, struct open_file_t *file, __u32 sectors)
{
        __u32 bytes_read;

        if ( sectors > file_file_left )
                sectors = file->file_left;
        bytes_read = sectors << SECTOR_SHIFT;

        file->file_left -= sectors;
        edx = file_file_sector;
        getfssec_edx(buf, edx, sectors);

        if ( bytes_read >= file->file_bytesleft ) {
                bytes_read = file->file_bytesleft;
                file->file_bytesleft = 0;

                close_file(file);
        } else
                file->file_bytesleft -= bytes_read;

        return bytes_read;
}
                
        








/**
 * mangle_dos_name:
 *
 * Mangle a dos filename component pointed to by FILENAME
 * into MangleBuf; ends on encountering any whitespace or 
 * slash.
 *
 * WARNING: saves pointers into the buffer for longname matchs!
 *
 * @param: filename
 * @param: MangleBuf
 *
 */
void mangle_dos_name(char *MangleBuf, char* filename)
{
       
        char *di = MangleBuf;
        char *NameStart = str;
        char al, ah;
        unsigned short ax;
        
        int cx = 11; /* # of bytes to write */
        int NameLen;
 loop:       
        do {
                al = *str ++;
                if ( al <= ' ' || al == '/')
                        goto end;
                if ( al == '.' )
                        goto is_period;

                if ( al >= 'a' && al <= 'z')
                        al -= 32;
                
                
                ah = cx; /* if the first byte (only!)...*/
                ax = (ah << 8) | al;
                printf("---ax = 0x%x---\n", ax);
                if ( ax == 0x0be5 )
                        al = 0x05;   /* change it to 05 hex */
                *di ++ = al;
                
        }while( --cx );       /* don't continue if too long */
        
        /* find the end for the benefit of longname search */
        do {
                al = *str ++;
                if ( al <= ' ' )
                        break;
        }while(  al != '/' );
 end:
       
        while( cx -- )
                *di ++ = ' ';
        *di = '\0'; 
        return ;

 is_period:
        al = ' ';
        do {
                if (cx <= 3)     /* if <= 3 characters left */
                        goto loop;      /* just ignore it */
                *di ++ = al;
        }while ( --cx );
        *di = '\0';
}




/**
 * search_dos_dir:
 *
 * search a specific directory for a pre-mangled filename in
 * MangleBuf, in the directory starting in sector SECTOR
 *
 * NOTE: This file considers finding a zero-length file an
 * error.  This is so we don't have to deal with that special
 * case elsewhere in the program (most loops have the test
 * at the end).
 *
 * @param: MangleBuf
 * @param: dir_sector, directory sector
 *
 * @out:  file pointer
 * @out:  file length (MAY BE ZERO!)
 * @out:  file attribute
 * @out:  dh, clobbered.
 *
 */
struct open_file_t* search_dos_dir(char *MangleBuf, __u32 dir_sector)
{
        struct open_file_t*  file;
        struct cache_struct* cs;
        struct fat_dir_entry *dir;
        struct fat_long_name_entry *long_dir;

        __u32 slots;
        
        file = allocate_file();
        if ( !file )
                goto alloc_failure;

        slots = (NameLen + 12) / 13;
        slots != 0x40;

        VFATInit = slots;
        VFATNext = slots;

 scansector:
        cs = get_cache_block(dir_sector);
        dir = (struct fat_dir_entry *)cs->data;
        entries = SECTOR_SIZE / 32;

 scanentry:
        if ( *(char *)dir == 0 )
                goto failure;
        if ( dir->attr != 0x0f )
                goto short_entry;

        /* Else it's a long name entry */
        long_dir = (struct fat_long_name_entry *)dir;
        id = long_dir->id;
        if ( id !=VFATNext )
                goto not_us;

        if ( id & 0x40 )
                VFATCsum = long_dir->checksum;/*get the initial checksum value*/
        else {
                if ( long_dir->checksum != VFATCsum )
                        goto not_us;
        }

 done_csum:
        id &= 0x3f;
        if ( !id )
                goto not_us;

        VFATNext = --id;
        index = id * 13;
        if ( index >= NameLen )
                goto not_us;
        
        di = NameStart;
        cx = 13;
 vfat_cmp:
        do {
                if ( index >= NameLen )
                        goto vfat_tail;
                
        }while(--cx);








}





/**
 * searchdir:
 * 
 * open a file
 *
 * @param: filename, the file we wanna open
 * @param: file_len, to return the file length
 *
 * @return: return the file structure on successful, or NULL.
 *
 */
struct open_file_t* searchdir(char *filename, __u32 *file_len)
{
        __u32 dir_sector, prev_dir;
        char  *p;

        struct open_file_t *file;

        dir_sector = CurrentDir;
        if ( *filename == '/' ) {
                dir_sector = RootDir;
                filename ++;
        }

 pathwalk:
        p = filename;

        while ( (*p > ' ') && (*p != '/') )
                p ++;
                
        //xchg(filename, p);
        if (filename == p)
                goto founddir;

        prev_dir = dir_sector;

        mangle_dos_name(filename);
        file = search_dos_dir(filename);
        if ( !file )
                goto notfound;

        if ( *p == '/' )
                goto isdir;
 isfile:
        if ( (attr & 0x18) || (*file_len == 0) ) 
                goto badfile;
        
        file->file_bytesleft = *file_len; /**/
        file->file_left = ( *file_len + SECTOR_SIZE -1 ) >> SECTOR_SHIFT;
        
        return file;

 isdir:
        if ( attr & 0x10 ) /* subdirectory */
                goto badfile;
        
        dir_sector = file->file_sector;
        close(file);
        
        goto pathwalk;

 founddir:
        ret;
 badfile:
        *file_len = 0;
 notfound:
        *file_len = 0;
        ret;
}




/**
 * readdir:
 *
 * read one file from a directory
 *
 * returns the file's name in the filename string buffer
 *
 * @param: filename
 * @param: file
 *
 */
void readdir(struct open_file_t* file, char* filename)
{

        
}


/**
 * getfatsector:
 *
 * check for a particular sector in the FAT cache.
 *
 */
struct cache_struct *getfatsector(__u32 sector)
{
        return get_cache_block(FAT + sector);
}


/**
 * nextsector:
 * 
 * given a sector  on input, return the next sector of the 
 * same filesystem object, which may be the root directory or a 
 * cluster chain. Returns EOF.
 *
 */
void nextsector(__u32 sector)
{







}




/**
 * nextcluster: 
 *
 * Advance a cluster pointer in clust_num to the next cluster
 * pointer at in the FAT tables. CF = 0 on return if end of file.
 *
 * @param: clust_num;
 *
 */
void nextcluster(__u32 clust_num)
{
        

 nextcluster_fat12:

 nextcluster_fat16:
        
 nextcluster_fat28:

}






/* init. the fs meta data */
void init_fs()
{
        int sectors_per_fat; 

        /* get the fat bpb information */
        getlinsec(&fat, 0, 1);

        TotalSectors = fat.bxSectors ? : fat.bsHugeSectors;
        FAT = fat.bxResSectors;

        sectors_per_fat = fat.bxFATsecs ? : fat.bxFATsecs_32;
        RootDir = RootDirArea = FAT + sectors_per_fat * fat.bxFATs;
        RootDirSize = (fat.bxRootDirEnts+SECTOR_SIZE/32-1) >> (SECTOR_SHFIT-5);
        DataArea = RootDirArea + RootDirSize;

        bsr(ClustShift, bxSecPerClust);
        ClustByteShift = ClustShift + SECTOR_SHIFT;
        ClustMask = fat.bxSecPerClust - 1;
        ClustSize = fat.bxSecPerClust << SECTOR_SIZE;

}