aboutsummaryrefslogtreecommitdiffstats
path: root/extlinux.c
blob: ac1d59cbedfe648d2d80aecbe358ea05bb1f5600 (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
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
/**
 * extlinux.c
 *
 *
 */
#include "ext2_fs.h"
#include "cache.h"
#include "types.h"

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

/* 
 * File structure, This holds the information for each currently open file 
 */
struct open_file_t {
        __u32 file_bytesleft;  /* Number of bytes left ( 0 = free ) */
        __u32 file_sector;     /* Next linear sector to read */
        __u32 file_in_sec;     /* Sector where inode lives */
        __u16 file_in_off;
        __u16 file_mode;
};

/*
  struct getc_file {
  __u16 gc_file;
  __u16 gc_bufbytes;
  __u16 gc_bufdata; 
  __u8  gc_unget_cnt;
  __u8  gc_unget_buf[9];  
  };
*/
struct getc_file {
        struct open_file_t * gc_file;             /* file pointer */
        __u16 gc_bufbytes;         /* Bytes left in buffer */
        __u16 gc_bufdata;          /* pointer to data in buffer */
        __u8  gc_unget_cnt;        /* Character pushed back count */
        __u8  gc_unget_buf[7];     /* Character pushed back buffer */
};




#define FILENAME_MAX_LG2 8
#define FILENAME_MAX_    ( 1 << FILENAME_MAX_LG2 )

#define LDLINUX_MAGIC    0x3eb202fex

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

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


#define MAX_SYMLINKS     64
#define SYMLINK_SECTORS  2

#define ROOT_DIR_WORD    0x002F
#define CUR_DIR_DWORD    0x0000002F2E




#define MAX_GETC_LG2       4
#define MAX_GETC           ( 1 << MAX_GETC_LG2 )
#define bytes_per_getc_lg2 ( 16 - MAX_GETC_LG2 )
#define bytes_per_getc     ( 1 << bytes_per_getc_lg2 )
#define secs_per_getc      ( bytes_per_getc / SECTOR_SIZE )
#define MAX_UNGET          7

#define getc_file_lg2       4
#define getc_file_size     ( 1 << getc_file_lg2 )

char GetCStack[getc_file_size * MAX_GETC] = {0,};
__u32 CurrentGetC = (__u32) &GetCStack[getc_file_size * MAX_GETC];



extern struct ext2_super_block sb;

struct ext2_inode ThisInode;
struct open_file_t Files[MAX_OPEN];


__u32 ClustBytesShift, ClustSize, ClustShift;
__u32 SecPerClust, ClustMask;
__u32 PtrsPerBlock1, PtrsPerBlock2;

/* a static value, which is the boot/extlinux's inode number */
__u32 CurrentDir = 13;





/**
 * strecpy:
 *
 * just like the function strcpy(), except it returns non-zero if overflow.
 *
 * well, in Syslinux, strcpy() will advance both the dst and src string pointer.
 * 
 */
int strecpy(char *dst, char *src, char *end)
{
        while ( *src != '\0' )
                *dst++ = *src++;
        *dst = '\0';
       
        if ( dst > end )
                return 1;
        else 
                return 0;
}






/*
 * NOTE! unlike strncmp, ext2_match_entry returns 1 for success, 0 for failure.
 *
 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
 */
static inline int ext2_match_entry (const char * const name,
					struct ext2_dir_entry * de)
{
	if (!de->d_inode)
		return 0;
	return !strncmp(name, de->d_name, de->d_name_len);
}


/*
 * p is at least 6 bytes before the end of page
 */
inline struct ext2_dir_entry *ext2_next_entry(struct ext2_dir_entry *p)
{
        return (struct ext2_dir_entry *)((char*)p + p->d_rec_len);
}



/**
 * 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_bytesleft == 0 ) /* find it */
                        return file;
                file ++;
        }
}


/**
 * getlinsec_ext:
 *
 * same as getlinsec, except load any sector from the zero
 * block as all zeros; use to load any data derived from
 * n ext2 block pointer, i.e. anything *except the superblock
 *
 */
void getlinsec_ext(char *buf, int sector, int sector_cnt)
{
        int ext_cnt = 0;
        
        if ( sector < SecPerClust ) {
                ext_cnt = SecPerClust - sector;
                memset(buf, 0, ext_cnt << SECTOR_SHIFT);
                buf += ext_cnt << SECTOR_SHIFT;
        }

        sector += ext_cnt;
        sector_cnt -= ext_cnt;
        getlinsec(buf, sector, sector_cnt);


}

void getonesec_ext(char *buf, int sector)
{
        getlinsec_ext(buf, sector, 1);
}



