Skip to content

Commit 4ff130e

Browse files
committed
implement byte-order swaps
1 parent a644add commit 4ff130e

File tree

4 files changed

+21
-52
lines changed

4 files changed

+21
-52
lines changed

lib/fs/ext2/dir.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct dircookie {
2121
uint cursor;
2222
};
2323

24+
// TODO, move to ext2.c
2425
static void ext2_dump_inode(struct ext2_inode *inode) {
2526
#if LOCAL_TRACE
2627
printf("mode: %d\n", inode->i_mode);
@@ -232,43 +233,6 @@ status_t ext2_opendir(fscookie *cookie, const char *name, dircookie **dcookie) {
232233

233234
*dcookie = dir;
234235

235-
#if 0
236-
buf = malloc(EXT2_BLOCK_SIZE(ext2->sb));
237-
238-
file_blocknum = 0;
239-
for (;;) {
240-
/* read in the offset */
241-
err = ext2_read_inode(ext2, &dir_inode, buf, file_blocknum * EXT2_BLOCK_SIZE(ext2->sb), EXT2_BLOCK_SIZE(ext2->sb));
242-
if (err <= 0) {
243-
free(buf);
244-
return -1;
245-
}
246-
247-
/* walk through the directory entries, looking for the one that matches */
248-
struct ext2_dir_entry_2 *ent;
249-
uint pos = 0;
250-
while (pos < EXT2_BLOCK_SIZE(ext2->sb)) {
251-
ent = (struct ext2_dir_entry_2 *)&buf[pos];
252-
253-
LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n",
254-
file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);
255-
256-
/* sanity check the record length */
257-
if (LE16(ent->rec_len) == 0)
258-
break;
259-
260-
pos += ROUNDUP(LE16(ent->rec_len), 4);
261-
}
262-
263-
file_blocknum++;
264-
265-
/* sanity check the directory. 4MB should be enough */
266-
if (file_blocknum > 1024) {
267-
free(buf);
268-
return -1;
269-
}
270-
}
271-
#endif
272236
return 0;
273237
}
274238
status_t ext2_readdir(dircookie *cookie, struct dirent *ent_out) {

lib/fs/ext2/ext2_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct ext2_group_desc {
9999

100100
/*
101101
* Structure of an inode on the disk
102+
* endian_swap_inode and ext2_load_inode deal with byte-order
102103
*/
103104
struct ext2_inode {
104105
uint16_t i_mode; /* File mode */

lib/fs/ext2/ext4_fs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define RO_COMPAT_EXTRA_ISIZE 0x040
66
#define RO_COMPAT_METADATA_CSUM 0x400
77

8+
// all fields in little-endian, LE16 and LE32 must be used
9+
// TODO? add a variant of endian_swap_inode
810
typedef struct {
911
uint16_t eh_magic;
1012
uint16_t eh_entries;
@@ -13,6 +15,8 @@ typedef struct {
1315
uint32_t eh_generation;
1416
} ext4_extent_header;
1517

18+
// all fields in little-endian, LE16 and LE32 must be used
19+
// TODO? add a variant of endian_swap_inode
1620
typedef struct {
1721
uint32_t ee_block;
1822
uint16_t ee_len;

lib/fs/ext2/io.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,32 @@ static blocknum_t file_block_to_fs_block(ext2_t *ext2, struct ext2_inode *inode,
131131
if (inode->i_flags & 0x80000) { // inode is stored using extents
132132
ext4_extent_header *eh = (ext4_extent_header*)&inode->i_block;
133133
//printf("its an extent based object\n");
134-
//printf("eh_magic: 0x%x\n", eh->eh_magic);
135-
//printf("eh_entries: %d\n", eh->eh_entries);
136-
//printf("eh_max: %d\n", eh->eh_max);
137-
//printf("eh_depth: %d\n", eh->eh_depth);
138-
//printf("eh_generation: %d\n", eh->eh_generation);
139-
if (eh->eh_magic != 0xf30a) {
134+
//printf("eh_magic: 0x%x\n", LE16(eh->eh_magic));
135+
//printf("eh_entries: %d\n", LE16(eh->eh_entries));
136+
//printf("eh_max: %d\n", LE16(eh->eh_max));
137+
//printf("eh_depth: %d\n", LE16(eh->eh_depth));
138+
//printf("eh_generation: %d\n", LE32(eh->eh_generation));
139+
if (LE16(eh->eh_magic) != 0xf30a) {
140140
puts("extent header magic invalid");
141141
return 0;
142142
}
143143
block = 0; // TODO
144-
if (eh->eh_depth == 0) {
144+
if (LE16(eh->eh_depth) == 0) {
145145
ext4_extent *extents = (ext4_extent*)( ((uint32_t)&inode->i_block) + 12);
146-
for (int i=0; i<eh->eh_entries; i++) {
146+
for (int i=0; i < LE16(eh->eh_entries); i++) {
147147
#if 0
148148
printf("extent %d\n", i);
149-
printf(" ee_block: %d\n", extents[i].ee_block);
150-
printf(" ee_len: %d\n", extents[i].ee_len);
151-
printf(" ee_start_hi: %d\n", extents[i].ee_start_hi);
152-
printf(" ee_start_lo: %d\n", extents[i].ee_start_lo);
149+
printf(" ee_block: %d\n", LE32(extents[i].ee_block));
150+
printf(" ee_len: %d\n", LE16(extents[i].ee_len));
151+
printf(" ee_start_hi: %d\n", LE16(extents[i].ee_start_hi));
152+
printf(" ee_start_lo: %d\n", LE32(extents[i].ee_start_lo));
153153
#endif
154-
if ((fileblock >= extents[i].ee_block) && (fileblock < (extents[i].ee_block + extents[i].ee_len))) {
155-
if (extents[i].ee_start_hi != 0) {
154+
if ((fileblock >= LE32(extents[i].ee_block)) && (fileblock < (LE32(extents[i].ee_block) + LE16(extents[i].ee_len)))) {
155+
if (LE16(extents[i].ee_start_hi) != 0) {
156156
puts("unsupported >32bit blocknr");
157157
return 0;
158158
}
159-
block = extents[i].ee_start_lo + (fileblock - extents[i].ee_block);
159+
block = LE32(extents[i].ee_start_lo) + (fileblock - LE32(extents[i].ee_block));
160160
}
161161
}
162162
}

0 commit comments

Comments
 (0)