| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870 |
- /* NebOS UEFI loader (x86-64).
- *
- * The kernel is a 32-bit i586 image linked at 0x7E00. This loader gives it
- * the same two boot structures the real-mode VESA stage produces:
- *
- * 0x500 struct boot_info (EFI flag, GOP pixel order, ACPI RSDP)
- * 0x600 struct vga_boot_fb_info
- * 0x1000 struct e820_map
- *
- * then leaves long mode and jumps to kernel_entry with paging off and flat
- * 32-bit segments. Nothing in the legacy boot chain is used here.
- */
- #include "efi.h"
- #include "bootinfo.h"
- #include "phys.h"
- #include "vga.h"
- #include "efi_entry.h"
- #define MAX_PHYS ((UINT32)MM_MAX_PHYS)
- #define PAGE_SIZE_ 0x1000u
- #define MAX_PAGES (MAX_PHYS / PAGE_SIZE_)
- #define E820_ADDR 0x1000u
- /* The kernel image is linked to live at 0x7E00. Its file image must stay
- * below the legacy pack staging gap at 0x60000 (kernel.ld asserts
- * __bss_start <= 0x60000); KERNEL_MAX is a generous sanity bound. */
- #define KERNEL_DEST 0x7E00u
- #define KERNEL_MAX 0x70000u
- /* Trampoline copy address: below the kernel, same low-memory slot kexec uses
- * (trampoline at 0x800, flat GDT inside the blob, stack at 0x7C00). */
- #define TRAMP_ADDR 0x800u
- #define TRAMP_MAX 0x800u
- extern unsigned char kernel_image_start[];
- extern unsigned char kernel_image_end[];
- extern unsigned char pack_image_start[];
- extern unsigned char pack_image_end[];
- extern unsigned char tramp_blob_start[];
- extern unsigned char tramp_blob_end[];
- /* GetMemoryMap buffer. A firmware map is normally a few KB; 64 KB is very
- * generous and keeps the retry loop simple. */
- static UINT8 memmap_buf[65536] __attribute__((aligned(8)));
- /* One bit per 4 KB page over the kernel's 1 GB identity window. Starts
- * all-used; only EfiConventional/BootServices memory is freed. EfiLoader*
- * (this image and the staging buffer) stays reserved, which is safe and
- * costs only a few hundred KB. */
- static UINT8 page_used[MAX_PAGES / 8];
- struct fb_saved {
- int valid;
- int rgbx;
- UINT32 width;
- UINT32 height;
- UINT32 pitch;
- UINT32 bpp;
- UINT64 base;
- UINT64 size;
- };
- struct e820_entry_disk {
- UINT64 base;
- UINT64 length;
- UINT32 type;
- UINT32 attrs;
- } __attribute__((packed));
- /* ---- serial console (COM1, 115200 8N1) --------------------------------- */
- static inline void outb(UINT16 port, UINT8 v)
- {
- __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
- }
- static inline UINT8 inb(UINT16 port)
- {
- UINT8 v;
- __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
- return v;
- }
- static void com1_init(void)
- {
- outb(0x3F9, 0x00); /* IER */
- outb(0x3FB, 0x80); /* LCR: DLAB */
- outb(0x3F8, 0x01); /* DLL: 115200 */
- outb(0x3F9, 0x00); /* DLM */
- outb(0x3FB, 0x03); /* LCR: 8N1 */
- outb(0x3FA, 0xC7); /* FIFO */
- outb(0x3FC, 0x0B); /* MCR: DTR/RTS/OUT2 */
- }
- static void com1_putc(char c)
- {
- unsigned int guard = 100000;
- while (!(inb(0x3FD) & 0x20) && guard--)
- ;
- outb(0x3F8, (UINT8)c);
- }
- static void com1_puts(const char *s)
- {
- while (*s) {
- if (*s == '\n')
- com1_putc('\r');
- com1_putc(*s++);
- }
- }
- static void com1_hex(UINT64 v, unsigned int digits)
- {
- static const char h[] = "0123456789ABCDEF";
- unsigned int i;
- for (i = digits; i > 0; i--)
- com1_putc(h[(v >> (4 * (i - 1))) & 0xF]);
- }
- static void com1_dec(UINT32 v)
- {
- char buf[11];
- unsigned int i = 0;
- if (v == 0) {
- com1_putc('0');
- return;
- }
- while (v) {
- buf[i++] = (char)('0' + v % 10);
- v /= 10;
- }
- while (i)
- com1_putc(buf[--i]);
- }
- static void conout_puts(EFI_SYSTEM_TABLE *st, const char *s)
- {
- CHAR16 buf[128];
- unsigned int i = 0;
- if (!st || !st->ConOut || !st->ConOut->OutputString)
- return;
- while (*s) {
- if (*s == '\n')
- buf[i++] = '\r';
- buf[i++] = (CHAR16)(UINT8)*s++;
- if (i >= 126) {
- buf[i] = 0;
- st->ConOut->OutputString(st->ConOut, buf);
- i = 0;
- }
- }
- if (i) {
- buf[i] = 0;
- st->ConOut->OutputString(st->ConOut, buf);
- }
- }
- static void halt_forever(void) __attribute__((noreturn));
- static void halt_forever(void)
- {
- for (;;)
- __asm__ volatile ("cli; hlt");
- }
- static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
- __attribute__((noreturn));
- static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
- {
- com1_puts("nebos-efi: ");
- com1_puts(msg);
- com1_puts("\n");
- conout_puts(st, "NebOS EFI loader: ");
- conout_puts(st, msg);
- conout_puts(st, "\n");
- halt_forever();
- }
- static void fail_after(const char *msg) __attribute__((noreturn));
- static void fail_after(const char *msg)
- {
- com1_puts("nebos-efi: ");
- com1_puts(msg);
- com1_puts("\n");
- halt_forever();
- }
- /* ---- helpers ----------------------------------------------------------- */
- static int guid_eq(const EFI_GUID *a, const EFI_GUID *b)
- {
- const UINT8 *pa = (const UINT8 *)a;
- const UINT8 *pb = (const UINT8 *)b;
- unsigned int i;
- for (i = 0; i < 16; i++)
- if (pa[i] != pb[i])
- return 0;
- return 1;
- }
- static UINT32 find_rsdp(EFI_SYSTEM_TABLE *st)
- {
- UINTN i;
- if (!st->ConfigurationTable)
- return 0;
- for (i = 0; i < st->NumberOfTableEntries; i++) {
- EFI_CONFIGURATION_TABLE *ct = &st->ConfigurationTable[i];
- UINT64 addr;
- if (!guid_eq(&ct->VendorGuid, &efi_acpi20_guid) &&
- !guid_eq(&ct->VendorGuid, &efi_acpi10_guid))
- continue;
- addr = (UINT64)(UINTN)ct->VendorTable;
- if (addr && addr <= 0xFFFFFFFFull)
- return (UINT32)addr;
- }
- return 0;
- }
- /* ---- GOP --------------------------------------------------------------- */
- static int gop_init(EFI_SYSTEM_TABLE *st, EFI_GRAPHICS_OUTPUT_PROTOCOL **out_gop,
- EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *out_info)
- {
- EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
- EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
- EFI_STATUS status;
- UINTN size;
- UINT32 i, best = 0, best_score = 0;
- UINT64 fb_base, fb_size;
- status = st->BootServices->LocateProtocol((EFI_GUID *)&efi_gop_guid,
- 0, (void **)&gop);
- if (status || !gop || !gop->Mode)
- return 0;
- for (i = 0; i < gop->Mode->MaxMode; i++) {
- UINT64 bytes;
- UINT32 score;
- status = gop->QueryMode(gop, i, &size, &info);
- if (status || !info)
- continue;
- if (info->PixelFormat != EFI_PIXEL_BGRX &&
- info->PixelFormat != EFI_PIXEL_RGBX)
- continue;
- if (info->HorizontalResolution < 320 ||
- info->VerticalResolution < 200)
- continue;
- if (info->HorizontalResolution > 1920 ||
- info->VerticalResolution > 65535)
- continue;
- if (info->PixelsPerScanLine < info->HorizontalResolution)
- continue;
- if (info->PixelsPerScanLine > 0x3FFF) /* pitch must fit u16 */
- continue;
- bytes = (UINT64)info->PixelsPerScanLine * 4 *
- info->VerticalResolution;
- if (bytes > 8ull * 1024 * 1024)
- continue;
- score = info->HorizontalResolution * info->VerticalResolution;
- if (info->PixelFormat == EFI_PIXEL_BGRX)
- score++; /* prefer BGRX on equal area */
- if (score > best_score) {
- best_score = score;
- best = i;
- }
- }
- if (!best_score)
- return 0;
- status = gop->SetMode(gop, best);
- if (status)
- return 0;
- info = gop->Mode->Info;
- if (!info)
- return 0;
- if (info->PixelFormat != EFI_PIXEL_BGRX &&
- info->PixelFormat != EFI_PIXEL_RGBX)
- return 0;
- if (info->HorizontalResolution < 320 ||
- info->HorizontalResolution > 1920 ||
- info->VerticalResolution < 200 ||
- info->VerticalResolution > 65535 ||
- info->PixelsPerScanLine < info->HorizontalResolution ||
- info->PixelsPerScanLine > 0x3FFF)
- return 0;
- {
- UINT64 bytes = (UINT64)info->PixelsPerScanLine * 4 *
- info->VerticalResolution;
- if (bytes > 8ull * 1024 * 1024)
- return 0;
- }
- fb_base = gop->Mode->FrameBufferBase;
- fb_size = gop->Mode->FrameBufferSize;
- if (!fb_base || fb_base > 0xFFFFFFFFull)
- return 0;
- if (fb_base + fb_size > 0x100000000ull)
- return 0;
- out_info->Version = info->Version;
- out_info->HorizontalResolution = info->HorizontalResolution;
- out_info->VerticalResolution = info->VerticalResolution;
- out_info->PixelFormat = info->PixelFormat;
- out_info->PixelInformation.RedMask = info->PixelInformation.RedMask;
- out_info->PixelInformation.GreenMask = info->PixelInformation.GreenMask;
- out_info->PixelInformation.BlueMask = info->PixelInformation.BlueMask;
- out_info->PixelInformation.ReservedMask =
- info->PixelInformation.ReservedMask;
- out_info->PixelsPerScanLine = info->PixelsPerScanLine;
- *out_gop = gop;
- return 1;
- }
- /* ---- E820 conversion --------------------------------------------------- */
- static inline int page_is_used(UINT32 p)
- {
- return (page_used[p >> 3] >> (p & 7)) & 1;
- }
- static inline void page_set_used(UINT32 p)
- {
- page_used[p >> 3] |= (UINT8)(1u << (p & 7));
- }
- static inline void page_set_free(UINT32 p)
- {
- page_used[p >> 3] &= (UINT8)~(1u << (p & 7));
- }
- /* Free only pages fully inside [base, base+len). */
- static void free_range(UINT64 base, UINT64 len)
- {
- UINT64 end;
- UINT32 p0, p1, p;
- if (base >= MAX_PHYS)
- return;
- end = base + len;
- if (end > MAX_PHYS)
- end = MAX_PHYS;
- p0 = (UINT32)((base + PAGE_SIZE_ - 1) / PAGE_SIZE_);
- p1 = (UINT32)(end / PAGE_SIZE_);
- for (p = p0; p < p1; p++)
- page_set_free(p);
- }
- /* Reserve every page touched by [base, base+len). */
- static void use_range(UINT64 base, UINT64 len)
- {
- UINT64 end;
- UINT32 p0, p1, p;
- if (base >= MAX_PHYS)
- return;
- end = base + len;
- if (end > MAX_PHYS)
- end = MAX_PHYS;
- p0 = (UINT32)(base / PAGE_SIZE_);
- p1 = (UINT32)((end + PAGE_SIZE_ - 1) / PAGE_SIZE_);
- for (p = p0; p < p1; p++)
- page_set_used(p);
- }
- static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
- UINTN desc_size, const struct fb_saved *fb)
- {
- volatile UINT32 *hdr = (volatile UINT32 *)E820_ADDR;
- volatile struct e820_entry_disk *entries =
- (volatile struct e820_entry_disk *)(E820_ADDR + 8);
- UINTN n = desc_size ? map_size / desc_size : 0;
- __asm__ volatile ("" : "+r"(hdr), "+r"(entries));
- UINTN i;
- UINT32 p, count = 0;
- UINT64 total = 0;
- for (i = 0; i < sizeof(page_used); i++)
- page_used[i] = 0xFF;
- for (i = 0; i < n; i++) {
- EFI_MEMORY_DESCRIPTOR *d =
- (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)map + i * desc_size);
- UINT64 len = d->NumberOfPages * PAGE_SIZE_;
- if (d->Type == EFI_CONVENTIONAL_MEMORY ||
- d->Type == EFI_BOOT_SERVICES_CODE ||
- d->Type == EFI_BOOT_SERVICES_DATA)
- free_range(d->PhysicalStart, len);
- }
- /* The scanout is MMIO, not RAM; make sure it can never be handed out
- * even when its BAR happens to sit below the 1 GB ceiling. */
- if (fb->valid)
- use_range(fb->base, fb->size);
- for (p = 0; p < MAX_PAGES; ) {
- UINT32 start;
- if (page_is_used(p)) {
- p++;
- continue;
- }
- start = p;
- while (p < MAX_PAGES && !page_is_used(p))
- p++;
- if (count < 32) {
- UINT64 len = (UINT64)(p - start) * PAGE_SIZE_;
- entries[count].base = (UINT64)start * PAGE_SIZE_;
- entries[count].length = len;
- entries[count].type = 1; /* E820_RAM */
- entries[count].attrs = 0;
- count++;
- total += len;
- }
- }
- hdr[0] = 0xE820E820u;
- hdr[1] = count;
- com1_puts("nebos-efi: E820 ");
- com1_dec(count);
- com1_puts(" entries, ");
- com1_dec((UINT32)(total >> 20));
- com1_puts(" MB usable\n");
- }
- /* ---- boot structures --------------------------------------------------- */
- /* The rootfs pack used to be copied to the top of usable RAM after
- * ExitBootServices. That walks into firmware code pages that OVMF maps
- * read-only (BootServicesCode is still listed as free memory), so the copy
- * faulted once the pack grew. Allocate a loader-data buffer instead:
- * firmware-owned pages are always writable to the loader. */
- static void write_bootinfo(UINT32 rsdp, int have_fb, int rgbx,
- UINT32 pack_addr, UINT32 pack_size)
- {
- volatile struct boot_info *bi =
- (volatile struct boot_info *)BOOT_INFO_ADDR;
- UINT32 words[BOOT_INFO_WORDS];
- unsigned int i;
- UINT32 sum = 0;
- __asm__ volatile ("" : "+r"(bi));
- for (i = 0; i < BOOT_INFO_WORDS; i++)
- words[i] = 0;
- words[0] = BOOT_INFO_MAGIC;
- words[1] = BOOT_INFO_VERSION;
- words[3] = rsdp;
- words[4] = BOOTINFO_FLAG_EFI |
- ((have_fb && rgbx) ? BOOTINFO_FLAG_RGBX : 0);
- words[5] = pack_addr;
- words[6] = pack_size;
- for (i = 0; i < BOOT_INFO_WORDS; i++)
- sum += words[i];
- words[2] = 0u - sum; /* the sum over the block is zero */
- bi->magic = words[0];
- bi->version = words[1];
- bi->checksum = words[2];
- bi->rsdp = words[3];
- bi->flags = words[4];
- bi->pack_addr = words[5];
- bi->pack_size = words[6];
- for (i = 0; i < 5; i++)
- bi->reserved[i] = words[7 + i];
- }
- static void write_fbinfo(const struct fb_saved *fb)
- {
- volatile struct vga_boot_fb_info *info = VGA_BOOT_FB;
- __asm__ volatile ("" : "+r"(info));
- if (!fb->valid) {
- info->valid = 0;
- info->width = 0;
- info->height = 0;
- info->pitch = 0;
- info->bpp = 0;
- info->lfb_base = 0;
- info->vram_64k = 0;
- return;
- }
- info->valid = 1;
- info->width = (unsigned short)fb->width;
- info->height = (unsigned short)fb->height;
- info->pitch = (unsigned short)fb->pitch;
- info->bpp = (unsigned char)fb->bpp;
- info->lfb_base = (unsigned int)fb->base;
- {
- /* GOP reports the mode buffer size, not total VRAM. Round up
- * so the kernel's VBE-era height clamp (vram_64k * 64K /
- * pitch) cannot shave rows off a 1920x1080 frame. */
- UINT64 blocks = (fb->size + 0xFFFFull) >> 16;
- info->vram_64k = (unsigned short)(blocks > 0xFFFF ? 0xFFFF :
- blocks);
- }
- }
- static void copy_trampoline(void)
- {
- UINT32 n = (UINT32)(tramp_blob_end - tramp_blob_start);
- UINT8 *dst = (UINT8 *)(UINTN)TRAMP_ADDR;
- UINT32 i;
- if (n == 0 || n > TRAMP_MAX)
- fail_after("trampoline blob size invalid");
- __asm__ volatile ("" : "+r"(dst));
- for (i = 0; i < n; i++)
- dst[i] = tramp_blob_start[i];
- com1_puts("nebos-efi: trampoline ");
- com1_dec(n);
- com1_puts(" bytes at 0x");
- com1_hex(TRAMP_ADDR, 3);
- com1_puts("\n");
- }
- /* ---- disk boot image ---------------------------------------------------- */
- static UINT8 io_buf[512] __attribute__((aligned(4096)));
- static UINT32 rd16(const UINT8 *p)
- {
- return (UINT32)p[0] | ((UINT32)p[1] << 8);
- }
- static UINT32 rd32(const UINT8 *p)
- {
- return (UINT32)p[0] | ((UINT32)p[1] << 8) |
- ((UINT32)p[2] << 16) | ((UINT32)p[3] << 24);
- }
- static int disk_read(EFI_BLOCK_IO_PROTOCOL *bio, UINT64 lba, void *buf,
- UINT32 bytes)
- {
- if (!bio || !bio->Media || !bytes || (bytes & 511u))
- return -1;
- if (bio->Media->BlockSize != 512)
- return -1;
- if (lba > bio->Media->LastBlock)
- return -1;
- if ((UINT64)(bytes / 512u) > bio->Media->LastBlock - lba + 1)
- return -1;
- if (bio->ReadBlocks(bio, bio->Media->MediaId, lba, bytes, buf))
- return -1;
- return 0;
- }
- /* The ESP is a partition; the kernel and rootfs live at absolute disk LBAs,
- * so strip the HD node and locate the parent whole-disk Block I/O handle. */
- static EFI_BLOCK_IO_PROTOCOL *disk_block_io(EFI_HANDLE image,
- EFI_BOOT_SERVICES *bs)
- {
- static UINT8 path[512];
- EFI_LOADED_IMAGE_PROTOCOL *li = 0;
- EFI_DEVICE_PATH_PROTOCOL *dp = 0, *p, *last = 0, *tp;
- EFI_BLOCK_IO_PROTOCOL *bio = 0;
- EFI_HANDLE handle = 0;
- UINTN len = 0, total = 0;
- UINT32 i;
- EFI_STATUS st;
- if (bs->HandleProtocol(image, (EFI_GUID *)&efi_loaded_image_guid,
- (void **)&li) || !li || !li->DeviceHandle)
- return 0;
- if (bs->HandleProtocol(li->DeviceHandle,
- (EFI_GUID *)&efi_device_path_guid,
- (void **)&dp) || !dp)
- return 0;
- p = dp;
- for (;;) {
- UINTN nl = (UINTN)p->Length[0] | ((UINTN)p->Length[1] << 8);
- if (p->Type == 0x7F && p->SubType == 0xFF)
- break;
- if (nl < 4 || total + nl > sizeof(path))
- return 0;
- last = p;
- total += nl;
- p = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)p + nl);
- }
- if (!last || last->Type != 0x04 || last->SubType != 0x01)
- return 0;
- len = (UINTN)((UINT8 *)last - (UINT8 *)dp);
- if (len + 4 > sizeof(path))
- return 0;
- for (i = 0; i < len; i++)
- path[i] = ((UINT8 *)dp)[i];
- path[len] = 0x7F;
- path[len + 1] = 0xFF;
- path[len + 2] = 4;
- path[len + 3] = 0;
- tp = (EFI_DEVICE_PATH_PROTOCOL *)path;
- st = bs->LocateDevicePath((EFI_GUID *)&efi_block_io_guid, &tp, &handle);
- if (st || !handle)
- return 0;
- if (bs->HandleProtocol(handle, (EFI_GUID *)&efi_block_io_guid,
- (void **)&bio) || !bio || !bio->Media)
- return 0;
- if (bio->Media->LogicalPartition || bio->Media->BlockSize != 512)
- return 0;
- return bio;
- }
- /* Read P1's boot sector and P2's NBROOT1 header. Returns the kernel size in
- * bytes and the pack size, or 0 when this is not a NebOS layout. */
- static int disk_sizes(EFI_BLOCK_IO_PROTOCOL *bio, UINT32 *kernel_lba,
- UINT32 *kernel_bytes, UINT32 *pack_lba,
- UINT32 *pack_bytes)
- {
- UINT32 ks, kl, pl, pb;
- if (disk_read(bio, 63, io_buf, 512) != 0)
- return 0;
- if (io_buf[510] != 0x55 || io_buf[511] != 0xAA)
- return 0;
- if (io_buf[3] != 'N' || io_buf[4] != 'E' || io_buf[5] != 'B' ||
- io_buf[6] != 'O' || io_buf[7] != 'S')
- return 0;
- ks = rd16(io_buf + 0x3E);
- kl = rd16(io_buf + 0x40);
- pl = rd32(io_buf + 0x42);
- if (!ks || kl < 1 || pl <= kl || kl + ks > pl)
- return 0;
- if ((UINT64)ks * 512u > KERNEL_MAX)
- return 0;
- if (disk_read(bio, pl, io_buf, 512) != 0)
- return 0;
- if (io_buf[0] != 'N' || io_buf[1] != 'B' || io_buf[2] != 'R' ||
- io_buf[3] != 'O' || io_buf[4] != 'O' || io_buf[5] != 'T' ||
- io_buf[6] != '1' || rd32(io_buf + 8) != 1)
- return 0;
- pb = rd32(io_buf + 12);
- if (!pb || pb > 0x800000u || !(rd32(io_buf + 20) & 0x1u))
- return 0;
- *kernel_lba = kl;
- *kernel_bytes = ks * 512u;
- *pack_lba = pl;
- *pack_bytes = pb;
- return 1;
- }
- /* ---- entry ------------------------------------------------------------- */
- EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
- {
- EFI_BOOT_SERVICES *bs;
- EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
- EFI_GRAPHICS_OUTPUT_MODE_INFORMATION info;
- struct fb_saved fb;
- EFI_PHYSICAL_ADDRESS staging = 0xFFFFFFFFull;
- EFI_PHYSICAL_ADDRESS pack_buf = 0xFFFFFFFFull;
- UINTN map_size, map_key = 0, desc_size = 0;
- UINT32 desc_ver = 0;
- UINT32 rsdp;
- EFI_BLOCK_IO_PROTOCOL *bio;
- UINT32 kernel_size, pack_size;
- UINT32 embed_ksize, embed_psize, disk_ksize = 0, disk_psize = 0;
- UINT32 disk_kl = 0, disk_pl = 0, alloc_ksize, alloc_psize;
- UINT32 pack_addr = 0;
- int disk_ok = 0, from_disk = 0;
- UINTN pages, i, tries;
- EFI_STATUS status;
- com1_init();
- com1_puts("nebos-efi: start\n");
- if (!st || !st->BootServices)
- fail_before(st, "no boot services");
- bs = st->BootServices;
- bs->SetWatchdogTimer(0, 0, 0, 0);
- embed_ksize = (UINT32)(kernel_image_end - kernel_image_start);
- embed_psize = (UINT32)(pack_image_end - pack_image_start);
- if (embed_ksize == 0 || embed_ksize > KERNEL_MAX)
- fail_before(st, "kernel image size invalid");
- if (embed_psize == 0 || embed_psize > 0x400000u)
- fail_before(st, "rootfs image size invalid");
- kernel_size = embed_ksize;
- pack_size = embed_psize;
- com1_puts("nebos-efi: embedded kernel ");
- com1_dec(embed_ksize);
- com1_puts(" bytes, entry 0x");
- com1_hex((UINT64)KERNEL_ENTRY, 8);
- com1_puts(", rootfs ");
- com1_dec(embed_psize);
- com1_puts(" bytes\n");
- /* Video: prefer BGRX (the byte order the kernel's shadow buffer uses);
- * RGBX is handled by a swizzling flush. */
- fb.valid = 0;
- fb.rgbx = 0;
- if (gop_init(st, &gop, &info)) {
- fb.valid = 1;
- fb.rgbx = (info.PixelFormat == EFI_PIXEL_RGBX);
- fb.width = info.HorizontalResolution;
- fb.height = info.VerticalResolution;
- fb.pitch = info.PixelsPerScanLine * 4;
- fb.bpp = 32;
- fb.base = gop->Mode->FrameBufferBase;
- fb.size = gop->Mode->FrameBufferSize;
- com1_puts("nebos-efi: GOP ");
- com1_dec(fb.width);
- com1_puts("x");
- com1_dec(fb.height);
- com1_puts(fb.rgbx ? " RGBX" : " BGRX");
- com1_puts(" pitch ");
- com1_dec(fb.pitch);
- com1_puts(" fb 0x");
- com1_hex(fb.base, 8);
- com1_puts("\n");
- } else {
- com1_puts("nebos-efi: no usable GOP mode, booting headless\n");
- conout_puts(st,
- "NebOS EFI loader: no usable GOP mode, booting headless\n");
- }
- rsdp = find_rsdp(st);
- if (rsdp) {
- com1_puts("nebos-efi: ACPI RSDP 0x");
- com1_hex(rsdp, 8);
- com1_puts("\n");
- }
- /* Prefer the installed P1/P2 image so persistent updates and `install`
- * take effect for both boot paths. A missing/corrupt disk image falls
- * back to the kernel/rootfs embedded in BOOTX64.EFI. */
- bio = disk_block_io(image, bs);
- if (bio && disk_sizes(bio, &disk_kl, &disk_ksize, &disk_pl,
- &disk_psize)) {
- disk_ok = 1;
- com1_puts("nebos-efi: disk image: kernel ");
- com1_dec(disk_ksize);
- com1_puts(" bytes at lba ");
- com1_dec(disk_kl);
- com1_puts(", rootfs ");
- com1_dec(disk_psize);
- com1_puts(" bytes at lba ");
- com1_dec(disk_pl);
- com1_puts("\n");
- } else {
- com1_puts("nebos-efi: no usable disk image; using embedded\n");
- }
- alloc_ksize = disk_ok && disk_ksize > embed_ksize ?
- disk_ksize : embed_ksize;
- alloc_psize = disk_ok && disk_psize > embed_psize ?
- disk_psize : embed_psize;
- alloc_psize = (alloc_psize + 511u) & ~511u;
- /* Stage the kernel below 4 GB; the low-memory trampoline copies it to
- * 0x7E00 after ExitBootServices, when low memory is ours. */
- pages = (alloc_ksize + PAGE_SIZE_ - 1) / PAGE_SIZE_;
- status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
- pages, &staging);
- if (status || staging > 0xFFFFFFFFull || staging < 0x100000ull ||
- staging + alloc_ksize > 0x100000000ull)
- fail_before(st, "cannot allocate kernel staging buffer");
- /* Rootfs pack: allocate a writable loader-data buffer below the
- * kernel's 1 GB identity window (and above the low 1 MB it
- * reserves). It must be below MAX_PHYS: the kernel only
- * identity-maps that much and ramfs_init() reads it after paging is
- * enabled. */
- pages = (alloc_psize + PAGE_SIZE_ - 1) / PAGE_SIZE_;
- pack_buf = MAX_PHYS;
- status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
- pages, &pack_buf);
- if (status || pack_buf < 0x100000ull ||
- pack_buf + alloc_psize > MAX_PHYS)
- fail_before(st, "cannot allocate rootfs buffer");
- if (disk_ok) {
- UINT32 pread = (disk_psize + 511u) & ~511u;
- if (disk_read(bio, disk_kl, (void *)(UINTN)staging,
- disk_ksize) == 0 &&
- pread <= alloc_psize &&
- disk_read(bio, disk_pl + 1u, (void *)(UINTN)pack_buf,
- pread) == 0) {
- kernel_size = disk_ksize;
- pack_size = disk_psize;
- from_disk = 1;
- com1_puts("nebos-efi: kernel + rootfs loaded from disk\n");
- } else {
- com1_puts("nebos-efi: disk read failed; using embedded\n");
- }
- }
- if (!from_disk) {
- for (i = 0; i < embed_ksize; i++)
- ((UINT8 *)(UINTN)staging)[i] = kernel_image_start[i];
- for (i = 0; i < embed_psize; i++)
- ((UINT8 *)(UINTN)pack_buf)[i] = pack_image_start[i];
- kernel_size = embed_ksize;
- pack_size = embed_psize;
- com1_puts("nebos-efi: using embedded kernel + rootfs\n");
- }
- com1_puts("nebos-efi: staged at 0x");
- com1_hex(staging, 8);
- com1_puts(", rootfs at 0x");
- com1_hex(pack_buf, 8);
- com1_puts("\n");
- /* GetMemoryMap + ExitBootServices. A map-key change makes the call
- * fail; fetch a fresh map and retry. */
- for (tries = 0; tries < 4; tries++) {
- map_size = sizeof(memmap_buf);
- status = bs->GetMemoryMap(&map_size,
- (EFI_MEMORY_DESCRIPTOR *)memmap_buf,
- &map_key, &desc_size, &desc_ver);
- if (status) {
- com1_puts("nebos-efi: GetMemoryMap status 0x");
- com1_hex(status, 16);
- com1_puts("\n");
- fail_before(st, "GetMemoryMap failed");
- }
- status = bs->ExitBootServices(image, map_key);
- if (!status)
- break;
- }
- if (status)
- fail_before(st, "ExitBootServices failed");
- /* From here on no firmware services are allowed. The firmware IDT and
- * timer are still live until the kernel installs its own, so keep
- * interrupts off. */
- __asm__ volatile ("cli");
- com1_puts("nebos-efi: boot services exited\n");
- if (!desc_size || map_size < desc_size)
- fail_after("memory map is empty");
- build_e820((EFI_MEMORY_DESCRIPTOR *)memmap_buf, map_size, desc_size,
- &fb);
- pack_addr = (UINT32)pack_buf;
- com1_puts("nebos-efi: rootfs at 0x");
- com1_hex(pack_addr, 8);
- com1_puts("\n");
- write_bootinfo(rsdp, fb.valid, fb.rgbx, pack_addr,
- pack_addr ? pack_size : 0);
- write_fbinfo(&fb);
- copy_trampoline();
- com1_puts("nebos-efi: jumping to kernel\n");
- ((void (*)(UINT32, UINT32, UINT32))(UINTN)TRAMP_ADDR)
- ((UINT32)staging, kernel_size, (UINT32)KERNEL_ENTRY);
- /* The trampoline never returns. */
- halt_forever();
- return EFI_SUCCESS;
- }
|