summaryrefslogtreecommitdiff
path: root/core/iso9660.c
blob: 1ce710f7be71acaf71500e389b0ac46c0be04075 (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
#include <stdio.h>
#include <string.h>
//#include "cache.h"
#include "core.h"
#include "disk.h"
#include "iso9660_fs.h"
#include "fs.h"

#define DEBUG 1

#define FILENAME_MAX_LG2 8
#define FILENAME_MAX     (1 << FILENAME_MAX_LG2)
#define MAX_OPEN_LG2     6
#define MAX_OPEN         (1 << MAX_OPEN_LG2)
#define ISO_SECTOR_SHIFT 11
#define ISO_SECTOR_SIZE  (1 << ISO_SECTOR_SHIFT)
#define ROOT_DIR_WORD    0x002f
#define TRACKBUF_SIZE    8192


struct open_file_t {
        sector_t file_sector;
        uint32_t file_bytesleft;
        uint32_t file_left;
};
extern char Files[];

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


extern char trackbuf[TRACKBUF_SIZE];
uint16_t BufSafe = TRACKBUF_SIZE >> ISO_SECTOR_SHIFT;
uint16_t BufSafeBytes = TRACKBUF_SIZE;

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

/* 
 * use to store the block shift, since we treat the hd-mode as 512 bytes
 * sector size, 2048 bytes block size. we still treat the cdrom as 2048  
 * bytes sector size and also the block size.
 */
int block_shift;

/**
 * allocate_file:
 *
 * allocate a file structure
 *
 */
struct open_file_t *allocate_file()
{
    struct open_file_t *file;
    int i = 0;
    
    file = (struct open_file_t *)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;
}

void getlinsec_cdrom(char *buf, sector_t sector_num, int sectors)
{
    com32sys_t regs;
    //static __lowmem char low_buf[65536]; 
    /* for safe, we use buf + (sectors << SECTOR_SHIFT) here */
    int high_addr = (buf + (sectors << ISO_SECTOR_SHIFT)) > (char *)0x100000;
        
    memset(&regs, 0, sizeof regs);
    regs.eax.l = sector_num;
    regs.ebp.l = sectors;
    
    if (high_addr) {
        regs.es = SEG(core_xfer_buf);
        regs.ebx.w[0] = OFFS(core_xfer_buf);
    } else {
        regs.es = SEG(buf);
        regs.ebx.w[0] = OFFS(buf);
    }

    call16(getlinsec, &regs, NULL);

    if (high_addr)
        memcpy(buf, core_xfer_buf, sectors << ISO_SECTOR_SHIFT);
}




/**
 * 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 iso_mangle_name(char *dst, char *src)
{
    char *p = dst;
    int i = FILENAME_MAX - 1;
    
    while ( *src > ' ' ) {
        if ( *src == '/' ) {
            if ( *(src+1) == '/' ) {
                i --;
                src ++;
                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, int *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;
}

static inline int cdrom_read_sectors(struct disk *disk, void *buf, int block, int count)
{
    /* changed those to _sector_ */
    block <<= block_shift;
    count <<= block_shift;
    return disk->rdwr_sectors(disk, buf, block, count, 0);
}

/**
 * iso_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
 *
 */
