rulimine/stage23/lib/uri.c

231 lines
5.8 KiB
C
Raw Normal View History

2020-11-02 11:20:34 +03:00
#include <stdint.h>
#include <stddef.h>
#include <lib/uri.h>
#include <lib/blib.h>
#include <lib/part.h>
#include <lib/libc.h>
#include <fs/file.h>
2020-11-05 03:37:45 +03:00
#include <mm/pmm.h>
#include <lib/print.h>
#include <pxe/tftp.h>
#include <tinf/tinf.h>
2020-11-02 11:20:34 +03:00
// A URI takes the form of: resource://root/path
// The following function splits up a URI into its componenets
bool uri_resolve(char *uri, char **resource, char **root, char **path) {
size_t length = strlen(uri) + 1;
char *buf = ext_mem_alloc(length);
memcpy(buf, uri, length);
uri = buf;
2020-11-02 11:20:34 +03:00
*resource = *root = *path = NULL;
// Get resource
for (size_t i = 0; ; i++) {
if (strlen(uri + i) < 3)
return false;
if (!memcmp(uri + i, "://", 3)) {
*resource = uri;
uri[i] = 0;
uri += i + 3;
break;
}
}
// Get root
for (size_t i = 0; ; i++) {
if (uri[i] == 0)
return false;
if (uri[i] == '/') {
*root = uri;
uri[i] = 0;
uri += i + 1;
break;
}
}
// Get path
if (*uri == 0)
return false;
*path = uri;
return true;
}
static bool parse_bios_partition(char *loc, int *drive, int *partition) {
2021-06-12 14:13:19 +03:00
uint64_t val;
2020-11-02 11:20:34 +03:00
for (size_t i = 0; ; i++) {
if (loc[i] == 0)
return false;
if (loc[i] == ':') {
loc[i] = 0;
if (*loc == 0) {
panic("Drive number cannot be omitted for hdd:// and odd://");
2020-11-02 11:20:34 +03:00
} else {
val = strtoui(loc, NULL, 10);
2021-06-12 14:13:19 +03:00
if (val < 1 || val > 256) {
panic("Drive number outside range 1-256");
2020-11-02 11:20:34 +03:00
}
*drive = val;
2020-11-02 11:20:34 +03:00
}
loc += i + 1;
break;
}
}
val = strtoui(loc, NULL, 10);
2021-06-12 14:13:19 +03:00
if (val > 256) {
panic("Partition number outside range 0-256");
2020-11-02 11:20:34 +03:00
}
2021-06-12 14:13:19 +03:00
*partition = val;
2020-11-02 11:20:34 +03:00
return true;
}
2021-10-21 02:27:05 +03:00
static struct file_handle *uri_hdd_dispatch(char *loc, char *path) {
int drive, partition;
2020-11-02 11:20:34 +03:00
if (!parse_bios_partition(loc, &drive, &partition))
2021-10-21 02:27:05 +03:00
return NULL;
2020-11-02 11:20:34 +03:00
2021-06-12 14:13:19 +03:00
struct volume *volume = volume_get_by_coord(false, drive, partition);
if (volume == NULL)
2021-10-21 02:27:05 +03:00
return NULL;
2021-10-21 02:27:05 +03:00
return fopen(volume, path);
}
2021-10-21 02:27:05 +03:00
static struct file_handle *uri_odd_dispatch(char *loc, char *path) {
int drive, partition;
if (!parse_bios_partition(loc, &drive, &partition))
2021-10-21 02:27:05 +03:00
return NULL;
2021-06-12 14:13:19 +03:00
struct volume *volume = volume_get_by_coord(true, drive, partition);
2021-03-04 11:15:10 +03:00
if (volume == NULL)
2021-10-21 02:27:05 +03:00
return NULL;
2020-11-02 11:20:34 +03:00
2021-10-21 02:27:05 +03:00
return fopen(volume, path);
2020-11-02 11:20:34 +03:00
}
2021-10-21 02:27:05 +03:00
static struct file_handle *uri_guid_dispatch(char *guid_str, char *path) {
2020-11-02 11:20:34 +03:00
struct guid guid;
if (!string_to_guid_be(&guid, guid_str))
2021-10-21 02:27:05 +03:00
return NULL;
2020-11-02 11:20:34 +03:00
2021-03-04 11:15:10 +03:00
struct volume *volume = volume_get_by_guid(&guid);
if (volume == NULL) {
if (!string_to_guid_mixed(&guid, guid_str))
2021-10-21 02:27:05 +03:00
return NULL;
2021-03-04 11:15:10 +03:00
volume = volume_get_by_guid(&guid);
if (volume == NULL)
2021-10-21 02:27:05 +03:00
return NULL;
}
2020-11-02 11:20:34 +03:00
2021-10-21 02:27:05 +03:00
return fopen(volume, path);
2020-11-02 11:20:34 +03:00
}
#if bios == 1
2021-10-21 02:27:05 +03:00
static struct file_handle *uri_tftp_dispatch(char *root, char *path) {
2020-11-05 03:37:45 +03:00
uint32_t ip;
if (!strcmp(root, "")) {
ip = 0;
} else {
if (inet_pton(root, &ip)) {
2021-08-12 08:40:29 +03:00
panic("tftp: Invalid ipv4 address: %s", root);
2020-11-05 03:37:45 +03:00
}
}
2021-10-21 02:27:05 +03:00
struct file_handle *ret = ext_mem_alloc(sizeof(struct file_handle));
if (!tftp_open(ret, ip, 69, path)) {
return NULL;
2020-11-05 03:37:45 +03:00
}
2021-10-21 02:27:05 +03:00
return ret;
2020-11-05 03:37:45 +03:00
}
2021-03-02 12:23:43 +03:00
#endif
2020-11-05 03:37:45 +03:00
2021-10-21 02:27:05 +03:00
static struct file_handle *uri_boot_dispatch(char *s_part, char *path) {
#if bios == 1
2021-03-13 11:08:01 +03:00
if (boot_volume->pxe)
2021-10-21 02:27:05 +03:00
return uri_tftp_dispatch(s_part, path);
2021-03-02 12:23:43 +03:00
#endif
2021-02-21 05:45:24 +03:00
int partition;
if (s_part[0] != '\0') {
uint64_t val = strtoui(s_part, NULL, 10);
2021-06-12 14:13:19 +03:00
if (val > 256) {
panic("Partition number outside range 0-256");
}
2021-06-12 14:13:19 +03:00
partition = val;
} else {
partition = boot_volume->partition;
}
2021-06-12 14:13:19 +03:00
struct volume *volume = volume_get_by_coord(boot_volume->is_optical,
boot_volume->index, partition);
2021-03-04 11:15:10 +03:00
if (volume == NULL)
2021-10-21 02:27:05 +03:00
return NULL;
2021-10-21 02:27:05 +03:00
return fopen(volume, path);
}
2021-10-21 02:27:05 +03:00
struct file_handle *uri_open(char *uri) {
struct file_handle *ret;
2020-11-02 11:20:34 +03:00
char *resource, *root, *path;
uri_resolve(uri, &resource, &root, &path);
2020-12-09 15:02:05 +03:00
if (resource == NULL) {
panic("No resource specified for URI `%s`.", uri);
}
bool compressed = false;
if (*resource == '$') {
compressed = true;
resource++;
}
if (!strcmp(resource, "bios")) {
panic("bios:// resource is no longer supported. Check CONFIG.md for hdd:// and odd://");
} else if (!strcmp(resource, "hdd")) {
2021-10-21 02:27:05 +03:00
ret = uri_hdd_dispatch(root, path);
} else if (!strcmp(resource, "odd")) {
2021-10-21 02:27:05 +03:00
ret = uri_odd_dispatch(root, path);
2020-12-09 15:02:05 +03:00
} else if (!strcmp(resource, "boot")) {
2021-10-21 02:27:05 +03:00
ret = uri_boot_dispatch(root, path);
2020-11-02 11:20:34 +03:00
} else if (!strcmp(resource, "guid")) {
2021-10-21 02:27:05 +03:00
ret = uri_guid_dispatch(root, path);
2020-12-10 09:22:06 +03:00
} else if (!strcmp(resource, "uuid")) {
2021-10-21 02:27:05 +03:00
ret = uri_guid_dispatch(root, path);
#if bios == 1
2020-11-05 03:37:45 +03:00
} else if (!strcmp(resource, "tftp")) {
2021-10-21 02:27:05 +03:00
ret = uri_tftp_dispatch(root, path);
2021-03-02 12:23:43 +03:00
#endif
2020-11-02 11:20:34 +03:00
} else {
panic("Resource `%s` not valid.", resource);
}
2021-10-21 02:27:05 +03:00
if (compressed && ret != NULL) {
struct file_handle *compressed_fd = ext_mem_alloc(sizeof(struct file_handle));
fread(ret, &compressed_fd->size, ret->size - 4, sizeof(uint32_t));
compressed_fd->fd = ext_mem_alloc(compressed_fd->size);
void *src = freadall(ret, MEMMAP_BOOTLOADER_RECLAIMABLE);
if (tinf_gzip_uncompress(compressed_fd->fd, src, ret->size))
panic("tinf error");
2021-10-21 02:27:05 +03:00
fclose(ret);
compressed_fd->is_memfile = true;
ret = compressed_fd;
}
return ret;
2020-11-02 11:20:34 +03:00
}