aboutsummaryrefslogtreecommitdiffstats
path: root/isolinux.c
blob: 7228645e25230db246636b4cc2b471b221199b22 (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
#include "iso_fs.h"


/*
 * I also find that we don't use cache here any more 
 */
//#include "cache.h"

/*
 * well, we defined the getlinsec() function here 
 * Since the sector size of iso is different from 
 * the disk 
 */
//#include "disklab.h"

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


#define DEBUG 1


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

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

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

#define ROOT_DIR_WORD    0x002f




struct open_file_t {
        __u32 file_sector;
        __u32 file_bytesleft;
        __u32 file_left;
        __u32 pad;
};


struct dir_t {
        __u32 dir_lba;        /* Directory start (LBA) */
        __u32 dir_len;        /* Length in bytes */
        __u32 dir_clust;      /* Length in clusters */
};



struct open_file_t Files[MAX_OPEN];

struct dir_t RootDir;
struct dir_t CurrentDir;


#define trackbufsize 8192
char trackbuf[trackbufsize] = {0,};
__u16 BufSafe = trackbufsize >> SECTOR_SHIFT;
__u16 BufSafeBytes = trackbufsize;

char ISOFileName[64];      /* ISO filename canonicalizatin buffer */
char *ISOFileNameEnd = &ISOFileName[64];

__u32 FirstSecSum;         /* checksum of bytes 64-2048 */
__u32 ImageDwords;         /* isolinux.bin size, dwords */
__u32 InitStack;           /* Initial stack pointer (SS:SP) */
__u16 DiskSys;             /* Last INT 13h call */
__u16 ImageSectors;        /* isolinux.bin size, sectors */

/* These following two are accessed as a single dword ... */
__u16 GetlinsecPtr;        /* the sector-read pointer */
__u16 BIOSType;
__u8  DiskError;           /* Error code for disk I/O */
__u8  DriveNumber;         /* CD-ROM BIOS drive number */
__u8  ISOFlags;            /* Flags for ISO directory search */
__u8  RetryCount;          /* Used for ISO directory search */
__u16 bsSecPerTrack;       /* Used in hybrid mode */
__u16 bsHeads;             /* Used in hybrid mode */


/* El Torito spec packet */
__u8  spec_packet;	   /*  Size of packet */
__u8  sp_media;	 	   /*  Media type */
__u8  sp_drive;		   /*  Drive number */
__u8  sp_controller;	   /*  Controller index */
__u32 sp_lba;		   /*  LBA for emulated disk image */
__u32 sp_devspec;	   /*  IDE/SCSI information */
__u16 sp_buffer;	   /*  User-provided buffer */
__u16 sp_loadseg;	   /*  Load segment */
__u16 sp_sectors;	   /*  Sector count */
__u8  sp_chs;		   /*  Simulated CHS geometry */
__u8  sp_dummy;		   /*  Scratch, safe to overwrite */





/**
 * allocate_file:
 *
 * allocate a file structure
 *
 */
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 */
}
  



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

extern int fd;
void getlinsec(char *buf, int sector, int sector_cnt)
{
        int bytes_read;

#if 0           /* Debug message */
                printf("/**************************************************\n");
                printf("You are reading stores at sector --0x%x--0x%x\n", 
                       sector, sector + sector_cnt -1);
                printf("**************************************************/\n");
#endif

        if ( lseek(fd, sector*SECTOR_SIZE, SEEK_SET) < 0 ) {
                printf("seek file iso.img error ....\n");
                return;
        }

        if ( (bytes_read = read(fd, buf, SECTOR_SIZE*sector_cnt)) < SECTOR_SIZE*sector_cnt) 
                printf("%s:%s: read %d bytes less than %d bytes..\n", 
                       __FILE__, __FUNCTION__, bytes_read, SECTOR_SIZE * sector_cnt);
        
}




/**
 * mangle_name:
 *
 * Mangle a filename pointed to by src into a buffer pointed
 * to by dst; ends on encountering any whitespace.
 * dst is preserved.
 *
 * This verifies that a filename is < FilENAME_MAX characters, 
 * doesn't contain whitespace, zero-pads the output buffer,
 * and removes trailing dots and redumndant slashes, so "repe
 * cmpsb" can do a compare, and the path-searching routine gets
 * a bit of an easier job.
 *
 */
void mangle_name(char *dst, char *src)
{

        char slash_before;
        char *p = dst;

        int i = FILENAME_MAX - 1;

        while ( *src > ' ' ) {
                if ( *src == '/' ) {
                        while ( *src == '/' ) {
                                i --;
                                src ++;
                        }
                        *dst++ = '/';
                        continue;
                }

                *dst++ = *src ++;
                i --;
        }
        

        while ( 1 ) {
                if ( dst >= p )
                        break;
                
                if ( (*(dst-1) != '.') && (*(dst-1) != '/') ) 
                        break;

                dst --;
                i ++;
        }

        i ++;
        for (; i > 0; i -- )
                *dst++ = '\0';

}
        


