Experiment with loading Linux.

parent 972fbb0a
Pipeline #59318491 canceled with stages
in 21 minutes and 22 seconds
Subproject commit 87d3dfee24c355145c57e8b19163bc914e1ff7bd
Subproject commit 279992a8f9a23ddb2db5bcb9000c30874e4877a0
......@@ -3,6 +3,7 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <asmc_api.h>
struct gdt_entry {
unsigned int limit_low : 16;
......@@ -95,8 +96,404 @@ void load_linux_gdt() {
reload_gdt(gdt, 4);
}
typedef uint8_t __u8;
typedef uint16_t __u16;
typedef uint32_t __u32;
typedef uint64_t __u64;
// From include/uapi/linux/screen_info.h
struct screen_info {
__u8 orig_x; /* 0x00 */
__u8 orig_y; /* 0x01 */
__u16 ext_mem_k; /* 0x02 */
__u16 orig_video_page; /* 0x04 */
__u8 orig_video_mode; /* 0x06 */
__u8 orig_video_cols; /* 0x07 */
__u8 flags; /* 0x08 */
__u8 unused2; /* 0x09 */
__u16 orig_video_ega_bx;/* 0x0a */
__u16 unused3; /* 0x0c */
__u8 orig_video_lines; /* 0x0e */
__u8 orig_video_isVGA; /* 0x0f */
__u16 orig_video_points;/* 0x10 */
/* VESA graphic mode -- linear frame buffer */
__u16 lfb_width; /* 0x12 */
__u16 lfb_height; /* 0x14 */
__u16 lfb_depth; /* 0x16 */
__u32 lfb_base; /* 0x18 */
__u32 lfb_size; /* 0x1c */
__u16 cl_magic, cl_offset; /* 0x20 */
__u16 lfb_linelength; /* 0x24 */
__u8 red_size; /* 0x26 */
__u8 red_pos; /* 0x27 */
__u8 green_size; /* 0x28 */
__u8 green_pos; /* 0x29 */
__u8 blue_size; /* 0x2a */
__u8 blue_pos; /* 0x2b */
__u8 rsvd_size; /* 0x2c */
__u8 rsvd_pos; /* 0x2d */
__u16 vesapm_seg; /* 0x2e */
__u16 vesapm_off; /* 0x30 */
__u16 pages; /* 0x32 */
__u16 vesa_attributes; /* 0x34 */
__u32 capabilities; /* 0x36 */
__u32 ext_lfb_base; /* 0x3a */
__u8 _reserved[2]; /* 0x3e */
} __attribute__((packed));
#define VIDEO_TYPE_MDA 0x10 /* Monochrome Text Display */
#define VIDEO_TYPE_CGA 0x11 /* CGA Display */
#define VIDEO_TYPE_EGAM 0x20 /* EGA/VGA in Monochrome Mode */
#define VIDEO_TYPE_EGAC 0x21 /* EGA in Color Mode */
#define VIDEO_TYPE_VGAC 0x22 /* VGA+ in Color Mode */
#define VIDEO_TYPE_VLFB 0x23 /* VESA VGA in graphic mode */
#define VIDEO_TYPE_PICA_S3 0x30 /* ACER PICA-61 local S3 video */
#define VIDEO_TYPE_MIPS_G364 0x31 /* MIPS Magnum 4000 G364 video */
#define VIDEO_TYPE_SGI 0x33 /* Various SGI graphics hardware */
#define VIDEO_TYPE_TGAC 0x40 /* DEC TGA */
#define VIDEO_TYPE_SUN 0x50 /* Sun frame buffer. */
#define VIDEO_TYPE_SUNPCI 0x51 /* Sun PCI based frame buffer. */
#define VIDEO_TYPE_PMAC 0x60 /* PowerMacintosh frame buffer. */
#define VIDEO_TYPE_EFI 0x70 /* EFI graphic mode */
#define VIDEO_FLAGS_NOCURSOR (1 << 0) /* The video mode has no cursor set */
#define VIDEO_CAPABILITY_SKIP_QUIRKS (1 << 0)
#define VIDEO_CAPABILITY_64BIT_BASE (1 << 1) /* Frame buffer base is 64-bit */
// From include/uapi/linux/apm_bios.h
struct apm_bios_info {
__u16 version;
__u16 cseg;
__u32 offset;
__u16 cseg_16;
__u16 dseg;
__u16 flags;
__u16 cseg_len;
__u16 cseg_16_len;
__u16 dseg_len;
};
// From arch/x86/include/uapi/asm/ist.h
struct ist_info {
__u32 signature;
__u32 command;
__u32 event;
__u32 perf_level;
};
// From include/uapi/linux/edd.h
#define EDD_MBR_SIG_MAX 16
#define EDDMAXNR 6
struct edd_device_params {
__u16 length;
__u16 info_flags;
__u32 num_default_cylinders;
__u32 num_default_heads;
__u32 sectors_per_track;
__u64 number_of_sectors;
__u16 bytes_per_sector;
__u32 dpte_ptr; /* 0xFFFFFFFF for our purposes */
__u16 key; /* = 0xBEDD */
__u8 device_path_info_length; /* = 44 */
__u8 reserved2;
__u16 reserved3;
__u8 host_bus_type[4];
__u8 interface_type[8];
union {
struct {
__u16 base_address;
__u16 reserved1;
__u32 reserved2;
} __attribute__ ((packed)) isa;
struct {
__u8 bus;
__u8 slot;
__u8 function;
__u8 channel;
__u32 reserved;
} __attribute__ ((packed)) pci;
/* pcix is same as pci */
struct {
__u64 reserved;
} __attribute__ ((packed)) ibnd;
struct {
__u64 reserved;
} __attribute__ ((packed)) xprs;
struct {
__u64 reserved;
} __attribute__ ((packed)) htpt;
struct {
__u64 reserved;
} __attribute__ ((packed)) unknown;
} interface_path;
union {
struct {
__u8 device;
__u8 reserved1;
__u16 reserved2;
__u32 reserved3;
__u64 reserved4;
} __attribute__ ((packed)) ata;
struct {
__u8 device;
__u8 lun;
__u8 reserved1;
__u8 reserved2;
__u32 reserved3;
__u64 reserved4;
} __attribute__ ((packed)) atapi;
struct {
__u16 id;
__u64 lun;
__u16 reserved1;
__u32 reserved2;
} __attribute__ ((packed)) scsi;
struct {
__u64 serial_number;
__u64 reserved;
} __attribute__ ((packed)) usb;
struct {
__u64 eui;
__u64 reserved;
} __attribute__ ((packed)) i1394;
struct {
__u64 wwid;
__u64 lun;
} __attribute__ ((packed)) fibre;
struct {
__u64 identity_tag;
__u64 reserved;
} __attribute__ ((packed)) i2o;
struct {
__u32 array_number;
__u32 reserved1;
__u64 reserved2;
} __attribute__ ((packed)) raid;
struct {
__u8 device;
__u8 reserved1;
__u16 reserved2;
__u32 reserved3;
__u64 reserved4;
} __attribute__ ((packed)) sata;
struct {
__u64 reserved1;
__u64 reserved2;
} __attribute__ ((packed)) unknown;
} device_path;
__u8 reserved4;
__u8 checksum;
} __attribute__ ((packed));
struct edd_info {
__u8 device;
__u8 version;
__u16 interface_support;
__u16 legacy_max_cylinder;
__u8 legacy_max_head;
__u8 legacy_sectors_per_track;
struct edd_device_params params;
} __attribute__ ((packed));
// From include/uapi/video/edid.h
struct edid_info {
unsigned char dummy[128];
};
// From arch/x86/include/uapi/asm/bootparam.h
struct setup_header {
__u8 setup_sects;
__u16 root_flags;
__u32 syssize;
__u16 ram_size;
__u16 vid_mode;
__u16 root_dev;
__u16 boot_flag;
__u16 jump;
__u32 header;
__u16 version;
__u32 realmode_swtch;
__u16 start_sys_seg;
__u16 kernel_version;
__u8 type_of_loader;
__u8 loadflags;
__u16 setup_move_size;
__u32 code32_start;
__u32 ramdisk_image;
__u32 ramdisk_size;
__u32 bootsect_kludge;
__u16 heap_end_ptr;
__u8 ext_loader_ver;
__u8 ext_loader_type;
__u32 cmd_line_ptr;
__u32 initrd_addr_max;
__u32 kernel_alignment;
__u8 relocatable_kernel;
__u8 min_alignment;
__u16 xloadflags;
__u32 cmdline_size;
__u32 hardware_subarch;
__u64 hardware_subarch_data;
__u32 payload_offset;
__u32 payload_length;
__u64 setup_data;
__u64 pref_address;
__u32 init_size;
__u32 handover_offset;
} __attribute__((packed));
struct sys_desc_table {
__u16 length;
__u8 table[14];
};
struct olpc_ofw_header {
__u32 ofw_magic; /* OFW signature */
__u32 ofw_version;
__u32 cif_handler; /* callback into OFW */
__u32 irq_desc_table;
} __attribute__((packed));
struct efi_info {
__u32 efi_loader_signature;
__u32 efi_systab;
__u32 efi_memdesc_size;
__u32 efi_memdesc_version;
__u32 efi_memmap;
__u32 efi_memmap_size;
__u32 efi_systab_hi;
__u32 efi_memmap_hi;
};
#define E820_MAX_ENTRIES_ZEROPAGE 128
struct boot_e820_entry {
__u64 addr;
__u64 size;
__u32 type;
} __attribute__((packed));
struct boot_params {
struct screen_info screen_info; /* 0x000 */
struct apm_bios_info apm_bios_info; /* 0x040 */
__u8 _pad2[4]; /* 0x054 */
__u64 tboot_addr; /* 0x058 */
struct ist_info ist_info; /* 0x060 */
__u64 acpi_rsdp_addr; /* 0x070 */
__u8 _pad3[8]; /* 0x078 */
__u8 hd0_info[16]; /* obsolete! */ /* 0x080 */
__u8 hd1_info[16]; /* obsolete! */ /* 0x090 */
struct sys_desc_table sys_desc_table; /* obsolete! */ /* 0x0a0 */
struct olpc_ofw_header olpc_ofw_header; /* 0x0b0 */
__u32 ext_ramdisk_image; /* 0x0c0 */
__u32 ext_ramdisk_size; /* 0x0c4 */
__u32 ext_cmd_line_ptr; /* 0x0c8 */
__u8 _pad4[116]; /* 0x0cc */
struct edid_info edid_info; /* 0x140 */
struct efi_info efi_info; /* 0x1c0 */
__u32 alt_mem_k; /* 0x1e0 */
__u32 scratch; /* Scratch field! */ /* 0x1e4 */
__u8 e820_entries; /* 0x1e8 */
__u8 eddbuf_entries; /* 0x1e9 */
__u8 edd_mbr_sig_buf_entries; /* 0x1ea */
__u8 kbd_status; /* 0x1eb */
__u8 secure_boot; /* 0x1ec */
__u8 _pad5[2]; /* 0x1ed */
/*
* The sentinel is set to a nonzero value (0xff) in header.S.
*
* A bootloader is supposed to only take setup_header and put
* it into a clean boot_params buffer. If it turns out that
* it is clumsy or too generous with the buffer, it most
* probably will pick up the sentinel variable too. The fact
* that this variable then is still 0xff will let kernel
* know that some variables in boot_params are invalid and
* kernel should zero out certain portions of boot_params.
*/
__u8 sentinel; /* 0x1ef */
__u8 _pad6[1]; /* 0x1f0 */
struct setup_header hdr; /* setup header */ /* 0x1f1 */
__u8 _pad7[0x290-0x1f1-sizeof(struct setup_header)];
__u32 edd_mbr_sig_buffer[EDD_MBR_SIG_MAX]; /* 0x290 */
struct boot_e820_entry e820_table[E820_MAX_ENTRIES_ZEROPAGE]; /* 0x2d0 */
__u8 _pad8[48]; /* 0xcd0 */
struct edd_info eddbuf[EDDMAXNR]; /* 0xd00 */
__u8 _pad9[276]; /* 0xeec */
} __attribute__((packed));
void *read_all_file(FILE *fin, size_t **size) {
size_t buf_size = 16;
char *buf = malloc(buf_size);
size_t buf_pos = 0;
while (1) {
size_t res = fread(buf + buf_pos, 1, buf_size - buf_pos, fin);
buf_pos += res;
if (buf_pos < buf_size) {
break;
} else {
buf_size *= 2;
buf = realloc(buf, buf_size);
}
}
if (size) {
*size = buf_pos;
}
return buf;
}
//struct boot_params boot_params __attribute__((aligned(16)));
void prepare_boot_params(struct boot_params *real_mode) {
//memset(&boot_params, 0, sizeof(boot_params));
struct setup_header *hdr = &real_mode->hdr;
hdr->vid_mode = 0xffff;
hdr->type_of_loader = 0xff;
hdr->loadflags = 0x81;
hdr->ramdisk_image = 0;
hdr->ramdisk_size = 0;
hdr->heap_end_ptr = 0; // FIXME
hdr->cmd_line_ptr = 0; // FIXME
}
#define SECT_SIZE 512
void load_linux() {
download_file("http://10.0.2.2:8080/bzImage", "/ram/cont/bzImage");
printf("Loading Linux kernel\n");
FILE *fin = fopen("/ram/cont/bzImage", "rb");
struct boot_params *real_mode = malloc(SECT_SIZE);
size_t res = fread(real_mode, SECT_SIZE, 1, fin);
assert(res == 1);
if (real_mode->hdr.setup_sects == 0) {
real_mode->hdr.setup_sects = 4;
}
size_t real_mode_size = SECT_SIZE * (1 + real_mode->hdr.setup_sects);
printf("Real mode code/data is 0x%x bytes long\n", real_mode_size);
real_mode = realloc(real_mode, real_mode_size);
res = fread(((char*)real_mode) + SECT_SIZE, SECT_SIZE, real_mode->hdr.setup_sects, fin);
assert(res == real_mode->hdr.setup_sects);
assert(real_mode->hdr.boot_flag == 0xaa55);
assert(real_mode->hdr.header == 0x53726448);
size_t prot_mode_size;
void *prot_mode = read_all_file(fin, &prot_mode_size);
printf("Protected mode code/data is 0x%x bytes long\n", prot_mode_size);
fclose(fin);
prepare_boot_params(real_mode);
load_linux_gdt();
free(real_mode);
free(prot_mode);
}
int main() {
printf("Hello, continuing second world!\n");
load_linux_gdt();
//load_linux();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment