-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c117eae
commit b638f11
Showing
9 changed files
with
225 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
struct initrd_header { | ||
uint8_t magic; | ||
char name[64]; | ||
uint32_t offset; | ||
uint32_t len; | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
int nheaders = (argc - 1) / 2; | ||
struct initrd_header headers[64]; | ||
printf("size of headers: %ld\n", sizeof(struct initrd_header)); | ||
uint32_t off = sizeof(struct initrd_header) * 64 + sizeof(int); | ||
size_t i; | ||
for (i = 0; i < nheaders; i++) { | ||
printf("writing file %s->%s at 0x%x\n", argv[i * 2 + 1], argv[i * 2 + 2], off); | ||
strcpy(headers[i].name, argv[i * 2 + 2]); | ||
headers[i].offset = off; | ||
FILE* stream =fopen(argv[i * 2 + 1], "r"); | ||
if (!stream) { | ||
printf("Error: file not found: %s\n", argv[i * 2 + 1]); | ||
return 1; | ||
} | ||
fseek(stream, 0, SEEK_END); | ||
headers[i].len = ftell(stream); | ||
off += headers[i].len; | ||
fclose(stream); | ||
headers[i].magic = 0xBF; | ||
} | ||
|
||
FILE* wstream = fopen("./initrd.img", "w"); | ||
uint8_t* data = (uint8_t*)malloc(off); | ||
fwrite(&nheaders, sizeof(uint32_t), 1, wstream); | ||
fwrite(headers, sizeof(struct initrd_header), 64, wstream); | ||
|
||
for (i = 0; i < nheaders; i++) { | ||
FILE* stream = fopen(argv[i * 2 + 1], "r"); | ||
uint8_t* buf = (uint8_t*)malloc(headers[i].len); | ||
fread(buf, 1, headers[i].len, stream); | ||
fwrite(buf, 1, headers[i].len, wstream); | ||
fclose(stream); | ||
free(buf); | ||
} | ||
|
||
fclose(wstream); | ||
free(data); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "initrd.h" | ||
|
||
initrd_header_t *initrd_header; // The header. | ||
initrd_file_header_t *file_headers; // The list of file headers. | ||
fs_node_t *initrd_root; // Our root directory node. | ||
fs_node_t *initrd_dev; // We also add a directory node for /dev, so we can mount devfs later on. | ||
fs_node_t *root_nodes; // List of file nodes. | ||
int nroot_nodes; // Number of file nodes. | ||
|
||
dir_entry_t dirent; | ||
|
||
static uint32_t initrd_read(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) { | ||
initrd_file_header_t header = file_headers[node->inode]; | ||
if (offset > header.len) return 0; | ||
if (offset+size > header.len) size = header.len - offset; | ||
memcpy(buffer, (uint8_t*) (header.offset + offset), size); | ||
return size; | ||
} | ||
|
||
static dir_entry_t *initrd_readdir(fs_node_t *node, uint32_t index) { | ||
if (node == initrd_root && index == 0) | ||
{ | ||
strcpy(dirent.name, "dev"); | ||
dirent.name[3] = 0; // Make sure the string is NULL-terminated. | ||
dirent.inode = 0; | ||
return &dirent; | ||
} | ||
|
||
if (index - 1 >= nroot_nodes) return NULL; | ||
strcpy(dirent.name, root_nodes[index-1].name); | ||
dirent.name[strlen(root_nodes[index-1].name)] = 0; // Make sure the string is NULL-terminated. | ||
dirent.inode = root_nodes[index-1].inode; | ||
return &dirent; | ||
} | ||
|
||
static fs_node_t *initrd_finddir(fs_node_t *node, char *name) { | ||
if (node == initrd_root && !strcmp(name, "dev")) return initrd_dev; | ||
for (size_t i = 0; i < nroot_nodes; i++) { | ||
if (!strcmp(name, root_nodes[i].name)) return &root_nodes[i]; | ||
} | ||
return NULL; | ||
} | ||
|
||
fs_node_t *initialise_initrd( uint32_t location) | ||
{ | ||
// Initialise the main and file header pointers and populate the root directory. | ||
initrd_header = (initrd_header_t *)location; | ||
file_headers = (initrd_file_header_t *) (location+sizeof(initrd_header_t)); | ||
// Initialise the root directory. | ||
initrd_root = (fs_node_t*)kmalloc(sizeof(fs_node_t)); | ||
strcpy(initrd_root->name, "initrd"); | ||
initrd_root->permissions = initrd_root->uid = initrd_root->gid = initrd_root->inode = initrd_root->len = 0; | ||
initrd_root->flags = FS_DIRECTORY; | ||
initrd_root->read = 0; | ||
initrd_root->write = 0; | ||
initrd_root->open = 0; | ||
initrd_root->close = 0; | ||
initrd_root->readdir = &initrd_readdir; | ||
initrd_root->finddir = &initrd_finddir; | ||
initrd_root->ptr = 0; | ||
initrd_root->impl = 0; | ||
// Initialise the /dev directory (required!) | ||
initrd_dev = (fs_node_t*)kmalloc(sizeof(fs_node_t)); | ||
strcpy(initrd_dev->name, "dev"); | ||
initrd_dev->permissions = initrd_dev->uid = initrd_dev->gid = initrd_dev->inode = initrd_dev->len = 0; | ||
initrd_dev->flags = FS_DIRECTORY; | ||
initrd_dev->read = 0; | ||
initrd_dev->write = 0; | ||
initrd_dev->open = 0; | ||
initrd_dev->close = 0; | ||
initrd_dev->readdir = &initrd_readdir; | ||
initrd_dev->finddir = &initrd_finddir; | ||
initrd_dev->ptr = 0; | ||
initrd_dev->impl = 0; | ||
// Allocate space for the files in the ramdisk | ||
root_nodes = (fs_node_t*)kmalloc(sizeof(fs_node_t) * initrd_header->nfiles); | ||
nroot_nodes = initrd_header->nfiles; | ||
// For every file... | ||
int i; | ||
for (i = 0; i < initrd_header->nfiles; i++) | ||
{ | ||
// Edit the file's header - currently it holds the file offset | ||
// relative to the start of the ramdisk. We want it relative to the start | ||
// of memory. | ||
file_headers[i].offset += location; | ||
// Create a new file node. | ||
strcpy(root_nodes[i].name, &file_headers[i].name); | ||
root_nodes[i].permissions = root_nodes[i].uid = root_nodes[i].gid = 0; | ||
root_nodes[i].len = file_headers[i].len; | ||
root_nodes[i].inode = i; | ||
root_nodes[i].flags = FS_FILE; | ||
root_nodes[i].read = &initrd_read; | ||
root_nodes[i].write = 0; | ||
root_nodes[i].readdir = 0; | ||
root_nodes[i].finddir = 0; | ||
root_nodes[i].open = 0; | ||
root_nodes[i].close = 0; | ||
root_nodes[i].impl = 0; | ||
} | ||
|
||
return initrd_root; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef INITRD_H | ||
#define INITRD_H | ||
|
||
#include "../vfs/vfs.h" | ||
|
||
typedef struct { | ||
uint32_t nfiles; | ||
} initrd_header_t; | ||
|
||
typedef struct { | ||
uint8_t magic; | ||
char name[64]; | ||
uint32_t offset; | ||
uint32_t len; | ||
} initrd_file_header_t; | ||
|
||
fs_node_t *initialise_initrd(uint32_t location); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
#include "vfs.h" | ||
|
||
fs_node_t *fs_root = NULL; | ||
|
||
uint32_t fs_write(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer) { | ||
struct fs_node_s *fs_root = NULL; | ||
|
||
uint32_t fs_write(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer) { | ||
if (!node->write) return node->write(node, offset, size, buffer); | ||
return 0; | ||
} | ||
|
||
uint32_t fs_read(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer) { | ||
|
||
uint32_t fs_read(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer) { | ||
if (!node->read) return node->read(node, offset, size, buffer); | ||
return 0; | ||
} | ||
|
||
void fs_open(fs_node_t* node, uint8_t read, uint8_t write) { | ||
|
||
void fs_open(struct fs_node_s* node, uint8_t read, uint8_t write) { | ||
if (!node->open) (node->open(node)); | ||
} | ||
|
||
void fs_close(fs_node_t* node) { | ||
|
||
void fs_close(struct fs_node_s* node) { | ||
if (!node->close) (node->close(node)); | ||
} | ||
|
||
dir_entry_t* readdir_fs(fs_node_t* node, uint32_t index) { | ||
|
||
dir_entry_t* readdir_fs(struct fs_node_s* node, uint32_t index) { | ||
if (!node->readdir && (node->flags & 7 == FS_DIRECTORY)) return node->readdir(node, index); | ||
return NULL; | ||
} | ||
|
||
fs_node_t* finddir_fs(fs_node_t*, char* name) { | ||
|
||
struct fs_node_s* finddir_fs(struct fs_node_s* node, char* name) { | ||
if (!node->finddir && (node->flags & 7 == FS_DIRECTORY)) return node->finddir(node, name); | ||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters