blob: 3d7b1bc10de90931d1ea1923e0af548c73a5384b [file] [log] [blame]
swissChili4418ca52021-06-14 17:36:00 -07001#include <dri/ata_pio/ata_pio.h>
swissChilie5adca52021-06-16 21:00:31 -07002#include <dri/fs/ext2/ext2.h>
swissChilief829f32021-06-13 20:00:54 -07003#include <kint.h>
swissChili4418ca52021-06-14 17:36:00 -07004#include <log.h>
5
6struct ext2_superblock ext2_read_superblock()
7{
8 uchar buffer[512 * 2];
9 ata_pio_read_sectors(buffer, 2, 2);
10
11 struct ext2_superblock *sb = (void *)(buffer);
12 return *sb;
13}
swissChilief829f32021-06-13 20:00:54 -070014
swissChilie5adca52021-06-16 21:00:31 -070015uint ext2_num_block_groups(struct ext2_superblock *sb)
16{
17 // This is a mildly janky way of rounding up
18 uint a = (sb->total_blocks - 1) / (sb->blocks_per_block_group + 1);
19 uint b = (sb->total_inodes - 1) / (sb->inodes_per_block_group + 1);
20
21 if (a == b)
22 {
23 return a;
24 }
25 else
26 {
27 kprintf(ERROR "EXT2 cannot find number of block groups, %d and %d "
28 "should equal.\n",
29 a, b);
30 kpanic("Corrupted filesystem");
31 }
32}
33
34uint ext2_block_size(struct ext2_superblock *sb)
35{
36 return 1024 << sb->block_size_shift;
37}
38
39void ext2_load_block_group_descriptor_table(struct ext2_superblock *sb,
40 uint num_block_groups)
41{
42 /**
43 * The BGDT (not to be confused with the GDT) is located the block after the
44 * superblock. On any block size EXCEPT 1024 (the minimum, remember that the
45 * block size is specified by X where 1024 << X is the real size) this is
46 * the second block (0-indexed, so 1). On 1024 this is the third block.
47 */
48 uint bgdt_block = 1;
49 uint block_size = ext2_block_size(sb);
50
51 if (block_size == 1024)
52 bgdt_block = 2;
53
54 kprintf(DEBUG "BGDT block = %d block size = %d\n", bgdt_block, block_size);
55}
56
swissChilief829f32021-06-13 20:00:54 -070057void ext2_mount(struct fs_node *where)
58{
swissChili4418ca52021-06-14 17:36:00 -070059 struct ext2_superblock sb = ext2_read_superblock();
60
swissChilie5adca52021-06-16 21:00:31 -070061 kprintf(DEBUG "EXT2 magic = 0x%x\n", sb.signature);
62
63 uint num_block_groups = ext2_num_block_groups(&sb);
64 ext2_load_block_group_descriptor_table(&sb, num_block_groups);
swissChilief829f32021-06-13 20:00:54 -070065}
swissChili9bd74de2021-06-15 20:30:48 -070066
67bool ext2_valid_filesystem()
68{
69 struct ext2_superblock sb = ext2_read_superblock();
70
71 return sb.signature == EXT2_SIGNATURE;
swissChilie5adca52021-06-16 21:00:31 -070072}