/**
 * compare the names si and di and report if they are
 * equal from an ISO 9600 perspective. 
 *
 * @param: de_name, the name from the file system.
 * @param: len, the length of de_name, and will return the real name of the de_name
 *              ';' and other terminates excluded.
 * @param: file_name, the name we want to check, is expected to end with a null
 *
 * @return: 1 on match, or 0.
 *
 */
int iso_compare_names(char *de_name, __u32 *len, char *file_name)
{
        
        
        char *p  = ISOFileName;
        char c1, c2;
        
        int i = 0;

        while ( (i < *len) && *de_name && (*de_name != ';') && (p < ISOFileNameEnd - 1) ) {
                *p++ = *de_name++;
                i++;
        }

        

        /* Remove terminal dots */
        while ( *(p-1) == '.' ) {
                if ( *len <= 2 )
                        break;
                
                if ( p <= ISOFileName )
                        break;
                p --;
                i--;
        }

        if ( i <= 0 )
                return 0;

        *p = '\0';
        
        /* return the 'real' length of de_name */
        *len = i;
        
        p = ISOFileName;

        /* i is the 'real' name length of file_name */
        while ( i ) {
                c1 = *p++;
                c2 = *file_name++;
                
                if ( (c1 == 0) && (c2 == 0) )
                        return 1; /* success */
                
                else if ( (c1 == 0) || ( c2 == 0 ) )
                        return 0;

                c1 |= 0x20;
                c2 |= 0x20;          /* convert to lower case */
                if ( c1 != c2 )
                        return 0;
                i --;
        }

        return 1;
}
                

/**
 * getfssec:
 *
 * Get multiple clusters from a file, given the file pointer.
 *
 * @param: buf
 * @param: file, the address of the open file structure
 * @param: sectors, how many we want to read at once 
 * @param: have_more, to indicate if we have reach the end of the file
 *
 */
__u32 getfssec(char *buf, struct open_file_t *file, __u32 sectors, int *have_more)
{
        __u32 bytes_read = sectors << SECTOR_SHIFT;

        if ( sectors > file->file_left )
                sectors = file->file_left;
                        
        getlinsec(buf, file->file_sector, sectors);
        
        file->file_sector += sectors;
        file->file_left   -= sectors;
        
        if ( bytes_read >= file->file_bytesleft ) {
                bytes_read = file->file_bytesleft;
                file->file_bytesleft = 0;
                *have_more = 0;
                close_file(file);
        } else {
                file->file_bytesleft -= bytes_read;
                *have_more = 1;
        }

        return bytes_read;
}





/**
 * do_search_dir:
 *
 * find a file or directory with name within the _dir_ directory.
 * 
 * the return value will tell us what we find, it's a file or dir?
 * on 1 be dir, 2 be file, 0 be error.
 *
 * res will return the result.
 *
 */