/**
 * open_inode:
 *
 * open a file indicated by an inode number in INR
 *
 * @param : inr, the inode number
 * @return: a open_file_t structure pointer
 *          file length in bytes
 *          the first 128 bytes of the inode
 *
 */
struct open_file_t * open_inode(unsigned int inr, __u32 *file_len)
{
        struct open_file_t *file;
        struct ext2_group_desc *desc;
        struct cache_struct *cs;

        __u32 inode_group, inode_offset;
        __u32 block_num, block_off;
        __u32 sector_num, sector_off;
        

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

        file->file_sector = 0;
        
        inr --;
        inode_group  = inr / sb.s_inodes_per_group;
        inode_offset = inr % sb.s_inodes_per_group;

        /* get bytes offset in desc table */
        inode_group <<= ext2_group_desc_lg2size;
        block_num = inode_group / ClustSize;
        block_off = inode_group % ClustSize;
        /* finally we get the real block number where the desc stores */
        block_num += sb.s_first_data_block + 1;
        
        /* compute the sector number where the group descriptor stores */
        sector_num = (block_num << ClustShift) + (block_off >> SECTOR_SHIFT);
        sector_off = block_off & (SECTOR_SIZE - 1 );

        cs = get_cache_sector(sector_num);
        desc = (struct ext2_group_desc *)(cs->data + sector_off); /* got the group desc */
        

        /* get the inode bytes offset in the inode table */
        inode_offset *= sb.s_inode_size;
        block_num = (inode_offset / ClustSize) + (desc->bg_inode_table);
        block_off = inode_offset % ClustSize;

        /* compute the sector number where the inode structure stores */
        sector_num = (block_num << ClustShift) + (block_off >> SECTOR_SHIFT);
        sector_off = block_off & (SECTOR_SIZE - 1);

        file->file_in_sec = sector_num;
        file->file_in_off = sector_off;
        
        cs = get_cache_sector(sector_num);
        memcpy(&ThisInode, cs->data + sector_off, EXT2_GOOD_OLD_INODE_SIZE);
        
        file->file_mode = ThisInode.i_mode;
        *file_len = file->file_bytesleft = ThisInode.i_size;
        
        if ( *file_len == 0 )
                return NULL;

        return file;
}


/**
 * close_file:
 *
 * Deallocates a file structure point by FILE
 *
 * @param: file, the file structure we want deallocate
 *
 */
void close_file(struct open_file_t *file)
{
        if ( file == NULL )
                return;

        file->file_bytesleft = 0;
        
}



/**
 * linsector:
 *
 * Convert a linear sector index in a file to linear sector number
 *
 * @param: line_sector, the linear sector number
 * @param: file, the pointer to open_file_t structure
 *
 * @return: the linear sector number, aka, the physic sector number
 */
__u32 linsector(__u32 lin_sector, struct open_file_t *file)
{


        struct cache_struct *cs;
        struct ext2_inode *inode;
        
        __u32 sector;
        __u32 the_block;        /* the block we are in now */
        __u32 next_block;
        __u32 block = lin_sector >> ClustShift;
        __u32 offset;          /* the offset of sector where block number stores */
        
        cs = get_cache_sector(file->file_in_sec);
        inode = (struct ext2_inode *)((__u32)cs->data + file->file_in_off);

        offset = (char *)&inode->i_block[block] - (char *)cs->data;
        if ( block < EXT2_NDIR_BLOCKS )
                goto direct;

        offset = (char *)&inode->i_block[EXT2_IND_BLOCK] - (char *)cs->data;
        block -= EXT2_NDIR_BLOCKS;
        if ( block <  PtrsPerBlock1 )
                goto ind1;

        offset = (char *)&inode->i_block[EXT2_DIND_BLOCK] - (char *)cs->data;
        block -= PtrsPerBlock1;
        if ( block < PtrsPerBlock2 )
                goto ind2;

        offset = (char *)&inode->i_block[EXT2_TIND_BLOCK] - (char *)cs->data;
        block -= PtrsPerBlock2;

 ind3:
        the_block = block / PtrsPerBlock2;
        next_block = block % PtrsPerBlock2;
        
        sector = (*(__u32 *)(cs->data + offset) << ClustShift)
                +(the_block >> (SECTOR_SHIFT - 2));
        cs = get_cache_sector(sector);
    
        offset = ( the_block & (SECTOR_SIZE >> 2) - 1 ) << 2;
        block = next_block;

 ind2:
        the_block = block / PtrsPerBlock1;
        next_block = block % PtrsPerBlock1;
        
        sector = (*(__u32*)(cs->data + offset) << ClustShift)
                +(the_block >> (SECTOR_SHIFT - 2));
        cs = get_cache_sector(sector);
        offset = ( the_block & (SECTOR_SIZE >> 2) - 1) << 2;
        block = next_block;

 ind1:
        sector = (*(__u32*)(cs->data + offset) << ClustShift)
                +(block >> (SECTOR_SHIFT - 2));
        cs = get_cache_sector(sector);
        offset = ( block & (SECTOR_SIZE >> 2) - 1) << 2;

 direct:
        sector = (*(__u32*)(cs->data + offset) << ClustShift) 
                +(lin_sector & ClustMask);
        
        return sector;
}  
        
       



