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> |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 5 | #include <io.h> |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 6 | #include <alloc.h> |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 7 | |
| 8 | inline uint ext2_block_size(struct ext2_superblock *sb) |
| 9 | { |
| 10 | return 1024 << sb->block_size_shift; |
| 11 | } |
| 12 | |
| 13 | void ext2_read_block(struct ext2_superblock *sb, void *buffer, uint block) |
| 14 | { |
| 15 | uint block_size = ext2_block_size(sb) / 512; |
| 16 | uint block_start = block_size * block; |
| 17 | |
| 18 | ata_pio_read_sectors(buffer, block_start, block_size); |
| 19 | } |
swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 20 | |
| 21 | struct ext2_superblock ext2_read_superblock() |
| 22 | { |
| 23 | uchar buffer[512 * 2]; |
| 24 | ata_pio_read_sectors(buffer, 2, 2); |
| 25 | |
| 26 | struct ext2_superblock *sb = (void *)(buffer); |
| 27 | return *sb; |
| 28 | } |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 29 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 30 | uint ext2_num_block_groups(struct ext2_superblock *sb) |
| 31 | { |
| 32 | // This is a mildly janky way of rounding up |
| 33 | uint a = (sb->total_blocks - 1) / (sb->blocks_per_block_group + 1); |
| 34 | uint b = (sb->total_inodes - 1) / (sb->inodes_per_block_group + 1); |
| 35 | |
| 36 | if (a == b) |
| 37 | { |
| 38 | return a; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | kprintf(ERROR "EXT2 cannot find number of block groups, %d and %d " |
| 43 | "should equal.\n", |
| 44 | a, b); |
| 45 | kpanic("Corrupted filesystem"); |
| 46 | } |
| 47 | } |
| 48 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 49 | struct ext2_block_group_descriptor ext2_load_block_group_descriptor( |
| 50 | struct ext2_superblock *sb, uint block_group) |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 51 | { |
| 52 | /** |
| 53 | * The BGDT (not to be confused with the GDT) is located the block after the |
| 54 | * superblock. On any block size EXCEPT 1024 (the minimum, remember that the |
| 55 | * block size is specified by X where 1024 << X is the real size) this is |
| 56 | * the second block (0-indexed, so 1). On 1024 this is the third block. |
| 57 | */ |
| 58 | uint bgdt_block = 1; |
| 59 | uint block_size = ext2_block_size(sb); |
| 60 | |
| 61 | if (block_size == 1024) |
| 62 | bgdt_block = 2; |
| 63 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 64 | const uint bgd_size = sizeof(struct ext2_block_group_descriptor); |
| 65 | |
| 66 | // Disk page that the BGD is on relative to the initial FILE SYSTEM block |
| 67 | uint hd_page = block_group / (512 / bgd_size); |
| 68 | // The offset from the beginning of that page the BGD is at |
| 69 | uint bgd_offset = block_group % (512 / bgd_size); |
| 70 | |
| 71 | struct ext2_block_group_descriptor descriptors[512 / bgd_size]; |
| 72 | kassert(sizeof(descriptors) == 512, "Wrong BGD size"); |
| 73 | |
| 74 | uint lba = (block_size / 512) * bgdt_block + hd_page; |
| 75 | |
| 76 | ata_pio_read_sectors(&descriptors, lba, 1); |
| 77 | |
| 78 | return descriptors[bgd_offset]; |
| 79 | } |
| 80 | |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 81 | static void print_entry(uint inode, const char *name, void *sb) |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 82 | { |
| 83 | kprintf("%d\t %s\n", inode, name); |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 84 | |
| 85 | struct ext2_inode in; |
| 86 | |
| 87 | if (ext2_find_inode(sb, inode, &in)) |
| 88 | { |
| 89 | if ((in.mode & EXT2_F_TYPE) == EXT2_S_IFREG) |
| 90 | { |
| 91 | char buffer[65]; |
| 92 | uint read = ext2_read_inode(sb, &in, buffer, 64); |
| 93 | buffer[read] = 0; |
| 94 | |
| 95 | kprintf("contents: %d\n'%s'\n", read, buffer); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 100 | } |
| 101 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 102 | void ext2_mount(struct fs_node *where) |
| 103 | { |
swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 104 | struct ext2_superblock sb = ext2_read_superblock(); |
| 105 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 106 | kprintf(DEBUG "EXT2 magic = 0x%x\n", sb.signature); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 107 | |
| 108 | // Read the root inode 2 |
| 109 | struct ext2_inode root; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 110 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 111 | if (ext2_find_inode(&sb, 2, &root)) |
| 112 | { |
| 113 | kprintf(OKAY "Found root inode 2 (size=0x%x, num_blocks=0x%x)\n", root.size, root.num_blocks); |
| 114 | // kprintf(DEBUG "Root.mode = 0x%x\n", root.mode & 0xf000); |
| 115 | kassert((root.mode & 0xf000) == EXT2_S_IFDIR, "Root (inode 2) is not a directory."); |
| 116 | |
| 117 | kprintf("ls /\n"); |
| 118 | kprintf("inode\t name\n"); |
| 119 | kprintf("--------------------\n"); |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 120 | ext2_dir_ls(&sb, &root, print_entry, &sb); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | { |
| 124 | kprintf(WARN "Failed to find root inode 2\n"); |
| 125 | } |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 126 | } |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 127 | |
| 128 | bool ext2_valid_filesystem() |
| 129 | { |
| 130 | struct ext2_superblock sb = ext2_read_superblock(); |
| 131 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 132 | kprintf(DEBUG "superblock signature is %d (0x%x)\n", sb.signature, sb.signature); |
| 133 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 134 | return sb.signature == EXT2_SIGNATURE; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 135 | } |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 136 | |
| 137 | void ext2_write_superblock(struct ext2_superblock *sb) |
| 138 | { |
| 139 | ushort *wp = (ushort *)sb; |
| 140 | |
| 141 | ata_pio_write_sectors(2, 2, wp); |
| 142 | } |
| 143 | |
| 144 | void ext2_corrupt_superblock_for_fun() |
| 145 | { |
| 146 | struct ext2_superblock sb = ext2_read_superblock(); |
| 147 | sb.signature = 0xDEAD; |
| 148 | ext2_write_superblock(&sb); |
| 149 | } |
| 150 | |
| 151 | bool ext2_find_inode(struct ext2_superblock *sb, uint number, struct ext2_inode *inode) |
| 152 | { |
| 153 | if (number == 0) |
| 154 | return false; |
| 155 | |
| 156 | uint block_group = (number - 1) / sb->inodes_per_block_group; |
| 157 | uint local_index = (number - 1) % sb->inodes_per_block_group; |
| 158 | |
| 159 | // Load this from the block group descriptor table |
| 160 | struct ext2_block_group_descriptor descriptor = |
| 161 | ext2_load_block_group_descriptor(sb, block_group); |
| 162 | |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 163 | // kprintf(DEBUG "Descriptor inode_table = 0x%x\n", descriptor.inode_table_start_block); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 164 | |
| 165 | // We need to figure out what FS block the inode is on, we know how many |
| 166 | // inodes there are total in this BGD and the number per page, so this is |
| 167 | // simple. |
| 168 | |
| 169 | const uint block_size = ext2_block_size(sb); |
| 170 | |
| 171 | const uint inodes_per_block = block_size / sizeof(struct ext2_inode); |
| 172 | |
| 173 | uint inode_block = local_index / inodes_per_block; |
| 174 | uint inode_index = local_index % inodes_per_block; |
| 175 | |
| 176 | struct ext2_inode inodes[block_size / sizeof(struct ext2_inode)]; |
| 177 | |
| 178 | ext2_read_block(sb, inodes, descriptor.inode_table_start_block + |
| 179 | inode_block); |
| 180 | |
| 181 | *inode = inodes[inode_index]; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool ext2_dir_ls(struct ext2_superblock *sb, |
| 187 | struct ext2_inode *dir, |
| 188 | void (*cb)(uint inode, |
| 189 | const char *name, |
| 190 | void *data), |
| 191 | void *data) |
| 192 | { |
| 193 | if ((dir->mode & 0xf000) != EXT2_S_IFDIR) |
| 194 | return false; |
| 195 | |
| 196 | for (int i = 0; i < dir->num_blocks; i++) |
| 197 | { |
| 198 | uchar buffer[ext2_block_size(sb)]; |
| 199 | ext2_read_inode_block(sb, dir, buffer, i); |
| 200 | |
| 201 | struct ext2_dirent *ent = (void *)buffer; |
| 202 | |
| 203 | // While there are files in this block |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 204 | while ((uint)ent < (uint)(buffer + ext2_block_size(sb))) |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 205 | { |
| 206 | if (ent->inode == 0) |
| 207 | return true; |
| 208 | |
| 209 | if (cb) |
| 210 | { |
| 211 | char name[257]; |
| 212 | |
| 213 | memcpy(name, ent->name, ent->name_len); |
| 214 | name[ent->name_len] = '\0'; |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 215 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 216 | cb(ent->inode, name, data); |
| 217 | } |
| 218 | |
| 219 | ent = (void *)(((uint)(void *)ent) + ent->rec_len); |
| 220 | } |
| 221 | // We ran out of files in this block, continue to the next one. This |
| 222 | // works because files cannot span blocks |
| 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 228 | ssize_t ext2_read_inode(struct ext2_superblock *sb, struct ext2_inode *inode, void *buffer, ssize_t size) |
| 229 | { |
| 230 | const uint block_size = ext2_block_size(sb); |
| 231 | char transfer[block_size]; |
| 232 | |
| 233 | uint fsize = MIN(inode->size, size); |
| 234 | uint i; |
| 235 | |
| 236 | // Transfer full blocks straight to the output buffer |
| 237 | for (i = 0; i < fsize / block_size; i++) |
| 238 | { |
| 239 | ext2_read_inode_block(sb, inode, buffer + i * block_size, i); |
| 240 | } |
| 241 | |
| 242 | // If we have part of a block left over read it here first, then transfer what we need |
| 243 | if (i * block_size < fsize) |
| 244 | { |
| 245 | uint remainder = fsize % block_size; |
| 246 | |
| 247 | ext2_read_inode_block(sb, inode, transfer, i); |
| 248 | memcpy(buffer + i * block_size, transfer, remainder); |
| 249 | } |
| 250 | |
| 251 | return fsize; |
| 252 | } |
| 253 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 254 | bool ext2_read_inode_block(struct ext2_superblock *sb, |
| 255 | struct ext2_inode *inode, |
| 256 | void *buffer, |
| 257 | uint block) |
| 258 | { |
| 259 | if (block >= 12) |
| 260 | { |
| 261 | kprintf(ERROR "Sorry, EXT2 can only access the first 12 (direct) blocks " |
| 262 | "of an inode for now. Indirect look-up will be added later\n"); |
| 263 | kpanic("Invalid inode block"); |
| 264 | } |
| 265 | |
| 266 | uint block_address = inode->blocks[block]; |
| 267 | |
| 268 | ext2_read_block(sb, buffer, block_address); |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 269 | |
| 270 | return true; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 271 | } |