Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 8c3e6bb

Browse files
committed
fix run commands not running elf bug
1 parent 1821c32 commit 8c3e6bb

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/kernel/fs/fat/fat16.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ static int fat16_find_root_entry_bytes(struct fat16_super *sb, const char *name,
2727
uint32_t sectors = ((sb->max_root_entries + entries_per_sector - 1) /
2828
entries_per_sector);
2929
uint8_t *sec = (uint8_t *)kmalloc(sb->bytes_per_sector);
30-
if (!sec)
31-
return -2;
3230
uint8_t shortname[11];
3331
make_shortname(name, (char *)shortname);
32+
/* Debug: log the requested shortname for lookup */
33+
{
34+
char dbgname[12];
35+
for (int i = 0; i < 11; ++i)
36+
dbgname[i] = shortname[i] ? shortname[i] : ' ';
37+
dbgname[11] = '\0';
38+
printk("fat16: find_root_entry looking for '%s' (orig='%s')\n",
39+
dbgname, name);
40+
}
3441
uint32_t first_free_off = 0xFFFFFFFF;
3542
for (uint32_t s = 0; s < sectors; ++s) {
3643
if (fat16_read_sector(sb, root_sector + s, sec) != 0) {
@@ -99,6 +106,7 @@ static int fat16_find_entry_in_dir_bytes(struct fat16_super *sb,
99106
uint32_t *free_off) {
100107
uint8_t shortname[11];
101108
make_shortname(name, (char *)shortname);
109+
102110
uint32_t entries_per_sector = sb->bytes_per_sector / 32;
103111
uint32_t first_free_off = 0xFFFFFFFF;
104112
uint8_t *sec = (uint8_t *)kmalloc(sb->bytes_per_sector);

src/kernel/task/elf.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,47 @@ int elf_run(const char *path) {
5151
uint32_t size = 0;
5252
int r = vfs_read_file_all(path, &buf, &size);
5353
if (r != 0 || !buf || size < sizeof(Elf64_Ehdr)) {
54-
if (buf)
54+
if (buf) {
5555
kfree(buf);
56-
printk("elf_run: failed to read '%s' (err=%d)\n",
57-
path, r);
58-
return -2;
56+
buf = NULL;
57+
}
58+
59+
int is_dir = 0;
60+
int rr = vfs_resolve_path(path, &is_dir, &size);
61+
if (rr != 0 || is_dir || size == 0) {
62+
printk("elf_run: failed to read '%s' (err=%d)\n", path,
63+
r);
64+
return -2;
65+
}
66+
buf = (void *)kmalloc((size_t)size + 1);
67+
if (!buf) {
68+
printk("elf_run: kmalloc failed for size %u\n", size);
69+
return -2;
70+
}
71+
int fd = vfs_open(path, 0, 0);
72+
if (fd < 0) {
73+
printk("elf_run: vfs_open failed for '%s' (fd=%d)\n",
74+
path, fd);
75+
kfree(buf);
76+
return -2;
77+
}
78+
uint32_t read_total = 0;
79+
while (read_total < size) {
80+
int got = vfs_read(
81+
fd, (void *)((uintptr_t)buf + read_total),
82+
size - read_total);
83+
if (got <= 0)
84+
break;
85+
read_total += (uint32_t)got;
86+
}
87+
vfs_close(fd);
88+
if (read_total == 0) {
89+
printk("elf_run: vfs_read returned 0 for '%s'\n", path);
90+
kfree(buf);
91+
return -2;
92+
}
93+
((uint8_t *)buf)[read_total] = '\0';
94+
size = read_total;
5995
}
6096

6197
Elf64_Ehdr *eh = (Elf64_Ehdr *)buf;

0 commit comments

Comments
 (0)