/**
 * getfssec:
 *
 * Get multiple sectors from a file 
 *
 * same as above, execpt si is a pointer to a open_file_t
 *
 * @param: ES:BX ----> buffer
 * @param: DS:SI ----> pointer to open_file_t
 * @param: cx    ----> sector count ( 0xffff = until end of file )
 * 
 * @return: ecx = number of bytes read
 *
 */
int getfssec(char *buf, struct open_file_t *file, int sectors, int *have_more)
{
        int sector_left, next_sector, sector_idx;
        int frag_start, con_sec_cnt;
        int bytes_read = sectors << SECTOR_SHIFT;


        if ( file->file_bytesleft <= 0)
                return 0;

        sector_left = (file->file_bytesleft + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
        if ( sectors > sector_left )
                sectors = sector_left;
        
 getfragment:
        sector_idx  = file->file_sector;
        next_sector = frag_start = linsector(sector_idx, file);
        con_sec_cnt = 0;
        
 getseccnt:
        do {
                con_sec_cnt ++;
                sectors --;
                if ( sectors == 0 )
                        break;
                
                /***
                 * well, in Syslinux that means in real mode, there's a 
                 * 64k block limit, so we should need handle it here.
                 * 
                 * But it's a user program that test the driver, so we 
                 * don't need to do this here but should remember do it
                 * when we move into Sylinux.
                 */
                sector_idx ++;
                next_sector ++;
        }while( next_sector == linsector(sector_idx, file) );
        
        
 do_read:
        /* Debug message */
#if 0
        printf("/**********************************************************\n");
        printf(" the file you are reading stores at sector --0x%x--0x%x\n", 
               frag_start, frag_start + con_sec_cnt -1);
        printf("**********************************************************/\n");
#endif

        getlinsec_ext(buf, frag_start, con_sec_cnt);
        buf += con_sec_cnt << 9;
        file->file_sector += con_sec_cnt;  /* next sector index */
        
        if ( sectors )
                goto getfragment;
 done:
        if ( bytes_read >= file->file_bytesleft ) {
                bytes_read = file->file_bytesleft;
                close_file(file);
                *have_more = 0;
                return bytes_read;
        }
        
        file->file_bytesleft -= bytes_read;
        *have_more = 1;

        return bytes_read;        
}






/**
 * searchdir:
 *
 * Search the root directory for a pre-mangle filename in FILENAME.
 *
 * @param: filename, the filename we want to search.
 *
 * @out  : a file pointer
 * @out  : file lenght in bytes
 *
 */
struct open_file_t* searchdir(char * filename, __u32 *file_len)
{
        struct open_file_t *file;
        struct ext2_dir_entry *de;
        __u8 file_mode;
        __u8 SymlinkCtr = MAX_SYMLINKS;
        
        
        __u32 have_more;
        __u32 inr = CurrentDir;
        __u32 ThisDir;
        __u32 EndBlock;
        
        int  flag;
        int  trackbufsize = 8192;
        char trackbuf[trackbufsize];
        
        char SymlinkBuf[SYMLINK_SECTORS * SECTOR_SIZE + 64];
        char *SymlinkTmpBuf = trackbuf;
        char *lnk_end;
        char *SymlinkTmpBufEnd = trackbuf + SYMLINK_SECTORS * SECTOR_SIZE+64;

        

 begin_path:
        while ( *filename == '/' ) { /* Absolute filename */
                inr = EXT2_ROOT_INO;
                filename ++;
        }
 open:
        if ( (file = open_inode(inr, file_len) ) == NULL )
                goto done;           /* if error, done */
        
        file_mode = file->file_mode >> S_IFSHIFT;

        
        /* It's a file */
        if ( file_mode == T_IFREG ) {
                if ( *filename == '\0' )
                        goto done;
                else
                        goto err;
        } 
                
        
        /* It's a directory */
        if ( file_mode == T_IFDIR ) {
                
                ThisDir = inr;
                
                if ( *filename == 0 )
                        goto err;
                while ( *filename == '/' )
                        filename ++;
        
                EndBlock = (__u32 )trackbuf + (SecPerClust << SECTOR_SHIFT);
        readdir:                
                /* read a clust at a time */
                getfssec(trackbuf, file, SecPerClust, &have_more);
                

                de = (struct ext2_dir_entry *)trackbuf;
                
        getent: 
                while ( 1 ) {
                        if ( (char *)de >= (char *)EndBlock ) {
                                if (have_more)
                                        goto readdir;
                                else
                                        goto err;
                        }
                        
                        /* Zero inode == void entry */
                        if ( de->d_inode == 0 ) {
                                de = ext2_next_entry(de);       
                                continue;
                        }
                        
                        if ( ext2_match_entry (filename, de) ) {
                                inr = de->d_inode;
                                filename += de->d_name_len;
                                if ( *filename == 0 )
                                        goto finish;
                                if ( *filename != '/' ) {       
                                        /* not match, try next */
                                        de = ext2_next_entry(de);
                                        filename -= de->d_name_len;
                                        continue;
                                }
                        finish:
                                close_file(file);
                                goto open;
                        }
                        
                        de = ext2_next_entry(de);
                } 
        }

        
        /*
         * It's a symlink.  We have to determine if it's a fast symlink
         * (data stored in the inode) or not (data stored as a regular
         * file.)  Either which way, we start from the directory
         * which we just visited if relative, or from the root directory
         * if absolute, and append any remaining part of the path.
         */
        if ( file_mode == T_IFLNK ) {
                if ( --SymlinkCtr == 0 ) /* too many links */
                        goto err;
                if ( *file_len >= SYMLINK_SECTORS * SECTOR_SIZE )
                        goto err;        /* Symlink too long */

                flag = ThisInode.i_file_acl ? SecPerClust : 0;

                if ( ThisInode.i_blocks == flag ) { /* fast symlink */

                        close_file(file);          /* we've got all we need */
                        memcpy(SymlinkTmpBuf, ThisInode.i_block, *file_len);
                        lnk_end = SymlinkTmpBuf + *file_len;

                } else {                           /* slow symlink */
                        getfssec(SymlinkTmpBuf, file, SYMLINK_SECTORS, &have_more);
                        lnk_end = SymlinkTmpBuf + *file_len;
                        
                }
        symlink_finish:
                /*
                 * well, this happens like:
                 * "/boot/xxx/y/z.abc" where xxx is a symlink to "/other/here"
                 * so, we should get a new name like:
                 * "/other/here/y/z.abc"
                 */
                if ( *filename != 0 )
                        *lnk_end++ = '/';
        no_slash:      
                if ( strecpy(lnk_end, filename, SymlinkTmpBufEnd) ) 
                        goto err_noclose;  /* buffer overflow */
                
                /*
                 * now copy it to the "real" buffer; we need to have
                 * two buffers so we avoid overwriting the tail on 
                 * the next copy.
                 */
                strcpy(SymlinkBuf, SymlinkTmpBuf);
                inr = ThisDir;
                filename = SymlinkBuf;
                goto begin_path;    /* we got a new path, so search it again */
        }
        
        /* Otherwise, something bad ... */
 err:
        close_file(file);
 err_noclose:
        *file_len = 0;
        file = NULL;
 done:        
        return file;
}







/**
 * open:
 * 
 * open the file
 *
 * @param: filename
 *
 * @return: return 1 one successful, or 0
 *
 */
struct open_file_t* ext2_open(char *filename)
{
        struct open_file_t *file;
        struct getc_file *gc_file;
        int file_len;
        
        file = searchdir(filename, &file_len);
        if ( !file )
                return NULL;

        CurrentGetC -= getc_file_size;
        if ( (char *)CurrentGetC < GetCStack ) { /* Stack full */
                close_file(file);
                return NULL;
        }

        gc_file  = (struct getc_file *) CurrentGetC;
        gc_file->gc_file = file;
        gc_file->gc_bufbytes = 0;
        gc_file->gc_unget_cnt = 0;
                                               
        return file;
}


/**
 * read function:
 * return the bytes read
 */
int ext2_read(struct open_file_t *file, char *buf, int size, int *have_more)
{
        
        int sector = (size + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
        return getfssec(buf, file, sector, have_more);
        
}

/**
 * init. the fs meta data
 */
void init_fs(struct ext2_super_block *sb)
{
        
        /* read the super block */
        getlinsec(sb, 2, 2);

        ClustBytesShift = sb->s_log_block_size + 10;
        ClustSize = 1 << ClustBytesShift;
        ClustShift = ClustBytesShift - SECTOR_SHIFT;

        SecPerClust = ClustSize >> SECTOR_SHIFT;
        ClustMask = SecPerClust - 1;
        
        PtrsPerBlock1 = 1 << (ClustBytesShift - 2 );
        PtrsPerBlock2 = 1 << ( (ClustBytesShift - 2) * 2);
}