diff --git a/boot/common/fs/uefi_sfs.c b/boot/common/fs/uefi_sfs.c index 589d783..c3f1310 100644 --- a/boot/common/fs/uefi_sfs.c +++ b/boot/common/fs/uefi_sfs.c @@ -95,7 +95,7 @@ struct vfs_drive *sfs_init(char *mountpoint) return drive; } -size_t sfs_read(char *filename, char *buffer, struct vfs_drive *dev, void *fsdata) +size_t sfs_read(char *filename, char **buffer, struct vfs_drive *dev, void *fsdata) { struct sfs_fsdata *data = (struct sfs_fsdata *)fsdata; EFI_FILE_PROTOCOL *volume = data->volume; @@ -113,7 +113,8 @@ size_t sfs_read(char *filename, char *buffer, struct vfs_drive *dev, void *fsdat return 0; } - mbstowcs(wfilename, (const char **)&filename, strlen(filename)); + size_t n = mbstowcs(wfilename, (const char **)&filename, strlen(filename)); + wfilename[n] = L'\0'; /* open the file */ status = volume->Open(volume, &file, wfilename, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM); @@ -134,16 +135,19 @@ size_t sfs_read(char *filename, char *buffer, struct vfs_drive *dev, void *fsdat file->GetInfo(file, &fi_guid, &fileinfo_size, fileinfo); len = fileinfo->FileSize; + mem_free(fileinfo); - debug("sfs_read: %u\n", len); - - buffer = (char *)mem_alloc(len * sizeof(char)); - if (!buffer) { + *buffer = (char *)mem_alloc(len * sizeof(char)); + if (!*buffer) { debug("sfs_read(): Failed to allocate memory for output buffer!\n"); return 0; } - file->Read(file, &len, buffer); + status = file->Read(file, &len, *buffer); + if (EFI_ERROR(status)) { + debug("sfs_read(): Failed to read file '%s': %s (%lx)\n", filename, efi_status_to_str(status), status); + return 0; + } /* close the file */ file->Close(file); diff --git a/boot/common/init.c b/boot/common/init.c index 379813d..453d1e0 100644 --- a/boot/common/init.c +++ b/boot/common/init.c @@ -31,7 +31,9 @@ void axboot_init() // read kernel -> test read char *buffer = NULL; - vfs_read("\\System\\axkrnl", buffer); + vfs_read("\\System\\axkrnl", &buffer); + + // TODO: Do something with the kernel :p mem_free(buffer); diff --git a/boot/common/lib/string.c b/boot/common/lib/string.c index de3ef8b..533806f 100644 --- a/boot/common/lib/string.c +++ b/boot/common/lib/string.c @@ -32,7 +32,7 @@ size_t mbstowcs(wchar_t *dest, const char **src, size_t len) return 0; } - while (count--) { + while (count) { if ((*dest = *lsrc) == 0) { lsrc = NULL; break; @@ -44,6 +44,7 @@ size_t mbstowcs(wchar_t *dest, const char **src, size_t len) lsrc++; dest++; + count--; } return len - count; diff --git a/boot/common/vfs/vfs.c b/boot/common/vfs/vfs.c index eabdd23..103928f 100644 --- a/boot/common/vfs/vfs.c +++ b/boot/common/vfs/vfs.c @@ -45,7 +45,7 @@ int vfs_init(char *root_mountpoint) return 1; } -size_t vfs_read(char *filename, char *buf) +size_t vfs_read(char *filename, char **buf) { if (boot_drive->fs->read == NULL) { debug("vfs_read(): Filesystem didn't set up a read function!"); diff --git a/boot/include/fs/uefi_sfs.h b/boot/include/fs/uefi_sfs.h index 791963a..97d7ad4 100644 --- a/boot/include/fs/uefi_sfs.h +++ b/boot/include/fs/uefi_sfs.h @@ -25,6 +25,6 @@ struct vfs_drive *sfs_init(char *mountpoint); -size_t sfs_read(char *filename, char *buffer, struct vfs_drive *dev, void *fsdata); +size_t sfs_read(char *filename, char **buffer, struct vfs_drive *dev, void *fsdata); #endif /* _FS_UEFI_SFS_H */ diff --git a/boot/include/vfs/vfs.h b/boot/include/vfs/vfs.h index 4002c89..507bf05 100644 --- a/boot/include/vfs/vfs.h +++ b/boot/include/vfs/vfs.h @@ -25,7 +25,7 @@ #include struct vfs_filesystem { - size_t (*read)(char *, char *, struct vfs_drive *, void *); + size_t (*read)(char *, char **, struct vfs_drive *, void *); uint8_t (*write)(char *, char *, size_t, struct vfs_drive *, void *); void *fsdata; @@ -40,7 +40,7 @@ int vfs_init(char *root_mountpoint); /* This function allocates `buf`. Passing a non-NULL value will result in an error. */ /* NOTE: Remember to free the allocated memory afterwards! */ -size_t vfs_read(char *filename, char *buf); +size_t vfs_read(char *filename, char **buf); int vfs_write(char *filename, char *buf, size_t len); /* Every platform will define this on its own */