diff options
author | Liu Aleaxander <Aleaxander@gmail.com> | 2009-05-24 15:15:07 +0800 |
---|---|---|
committer | Liu Aleaxander <Aleaxander@gmail.com> | 2009-05-24 15:15:07 +0800 |
commit | 60ec7dc442d580970c5e72bb972fb5f770160cf4 (patch) | |
tree | f3d2bf9285343c8444d2726abac65baa5edf8cbe | |
parent | 03fa5dd9baef4e78c011092ffae1b321c9a3e77e (diff) | |
download | devel-60ec7dc442d580970c5e72bb972fb5f770160cf4.tar.gz devel-60ec7dc442d580970c5e72bb972fb5f770160cf4.tar.xz devel-60ec7dc442d580970c5e72bb972fb5f770160cf4.zip |
make isolinux do the work
well, for now, we can read a file from a iso image.
we also create a iso_main.c file for isolinux 'cause
isolinux do not use the cache, and the block size is
2k instead of 512B
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | cache.c | 23 | ||||
-rw-r--r-- | fat_fs.h | 1 | ||||
-rw-r--r-- | iso_fs.h | 10 | ||||
-rw-r--r-- | iso_main.c | 124 | ||||
-rw-r--r-- | isolinux.c | 145 | ||||
-rw-r--r-- | ldlinux.c | 2 | ||||
-rw-r--r-- | main.c | 35 |
8 files changed, 259 insertions, 85 deletions
@@ -7,7 +7,7 @@ extlinux:cache.h disklab.h ext2_fs.h syslinux:cache.h disklab.h fat_fs.h gcc -g main.c cache.c disklab.c ldlinux.c -o syslinux -isolinux:cache.h iso_fs.h - gcc -g main.c cache.c isolinux.c -o isolinux +isolinux:iso_fs.h + gcc -g iso_main.c isolinux.c -o isolinux clean: (rm -f *~ *.o syslinux extlinux isolinux)
\ No newline at end of file @@ -1,4 +1,6 @@ #include "cache.h" +#include "disklab.h" + #include <stdio.h> #include <fcntl.h> #include <malloc.h> @@ -99,3 +101,24 @@ struct cache_struct * get_cache_block(__u32 block) return cs; } + + + + +/** + * Just print the sector, and according the LRU algorithm, + * Left most value is the most least secotr, and Right most + * value is the most Recent sector. I see it's a Left Right Used + * (LRU) algorithm; Just kidding:) + */ +void print_cache(void) +{ + int i = 0; + struct cache_struct *cs = cache; + for (; i < CACHE_ENTRIES; i++) { + cs = cs->next; + printf("%d ", cs->block); + } + + printf("\n"); +} @@ -3,6 +3,7 @@ #include "types.h" +#define ENABLE_CACHE 1 #define FAT_DIR_ENTRY_SIZE 32 @@ -3,12 +3,12 @@ #include "types.h" +/* let's off the cache here */ +#define ENABLE_CACHE 0 - - -struct iso_directory_record { +struct iso_dir_entry { __u8 length; /* 00 */ __u8 ext_attr_length; /* 01 */ __u8 extent[8]; /* 02 */ @@ -18,8 +18,8 @@ struct iso_directory_record { __u8 file_unit_size; /* 1a */ __u8 interleave; /* 1b */ __u8 volume_sequence_number[4]; /* 1c */ - __u8 name__len; /* 20 */ - __u8 name[]; /* 21 */ + __u8 name_len; /* 20 */ + //__u8 name[]; /* 21 */ }; diff --git a/iso_main.c b/iso_main.c new file mode 100644 index 0000000..efd753b --- /dev/null +++ b/iso_main.c @@ -0,0 +1,124 @@ +/** + * well, it's just for isolinux, 'cause the isolinux is kind of + * different, that don't use cache, and the block size is 2k but + * not 512 B + * + * so we make a iso_main for isolinux. + */ + + +/** + * This is just a test program, it does nothing but just + * init the cache first then use get_cache_sector to test + * the LRU algorithm with 12 fake-sector number. + * + * And it work well here + * + */ +#include <stdio.h> +#include <fcntl.h> +#include <sys/stat.h> + + +int fd; + + + +/* test function */ +void print_hex(char *data, int size) +{ + int i = 0; + while ( size --) { + + if ( (i != 0) && (i % 16 == 0) ) + printf("\n"); + i ++; + + if ( (*data < 0x20 ) || (*data > 0x80 ) ) { + printf("."); + data ++; + continue; + } + putc(*data, stdout); + data++; + + } + printf("\n"); +} + + +void usage(void) +{ + printf("USAGE: a.out fs.img filename\n"); + printf("---- fs.img means a filesytem image,it can be EXT, FAT and ISO9660 fs for now\n"); + printf("---- filename menas the file you wanna open, it can be \n"); + printf(" in one of the two following forms:\n"); + printf(" /file/name/xy.y, this is a full name, or \n"); + printf(" file/name/xy.z, in this case, the program will search\n"); + printf(" from the directory where the extlinux.sys stored\n"); + +} + + + + +/** + * well, it's just a test program that test if the fs driver + * would work on ext2/3(ext4 not added for now) filesystem + * well or not. so it's task is simple,too: open the ext2fs, + * then read what you want. + * + */ +int main(int argc, char *argv[]) +{ + + int bytes_read = 0; + int total_bytes = 0; + int have_more; + char *fs = argv[1]; + char *filename = argv[2]; + char buf[2048]; + struct cache_struct *cs; + struct open_file_t *file = NULL; + + + if (argc != 3 ) { + usage(); + return 0; + } + + + fd = open(fs, O_RDONLY); + if ( fd < 0 ) { + printf("File %s open error....\n", fs); + return 0; + } + + init_fs(); + + file = (struct open_file_t *)open_file(filename); + if ( ! file ) { + printf("open file error: file %s not found ....\n", filename); + close(fd); + return 0; + } + + /* + * The following message may be nosiy, but it told we how is it + * going well. + */ + do { + bytes_read = read_file(file, buf, 2048, &have_more); + printf("--------read %d bytes-------\n", bytes_read); + printf("----------------\n"); + print_hex(buf, bytes_read); + + total_bytes += bytes_read; + }while(have_more); + + printf("-------------total read %d bytes------\n", total_bytes); + + close(fd); + + return 0; +} @@ -1,5 +1,10 @@ #include "iso_fs.h" -#include "cache.h" + + +/* + * I also find that we don't use cache here any more + */ +//#include "cache.h" /* * well, we defined the getlinsec() function here @@ -49,17 +54,20 @@ struct dir_t { +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]; -struct dir_t RootDir; -struct dir_t CurrentDir; - __u32 FirstSecSum; /* checksum of bytes 64-2048 */ __u32 ImageDwords; /* isolinux.bin size, dwords */ __u32 InitStack; /* Initial stack pointer (SS:SP) */ @@ -131,13 +139,20 @@ void close_file(struct open_file_t *file) file->file_sector = 0; } - +extern int fd; void getlinsec(char *buf, int sector, int sector_cnt) { int bytes_read; +#if 1 /* 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 ext2.img error ....\n"); + printf("seek file iso.img error ....\n"); return; } @@ -175,7 +190,7 @@ void mangle_name(char *dst, char *src) while ( *src > ' ' ) { if ( *src == '/' ) { while ( *src == '/' ) { - cx --; + i --; src ++; } *dst++ = '/'; @@ -183,7 +198,7 @@ void mangle_name(char *dst, char *src) } *dst++ = *src ++; - cx --; + i --; } @@ -195,11 +210,11 @@ void mangle_name(char *dst, char *src) break; dst --; - cx ++; + i ++; } - cx ++; - for (; cx > 0; cx -- ) + i ++; + for (; i > 0; i -- ) *dst++ = '\0'; } @@ -224,18 +239,19 @@ int iso_compare_names(char *de_name, __u32 len, char *file_name) char *p = ISOFileName; char c1, c2; - while ( len && *de_name && (*de_name != ';') && (p < ISOFileNameEnd - 1) ) { + int i = len; + + while ( i && *de_name && (*de_name != ';') && (p < ISOFileNameEnd - 1) ) { *p++ = *de_name++; - len --; + i --; } - while ( 1 ) { + /* Remove terminal dots */ + while ( *(p-1) == '.' ) { if ( p <= ISOFileName ) break; - - if ( *(p-1) == '.' ) /* Remove terminal dots */ - p --; + p --; } *p = '\0'; @@ -243,7 +259,7 @@ int iso_compare_names(char *de_name, __u32 len, char *file_name) p = ISOFileName; - do { + while ( len ) { c1 = *p++; c2 = *file_name++; @@ -255,9 +271,12 @@ int iso_compare_names(char *de_name, __u32 len, char *file_name) c1 |= 0x20; c2 |= 0x20; /* convert to lower case */ - }while ( c1 == c2 ); + if ( c1 != c2 ) + return 0; + len --; + } - return 0; + return 1; } @@ -276,7 +295,7 @@ __u32 getfssec(char *buf, struct open_file_t *file, __u32 sectors, int *have_mor { __u32 bytes_read = sectors << SECTOR_SHIFT; - if ( sectros > file->file_left ) + if ( sectors > file->file_left ) sectors = file->file_left; getlinsec(buf, file->file_sector, sectors); @@ -316,9 +335,16 @@ 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 ) @@ -341,16 +367,10 @@ int do_search_dir(struct dir_t *dir, char *name, void **res) de = (struct iso_dir_entry *) (trackbuf + offset); - de_len = iso_dir->length; + de_len = de->length; if ( de_len == 0) { - file_pos = (file_pos+SECTOR_SIZE) & ~(SECTOR_SIZE-1); - file->file_sector ++; - if ( !have_more ) - return 0; - getfssec(trackbuf, file, BufSafe, &have_more); - offset = 0; - + offset = file_pos = (file_pos+SECTOR_SIZE) & ~(SECTOR_SIZE-1); continue; } @@ -362,12 +382,12 @@ int do_search_dir(struct dir_t *dir, char *name, void **res) int slop = trackbufsize - offset + de_len; memcpy(tmpde, de, slop); offset &= trackbufsize - 1; - file->sector ++; + file->file_sector ++; if ( offset ) { if ( !have_more ) return 0; getfssec(trackbuf, file, BufSafe, &have_more); - memcpy((void*) tmp + slop, trackbuf, offset); + memcpy((void*)tmpde + slop, trackbuf, offset); } de = tmpde; } @@ -378,14 +398,15 @@ int do_search_dir(struct dir_t *dir, char *name, void **res) } de_name_len = de->name_len; - if ( iso_compare_names(de->name, name, de_name_len) ) + de_name = (char *)((void *)de + 0x21); + if ( iso_compare_names(de_name, de_name_len, name) ) break; /* we found it */ file_pos += de_len; } - if ( *(filename+de_name) && (*(name+de_name) != '/' ) ) { + if ( *(name+de_name_len) && (*(name+de_name_len) != '/' ) ) { *res = NULL; return 0; } @@ -393,7 +414,7 @@ int do_search_dir(struct dir_t *dir, char *name, void **res) if ( de->flags & 0x02 ) { /* it's a directory */ - dir = CurrentDir; + dir = &CurrentDir; dir->dir_lba = *(__u32 *)de->extent; dir->dir_len = *(__u32 *)de->size; @@ -444,12 +465,13 @@ struct open_file_t *searchdir(char *filename) struct iso_dir_entry *de; void *res; - + int ret; int flag = 0; - dir = CurrentDir; + + dir = &CurrentDir; if ( *filename == '/' ) { - dir = RootDir; + dir = &RootDir; filename ++; } @@ -457,7 +479,7 @@ struct open_file_t *searchdir(char *filename) ret = do_search_dir(dir, filename, &res); if ( ret == 1 ) - dir = (struct dir_t *) res; + dir = (struct dir_t *)res; else if ( ret == 2 ) break; else @@ -487,22 +509,47 @@ struct open_file_t *searchdir(char *filename) } } + + + +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); + CurrentDir.dir_lba = RootDir.dir_lba = *(__u32 *)(trackbuf + 156 + 2); #ifdef DEBUG - printf("Root directory at LBA = 0x%x\n", RootDir->dir_lba); + 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; + CurrentDir.dir_len = RootDir.dir_len = *(__u32*)(trackbuf + 156 + 10); + CurrentDir.dir_clust = RootDir.dir_clust = (RootDir.dir_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT; /* @@ -528,15 +575,17 @@ void init_fs() CurrentDirName[len] = '/'; CurrentDirName[len+1] = '\0'; - CurrentDir->dir_len = file->file_bytesleft; - CurrentDir->dir_clust = file->file_left; - CurrentDir->dir_lba = file->file_sector; + 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", CurrentDirName->dir_lba); + printf("isolinux directory at LBA = %0x%x\n", CurrentDir.dir_lba); #endif + + return; no_isolinux_dir: - ; + printf("No isolinux directory found\n"); } @@ -310,7 +310,7 @@ void __getfssec(char *buf, struct open_file_t *file, __u32 sectors) break; }while( next_sector == (++curr_sector) ); -#if 1 /* Debug message */ +#if 0 /* Debug message */ printf("/**************************************************\n"); printf("You are reading stores at sector --0x%x--0x%x\n", frag_start, frag_start + con_sec_cnt -1); @@ -7,37 +7,14 @@ * */ #include <stdio.h> -#include "cache.h" - - #include <fcntl.h> #include <sys/stat.h> - +#include "cache.h" int fd; -extern struct cache_struct cache[CACHE_ENTRIES + 1]; - - -/** - * Just print the sector, and according the LRU algorithm, - * Left most value is the most least secotr, and Right most - * value is the most Recent sector. I see it's a Left Right Used - * (LRU) algorithm; Just kidding:) - */ -void print_cache(void) -{ - int i = 0; - struct cache_struct *cs = cache; - for (; i < CACHE_ENTRIES; i++) { - cs = cs->next; - printf("%d ", cs->block); - } - - printf("\n"); -} /* test function */ @@ -65,8 +42,8 @@ void print_hex(char *data, int size) void usage(void) { - printf("USAGE: a.out ext2fs.img filename\n"); - printf("---- ext2fs.img means a ext2 filesytem image\n"); + printf("USAGE: a.out fs.img filename\n"); + printf("---- fs.img means a filesytem image, it can be EXT, FAT and ISO9660 fs for now\n"); printf("---- filename menas the file you wanna open, it can be \n"); printf(" in one of the two following forms:\n"); printf(" /file/name/xy.y, this is a full name, or \n"); @@ -91,7 +68,7 @@ int main(int argc, char *argv[]) int bytes_read = 0; int total_bytes = 0; int have_more; - char *ext2fs = argv[1]; + char *fs = argv[1]; char *filename = argv[2]; char buf[1024]; struct cache_struct *cs; @@ -107,9 +84,9 @@ int main(int argc, char *argv[]) cache_init(); - fd = open(ext2fs, O_RDONLY); + fd = open(fs, O_RDONLY); if ( fd < 0 ) { - printf("File %s open error....\n", ext2fs); + printf("File %s open error....\n", fs); return 0; } |