2021-02-21 05:45:24 +03:00
|
|
|
#include <fs/iso9660.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2021-02-21 05:45:24 +03:00
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <mm/pmm.h>
|
|
|
|
|
2022-07-04 21:16:33 +03:00
|
|
|
#define ISO9660_SECTOR_SIZE (2 << 10)
|
|
|
|
|
|
|
|
struct iso9660_context {
|
|
|
|
struct volume *vol;
|
|
|
|
void *root;
|
|
|
|
uint32_t root_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct iso9660_file_handle {
|
|
|
|
struct iso9660_context *context;
|
|
|
|
uint32_t LBA;
|
|
|
|
uint32_t size;
|
|
|
|
};
|
|
|
|
|
2021-02-21 05:45:24 +03:00
|
|
|
#define ISO9660_FIRST_VOLUME_DESCRIPTOR 0x10
|
|
|
|
#define ISO9660_VOLUME_DESCRIPTOR_SIZE ISO9660_SECTOR_SIZE
|
|
|
|
#define ROCK_RIDGE_MAX_FILENAME 255
|
|
|
|
|
|
|
|
// --- Both endian structures ---
|
|
|
|
struct BE16_t { uint16_t little, big; } __attribute__((packed));
|
|
|
|
struct BE32_t { uint32_t little, big; } __attribute__((packed));
|
|
|
|
|
|
|
|
// --- Directory entries ---
|
|
|
|
struct iso9660_directory_entry {
|
|
|
|
uint8_t length;
|
|
|
|
uint8_t extended_attribute_length;
|
|
|
|
struct BE32_t extent;
|
|
|
|
struct BE32_t extent_size;
|
|
|
|
uint8_t datetime[7];
|
|
|
|
uint8_t flags;
|
|
|
|
uint8_t interleaved_unit_size;
|
|
|
|
uint8_t interleaved_gap_size;
|
|
|
|
struct BE16_t volume_seq;
|
|
|
|
uint8_t filename_size;
|
2021-03-20 13:52:53 +03:00
|
|
|
char name[];
|
2021-02-21 05:45:24 +03:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
// --- Volume descriptors ---
|
|
|
|
// VDT = Volume Descriptor Type
|
|
|
|
enum {
|
|
|
|
ISO9660_VDT_BOOT_RECORD,
|
|
|
|
ISO9660_VDT_PRIMARY,
|
|
|
|
ISO9660_VDT_SUPPLEMENTARY,
|
|
|
|
ISO9660_VDT_PARTITION_DESCRIPTOR,
|
|
|
|
ISO9660_VDT_TERMINATOR = 255
|
|
|
|
};
|
|
|
|
|
|
|
|
struct iso9660_volume_descriptor {
|
|
|
|
uint8_t type;
|
|
|
|
char identifier[5];
|
|
|
|
uint8_t version;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct iso9660_primary_volume {
|
2021-05-28 17:05:42 +03:00
|
|
|
struct iso9660_volume_descriptor volume_descriptor;
|
2021-03-01 22:43:27 +03:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint8_t unused0[1];
|
|
|
|
char system_identifier[32];
|
|
|
|
char volume_identifier[32];
|
|
|
|
uint8_t unused1[8];
|
|
|
|
struct BE32_t space_size;
|
|
|
|
uint8_t unused2[32];
|
|
|
|
struct BE16_t set_size;
|
|
|
|
struct BE16_t volume_seq;
|
|
|
|
struct BE16_t LBA_size;
|
|
|
|
struct BE32_t path_table_size;
|
|
|
|
|
|
|
|
uint32_t LBA_path_table_little;
|
|
|
|
uint32_t LBA_optional_path_table_little;
|
|
|
|
uint32_t LBA_path_table_big;
|
|
|
|
uint32_t LBA_optional_path_table_big;
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2021-03-01 22:43:27 +03:00
|
|
|
struct iso9660_directory_entry root;
|
|
|
|
} __attribute__((packed));
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2021-03-01 22:43:27 +03:00
|
|
|
uint8_t padding[2041];
|
|
|
|
};
|
2021-02-21 05:45:24 +03:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
|
|
|
|
// --- Implementation ---
|
2021-02-22 20:37:30 +03:00
|
|
|
struct iso9660_contexts_node {
|
|
|
|
struct iso9660_context context;
|
|
|
|
struct iso9660_contexts_node *next;
|
|
|
|
};
|
2021-10-21 02:27:05 +03:00
|
|
|
|
|
|
|
static struct iso9660_contexts_node *contexts = NULL;
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2021-02-25 03:40:02 +03:00
|
|
|
static void iso9660_find_PVD(struct iso9660_volume_descriptor *desc, struct volume *vol) {
|
2021-02-21 05:45:24 +03:00
|
|
|
uint32_t lba = ISO9660_FIRST_VOLUME_DESCRIPTOR;
|
|
|
|
while (true) {
|
|
|
|
volume_read(vol, desc, lba * ISO9660_SECTOR_SIZE, ISO9660_SECTOR_SIZE);
|
|
|
|
|
|
|
|
switch (desc->type) {
|
|
|
|
case ISO9660_VDT_PRIMARY:
|
|
|
|
return;
|
|
|
|
case ISO9660_VDT_TERMINATOR:
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(false, "ISO9660: no primary volume descriptor");
|
2021-02-21 05:45:24 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++lba;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 03:40:02 +03:00
|
|
|
static void iso9660_cache_root(struct volume *vol,
|
2021-02-22 08:14:27 +03:00
|
|
|
void **root,
|
|
|
|
uint32_t *root_size) {
|
2021-02-21 05:45:24 +03:00
|
|
|
struct iso9660_primary_volume pv;
|
|
|
|
iso9660_find_PVD((struct iso9660_volume_descriptor *)&pv, vol);
|
|
|
|
|
2021-02-22 08:14:27 +03:00
|
|
|
*root_size = pv.root.extent_size.little;
|
|
|
|
*root = ext_mem_alloc(*root_size);
|
|
|
|
volume_read(vol, *root, pv.root.extent.little * ISO9660_SECTOR_SIZE, *root_size);
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-25 03:40:02 +03:00
|
|
|
static struct iso9660_context *iso9660_get_context(struct volume *vol) {
|
2021-02-22 20:37:30 +03:00
|
|
|
struct iso9660_contexts_node *current = contexts;
|
|
|
|
while (current) {
|
2021-03-04 11:15:10 +03:00
|
|
|
if (current->context.vol == vol)
|
2021-02-22 20:37:30 +03:00
|
|
|
return ¤t->context;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The context is not cached at this point
|
|
|
|
struct iso9660_contexts_node *node = ext_mem_alloc(sizeof(struct iso9660_contexts_node));
|
2021-03-04 11:15:10 +03:00
|
|
|
node->context.vol = vol;
|
2021-02-22 20:37:30 +03:00
|
|
|
iso9660_cache_root(vol, &node->context.root, &node->context.root_size);
|
|
|
|
|
|
|
|
node->next = contexts;
|
|
|
|
contexts = node;
|
|
|
|
return &node->context;
|
|
|
|
}
|
|
|
|
|
2021-06-12 09:03:49 +03:00
|
|
|
static bool load_name(char *buf, struct iso9660_directory_entry *entry) {
|
2021-03-20 13:52:53 +03:00
|
|
|
unsigned char* sysarea = ((unsigned char*)entry) + sizeof(struct iso9660_directory_entry) + entry->filename_size;
|
|
|
|
int sysarea_len = entry->length - sizeof(struct iso9660_directory_entry) - entry->filename_size;
|
|
|
|
if ((entry->filename_size & 0x1) == 0) {
|
|
|
|
sysarea++;
|
|
|
|
sysarea_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rrnamelen = 0;
|
|
|
|
while ((sysarea_len >= 4) && ((sysarea[3] == 1) || (sysarea[2] == 2))) {
|
|
|
|
if (sysarea[0] == 'N' && sysarea[1] == 'M') {
|
|
|
|
rrnamelen = sysarea[2] - 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sysarea_len -= sysarea[2];
|
|
|
|
sysarea += sysarea[2];
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
|
|
|
|
2021-03-20 13:52:53 +03:00
|
|
|
size_t name_len = 0;
|
|
|
|
if (rrnamelen) {
|
|
|
|
/* rock ridge naming scheme */
|
|
|
|
name_len = rrnamelen;
|
|
|
|
memcpy(buf, sysarea + 5, name_len);
|
|
|
|
buf[name_len] = 0;
|
2021-06-12 09:03:49 +03:00
|
|
|
return true;
|
2021-03-20 13:52:53 +03:00
|
|
|
} else {
|
|
|
|
name_len = entry->filename_size;
|
|
|
|
size_t j;
|
|
|
|
for (j = 0; j < name_len; j++) {
|
|
|
|
if (entry->name[j] == ';')
|
|
|
|
break;
|
|
|
|
if (entry->name[j] == '.' && entry->name[j+1] == ';')
|
|
|
|
break;
|
2021-06-22 13:36:45 +03:00
|
|
|
buf[j] = entry->name[j];
|
2021-03-20 13:52:53 +03:00
|
|
|
}
|
|
|
|
buf[j] = 0;
|
2021-06-12 09:03:49 +03:00
|
|
|
return false;
|
2021-03-20 13:52:53 +03:00
|
|
|
}
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
|
|
|
|
2021-06-12 09:03:49 +03:00
|
|
|
static struct iso9660_directory_entry *iso9660_find(void *buffer, uint32_t size, const char *filename) {
|
2021-02-21 05:45:24 +03:00
|
|
|
while (size) {
|
|
|
|
struct iso9660_directory_entry *entry = buffer;
|
2021-03-20 13:52:53 +03:00
|
|
|
|
|
|
|
if (entry->length == 0) {
|
|
|
|
if (size <= ISO9660_SECTOR_SIZE)
|
|
|
|
return NULL;
|
|
|
|
size_t prev_size = size;
|
|
|
|
size = ALIGN_DOWN(size, ISO9660_SECTOR_SIZE);
|
|
|
|
buffer += prev_size - size;
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2021-06-12 09:03:49 +03:00
|
|
|
char entry_filename[128];
|
|
|
|
bool rr = load_name(entry_filename, entry);
|
|
|
|
|
2022-09-01 15:02:53 +03:00
|
|
|
if (rr && !case_insensitive_fopen) {
|
2021-06-12 09:03:49 +03:00
|
|
|
if (strcmp(filename, entry_filename) == 0) {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcasecmp(filename, entry_filename) == 0) {
|
|
|
|
return buffer;
|
|
|
|
}
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
2021-03-20 13:52:53 +03:00
|
|
|
|
|
|
|
size -= entry->length;
|
|
|
|
buffer += entry->length;
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-04 21:16:33 +03:00
|
|
|
static void iso9660_read(struct file_handle *handle, void *buf, uint64_t loc, uint64_t count);
|
|
|
|
static void iso9660_close(struct file_handle *file);
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2022-07-04 21:16:33 +03:00
|
|
|
struct file_handle *iso9660_open(struct volume *vol, const char *path) {
|
2021-02-21 05:45:24 +03:00
|
|
|
char buf[6];
|
|
|
|
const uint64_t signature = ISO9660_FIRST_VOLUME_DESCRIPTOR * ISO9660_SECTOR_SIZE + 1;
|
|
|
|
volume_read(vol, buf, signature, 5);
|
|
|
|
buf[5] = '\0';
|
2022-07-04 21:16:33 +03:00
|
|
|
if (strcmp(buf, "CD001") != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iso9660_file_handle *ret = ext_mem_alloc(sizeof(struct iso9660_file_handle));
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2021-02-22 20:37:30 +03:00
|
|
|
ret->context = iso9660_get_context(vol);
|
2021-02-21 05:45:24 +03:00
|
|
|
|
|
|
|
while (*path == '/')
|
|
|
|
++path;
|
|
|
|
|
2021-02-22 20:37:30 +03:00
|
|
|
struct iso9660_directory_entry *current = ret->context->root;
|
|
|
|
uint32_t current_size = ret->context->root_size;
|
2021-02-21 05:45:24 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
bool first = true;
|
|
|
|
|
2021-02-21 05:45:24 +03:00
|
|
|
uint32_t next_sector = 0;
|
|
|
|
uint32_t next_size = 0;
|
|
|
|
|
|
|
|
char filename[ROCK_RIDGE_MAX_FILENAME];
|
|
|
|
while (true) {
|
|
|
|
char *aux = filename;
|
|
|
|
while (!(*path == '/' || *path == '\0'))
|
|
|
|
*aux++ = *path++;
|
|
|
|
*aux = '\0';
|
|
|
|
|
|
|
|
struct iso9660_directory_entry *entry = iso9660_find(current, current_size, filename);
|
2022-07-04 21:16:33 +03:00
|
|
|
if (!entry) {
|
|
|
|
pmm_free(ret, sizeof(struct iso9660_file_handle));
|
|
|
|
return NULL; // Not found :(
|
|
|
|
}
|
2021-02-21 05:45:24 +03:00
|
|
|
|
|
|
|
next_sector = entry->extent.little;
|
|
|
|
next_size = entry->extent_size.little;
|
|
|
|
|
|
|
|
if (*path++ == '\0')
|
|
|
|
break; // Found :)
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if (!first) {
|
|
|
|
pmm_free(current, current_size);
|
|
|
|
}
|
|
|
|
|
2021-02-21 05:45:24 +03:00
|
|
|
current_size = next_size;
|
|
|
|
current = ext_mem_alloc(current_size);
|
2021-10-21 02:27:05 +03:00
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
2021-02-21 05:45:24 +03:00
|
|
|
volume_read(vol, current, next_sector * ISO9660_SECTOR_SIZE, current_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->LBA = next_sector;
|
|
|
|
ret->size = next_size;
|
2022-07-04 21:16:33 +03:00
|
|
|
|
|
|
|
struct file_handle *handle = ext_mem_alloc(sizeof(struct file_handle));
|
|
|
|
|
|
|
|
handle->fd = ret;
|
|
|
|
handle->read = (void *)iso9660_read;
|
|
|
|
handle->close = (void *)iso9660_close;
|
|
|
|
handle->size = ret->size;
|
|
|
|
handle->vol = vol;
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2022-07-04 21:16:33 +03:00
|
|
|
handle->efi_part_handle = vol->efi_part_handle;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return handle;
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 21:16:33 +03:00
|
|
|
static void iso9660_read(struct file_handle *file, void *buf, uint64_t loc, uint64_t count) {
|
|
|
|
struct iso9660_file_handle *f = file->fd;
|
|
|
|
volume_read(f->context->vol, buf, f->LBA * ISO9660_SECTOR_SIZE + loc, count);
|
2021-10-21 02:27:05 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 21:16:33 +03:00
|
|
|
static void iso9660_close(struct file_handle *file) {
|
|
|
|
pmm_free(file->fd, sizeof(struct iso9660_file_handle));
|
2021-02-21 05:45:24 +03:00
|
|
|
}
|