uint32_t iso_getfssec(struct fs_info *fs, char *buf, 
                      void *open_file, int sectors, int *have_more)
{
    uint32_t bytes_read = sectors << ISO_SECTOR_SHIFT;
    struct open_file_t *file = (struct open_file_t *)open_file;
    struct disk *disk = fs->fs_dev->disk;
    
    if ( sectors > file->file_left )
        sectors = file->file_left;
    
    cdrom_read_sectors(disk, buf, file->file_sector, sectors);
    
    file->file_sector += sectors;
    file->file_left   -= sectors;
    
    if ( bytes_read >= file->file_bytesleft ) {
        bytes_read = file->file_bytesleft;
        *have_more = 0;
    } else
        *have_more = 1;
    file->file_bytesleft -= bytes_read;
    
    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 fs_info *fs, struct dir_t *dir, 
                  char *name, uint32_t *file_len, void **res)
{
    struct open_file_t *file;
    struct iso_dir_entry *de;
    struct iso_dir_entry tmpde;
    
    uint32_t offset = 0;  /* let's start it with the start */
    uint32_t 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;
    
    iso_getfssec(fs, 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 + TRACKBUF_SIZE) ) {
            if ( !have_more ) 
                return 0;
            
            iso_getfssec(fs, 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+ISO_SECTOR_SIZE) & ~(ISO_SECTOR_SIZE-1);
            continue;
        }
        
        
        offset += de_len;
        
        /* Make sure we have a full directory entry */
        if ( offset >= TRACKBUF_SIZE ) {
            int slop = TRACKBUF_SIZE - offset + de_len;
            memcpy(&tmpde, de, slop);
            offset &= TRACKBUF_SIZE - 1;
            file->file_sector ++;
            if ( offset ) {
                if ( !have_more ) 
                    return 0;
                iso_getfssec(fs, 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 = *(uint32_t *)de->extent;
        dir->dir_len = *(uint32_t *)de->size;
        dir->dir_clust = (dir->dir_len + ISO_SECTOR_SIZE - 1) >> ISO_SECTOR_SHIFT;
        
        *file_len = dir->dir_len;
        *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    = *(uint32_t *)de->extent;
        file->file_bytesleft = *(uint32_t *)de->size;
        file->file_left = (file->file_bytesleft + ISO_SECTOR_SIZE - 1) >> ISO_SECTOR_SHIFT;

        *file_len = file->file_bytesleft;
        *res = file;
        
        /* Mark we got a file */
        return 2;
    }    
}


/**
 * iso_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)
 *
 */
void iso_searchdir(char *filename, struct file *file)
{
    struct open_file_t *open_file = NULL;
    struct dir_t *dir;
    uint32_t file_len;
    int ret;
    void *res;
        
    dir = &CurrentDir;
    if ( *filename == '/' ) {
        dir = &RootDir;
        filename ++;
    }

    while ( *filename ) {
        ret = do_search_dir(file->fs, dir, filename, &file_len, &res);
        if ( ret == 1 )
            dir = (struct dir_t *)res;
        else if ( ret == 2 )
            break;
        else 
            goto err;
       
        /* 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 ) {
        open_file = (struct open_file_t *)res;
        goto found;
    } else {
        open_file = allocate_file();
        if ( !open_file )
            goto err;
        
        open_file->file_sector = dir->dir_lba;
        open_file->file_bytesleft = dir->dir_len;
        open_file->file_left = (dir->dir_len + ISO_SECTOR_SIZE - 1) >> ISO_SECTOR_SHIFT;
        goto found;
    }
 err:
    close_file(open_file);
    file_len = 0;
    open_file = NULL;
 
 found:
    file->file_len = file_len;
    file->open_file = (void*)open_file;

#if 0
    if (open_file) {
        printf("file bytesleft: %d\n", open_file->file_bytesleft);
        printf("file sector   : %d\n", open_file->file_sector);
        printf("file in sector: %d\n", open_file->file_in_sec);
        printf("file offsector: %d\n", open_file->file_in_off);
    }
#endif
}

void iso_load_config(com32sys_t *regs)
{
    char *config_name = "isolinux.cfg";
    com32sys_t out_regs;
    
    strcpy(ConfigName, config_name);
    
    regs->edi.w[0] = OFFS_WRT(ConfigName, 0);
    memset(&out_regs, 0, sizeof out_regs);
    call16(core_open, regs, &out_regs);
}


int iso_fs_init(struct fs_info *fs)
{
    char *iso_dir;
    char *boot_dir  = "/boot/isolinux";
    char *isolinux_dir = "/isolinux";
    int len;
    int bi_pvd = 16;   
    struct file file;
    struct open_file_t *open_file;
    struct disk *disk = fs->fs_dev->disk;
    
    block_shift = ISO_SECTOR_SHIFT - disk->sector_shift;
    cdrom_read_sectors(disk, trackbuf, bi_pvd, 1);
    CurrentDir.dir_lba = RootDir.dir_lba = *(uint32_t *)(trackbuf + 156 + 2);
    
#ifdef DEBUG
    printf("Root directory at LBA = 0x%x\n", RootDir.dir_lba);
#endif
    
    CurrentDir.dir_len = RootDir.dir_len = *(uint32_t*)(trackbuf + 156 + 10);
    CurrentDir.dir_clust = RootDir.dir_clust = (RootDir.dir_len + ISO_SECTOR_SIZE - 1) >> ISO_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
     */
    *(uint16_t *)CurrentDirName = ROOT_DIR_WORD;
    
    iso_dir = boot_dir;
    file.fs = fs;
    iso_searchdir(boot_dir, &file);         /* search for /boot/isolinux */
    if ( !file.file_len ) {
        iso_dir = isolinux_dir;
        iso_searchdir(isolinux_dir, &file); /* search for /isolinux */
        if ( !file.file_len ) {
            printf("No isolinux directory found!\n");
            return 0;
        }            
    }
    
    strcpy(CurrentDirName, iso_dir);
    len = strlen(CurrentDirName);
    CurrentDirName[len]    = '/';
    CurrentDirName[len+1]  = '\0';
    
    open_file = (struct open_file_t *)file.open_file;
    CurrentDir.dir_len    = open_file->file_bytesleft;
    CurrentDir.dir_clust  = open_file->file_left;
    CurrentDir.dir_lba    = open_file->file_sector;
    close_file(open_file);
    
#ifdef DEBUG
    printf("isolinux directory at LBA = 0x%x\n", CurrentDir.dir_lba);
#endif  
      
    /* we do not use cache for now, so we can just return 0 */
    return 0;
}


const struct fs_ops iso_fs_ops = {
    .fs_name       = "iso",
    .fs_init       = iso_fs_init,
    .searchdir     = iso_searchdir,
    .getfssec      = iso_getfssec,
    .mangle_name   = iso_mangle_name,
    .unmangle_name = NULL,
    .load_config   = iso_load_config
};