swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 1 | #include <dri/ata_pio/ata_pio.h> |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 2 | #include <dri/fs/ext2/ext2.h> |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 3 | #include <kint.h> |
swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 4 | #include <log.h> |
| 5 | |
| 6 | struct 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 | } |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 14 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 15 | uint 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 | |
| 34 | uint ext2_block_size(struct ext2_superblock *sb) |
| 35 | { |
| 36 | return 1024 << sb->block_size_shift; |
| 37 | } |
| 38 | |
| 39 | void 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 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 57 | void ext2_mount(struct fs_node *where) |
| 58 | { |
swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 59 | struct ext2_superblock sb = ext2_read_superblock(); |
| 60 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 61 | 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); |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 65 | } |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 66 | |
| 67 | bool ext2_valid_filesystem() |
| 68 | { |
| 69 | struct ext2_superblock sb = ext2_read_superblock(); |
| 70 | |
| 71 | return sb.signature == EXT2_SIGNATURE; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 72 | } |