int do_search_dir(struct dir_t *dir, char *name, void **res)
{
        struct open_file_t *file;
        struct iso_dir_entry *de;
        struct iso_dir_entry *tmpde;
        

        __u32 offset = 0;  /* let's start it with the start */
        __u32 file_pos = 0;
        
        char *de_name;
        int de_len;
        int de_name_len;
        int have_more;

        file = allocate_file();
        if ( !file )
                return 0;
        
        file->file_left = dir->dir_clust;
        file->file_sector = dir->dir_lba;

        getfssec(trackbuf, file, BufSafe, &have_more);
        de = (struct iso_dir_entry *)trackbuf;

        while ( file_pos < dir->dir_len ) {
                int found = 0;

                if ( (char *)de >= (char *)(trackbuf + trackbufsize) ) {
                        if ( !have_more ) 
                                return 0;
                        
                        getfssec(trackbuf, file, BufSafe, &have_more);
                        offset = 0;
                }
                
                de = (struct iso_dir_entry *) (trackbuf + offset);
                
                de_len = de->length;
                
                if ( de_len == 0) {
                        offset = file_pos = (file_pos+SECTOR_SIZE) & ~(SECTOR_SIZE-1);
                        continue;
                }
                        
                        
                offset += de_len;

                /* Make sure we have a full directory entry */
                if ( offset >= trackbufsize ) {
                        int slop = trackbufsize - offset + de_len;
                        memcpy(tmpde, de, slop);
                        offset &= trackbufsize - 1;
                        file->file_sector ++;
                        if ( offset ) {
                                if ( !have_more ) 
                                        return 0;
                                getfssec(trackbuf, file, BufSafe, &have_more);
                                memcpy((void*)tmpde + slop, trackbuf, offset);
                        }
                        de = tmpde;
                }

                if ( de_len < 33 ) {
                        printf("Corrutped directory entry in sector %d\n", file->file_sector);
                        return 0;
                }

                de_name_len = de->name_len;
                de_name = (char *)((void *)de + 0x21);

                
                if ( (de_name_len == 1) && (*de_name == 0) ) {
                        found = iso_compare_names(".", &de_name_len, name);
                        
                } else if ( (de_name_len == 1) && (*de_name == 1) ) {
                        de_name_len == 2;
                        found = iso_compare_names("..", &de_name_len, name);
                        
                } else 
                        found = iso_compare_names(de_name, &de_name_len, name);
                        
                if (found)
                        break;

                file_pos += de_len;
        }

        if ( file_pos >= dir->dir_len ) 
                return 0; /* not found */


        if ( *(name+de_name_len) && (*(name+de_name_len) != '/' ) ) {
                printf("Something wrong happened during searching file %s\n", name);

                *res = NULL;
                return 0;
        }
        
        if ( de->flags & 0x02 ) {
                /* it's a directory */
                
                dir = &CurrentDir;
                
                dir->dir_lba = *(__u32 *)de->extent;
                dir->dir_len = *(__u32 *)de->size;
                dir->dir_clust = (dir->dir_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;

                *res = dir;
                
                /* we can close it now */
                close_file(file); 
                
                /* Mark we got a directory */
                return 1;

        } else {
                /* it's a file */
                file->file_sector    = *(__u32 *)de->extent;
                file->file_bytesleft = *(__u32 *)de->size;
                file->file_left = (file->file_bytesleft + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
                
                *res = file;

                /* Mark we got a file */
                return 2;
        }

}




/**
 * searchdir:
 *
 * open a file
 *
 * searchdir_iso is a special entry point for ISOLINUX only. In addition
 * to the above, searchdir_iso passes a file flag mask in AL. This is 
 * useful for searching for directories.
 *
 * well, it's not like the searchidr function in EXT fs or FAT fs; it also
 * can read a diretory.(Just thought of mine, liu)
 *
 */
struct open_file_t *searchdir(char *filename)
{
        struct open_file_t *file;
        struct dir_t *dir;
        struct iso_dir_entry *de;

        void *res;
        int ret;
                
        
        dir = &CurrentDir;
        if ( *filename == '/' ) {
                dir = &RootDir;
                filename ++;
        }

        while ( *filename ) {
                
                ret = do_search_dir(dir, filename, &res);
                if ( ret == 1 )
                        dir = (struct dir_t *)res;
                else if ( ret == 2 )
                        break;
                else 
                        return NULL;
                
                /* find the end */
                while ( *filename && (*filename != '/') )
                        filename ++;
                
                /* skip the slash */
                while ( *filename && (*filename == '/') )
                        filename++;   
        }

        /* well , we need recheck it , becuase it can be a directory */
        if ( ret == 2 ) 
                return (struct open_file_t *)res;
        else {
                file = allocate_file();
                if ( !file )
                        return NULL;

                file->file_sector = dir->dir_lba;
                file->file_bytesleft = dir->dir_len;
                file->file_left = (dir->dir_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
                return file;               
        }
}




struct open_file_t *open_file(char *filename)
{
        return searchdir(filename);
}


__u32 read_file(struct open_file_t *file, char *buf, int size, int *have_more)
{
        __u32 sectors = ( size + SECTOR_SIZE - 1 ) >> SECTOR_SHIFT;
        
        return getfssec(buf, file, sectors, have_more);
}


        
char *boot_dir  = "/boot/isolinux";
char *isolinux_dir = "/isolinux";

int bi_pvd = 16;

void init_fs()
{
        char CurrentDirName[64];
        char *iso_dir;

        struct open_file_t *file;
        int len;
        
        getlinsec(trackbuf, bi_pvd, 1);

        CurrentDir.dir_lba = RootDir.dir_lba = *(__u32 *)(trackbuf + 156 + 2);

#ifdef DEBUG
        printf("Root directory at LBA = 0x%x\n", RootDir.dir_lba);
#endif

        CurrentDir.dir_len = RootDir.dir_len = *(__u32*)(trackbuf + 156 + 10);
        CurrentDir.dir_clust = RootDir.dir_clust = (RootDir.dir_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
        

        /*
         * Look for an isolinux directory, and if found,
         * make it the current directory instead of the
         * root directory.
         *
         * Also copy the name of the directory to CurrrentDirName
         */
        *(__u16*)CurrentDirName = ROOT_DIR_WORD;
        
        iso_dir = boot_dir;
        file = searchdir(boot_dir);         /* search for /boot/isolinux */
        if ( !file ) {
                iso_dir = isolinux_dir;
                file = searchdir(isolinux_dir); /* search for /isolinux */
                if ( !file )
                        goto no_isolinux_dir;
        }

        strcpy(CurrentDirName, iso_dir);
        len = strlen(CurrentDirName);
        CurrentDirName[len] = '/';
        CurrentDirName[len+1] = '\0';

        CurrentDir.dir_len = file->file_bytesleft;
        CurrentDir.dir_clust = file->file_left;
        CurrentDir.dir_lba = file->file_sector;
        close_file(file);

#ifdef DEBUG
        printf("isolinux directory at LBA = %0x%x\n", CurrentDir.dir_lba);
#endif
        
        return;

 no_isolinux_dir:
        printf("No isolinux directory found\n");
}