aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/fs/marfs
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/fs/marfs')
-rw-r--r--src/kernel/fs/marfs/directory.c96
-rw-r--r--src/kernel/fs/marfs/disklevel.c30
-rw-r--r--src/kernel/fs/marfs/marfs.h40
-rw-r--r--src/kernel/fs/marfs/new_file.c169
-rw-r--r--src/kernel/fs/marfs/read_whole_file.c90
-rw-r--r--src/kernel/fs/marfs/sectorlevel.c143
6 files changed, 296 insertions, 272 deletions
diff --git a/src/kernel/fs/marfs/directory.c b/src/kernel/fs/marfs/directory.c
index fd8ecd6..f4a5b04 100644
--- a/src/kernel/fs/marfs/directory.c
+++ b/src/kernel/fs/marfs/directory.c
@@ -6,54 +6,58 @@
uint32_t marfs_new_dir(uint32_t uid)
{
- return marfs_new_file(0, 0, uid, 0, 1);
+ return marfs_new_file(0, 0, uid, 0, 1);
}
-
void marfs_add_to_dir(uint32_t lba_inode, char *filename, uint32_t lba)
{
- struct marfs_inode *inode = (struct marfs_inode *) ata_read28(interface, lba_inode);
-
- // Read the content
- uint8_t *old = marfs_allocate_and_read_whole_file(lba_inode);
-
- // Allocate memory
- uint8_t *contents = (uint8_t *) kmalloc((uint32_t) (inode->size + strlen(filename) + 1 + 4));
-
- // Copy the content
- uint8_t last_was_null = 0;
- uint64_t new_size = 0;
- for (uint64_t i = 0; i < inode->size; i++) {
- if (old[i] == 0 && last_was_null) continue;
-
- contents[new_size++] = old[i];
- last_was_null = (uint8_t) (old[i] == 0);
- }
- kfree(old);
-
- // Append new file
- for (size_t i = 0; i <= strlen(filename); i++) contents[new_size++] = (uint8_t) filename[i];
- for (signed char j = 24; j > 0; j -= 8) contents[new_size++] = (uint8_t) ((lba >> j) & 0xFF);
-
- // Free the blocks
- uint32_t new_size_in_blocks = (uint32_t) (new_size / 512);
- if (new_size % 512) new_size_in_blocks++;
- for (uint32_t i = 0; i < new_size_in_blocks; i++)
- marfs_mark_block_as_free(marfs_get_block(inode, i));
-
- // Overwrite
- uint32_t aux_inode = marfs_new_file(new_size, contents, 0xDEADBEEF, 0, 0);
- struct marfs_inode *real_aux_inode = (struct marfs_inode *) ata_read28(interface, aux_inode);
- for (uint8_t i = 0; i < 10; i++) inode->DBPs[i] = real_aux_inode->DBPs[i];
- inode->ext_1 = real_aux_inode->ext_1;
- inode->ext_2 = real_aux_inode->ext_2;
- inode->ext_3 = real_aux_inode->ext_3;
- inode->ext_4 = real_aux_inode->ext_4;
- real_aux_inode->is_used = 0;
- ata_write28(interface, aux_inode, (uint8_t *) real_aux_inode);
- kfree(real_aux_inode);
-
- inode->size = new_size;
- ata_write28(interface, lba_inode, (uint8_t *) inode);
- kfree(inode);
+ struct marfs_inode *inode = (struct marfs_inode *)ata_read28(interface, lba_inode);
+
+ // Read the content
+ uint8_t *old = marfs_allocate_and_read_whole_file(lba_inode);
+
+ // Allocate memory
+ uint8_t *contents = (uint8_t *)kmalloc((uint32_t)(inode->size + strlen(filename) + 1 + 4));
+
+ // Copy the content
+ uint8_t last_was_null = 0;
+ uint64_t new_size = 0;
+ for (uint64_t i = 0; i < inode->size; i++) {
+ if (old[i] == 0 && last_was_null)
+ continue;
+
+ contents[new_size++] = old[i];
+ last_was_null = (uint8_t)(old[i] == 0);
+ }
+ kfree(old);
+
+ // Append new file
+ for (size_t i = 0; i <= strlen(filename); i++)
+ contents[new_size++] = (uint8_t)filename[i];
+ for (signed char j = 24; j > 0; j -= 8)
+ contents[new_size++] = (uint8_t)((lba >> j) & 0xFF);
+
+ // Free the blocks
+ uint32_t new_size_in_blocks = (uint32_t)(new_size / 512);
+ if (new_size % 512)
+ new_size_in_blocks++;
+ for (uint32_t i = 0; i < new_size_in_blocks; i++)
+ marfs_mark_block_as_free(marfs_get_block(inode, i));
+
+ // Overwrite
+ uint32_t aux_inode = marfs_new_file(new_size, contents, 0xDEADBEEF, 0, 0);
+ struct marfs_inode *real_aux_inode = (struct marfs_inode *)ata_read28(interface, aux_inode);
+ for (uint8_t i = 0; i < 10; i++)
+ inode->DBPs[i] = real_aux_inode->DBPs[i];
+ inode->ext_1 = real_aux_inode->ext_1;
+ inode->ext_2 = real_aux_inode->ext_2;
+ inode->ext_3 = real_aux_inode->ext_3;
+ inode->ext_4 = real_aux_inode->ext_4;
+ real_aux_inode->is_used = 0;
+ ata_write28(interface, aux_inode, (uint8_t *)real_aux_inode);
+ kfree(real_aux_inode);
+
+ inode->size = new_size;
+ ata_write28(interface, lba_inode, (uint8_t *)inode);
+ kfree(inode);
}
diff --git a/src/kernel/fs/marfs/disklevel.c b/src/kernel/fs/marfs/disklevel.c
index 106540e..f814f80 100644
--- a/src/kernel/fs/marfs/disklevel.c
+++ b/src/kernel/fs/marfs/disklevel.c
@@ -4,21 +4,23 @@
void marfs_format(void)
{
- // Create superblock
- struct marfs_superblock sb;
- sb.signature = 0x1083B99F34B59645; // Huh, magic?!
- sb.n_inodes = (marfs_get_max_lba() - 2) >> 5;
- sb.n_chunks = (marfs_get_max_lba() - (2 + sb.n_inodes)) >> 9;
- sb.n_first_unallocated_inode = 0;
- sb.s_first_inode = 2;
- sb.s_first_chunk = 2 + sb.n_inodes;
+ // Create superblock
+ struct marfs_superblock sb;
+ sb.signature = 0x1083B99F34B59645; // Huh, magic?!
+ sb.n_inodes = (marfs_get_max_lba() - 2) >> 5;
+ sb.n_chunks = (marfs_get_max_lba() - (2 + sb.n_inodes)) >> 9;
+ sb.n_first_unallocated_inode = 0;
+ sb.s_first_inode = 2;
+ sb.s_first_chunk = 2 + sb.n_inodes;
- // Write to disk
- marfs_write_superblock(&sb);
+ // Write to disk
+ marfs_write_superblock(&sb);
- // Initialize the inodes
- for (uint32_t i = 0; i < sb.n_inodes; i++) ata_clear28(interface, 2 + i);
+ // Initialize the inodes
+ for (uint32_t i = 0; i < sb.n_inodes; i++)
+ ata_clear28(interface, 2 + i);
- // Initialize the chunks
- for (uint32_t i = 0; i < sb.n_chunks; i++) ata_clear28(interface, sb.s_first_chunk + i * 512);
+ // Initialize the chunks
+ for (uint32_t i = 0; i < sb.n_chunks; i++)
+ ata_clear28(interface, sb.s_first_chunk + i * 512);
}
diff --git a/src/kernel/fs/marfs/marfs.h b/src/kernel/fs/marfs/marfs.h
index 67b693c..f2dfa1c 100644
--- a/src/kernel/fs/marfs/marfs.h
+++ b/src/kernel/fs/marfs/marfs.h
@@ -4,29 +4,29 @@
#include <stdint.h>
struct marfs_superblock {
- uint64_t signature;
- uint32_t n_inodes;
- uint32_t n_chunks;
- uint32_t n_first_unallocated_inode;
- uint32_t s_first_inode;
- uint32_t s_first_chunk;
+ uint64_t signature;
+ uint32_t n_inodes;
+ uint32_t n_chunks;
+ uint32_t n_first_unallocated_inode;
+ uint32_t s_first_inode;
+ uint32_t s_first_chunk;
} __attribute__((packed));
struct marfs_inode {
- uint64_t size;
- uint32_t creation_time;
- uint32_t last_mod_time;
- uint32_t last_access_time;
- uint32_t n_blocks;
- uint32_t DBPs[10];
- uint32_t ext_1;
- uint32_t ext_2;
- uint32_t ext_3;
- uint32_t ext_4;
- uint32_t uid;
- uint8_t is_app;
- uint8_t is_dir;
- uint8_t is_used;
+ uint64_t size;
+ uint32_t creation_time;
+ uint32_t last_mod_time;
+ uint32_t last_access_time;
+ uint32_t n_blocks;
+ uint32_t DBPs[10];
+ uint32_t ext_1;
+ uint32_t ext_2;
+ uint32_t ext_3;
+ uint32_t ext_4;
+ uint32_t uid;
+ uint8_t is_app;
+ uint8_t is_dir;
+ uint8_t is_used;
} __attribute__((packed));
struct ata_interface *interface;
diff --git a/src/kernel/fs/marfs/new_file.c b/src/kernel/fs/marfs/new_file.c
index 8f85ffa..d278ad2 100644
--- a/src/kernel/fs/marfs/new_file.c
+++ b/src/kernel/fs/marfs/new_file.c
@@ -7,98 +7,105 @@ static uint8_t last_max_level = 0;
void marfs_update_recursive(uint8_t level, uint32_t i, uint32_t rec_lba, uint32_t real_lba)
{
- if (level > last_max_level) last_max_level = level;
- uint32_t *contents = (uint32_t *) ata_read28(interface, rec_lba);
+ if (level > last_max_level)
+ last_max_level = level;
+ uint32_t *contents = (uint32_t *)ata_read28(interface, rec_lba);
- uint32_t idx = i - 10;
- if (last_max_level > 1) idx -= 1 << 7;
- if (last_max_level > 2) idx -= 1 << (7 * 2);
- if (last_max_level > 3) idx -= 1 << (7 * 3);
- idx >>= 7 * (level - 1);
+ uint32_t idx = i - 10;
+ if (last_max_level > 1)
+ idx -= 1 << 7;
+ if (last_max_level > 2)
+ idx -= 1 << (7 * 2);
+ if (last_max_level > 3)
+ idx -= 1 << (7 * 3);
+ idx >>= 7 * (level - 1);
- if (level > 1) {
- if (!contents[idx]) {
- contents[idx] = marfs_get_free_lba_block();
- marfs_mark_block_as_used(contents[idx]);
- }
- } else {
- contents[idx] = real_lba;
- }
+ if (level > 1) {
+ if (!contents[idx]) {
+ contents[idx] = marfs_get_free_lba_block();
+ marfs_mark_block_as_used(contents[idx]);
+ }
+ } else {
+ contents[idx] = real_lba;
+ }
- ata_write28(interface, rec_lba, (uint8_t *) contents);
+ ata_write28(interface, rec_lba, (uint8_t *)contents);
- uint32_t contents_idx = contents[idx];
- kfree(contents);
- if (level != 1) {
- marfs_update_recursive((uint8_t) (level - 1), i, contents_idx, real_lba);
- }
- last_max_level = 0;
+ uint32_t contents_idx = contents[idx];
+ kfree(contents);
+ if (level != 1) {
+ marfs_update_recursive((uint8_t)(level - 1), i, contents_idx, real_lba);
+ }
+ last_max_level = 0;
}
uint32_t marfs_new_file(uint64_t size, uint8_t *data, uint32_t uid, uint8_t exec, uint8_t dir)
{
- struct marfs_inode *inode = (struct marfs_inode *) kmalloc(512);
- inode->size = size;
- inode->creation_time = inode->last_mod_time = inode->last_access_time = 0; // TODO: POSIX time
- inode->n_blocks = (uint32_t) (size / 512);
- if (size % 512) inode->n_blocks++;
- inode->uid = uid;
- inode->is_app = exec;
- inode->is_dir = dir;
- inode->is_used = 1;
+ struct marfs_inode *inode = (struct marfs_inode *)kmalloc(512);
+ inode->size = size;
+ inode->creation_time = inode->last_mod_time = inode->last_access_time =
+ 0; // TODO: POSIX time
+ inode->n_blocks = (uint32_t)(size / 512);
+ if (size % 512)
+ inode->n_blocks++;
+ inode->uid = uid;
+ inode->is_app = exec;
+ inode->is_dir = dir;
+ inode->is_used = 1;
- uint32_t size_in_blocks = inode->n_blocks;
+ uint32_t size_in_blocks = inode->n_blocks;
- uint32_t lba_singly, lba_doubly, lba_triply, lba_quadruply;
- lba_singly = lba_doubly = lba_triply = lba_quadruply = 0;
- for (uint32_t i = 0; i < size_in_blocks; i++) {
- uint32_t this_block = marfs_get_free_lba_block();
- if (i != size_in_blocks - 1) {
- ata_write28(interface, this_block, data);
- } else if (size % 512) {
- uint8_t contents[512] = {0};
- for (uint16_t i = 0; i < size % 512; i++) contents[i] = data[i];
- ata_write28(interface, this_block, contents);
- }
- data += 512;
- marfs_mark_block_as_used(this_block);
+ uint32_t lba_singly, lba_doubly, lba_triply, lba_quadruply;
+ lba_singly = lba_doubly = lba_triply = lba_quadruply = 0;
+ for (uint32_t i = 0; i < size_in_blocks; i++) {
+ uint32_t this_block = marfs_get_free_lba_block();
+ if (i != size_in_blocks - 1) {
+ ata_write28(interface, this_block, data);
+ } else if (size % 512) {
+ uint8_t contents[512] = { 0 };
+ for (uint16_t i = 0; i < size % 512; i++)
+ contents[i] = data[i];
+ ata_write28(interface, this_block, contents);
+ }
+ data += 512;
+ marfs_mark_block_as_used(this_block);
- if (i > 9 + (128 * 128 * 128)) {
- if (!lba_quadruply) {
- lba_quadruply = marfs_get_free_lba_block();
- marfs_mark_block_as_used(lba_quadruply);
- inode->ext_4 = lba_quadruply;
- }
- marfs_update_recursive(4, i, lba_quadruply, this_block);
- } else if (i > 9 + (128 * 128)) {
- if (!lba_triply) {
- lba_triply = marfs_get_free_lba_block();
- marfs_mark_block_as_used(lba_triply);
- inode->ext_3 = lba_triply;
- }
- marfs_update_recursive(3, i, lba_triply, this_block);
- } else if (i > 9 + 128) {
- if (!lba_doubly) {
- lba_doubly = marfs_get_free_lba_block();
- marfs_mark_block_as_used(lba_doubly);
- inode->ext_2 = lba_doubly;
- }
- marfs_update_recursive(2, i, lba_doubly, this_block);
- } else if (i > 9) {
- if (!lba_singly) {
- lba_singly = marfs_get_free_lba_block();
- marfs_mark_block_as_used(lba_singly);
- inode->ext_1 = lba_singly;
- }
- marfs_update_recursive(1, i, lba_singly, this_block);
- } else {
- inode->DBPs[i] = this_block;
- }
- }
+ if (i > 9 + (128 * 128 * 128)) {
+ if (!lba_quadruply) {
+ lba_quadruply = marfs_get_free_lba_block();
+ marfs_mark_block_as_used(lba_quadruply);
+ inode->ext_4 = lba_quadruply;
+ }
+ marfs_update_recursive(4, i, lba_quadruply, this_block);
+ } else if (i > 9 + (128 * 128)) {
+ if (!lba_triply) {
+ lba_triply = marfs_get_free_lba_block();
+ marfs_mark_block_as_used(lba_triply);
+ inode->ext_3 = lba_triply;
+ }
+ marfs_update_recursive(3, i, lba_triply, this_block);
+ } else if (i > 9 + 128) {
+ if (!lba_doubly) {
+ lba_doubly = marfs_get_free_lba_block();
+ marfs_mark_block_as_used(lba_doubly);
+ inode->ext_2 = lba_doubly;
+ }
+ marfs_update_recursive(2, i, lba_doubly, this_block);
+ } else if (i > 9) {
+ if (!lba_singly) {
+ lba_singly = marfs_get_free_lba_block();
+ marfs_mark_block_as_used(lba_singly);
+ inode->ext_1 = lba_singly;
+ }
+ marfs_update_recursive(1, i, lba_singly, this_block);
+ } else {
+ inode->DBPs[i] = this_block;
+ }
+ }
- // Write the inode
- uint32_t inode_lba = marfs_get_free_lba_inode();
- ata_write28(interface, inode_lba, (uint8_t *) inode);
+ // Write the inode
+ uint32_t inode_lba = marfs_get_free_lba_inode();
+ ata_write28(interface, inode_lba, (uint8_t *)inode);
- return inode_lba;
+ return inode_lba;
} \ No newline at end of file
diff --git a/src/kernel/fs/marfs/read_whole_file.c b/src/kernel/fs/marfs/read_whole_file.c
index f530441..eb49dfe 100644
--- a/src/kernel/fs/marfs/read_whole_file.c
+++ b/src/kernel/fs/marfs/read_whole_file.c
@@ -7,63 +7,71 @@ static uint8_t last_max_level = 0;
uint32_t marfs_get_recursive(uint8_t level, uint32_t i, uint32_t rec_lba)
{
- if (level > last_max_level) last_max_level = level;
- uint32_t *contents = (uint32_t *) ata_read28(interface, rec_lba);
- uint32_t idx = i - 10;
- if (last_max_level > 1) idx -= 1 << 7;
- if (last_max_level > 2) idx -= 1 << (7 * 2);
- if (last_max_level > 3) idx -= 1 << (7 * 3);
- idx >>= 7 * (level - 1);
+ if (level > last_max_level)
+ last_max_level = level;
+ uint32_t *contents = (uint32_t *)ata_read28(interface, rec_lba);
+ uint32_t idx = i - 10;
+ if (last_max_level > 1)
+ idx -= 1 << 7;
+ if (last_max_level > 2)
+ idx -= 1 << (7 * 2);
+ if (last_max_level > 3)
+ idx -= 1 << (7 * 3);
+ idx >>= 7 * (level - 1);
- uint32_t next_rec_lba = contents[idx];
- kfree(contents);
+ uint32_t next_rec_lba = contents[idx];
+ kfree(contents);
- uint32_t toRet;
- if (level > 1) toRet = marfs_get_recursive((uint8_t) (level - 1), i, next_rec_lba);
- else toRet = next_rec_lba;
- last_max_level = 0;
- return toRet;
+ uint32_t toRet;
+ if (level > 1)
+ toRet = marfs_get_recursive((uint8_t)(level - 1), i, next_rec_lba);
+ else
+ toRet = next_rec_lba;
+ last_max_level = 0;
+ return toRet;
}
uint32_t marfs_get_block(struct marfs_inode *inode, uint32_t i)
{
- if (i > 9 + (128 * 128 * 128)) {
- return marfs_get_recursive(4, i, inode->ext_4);
- } else if (i > 9 + (128 * 128)) {
- return marfs_get_recursive(3, i, inode->ext_3);
- } else if (i > 9 + 128) {
- return marfs_get_recursive(2, i, inode->ext_2);
- } else if (i > 9) {
- return marfs_get_recursive(1, i, inode->ext_1);
- } else {
- return inode->DBPs[i];
- }
+ if (i > 9 + (128 * 128 * 128)) {
+ return marfs_get_recursive(4, i, inode->ext_4);
+ } else if (i > 9 + (128 * 128)) {
+ return marfs_get_recursive(3, i, inode->ext_3);
+ } else if (i > 9 + 128) {
+ return marfs_get_recursive(2, i, inode->ext_2);
+ } else if (i > 9) {
+ return marfs_get_recursive(1, i, inode->ext_1);
+ } else {
+ return inode->DBPs[i];
+ }
}
void marfs_read_whole_file(uint32_t lba_inode, uint8_t *buffer)
{
- struct marfs_inode *inode = (struct marfs_inode *) ata_read28(interface, lba_inode);
+ struct marfs_inode *inode = (struct marfs_inode *)ata_read28(interface, lba_inode);
- uint32_t size_in_blocks = inode->n_blocks;
- for (uint32_t i = 0; i < size_in_blocks; i++) {
- uint32_t this_block = marfs_get_block(inode, i);
- uint8_t *this_block_contents = ata_read28(interface, this_block);
- uint16_t upper_bound = (uint16_t) ((i != size_in_blocks - 1) ? 512 : (inode->size % 512));
- for (uint16_t j = 0; j < upper_bound; j++) buffer[(i * 512) + j] = this_block_contents[j];
- kfree(this_block_contents);
- }
+ uint32_t size_in_blocks = inode->n_blocks;
+ for (uint32_t i = 0; i < size_in_blocks; i++) {
+ uint32_t this_block = marfs_get_block(inode, i);
+ uint8_t *this_block_contents = ata_read28(interface, this_block);
+ uint16_t upper_bound =
+ (uint16_t)((i != size_in_blocks - 1) ? 512 : (inode->size % 512));
+ for (uint16_t j = 0; j < upper_bound; j++)
+ buffer[(i * 512) + j] = this_block_contents[j];
+ kfree(this_block_contents);
+ }
- kfree(inode);
+ kfree(inode);
}
// TODO: Beautify
uint8_t *marfs_allocate_and_read_whole_file(uint32_t lba_inode)
{
- struct marfs_inode *inode = (struct marfs_inode *) ata_read28(interface, lba_inode);
- uint64_t size = inode->size;
- kfree(inode);
+ struct marfs_inode *inode = (struct marfs_inode *)ata_read28(interface, lba_inode);
+ uint64_t size = inode->size;
+ kfree(inode);
- uint8_t *buffer = (uint8_t *) kmalloc((uint32_t) size);
- marfs_read_whole_file(lba_inode, buffer);
- return buffer;
+ uint8_t *buffer = (uint8_t *)kmalloc((uint32_t)size);
+ marfs_read_whole_file(lba_inode, buffer);
+ return buffer;
} \ No newline at end of file
diff --git a/src/kernel/fs/marfs/sectorlevel.c b/src/kernel/fs/marfs/sectorlevel.c
index 96a24ba..e84e1f2 100644
--- a/src/kernel/fs/marfs/sectorlevel.c
+++ b/src/kernel/fs/marfs/sectorlevel.c
@@ -5,114 +5,117 @@
uint8_t marfs_init(struct ata_interface *_interface)
{
- interface = _interface;
- uint16_t identify_data[256 * 2];
- uint8_t ret = ata_identify(interface, identify_data);
- max_lba = (identify_data[61] << 16) + identify_data[60];
- return ret;
+ interface = _interface;
+ uint16_t identify_data[256 * 2];
+ uint8_t ret = ata_identify(interface, identify_data);
+ max_lba = (identify_data[61] << 16) + identify_data[60];
+ return ret;
}
uint32_t marfs_get_max_lba(void)
{
- return max_lba;
+ return max_lba;
}
uint8_t marfs_write_mbr(uint8_t *mbr)
{
- return ata_write28(interface, 0, mbr);
+ return ata_write28(interface, 0, mbr);
}
struct marfs_superblock *marfs_read_superblock()
{
- struct marfs_superblock *p = (struct marfs_superblock *) ata_read28(interface, 1);
- sb_cache = *p;
- return p;
+ struct marfs_superblock *p = (struct marfs_superblock *)ata_read28(interface, 1);
+ sb_cache = *p;
+ return p;
}
uint8_t marfs_write_superblock(struct marfs_superblock *sb)
{
- sb_cache = *sb;
- return ata_write28(interface, 1, (uint8_t *) sb);
+ sb_cache = *sb;
+ return ata_write28(interface, 1, (uint8_t *)sb);
}
uint32_t marfs_get_free_lba_block(void)
{
- uint32_t offset = 2 + sb_cache.s_first_chunk;
- uint8_t *p = 0;
- for (uint32_t i = 0; i < sb_cache.n_chunks; i++) {
- p = ata_read28(interface, offset);
- if (!(*p & 0x80)) break;
- kfree(p);
- offset += 512;
- }
-
- offset++;
- for (uint16_t i = 1; i < 512; i++) {
- if (!p[i]) break;
- offset++;
- }
- kfree(p);
-
- ata_clear28(interface, offset);
-
- return offset;
+ uint32_t offset = 2 + sb_cache.s_first_chunk;
+ uint8_t *p = 0;
+ for (uint32_t i = 0; i < sb_cache.n_chunks; i++) {
+ p = ata_read28(interface, offset);
+ if (!(*p & 0x80))
+ break;
+ kfree(p);
+ offset += 512;
+ }
+
+ offset++;
+ for (uint16_t i = 1; i < 512; i++) {
+ if (!p[i])
+ break;
+ offset++;
+ }
+ kfree(p);
+
+ ata_clear28(interface, offset);
+
+ return offset;
}
static uint8_t marfs_mark_block(uint32_t lba_sector, uint8_t mode)
{
- lba_sector -= 2;
- lba_sector -= sb_cache.s_first_chunk;
- uint16_t block_in_chunk = (uint16_t) (lba_sector % 512);
- lba_sector /= 512;
- lba_sector = 2 + sb_cache.s_first_chunk + (512 * lba_sector);
-
- uint8_t *p = ata_read28(interface, lba_sector);
- p[block_in_chunk] = mode;
-
- if (mode == 0) {
- p[0] = 0;
- } else {
- uint8_t full_chunk = 1;
- for (uint16_t i = 1; i < 512; i++) {
- if (!p[i]) {
- full_chunk = 0;
- break;
- }
- }
- p[0] = full_chunk;
- }
-
- uint8_t ret = ata_write28(interface, lba_sector, p);
- kfree(p);
- return ret;
+ lba_sector -= 2;
+ lba_sector -= sb_cache.s_first_chunk;
+ uint16_t block_in_chunk = (uint16_t)(lba_sector % 512);
+ lba_sector /= 512;
+ lba_sector = 2 + sb_cache.s_first_chunk + (512 * lba_sector);
+
+ uint8_t *p = ata_read28(interface, lba_sector);
+ p[block_in_chunk] = mode;
+
+ if (mode == 0) {
+ p[0] = 0;
+ } else {
+ uint8_t full_chunk = 1;
+ for (uint16_t i = 1; i < 512; i++) {
+ if (!p[i]) {
+ full_chunk = 0;
+ break;
+ }
+ }
+ p[0] = full_chunk;
+ }
+
+ uint8_t ret = ata_write28(interface, lba_sector, p);
+ kfree(p);
+ return ret;
}
uint8_t marfs_mark_block_as_free(uint32_t lba_sector)
{
- return marfs_mark_block(lba_sector, 0);
+ return marfs_mark_block(lba_sector, 0);
}
uint8_t marfs_mark_block_as_used(uint32_t lba_sector)
{
- return marfs_mark_block(lba_sector, 1);
+ return marfs_mark_block(lba_sector, 1);
}
uint32_t marfs_get_free_lba_inode(void)
{
- uint32_t offset;
- for (offset = 2; offset < 2 + sb_cache.n_inodes; offset++) {
- struct marfs_inode *inode = (struct marfs_inode *) ata_read28(interface, offset);
- uint8_t used = inode->is_used;
- kfree(inode);
- if (!used) break;
- }
- return offset;
+ uint32_t offset;
+ for (offset = 2; offset < 2 + sb_cache.n_inodes; offset++) {
+ struct marfs_inode *inode = (struct marfs_inode *)ata_read28(interface, offset);
+ uint8_t used = inode->is_used;
+ kfree(inode);
+ if (!used)
+ break;
+ }
+ return offset;
}
void marfs_mark_inode_as_free(uint32_t lba_sector)
{
- struct marfs_inode *inode = (struct marfs_inode *) ata_read28(interface, lba_sector);
- inode->is_used = 0;
- ata_write28(interface, lba_sector, (uint8_t *) inode);
- kfree(inode);
+ struct marfs_inode *inode = (struct marfs_inode *)ata_read28(interface, lba_sector);
+ inode->is_used = 0;
+ ata_write28(interface, lba_sector, (uint8_t *)inode);
+ kfree(inode);
} \ No newline at end of file