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>
|
2021-02-21 07:01:18 +03:00
|
|
|
#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) {
|
2020-11-10 13:12:56 +03:00
|
|
|
size_t length = strlen(uri) + 1;
|
2020-12-31 05:26:19 +03:00
|
|
|
char *buf = ext_mem_alloc(length);
|
2020-11-10 13:12:56 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (bios)
|
2020-11-02 11:20:34 +03:00
|
|
|
// BIOS partitions are specified in the <BIOS drive>:<partition> form.
|
|
|
|
// The drive may be omitted, the partition cannot.
|
|
|
|
static bool parse_bios_partition(char *loc, uint8_t *drive, uint8_t *partition) {
|
2020-11-05 14:50:42 +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) {
|
|
|
|
*drive = boot_drive;
|
|
|
|
} else {
|
2020-11-05 14:50:42 +03:00
|
|
|
val = strtoui(loc, NULL, 10);
|
|
|
|
if (val < 1 || val > 16) {
|
2020-11-02 11:20:34 +03:00
|
|
|
panic("BIOS drive number outside range 1-16");
|
|
|
|
}
|
2020-11-05 14:50:42 +03:00
|
|
|
*drive = (val - 1) + 0x80;
|
2020-11-02 11:20:34 +03:00
|
|
|
}
|
|
|
|
loc += i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*loc == 0)
|
|
|
|
return false;
|
|
|
|
|
2020-11-05 14:50:42 +03:00
|
|
|
val = strtoui(loc, NULL, 10);
|
|
|
|
if (val < 1 || val > 256) {
|
2020-11-02 11:20:34 +03:00
|
|
|
panic("BIOS partition number outside range 1-256");
|
|
|
|
}
|
2020-11-05 14:50:42 +03:00
|
|
|
*partition = val - 1;
|
2020-11-02 11:20:34 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool uri_bios_dispatch(struct file_handle *fd, char *loc, char *path) {
|
|
|
|
uint8_t drive, partition;
|
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
struct volume volume = {0};
|
2020-11-02 11:20:34 +03:00
|
|
|
if (!parse_bios_partition(loc, &drive, &partition))
|
|
|
|
return false;
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
if (!volume_get_by_coord(&volume, drive, partition))
|
2020-11-02 12:17:20 +03:00
|
|
|
return false;
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
if (fopen(fd, &volume, path))
|
2020-11-02 11:20:34 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-11-02 11:20:34 +03:00
|
|
|
|
|
|
|
static bool uri_guid_dispatch(struct file_handle *fd, char *guid_str, char *path) {
|
|
|
|
struct guid guid;
|
2021-01-17 18:36:17 +03:00
|
|
|
if (!string_to_guid_be(&guid, guid_str))
|
2020-11-02 11:20:34 +03:00
|
|
|
return false;
|
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
struct volume part = {0};
|
2021-02-06 16:40:55 +03:00
|
|
|
if (!volume_get_by_guid(&part, &guid)) {
|
2021-01-17 18:36:17 +03:00
|
|
|
if (!string_to_guid_mixed(&guid, guid_str))
|
|
|
|
return false;
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
if (!volume_get_by_guid(&part, &guid))
|
2021-01-17 18:36:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-02 11:20:34 +03:00
|
|
|
|
2020-11-02 12:17:20 +03:00
|
|
|
if (fopen(fd, &part, path))
|
2020-11-02 11:20:34 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (bios)
|
2020-11-05 03:37:45 +03:00
|
|
|
static bool uri_tftp_dispatch(struct file_handle *fd, char *root, char *path) {
|
|
|
|
uint32_t ip;
|
|
|
|
if (!strcmp(root, "")) {
|
|
|
|
ip = 0;
|
|
|
|
} else {
|
|
|
|
if (inet_pton(root, &ip)) {
|
|
|
|
panic("invalid ipv4 address: %s", root);
|
|
|
|
}
|
|
|
|
print("\nip: %x\n", ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tftp_file_handle *cfg = conv_mem_alloc(sizeof(struct tftp_file_handle));
|
|
|
|
if(tftp_open(cfg, ip, 69, path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:52:25 +03:00
|
|
|
fd->is_memfile = false;
|
2020-11-05 03:37:45 +03:00
|
|
|
fd->fd = cfg;
|
|
|
|
fd->read = tftp_read;
|
|
|
|
fd->size = cfg->file_size;
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-11-05 03:37:45 +03:00
|
|
|
|
2020-12-29 03:01:38 +03:00
|
|
|
static bool uri_boot_dispatch(struct file_handle *fd, char *s_part, char *path) {
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (bios)
|
2020-12-29 03:01:38 +03:00
|
|
|
if (booted_from_pxe)
|
|
|
|
return uri_tftp_dispatch(fd, s_part, path);
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-12-29 03:01:38 +03:00
|
|
|
|
2021-02-21 05:45:24 +03:00
|
|
|
int partition;
|
2020-12-29 03:01:38 +03:00
|
|
|
|
|
|
|
if (s_part[0] != '\0') {
|
|
|
|
uint64_t val = strtoui(s_part, NULL, 10);
|
|
|
|
if (val < 1 || val > 256) {
|
|
|
|
panic("Partition number outside range 1-256");
|
|
|
|
}
|
|
|
|
partition = val - 1;
|
2021-02-21 05:45:24 +03:00
|
|
|
} else if (booted_from_cd || boot_partition != -1) {
|
|
|
|
partition = boot_partition;
|
2020-12-29 03:01:38 +03:00
|
|
|
} else {
|
2021-02-21 05:45:24 +03:00
|
|
|
panic("Boot partition information is unavailable.");
|
2020-12-29 03:01:38 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
struct volume part = {0};
|
2021-02-06 16:40:55 +03:00
|
|
|
if (!volume_get_by_coord(&part, boot_drive, partition))
|
2020-12-29 03:01:38 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (fopen(fd, &part, path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:20:34 +03:00
|
|
|
bool uri_open(struct file_handle *fd, char *uri) {
|
2020-12-27 02:27:47 +03:00
|
|
|
bool 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);
|
|
|
|
}
|
|
|
|
|
2020-12-27 02:27:47 +03:00
|
|
|
bool compressed = false;
|
|
|
|
if (*resource == '$') {
|
|
|
|
compressed = true;
|
|
|
|
resource++;
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
if (0) {
|
|
|
|
#if defined (bios)
|
|
|
|
} else if (!strcmp(resource, "bios")) {
|
2020-12-27 02:27:47 +03:00
|
|
|
ret = uri_bios_dispatch(fd, root, path);
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-12-09 15:02:05 +03:00
|
|
|
} else if (!strcmp(resource, "boot")) {
|
2020-12-27 02:27:47 +03:00
|
|
|
ret = uri_boot_dispatch(fd, root, path);
|
2020-11-02 11:20:34 +03:00
|
|
|
} else if (!strcmp(resource, "guid")) {
|
2020-12-27 02:27:47 +03:00
|
|
|
ret = uri_guid_dispatch(fd, root, path);
|
2020-12-10 09:22:06 +03:00
|
|
|
} else if (!strcmp(resource, "uuid")) {
|
2020-12-27 02:27:47 +03:00
|
|
|
ret = uri_guid_dispatch(fd, root, path);
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (bios)
|
2020-11-05 03:37:45 +03:00
|
|
|
} else if (!strcmp(resource, "tftp")) {
|
2020-12-27 02:27:47 +03:00
|
|
|
ret = uri_tftp_dispatch(fd, 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);
|
|
|
|
}
|
2020-12-27 02:27:47 +03:00
|
|
|
|
|
|
|
if (compressed && ret) {
|
|
|
|
struct file_handle compressed_fd = {0};
|
|
|
|
fread(fd, &compressed_fd.size, fd->size - 4, sizeof(uint32_t));
|
2021-01-03 01:05:30 +03:00
|
|
|
compressed_fd.fd = ext_mem_alloc_aligned(compressed_fd.size, 4096);
|
2020-12-27 02:27:47 +03:00
|
|
|
void *src = ext_mem_alloc(fd->size);
|
|
|
|
fread(fd, src, 0, fd->size);
|
|
|
|
if (tinf_gzip_uncompress(compressed_fd.fd, src, fd->size))
|
|
|
|
panic("tinf error");
|
2021-01-02 23:44:27 +03:00
|
|
|
compressed_fd.is_memfile = true;
|
2020-12-27 02:27:47 +03:00
|
|
|
*fd = compressed_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2020-11-02 11:20:34 +03:00
|
|
|
}
|