2020-04-14 06:20:55 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fs/file.h>
|
2022-11-30 03:48:00 +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-03-04 00:38:28 +03:00
|
|
|
#include <lib/print.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.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) {
|
2022-07-04 21:16:33 +03:00
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if ((ret = fat32_get_label(part)) != NULL) {
|
|
|
|
return ret;
|
2022-07-02 11:43:10 +03:00
|
|
|
}
|
2022-11-30 03:48:00 +03:00
|
|
|
if ((ret = ext2_get_label(part)) != NULL) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-02 11:43:10 +03:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
bool fs_get_guid(struct guid *guid, struct volume *part) {
|
2022-11-30 03:48:00 +03:00
|
|
|
if (ext2_get_guid(guid, part) == true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:25:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-14 06:20:55 +03:00
|
|
|
|
2022-09-01 15:02:53 +03:00
|
|
|
bool case_insensitive_fopen = false;
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *fopen(struct volume *part, const char *filename) {
|
2022-07-07 11:08:55 +03:00
|
|
|
size_t filename_new_len = strlen(filename) + 2;
|
|
|
|
char *filename_new = ext_mem_alloc(filename_new_len);
|
|
|
|
|
|
|
|
if (filename[0] != '/') {
|
|
|
|
filename_new[0] = '/';
|
|
|
|
strcpy(&filename_new[1], filename);
|
|
|
|
} else {
|
|
|
|
strcpy(filename_new, filename);
|
2022-03-26 10:36:53 +03:00
|
|
|
}
|
|
|
|
|
2022-07-07 11:08:55 +03:00
|
|
|
filename = filename_new;
|
|
|
|
|
2022-07-04 21:16:33 +03:00
|
|
|
struct file_handle *ret;
|
2022-03-26 10:36:53 +03:00
|
|
|
|
2021-03-13 11:08:01 +03:00
|
|
|
if (part->pxe) {
|
2023-01-17 19:40:43 +03:00
|
|
|
if ((ret = tftp_open(part, "", filename)) == NULL) {
|
2022-07-04 21:16:33 +03:00
|
|
|
return NULL;
|
2021-10-21 02:27:05 +03:00
|
|
|
}
|
2022-07-07 11:08:55 +03:00
|
|
|
goto success;
|
2021-03-13 11:08:01 +03:00
|
|
|
}
|
|
|
|
|
2022-11-30 03:48:00 +03:00
|
|
|
if ((ret = ext2_open(part, filename)) != NULL) {
|
|
|
|
goto success;
|
|
|
|
}
|
2022-07-04 21:16:33 +03:00
|
|
|
if ((ret = iso9660_open(part, filename)) != NULL) {
|
2022-07-07 11:08:55 +03:00
|
|
|
goto success;
|
2020-05-01 18:19:29 +03:00
|
|
|
}
|
2022-07-09 13:42:34 +03:00
|
|
|
if ((ret = fat32_open(part, filename)) != NULL) {
|
2022-07-07 11:08:55 +03:00
|
|
|
goto success;
|
2021-09-03 11:03:44 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
return NULL;
|
2022-07-07 11:08:55 +03:00
|
|
|
|
|
|
|
success:
|
|
|
|
ret->path = (char *)filename;
|
|
|
|
|
|
|
|
return ret;
|
2021-10-21 02:27:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void fclose(struct file_handle *fd) {
|
|
|
|
if (fd->is_memfile) {
|
|
|
|
if (fd->readall == false) {
|
|
|
|
pmm_free(fd->fd, fd->size);
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-04 21:16:33 +03:00
|
|
|
fd->close(fd);
|
2021-10-21 02:27:05 +03:00
|
|
|
}
|
2022-10-14 01:59:16 +03:00
|
|
|
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 {
|
2022-07-04 21:16:33 +03:00
|
|
|
fd->read(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;
|
|
|
|
}
|
2022-09-28 02:11:26 +03:00
|
|
|
memmap_alloc_range((uint64_t)(size_t)fd->fd, ALIGN_UP(fd->size, 4096), type, 0, 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);
|
2022-07-04 21:16:33 +03:00
|
|
|
fd->read(fd, ret, 0, fd->size);
|
2022-10-14 01:59:16 +03:00
|
|
|
fd->close(fd);
|
|
|
|
fd->fd = ret;
|
|
|
|
fd->readall = true;
|
|
|
|
fd->is_memfile = true;
|
2021-01-02 23:44:27 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2020-04-14 06:20:55 +03:00
|
|
|
}
|