2020-05-06 18:00:41 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <protos/chainload.h>
|
|
|
|
#include <lib/part.h>
|
|
|
|
#include <lib/config.h>
|
|
|
|
#include <lib/blib.h>
|
|
|
|
#include <drivers/disk.h>
|
2020-09-02 10:55:56 +03:00
|
|
|
#include <lib/term.h>
|
2021-05-06 05:31:05 +03:00
|
|
|
#include <lib/fb.h>
|
|
|
|
#include <lib/uri.h>
|
|
|
|
#include <lib/print.h>
|
2021-07-07 06:54:40 +03:00
|
|
|
#include <lib/libc.h>
|
2021-03-17 21:12:31 +03:00
|
|
|
#include <sys/idt.h>
|
2021-04-04 05:51:55 +03:00
|
|
|
#include <drivers/vga_textmode.h>
|
2021-05-06 05:31:05 +03:00
|
|
|
#include <mm/pmm.h>
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-05-06 05:31:05 +03:00
|
|
|
# include <efi.h>
|
|
|
|
#endif
|
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if bios == 1
|
2020-05-06 18:00:41 +03:00
|
|
|
|
2021-03-17 21:12:31 +03:00
|
|
|
__attribute__((noinline))
|
|
|
|
__attribute__((section(".realmode")))
|
2020-09-25 23:57:57 +03:00
|
|
|
static void spinup(uint8_t drive) {
|
2021-05-29 19:07:28 +03:00
|
|
|
struct idtr real_mode_idt;
|
|
|
|
real_mode_idt.limit = 0x3ff;
|
|
|
|
real_mode_idt.ptr = 0;
|
2021-03-17 21:12:31 +03:00
|
|
|
|
2020-09-16 18:22:05 +03:00
|
|
|
asm volatile (
|
2021-03-17 21:12:31 +03:00
|
|
|
"cli\n\t"
|
2020-09-26 01:28:36 +03:00
|
|
|
"cld\n\t"
|
|
|
|
|
2021-05-29 19:07:28 +03:00
|
|
|
"lidt (%%eax)\n\t"
|
2021-03-17 21:12:31 +03:00
|
|
|
|
2021-05-29 19:07:28 +03:00
|
|
|
"pushl $0x08\n\t"
|
|
|
|
"pushl $1f\n\t"
|
|
|
|
"lret\n\t"
|
2020-05-06 18:00:41 +03:00
|
|
|
"1: .code16\n\t"
|
2021-05-29 19:07:28 +03:00
|
|
|
"movw $0x10, %%ax\n\t"
|
|
|
|
"movw %%ax, %%ds\n\t"
|
|
|
|
"movw %%ax, %%es\n\t"
|
|
|
|
"movw %%ax, %%fs\n\t"
|
|
|
|
"movw %%ax, %%gs\n\t"
|
|
|
|
"movw %%ax, %%ss\n\t"
|
|
|
|
"movl %%cr0, %%eax\n\t"
|
|
|
|
"andb $0xfe, %%al\n\t"
|
|
|
|
"movl %%eax, %%cr0\n\t"
|
|
|
|
"movl $1f, %%eax\n\t"
|
|
|
|
"pushw $0\n\t"
|
|
|
|
"pushw %%ax\n\t"
|
|
|
|
"lret\n\t"
|
2020-08-27 01:44:16 +03:00
|
|
|
"1:\n\t"
|
2021-05-29 19:07:28 +03:00
|
|
|
"xorw %%ax, %%ax\n\t"
|
|
|
|
"movw %%ax, %%ds\n\t"
|
|
|
|
"movw %%ax, %%es\n\t"
|
|
|
|
"movw %%ax, %%fs\n\t"
|
|
|
|
"movw %%ax, %%gs\n\t"
|
|
|
|
"movw %%ax, %%ss\n\t"
|
2020-09-26 01:28:36 +03:00
|
|
|
|
|
|
|
"sti\n\t"
|
|
|
|
|
2021-05-29 19:07:28 +03:00
|
|
|
"pushw $0\n\t"
|
|
|
|
"pushw $0x7c00\n\t"
|
|
|
|
"lret\n\t"
|
2020-09-26 01:28:36 +03:00
|
|
|
|
2020-09-16 18:22:05 +03:00
|
|
|
".code32\n\t"
|
|
|
|
:
|
2021-03-17 21:12:31 +03:00
|
|
|
: "a" (&real_mode_idt), "d" (drive)
|
2020-07-24 16:38:55 +03:00
|
|
|
: "memory"
|
2020-05-06 18:00:41 +03:00
|
|
|
);
|
|
|
|
}
|
2020-09-25 23:57:57 +03:00
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
void chainload(char *config) {
|
2020-11-05 14:50:42 +03:00
|
|
|
uint64_t val;
|
|
|
|
|
2020-09-25 23:57:57 +03:00
|
|
|
int part; {
|
2020-11-27 21:33:34 +03:00
|
|
|
char *part_config = config_get_value(config, 0, "PARTITION");
|
|
|
|
if (part_config == NULL) {
|
2021-06-12 14:13:19 +03:00
|
|
|
part = 0;
|
2020-09-25 23:57:57 +03:00
|
|
|
} else {
|
2020-11-27 21:33:34 +03:00
|
|
|
val = strtoui(part_config, NULL, 10);
|
2021-06-12 14:13:19 +03:00
|
|
|
if (val > 256) {
|
|
|
|
panic("BIOS partition number outside range 0-256");
|
2020-11-02 00:07:10 +03:00
|
|
|
}
|
2021-06-12 14:13:19 +03:00
|
|
|
part = val;
|
2020-09-25 23:57:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int drive; {
|
2020-11-27 21:33:34 +03:00
|
|
|
char *drive_config = config_get_value(config, 0, "DRIVE");
|
|
|
|
if (drive_config == NULL) {
|
2020-09-25 23:57:57 +03:00
|
|
|
panic("DRIVE not specified");
|
|
|
|
}
|
2020-11-27 21:33:34 +03:00
|
|
|
val = strtoui(drive_config, NULL, 10);
|
2021-06-12 14:13:19 +03:00
|
|
|
if (val < 1 || val > 256) {
|
|
|
|
panic("BIOS drive number outside range 1-256");
|
2020-11-02 00:07:10 +03:00
|
|
|
}
|
2021-06-12 14:13:19 +03:00
|
|
|
drive = val;
|
2020-09-25 23:57:57 +03:00
|
|
|
}
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t rows, cols;
|
2021-04-04 05:51:55 +03:00
|
|
|
init_vga_textmode(&rows, &cols, false);
|
2020-09-25 23:57:57 +03:00
|
|
|
|
2021-06-12 14:13:19 +03:00
|
|
|
struct volume *p = volume_get_by_coord(false, drive, part);
|
2020-09-25 23:57:57 +03:00
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
volume_read(p, (void *)0x7c00, 0, 512);
|
2020-09-25 23:57:57 +03:00
|
|
|
|
|
|
|
spinup(drive);
|
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#elif uefi == 1
|
2021-05-06 05:31:05 +03:00
|
|
|
|
|
|
|
void chainload(char *config) {
|
|
|
|
EFI_STATUS status;
|
|
|
|
|
|
|
|
char *image_path = config_get_value(config, 0, "IMAGE_PATH");
|
|
|
|
if (image_path == NULL)
|
|
|
|
panic("chainload: IMAGE_PATH not specified");
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *image;
|
|
|
|
if ((image = uri_open(image_path)) == NULL)
|
2021-06-30 19:44:31 +03:00
|
|
|
panic("chainload: Failed to open image with path `%s`. Is the path correct?", image_path);
|
2021-05-06 05:31:05 +03:00
|
|
|
|
2021-09-27 05:04:15 +03:00
|
|
|
EFI_HANDLE efi_part_handle = image->efi_part_handle;
|
|
|
|
|
2021-07-07 06:54:40 +03:00
|
|
|
void *_ptr = freadall(image, MEMMAP_RESERVED);
|
2021-05-12 13:53:06 +03:00
|
|
|
size_t image_size = image->size;
|
2021-07-07 06:54:40 +03:00
|
|
|
void *ptr;
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->AllocatePool(EfiLoaderData, image_size, &ptr);
|
2021-07-07 06:54:40 +03:00
|
|
|
if (status)
|
|
|
|
panic("chainload: Allocation failure");
|
|
|
|
memcpy(ptr, _ptr, image_size);
|
2021-05-06 05:31:05 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
pmm_free(_ptr, image->size);
|
|
|
|
fclose(image);
|
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
term_deinit();
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t req_width = 0, req_height = 0, req_bpp = 0;
|
2021-05-06 05:31:05 +03:00
|
|
|
|
|
|
|
char *resolution = config_get_value(config, 0, "RESOLUTION");
|
|
|
|
if (resolution != NULL)
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, resolution);
|
|
|
|
|
|
|
|
struct fb_info fbinfo;
|
|
|
|
if (!fb_init(&fbinfo, req_width, req_height, req_bpp))
|
|
|
|
panic("chainload: Unable to set video mode");
|
|
|
|
|
|
|
|
pmm_release_uefi_mem();
|
|
|
|
|
|
|
|
MEMMAP_DEVICE_PATH memdev_path[2];
|
|
|
|
|
|
|
|
memdev_path[0].Header.Type = HARDWARE_DEVICE_PATH;
|
|
|
|
memdev_path[0].Header.SubType = HW_MEMMAP_DP;
|
|
|
|
memdev_path[0].Header.Length[0] = sizeof(MEMMAP_DEVICE_PATH);
|
|
|
|
memdev_path[0].Header.Length[1] = sizeof(MEMMAP_DEVICE_PATH) >> 8;
|
|
|
|
|
|
|
|
memdev_path[0].MemoryType = EfiLoaderData;
|
|
|
|
memdev_path[0].StartingAddress = (uintptr_t)ptr;
|
2021-05-12 13:53:06 +03:00
|
|
|
memdev_path[0].EndingAddress = (uintptr_t)ptr + image_size;
|
2021-05-06 05:31:05 +03:00
|
|
|
|
|
|
|
memdev_path[1].Header.Type = END_DEVICE_PATH_TYPE;
|
|
|
|
memdev_path[1].Header.SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
|
2021-05-12 13:53:06 +03:00
|
|
|
memdev_path[1].Header.Length[0] = sizeof(EFI_DEVICE_PATH);
|
|
|
|
memdev_path[1].Header.Length[1] = sizeof(EFI_DEVICE_PATH) >> 8;
|
2021-05-06 05:31:05 +03:00
|
|
|
|
|
|
|
EFI_HANDLE new_handle = 0;
|
|
|
|
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->LoadImage(0, efi_image_handle,
|
|
|
|
(EFI_DEVICE_PATH *)memdev_path,
|
|
|
|
ptr, image_size, &new_handle);
|
2021-05-06 05:31:05 +03:00
|
|
|
if (status) {
|
|
|
|
panic("chainload: LoadImage failure (%x)", status);
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:53:06 +03:00
|
|
|
// Apparently we need to make sure that the DeviceHandle field is the same
|
|
|
|
// as us (the loader) for some EFI images to properly work (Windows for instance)
|
|
|
|
EFI_GUID loaded_img_prot_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
|
|
|
|
|
|
|
|
EFI_LOADED_IMAGE_PROTOCOL *new_handle_loaded_image = NULL;
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->HandleProtocol(new_handle, &loaded_img_prot_guid,
|
|
|
|
(void **)&new_handle_loaded_image);
|
2021-05-12 13:53:06 +03:00
|
|
|
if (status) {
|
2021-05-20 02:16:39 +03:00
|
|
|
panic("chainload: HandleProtocol failure (%x)", status);
|
2021-05-12 13:53:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:04:15 +03:00
|
|
|
if (efi_part_handle != 0) {
|
|
|
|
new_handle_loaded_image->DeviceHandle = efi_part_handle;
|
|
|
|
}
|
2021-05-12 13:53:06 +03:00
|
|
|
|
2021-07-06 10:59:49 +03:00
|
|
|
UINTN exit_data_size = 0;
|
|
|
|
CHAR16 *exit_data = NULL;
|
2021-08-25 23:03:00 +03:00
|
|
|
EFI_STATUS exit_status = gBS->StartImage(new_handle, &exit_data_size, &exit_data);
|
2021-07-06 10:59:49 +03:00
|
|
|
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->Exit(efi_image_handle, exit_status, exit_data_size, exit_data);
|
2021-05-06 05:31:05 +03:00
|
|
|
if (status) {
|
2021-07-06 10:59:49 +03:00
|
|
|
panic("chainload: Exit failure (%x)", status);
|
2021-05-06 05:31:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|