Skip to content

Commit c176dde

Browse files
committed
can mmap a file now
1 parent 234c808 commit c176dde

7 files changed

+82
-9
lines changed

iso9660.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ void *sector(void *baseptr, unsigned int sectornum)
1414
return &cdimg[sectornum * 2048];
1515
}
1616

17-
DiskFile * iso9660_enumfiles(void *baseptr)
17+
const DirectoryRecord *get_first_entry(void *baseptr)
1818
{
19+
1920
const PrimaryVolumeDescriptor *pvd = sector(baseptr, 16);
2021

2122
const DirectoryRecord *rootrecord = &pvd->root_directory_record;
2223
assert(rootrecord->record_len == sizeof(DirectoryRecord) + rootrecord->id_len);
2324

2425
const DirectoryRecord *entry = sector(baseptr, rootrecord->data_sector);
26+
return entry;
27+
}
28+
29+
DiskFile * iso9660_enumfiles(void *baseptr)
30+
{
31+
const DirectoryRecord *entry = get_first_entry(baseptr);
2532
int i=0;
2633
do {
2734
DiskFile *fp = &g_files[i++];

iso9660.h

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ typedef struct PACKED PathTableEntry {
8989
// and then a padding byte if id_len is odd
9090
} PathTableEntry;
9191

92+
93+
const DirectoryRecord *get_first_entry(void *baseptr);
9294
#define NEXT_PATH_TABLE_ENTRY(E) \
9395
((PathTableEntry *) (((u8 *) E) + sizeof(PathTableEntry) + E->id_len + (E->id_len & 0x1)))
9496

kmain.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "asmhelpers.h"
22
#include "kb.h"
33
#include "vgatext.h"
4-
#include "memlib.h"
54
#include "video.h"
65
#include "ata.h"
76
#include "DiskFile.h"
87
#include "kernel.h"
98
#include "virtualmem.h"
9+
#include "memlib.h"
1010

1111
extern char START_BSS[], END_BSS[];
1212
void setup_interrupts(void *idtaddr);
@@ -27,10 +27,10 @@ kmain(void)
2727
init_ata();
2828
ata_disk *d = &disks[0];
2929
mmap_disk(d);
30-
31-
DiskFile * df = iso9660_fopen_r((void *) 0x100000, "STARWARS.VGA");
30+
kprintf("got to here\n");
31+
mmap("STARWARS.VGA", 0x70000000);
3232
setup_timer(30);
33-
play_video(df->data + 4000 * 200);
33+
play_video(0x70000000 + 4000 * 200);
3434

3535
while (1) yield();
3636

memlib.c

+9
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,12 @@ char *strcpy(char * dest, const char * src)
114114
;
115115
return ret;
116116
}
117+
118+
int
119+
strcmp(const char *s1, const char *s2)
120+
{
121+
for ( ; *s1 == *s2; s1++, s2++)
122+
if (*s1 == '\0')
123+
return 0;
124+
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
125+
}

memlib.h

+19
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,23 @@
33

44
#include <string.h>
55

6+
7+
void * memcpy(void *_dest, const void *_src, size_t n);
8+
9+
void * memset(void *_dest, int val, size_t length);
10+
11+
void *memmove(void *dest, const void *src, size_t n);
12+
13+
int memcmp(const void *s1, const void *s2, size_t n);
14+
int strncmp(const char *s1, const char *s2, size_t n);
15+
int strcmp(const char *s1, const char *s2);
16+
17+
char *strncpy(char *dest, const char *src, size_t n);
18+
int strlen(const char *s);
19+
20+
char *strchr(const char *s, int c);
21+
22+
char *strcat(char *dest, const char *src);
23+
24+
char *strcpy(char * dest, const char * src);
625
#endif

virtualmem.c

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "kernel.h"
22
#include "asmhelpers.h"
33
#include "ata.h"
4+
#include "iso9660.h"
5+
#include "memlib.h"
6+
#include "virtualmem.h"
47

58
#define PAGEDIR_ADDR 0x80000
69
#define PT0_ADDR 0x81000
@@ -64,12 +67,44 @@ handle_page_fault()
6467

6568
void
6669
mmap_disk(ata_disk *d) {
67-
uint32_t iso_page_start = 0x100;
6870
uint32_t page_size = 0x1000;
6971
uint32_t npages = d->max_lba * d->sector_size / page_size + 1;
70-
72+
7173
for (uint32_t i = 0; i < npages; i++) {
7274
uint32_t lba = i * page_size / d->sector_size;
73-
ptable[i + iso_page_start] = DISK_MEMORY_ADDR_FLAG + (lba << 4); // 0x4[lba]0
75+
uint32_t page_table_index = i + (ISO_START >> 12);
76+
ptable[page_table_index] = DISK_MEMORY_ADDR_FLAG + (lba << 4); // 0x4[lba]0
77+
}
78+
}
79+
80+
const DirectoryRecord *find_file(const char* filename){
81+
82+
const DirectoryRecord *entry = get_first_entry((void*)(uint32_t)(ISO_START) );
83+
while((entry->record_len > 0)){
84+
if( strncmp(filename, entry->id, strlen(filename)) == 0) {
85+
return entry;
86+
}
87+
entry = NEXT_DIR_ENTRY(entry);
88+
}
89+
return NULL;
90+
}
91+
92+
int
93+
mmap(char *filename, uint32_t virt_addr){
94+
// using the mmaped iso, determine the first lba and size of file
95+
uint32_t first_lba;
96+
uint32_t num_lbas;
97+
const DirectoryRecord *entry = find_file(filename);
98+
if (entry == NULL){
99+
kprintf("can't find %s\n", filename);
100+
return -1;
74101
}
102+
first_lba = entry->data_sector;
103+
num_lbas = entry->data_len / 2048;
104+
kprintf("%s at %x for %d sectors\n", filename, first_lba, num_lbas);
105+
// mmap the file to the given virt addr using "the fours" scheme.
106+
// (assumes ratio of sector size to page size is 1 : 2
107+
for(uint32_t lba = 0; lba < num_lbas; lba += 2)
108+
ptable[(virt_addr >> 12) + lba/2] = DISK_MEMORY_ADDR_FLAG + ((first_lba + lba) << 4); //0x4[lba]0
109+
return 0;
75110
}

virtualmem.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#ifndef VIRTUALMEM_H
22
#define VIRTUALMEM_H
3-
3+
#define ISO_START 0x100000
44
#include "ata.h"
55

66
void handle_page_fault(void);
77
void setup_paging(void);
88
void mmap_disk(ata_disk *d);
9+
int mmap(char *filename, uint32_t virt_addr);
910
#endif

0 commit comments

Comments
 (0)