2020-04-14 06:20:55 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fs/file.h>
|
2020-06-05 20:33:51 +03:00
|
|
|
#include <fs/ext2.h>
|
2020-05-01 18:19:29 +03:00
|
|
|
#include <fs/fat32.h>
|
2021-02-21 05:45:24 +03:00
|
|
|
#include <fs/iso9660.h>
|
2021-09-03 11:03:44 +03:00
|
|
|
#include <fs/ntfs.h>
|
2021-03-04 00:38:28 +03:00
|
|
|
#include <lib/print.h>
|
2020-04-14 06:20:55 +03:00
|
|
|
#include <lib/blib.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-11-01 23:25:35 +03:00
|
|
|
#include <lib/part.h>
|
2021-01-02 23:44:27 +03:00
|
|
|
#include <lib/libc.h>
|
2021-03-13 11:08:01 +03:00
|
|
|
#include <pxe/tftp.h>
|
2020-11-01 23:25:35 +03:00
|
|
|
|
2022-07-02 11:43:10 +03:00
|
|
|
char *fs_get_label(struct volume *part) {
|
|
|
|
if (fat32_check_signature(part)) {
|
|
|
|
return fat32_get_label(part);
|
|
|
|
}
|
|
|
|
if (ext2_check_signature(part)) {
|
|
|
|
return ext2_get_label(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
bool fs_get_guid(struct guid *guid, struct volume *part) {
|
2020-11-01 23:25:35 +03:00
|
|
|
if (ext2_check_signature(part)) {
|
|
|
|
return ext2_get_guid(guid, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-14 06:20:55 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *fopen(struct volume *part, const char *filename) {
|
|
|
|
struct file_handle *ret = ext_mem_alloc(sizeof(struct file_handle));
|
|
|
|
|
2021-01-03 01:52:25 +03:00
|
|
|
ret->is_memfile = false;
|
2021-10-21 02:27:05 +03:00
|
|
|
ret->readall = false;
|
2021-01-03 01:52:25 +03:00
|
|
|
|
2021-11-24 14:24:17 +03:00
|
|
|
ret->vol = part;
|
|
|
|
|
2022-03-26 10:36:53 +03:00
|
|
|
if (strlen(filename) + 2 > PATH_MAX) {
|
|
|
|
panic(true, "fopen: Path too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->path[0] = '/';
|
|
|
|
|
|
|
|
strcpy(ret->path + 1, filename);
|
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if bios == 1
|
2021-03-13 11:08:01 +03:00
|
|
|
if (part->pxe) {
|
2021-10-21 02:27:05 +03:00
|
|
|
if (!tftp_open(ret, 0, 69, filename)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return ret;
|
2021-03-13 11:08:01 +03:00
|
|
|
}
|
2021-03-13 11:45:17 +03:00
|
|
|
#endif
|
2021-03-13 11:08:01 +03:00
|
|
|
|
2021-09-27 05:04:15 +03:00
|
|
|
#if uefi == 1
|
|
|
|
ret->efi_part_handle = part->efi_part_handle;
|
|
|
|
#endif
|
|
|
|
|
2021-02-25 03:40:02 +03:00
|
|
|
if (iso9660_check_signature(part)) {
|
2021-02-21 05:45:24 +03:00
|
|
|
struct iso9660_file_handle *fd = ext_mem_alloc(sizeof(struct iso9660_file_handle));
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if (!iso9660_open(fd, part, filename)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-02-21 05:45:24 +03:00
|
|
|
|
|
|
|
ret->fd = (void *)fd;
|
|
|
|
ret->read = (void *)iso9660_read;
|
2021-10-21 02:27:05 +03:00
|
|
|
ret->close = (void *)iso9660_close;
|
2021-02-21 05:45:24 +03:00
|
|
|
ret->size = fd->size;
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
return ret;
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
|
|
|
|
2020-11-02 12:17:20 +03:00
|
|
|
if (ext2_check_signature(part)) {
|
2020-12-31 05:26:19 +03:00
|
|
|
struct ext2_file_handle *fd = ext_mem_alloc(sizeof(struct ext2_file_handle));
|
2020-04-15 09:48:35 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if (!ext2_open(fd, part, filename)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-04-15 09:48:35 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
ret->fd = (void *)fd;
|
2020-11-02 12:17:20 +03:00
|
|
|
ret->read = (void *)ext2_read;
|
2021-10-21 02:27:05 +03:00
|
|
|
ret->close = (void *)ext2_close;
|
2020-11-02 12:17:20 +03:00
|
|
|
ret->size = fd->size;
|
2020-04-15 09:48:35 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
return ret;
|
2020-04-15 09:48:35 +03:00
|
|
|
}
|
2020-04-14 06:20:55 +03:00
|
|
|
|
2020-11-02 12:17:20 +03:00
|
|
|
if (fat32_check_signature(part)) {
|
2020-12-31 05:26:19 +03:00
|
|
|
struct fat32_file_handle *fd = ext_mem_alloc(sizeof(struct fat32_file_handle));
|
2020-05-01 18:19:29 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if (!fat32_open(fd, part, filename)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-05-01 18:19:29 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
ret->fd = (void *)fd;
|
2020-11-02 12:17:20 +03:00
|
|
|
ret->read = (void *)fat32_read;
|
2021-10-21 02:27:05 +03:00
|
|
|
ret->close = (void *)fat32_close;
|
2020-11-02 12:17:20 +03:00
|
|
|
ret->size = fd->size_bytes;
|
2020-05-01 18:19:29 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
return ret;
|
2020-05-01 18:19:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:03:44 +03:00
|
|
|
if (ntfs_check_signature(part)) {
|
|
|
|
struct ntfs_file_handle *fd = ext_mem_alloc(sizeof(struct ntfs_file_handle));
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if (!ntfs_open(fd, part, filename)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-03 11:03:44 +03:00
|
|
|
|
|
|
|
ret->fd = (void *)fd;
|
|
|
|
ret->read = (void *)ntfs_read;
|
2022-01-21 17:39:18 +03:00
|
|
|
ret->close = (void *)ntfs_close;
|
2021-09-03 11:03:44 +03:00
|
|
|
ret->size = fd->size_bytes;
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
return ret;
|
2021-09-03 11:03:44 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
fail:
|
|
|
|
pmm_free(ret, sizeof(struct file_handle));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fclose(struct file_handle *fd) {
|
|
|
|
if (fd->is_memfile) {
|
|
|
|
if (fd->readall == false) {
|
|
|
|
pmm_free(fd->fd, fd->size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd->close(fd->fd);
|
|
|
|
}
|
|
|
|
pmm_free(fd, sizeof(struct file_handle));
|
2020-04-14 06:20:55 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
void fread(struct file_handle *fd, void *buf, uint64_t loc, uint64_t count) {
|
2021-01-02 23:44:27 +03:00
|
|
|
if (fd->is_memfile) {
|
|
|
|
memcpy(buf, fd->fd + loc, count);
|
|
|
|
} else {
|
2021-10-21 02:27:05 +03:00
|
|
|
fd->read(fd->fd, buf, loc, count);
|
2021-01-02 23:44:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *freadall(struct file_handle *fd, uint32_t type) {
|
|
|
|
if (fd->is_memfile) {
|
2022-03-26 11:27:09 +03:00
|
|
|
if (fd->readall) {
|
|
|
|
return fd->fd;
|
|
|
|
}
|
2021-05-06 05:31:05 +03:00
|
|
|
memmap_alloc_range((uint64_t)(size_t)fd->fd, ALIGN_UP(fd->size, 4096), type, false, true, false, false);
|
2021-10-21 02:27:05 +03:00
|
|
|
fd->readall = true;
|
2021-01-02 23:44:27 +03:00
|
|
|
return fd->fd;
|
|
|
|
} else {
|
2021-04-15 03:21:38 +03:00
|
|
|
void *ret = ext_mem_alloc_type(fd->size, type);
|
2021-10-21 02:27:05 +03:00
|
|
|
fd->read(fd->fd, ret, 0, fd->size);
|
2021-01-02 23:44:27 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2020-04-14 06:20:55 +03:00
|
|
|
}
|