main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /* NebOS UEFI loader (x86-64).
  2. *
  3. * The kernel is a 32-bit i586 image linked at 0x7E00. This loader gives it
  4. * the same two boot structures the real-mode VESA stage produces:
  5. *
  6. * 0x500 struct boot_info (EFI flag, GOP pixel order, ACPI RSDP)
  7. * 0x600 struct vga_boot_fb_info
  8. * 0x1000 struct e820_map
  9. *
  10. * then leaves long mode and jumps to kernel_entry with paging off and flat
  11. * 32-bit segments. Nothing in the legacy boot chain is used here.
  12. */
  13. #include "efi.h"
  14. #include "bootinfo.h"
  15. #include "phys.h"
  16. #include "vga.h"
  17. #include "efi_entry.h"
  18. #define MAX_PHYS ((UINT32)MM_MAX_PHYS)
  19. #define PAGE_SIZE_ 0x1000u
  20. #define MAX_PAGES (MAX_PHYS / PAGE_SIZE_)
  21. #define E820_ADDR 0x1000u
  22. /* The kernel image is linked to live at 0x7E00. Its file image must stay
  23. * below the legacy pack staging gap at 0x60000 (kernel.ld asserts
  24. * __bss_start <= 0x60000); KERNEL_MAX is a generous sanity bound. */
  25. #define KERNEL_DEST 0x7E00u
  26. #define KERNEL_MAX 0x70000u
  27. /* Trampoline copy address: below the kernel, same low-memory slot kexec uses
  28. * (trampoline at 0x800, flat GDT inside the blob, stack at 0x7C00). */
  29. #define TRAMP_ADDR 0x800u
  30. #define TRAMP_MAX 0x800u
  31. extern unsigned char kernel_image_start[];
  32. extern unsigned char kernel_image_end[];
  33. extern unsigned char pack_image_start[];
  34. extern unsigned char pack_image_end[];
  35. extern unsigned char tramp_blob_start[];
  36. extern unsigned char tramp_blob_end[];
  37. /* GetMemoryMap buffer. A firmware map is normally a few KB; 64 KB is very
  38. * generous and keeps the retry loop simple. */
  39. static UINT8 memmap_buf[65536] __attribute__((aligned(8)));
  40. /* One bit per 4 KB page over the kernel's 512 MB identity window. Starts
  41. * all-used; only EfiConventional/BootServices memory is freed. EfiLoader*
  42. * (this image and the staging buffer) stays reserved, which is safe and
  43. * costs only a few hundred KB. */
  44. static UINT8 page_used[MAX_PAGES / 8];
  45. struct fb_saved {
  46. int valid;
  47. int rgbx;
  48. UINT32 width;
  49. UINT32 height;
  50. UINT32 pitch;
  51. UINT32 bpp;
  52. UINT64 base;
  53. UINT64 size;
  54. };
  55. struct e820_entry_disk {
  56. UINT64 base;
  57. UINT64 length;
  58. UINT32 type;
  59. UINT32 attrs;
  60. } __attribute__((packed));
  61. /* ---- serial console (COM1, 115200 8N1) --------------------------------- */
  62. static inline void outb(UINT16 port, UINT8 v)
  63. {
  64. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  65. }
  66. static inline UINT8 inb(UINT16 port)
  67. {
  68. UINT8 v;
  69. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  70. return v;
  71. }
  72. static void com1_init(void)
  73. {
  74. outb(0x3F9, 0x00); /* IER */
  75. outb(0x3FB, 0x80); /* LCR: DLAB */
  76. outb(0x3F8, 0x01); /* DLL: 115200 */
  77. outb(0x3F9, 0x00); /* DLM */
  78. outb(0x3FB, 0x03); /* LCR: 8N1 */
  79. outb(0x3FA, 0xC7); /* FIFO */
  80. outb(0x3FC, 0x0B); /* MCR: DTR/RTS/OUT2 */
  81. }
  82. static void com1_putc(char c)
  83. {
  84. unsigned int guard = 100000;
  85. while (!(inb(0x3FD) & 0x20) && guard--)
  86. ;
  87. outb(0x3F8, (UINT8)c);
  88. }
  89. static void com1_puts(const char *s)
  90. {
  91. while (*s) {
  92. if (*s == '\n')
  93. com1_putc('\r');
  94. com1_putc(*s++);
  95. }
  96. }
  97. static void com1_hex(UINT64 v, unsigned int digits)
  98. {
  99. static const char h[] = "0123456789ABCDEF";
  100. unsigned int i;
  101. for (i = digits; i > 0; i--)
  102. com1_putc(h[(v >> (4 * (i - 1))) & 0xF]);
  103. }
  104. static void com1_dec(UINT32 v)
  105. {
  106. char buf[11];
  107. unsigned int i = 0;
  108. if (v == 0) {
  109. com1_putc('0');
  110. return;
  111. }
  112. while (v) {
  113. buf[i++] = (char)('0' + v % 10);
  114. v /= 10;
  115. }
  116. while (i)
  117. com1_putc(buf[--i]);
  118. }
  119. static void conout_puts(EFI_SYSTEM_TABLE *st, const char *s)
  120. {
  121. CHAR16 buf[128];
  122. unsigned int i = 0;
  123. if (!st || !st->ConOut || !st->ConOut->OutputString)
  124. return;
  125. while (*s) {
  126. if (*s == '\n')
  127. buf[i++] = '\r';
  128. buf[i++] = (CHAR16)(UINT8)*s++;
  129. if (i >= 126) {
  130. buf[i] = 0;
  131. st->ConOut->OutputString(st->ConOut, buf);
  132. i = 0;
  133. }
  134. }
  135. if (i) {
  136. buf[i] = 0;
  137. st->ConOut->OutputString(st->ConOut, buf);
  138. }
  139. }
  140. static void halt_forever(void) __attribute__((noreturn));
  141. static void halt_forever(void)
  142. {
  143. for (;;)
  144. __asm__ volatile ("cli; hlt");
  145. }
  146. static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
  147. __attribute__((noreturn));
  148. static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
  149. {
  150. com1_puts("nebos-efi: ");
  151. com1_puts(msg);
  152. com1_puts("\n");
  153. conout_puts(st, "NebOS EFI loader: ");
  154. conout_puts(st, msg);
  155. conout_puts(st, "\n");
  156. halt_forever();
  157. }
  158. static void fail_after(const char *msg) __attribute__((noreturn));
  159. static void fail_after(const char *msg)
  160. {
  161. com1_puts("nebos-efi: ");
  162. com1_puts(msg);
  163. com1_puts("\n");
  164. halt_forever();
  165. }
  166. /* ---- helpers ----------------------------------------------------------- */
  167. static int guid_eq(const EFI_GUID *a, const EFI_GUID *b)
  168. {
  169. const UINT8 *pa = (const UINT8 *)a;
  170. const UINT8 *pb = (const UINT8 *)b;
  171. unsigned int i;
  172. for (i = 0; i < 16; i++)
  173. if (pa[i] != pb[i])
  174. return 0;
  175. return 1;
  176. }
  177. static UINT32 find_rsdp(EFI_SYSTEM_TABLE *st)
  178. {
  179. UINTN i;
  180. if (!st->ConfigurationTable)
  181. return 0;
  182. for (i = 0; i < st->NumberOfTableEntries; i++) {
  183. EFI_CONFIGURATION_TABLE *ct = &st->ConfigurationTable[i];
  184. UINT64 addr;
  185. if (!guid_eq(&ct->VendorGuid, &efi_acpi20_guid) &&
  186. !guid_eq(&ct->VendorGuid, &efi_acpi10_guid))
  187. continue;
  188. addr = (UINT64)(UINTN)ct->VendorTable;
  189. if (addr && addr <= 0xFFFFFFFFull)
  190. return (UINT32)addr;
  191. }
  192. return 0;
  193. }
  194. /* ---- GOP --------------------------------------------------------------- */
  195. static int gop_init(EFI_SYSTEM_TABLE *st, EFI_GRAPHICS_OUTPUT_PROTOCOL **out_gop,
  196. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *out_info)
  197. {
  198. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
  199. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
  200. EFI_STATUS status;
  201. UINTN size;
  202. UINT32 i, best = 0, best_score = 0;
  203. UINT64 fb_base, fb_size;
  204. status = st->BootServices->LocateProtocol((EFI_GUID *)&efi_gop_guid,
  205. 0, (void **)&gop);
  206. if (status || !gop || !gop->Mode)
  207. return 0;
  208. for (i = 0; i < gop->Mode->MaxMode; i++) {
  209. UINT64 bytes;
  210. UINT32 score;
  211. status = gop->QueryMode(gop, i, &size, &info);
  212. if (status || !info)
  213. continue;
  214. if (info->PixelFormat != EFI_PIXEL_BGRX &&
  215. info->PixelFormat != EFI_PIXEL_RGBX)
  216. continue;
  217. if (info->HorizontalResolution < 320 ||
  218. info->VerticalResolution < 200)
  219. continue;
  220. if (info->HorizontalResolution > 1280 ||
  221. info->VerticalResolution > 65535)
  222. continue;
  223. if (info->PixelsPerScanLine < info->HorizontalResolution)
  224. continue;
  225. if (info->PixelsPerScanLine > 0x3FFF) /* pitch must fit u16 */
  226. continue;
  227. bytes = (UINT64)info->PixelsPerScanLine * 4 *
  228. info->VerticalResolution;
  229. if (bytes > 8ull * 1024 * 1024)
  230. continue;
  231. score = info->HorizontalResolution * info->VerticalResolution;
  232. if (info->PixelFormat == EFI_PIXEL_BGRX)
  233. score++; /* prefer BGRX on equal area */
  234. if (score > best_score) {
  235. best_score = score;
  236. best = i;
  237. }
  238. }
  239. if (!best_score)
  240. return 0;
  241. status = gop->SetMode(gop, best);
  242. if (status)
  243. return 0;
  244. info = gop->Mode->Info;
  245. if (!info)
  246. return 0;
  247. if (info->PixelFormat != EFI_PIXEL_BGRX &&
  248. info->PixelFormat != EFI_PIXEL_RGBX)
  249. return 0;
  250. if (info->HorizontalResolution < 320 ||
  251. info->HorizontalResolution > 1280 ||
  252. info->VerticalResolution < 200 ||
  253. info->VerticalResolution > 65535 ||
  254. info->PixelsPerScanLine < info->HorizontalResolution ||
  255. info->PixelsPerScanLine > 0x3FFF)
  256. return 0;
  257. {
  258. UINT64 bytes = (UINT64)info->PixelsPerScanLine * 4 *
  259. info->VerticalResolution;
  260. if (bytes > 8ull * 1024 * 1024)
  261. return 0;
  262. }
  263. fb_base = gop->Mode->FrameBufferBase;
  264. fb_size = gop->Mode->FrameBufferSize;
  265. if (!fb_base || fb_base > 0xFFFFFFFFull)
  266. return 0;
  267. if (fb_base + fb_size > 0x100000000ull)
  268. return 0;
  269. out_info->Version = info->Version;
  270. out_info->HorizontalResolution = info->HorizontalResolution;
  271. out_info->VerticalResolution = info->VerticalResolution;
  272. out_info->PixelFormat = info->PixelFormat;
  273. out_info->PixelInformation.RedMask = info->PixelInformation.RedMask;
  274. out_info->PixelInformation.GreenMask = info->PixelInformation.GreenMask;
  275. out_info->PixelInformation.BlueMask = info->PixelInformation.BlueMask;
  276. out_info->PixelInformation.ReservedMask =
  277. info->PixelInformation.ReservedMask;
  278. out_info->PixelsPerScanLine = info->PixelsPerScanLine;
  279. *out_gop = gop;
  280. return 1;
  281. }
  282. /* ---- E820 conversion --------------------------------------------------- */
  283. static inline int page_is_used(UINT32 p)
  284. {
  285. return (page_used[p >> 3] >> (p & 7)) & 1;
  286. }
  287. static inline void page_set_used(UINT32 p)
  288. {
  289. page_used[p >> 3] |= (UINT8)(1u << (p & 7));
  290. }
  291. static inline void page_set_free(UINT32 p)
  292. {
  293. page_used[p >> 3] &= (UINT8)~(1u << (p & 7));
  294. }
  295. /* Free only pages fully inside [base, base+len). */
  296. static void free_range(UINT64 base, UINT64 len)
  297. {
  298. UINT64 end;
  299. UINT32 p0, p1, p;
  300. if (base >= MAX_PHYS)
  301. return;
  302. end = base + len;
  303. if (end > MAX_PHYS)
  304. end = MAX_PHYS;
  305. p0 = (UINT32)((base + PAGE_SIZE_ - 1) / PAGE_SIZE_);
  306. p1 = (UINT32)(end / PAGE_SIZE_);
  307. for (p = p0; p < p1; p++)
  308. page_set_free(p);
  309. }
  310. /* Reserve every page touched by [base, base+len). */
  311. static void use_range(UINT64 base, UINT64 len)
  312. {
  313. UINT64 end;
  314. UINT32 p0, p1, p;
  315. if (base >= MAX_PHYS)
  316. return;
  317. end = base + len;
  318. if (end > MAX_PHYS)
  319. end = MAX_PHYS;
  320. p0 = (UINT32)(base / PAGE_SIZE_);
  321. p1 = (UINT32)((end + PAGE_SIZE_ - 1) / PAGE_SIZE_);
  322. for (p = p0; p < p1; p++)
  323. page_set_used(p);
  324. }
  325. static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
  326. UINTN desc_size, const struct fb_saved *fb)
  327. {
  328. volatile UINT32 *hdr = (volatile UINT32 *)E820_ADDR;
  329. volatile struct e820_entry_disk *entries =
  330. (volatile struct e820_entry_disk *)(E820_ADDR + 8);
  331. UINTN n = desc_size ? map_size / desc_size : 0;
  332. __asm__ volatile ("" : "+r"(hdr), "+r"(entries));
  333. UINTN i;
  334. UINT32 p, count = 0;
  335. UINT64 total = 0;
  336. for (i = 0; i < sizeof(page_used); i++)
  337. page_used[i] = 0xFF;
  338. for (i = 0; i < n; i++) {
  339. EFI_MEMORY_DESCRIPTOR *d =
  340. (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)map + i * desc_size);
  341. UINT64 len = d->NumberOfPages * PAGE_SIZE_;
  342. if (d->Type == EFI_CONVENTIONAL_MEMORY ||
  343. d->Type == EFI_BOOT_SERVICES_CODE ||
  344. d->Type == EFI_BOOT_SERVICES_DATA)
  345. free_range(d->PhysicalStart, len);
  346. }
  347. /* The scanout is MMIO, not RAM; make sure it can never be handed out
  348. * even when its BAR happens to sit below the 512 MB ceiling. */
  349. if (fb->valid)
  350. use_range(fb->base, fb->size);
  351. for (p = 0; p < MAX_PAGES; ) {
  352. UINT32 start;
  353. if (page_is_used(p)) {
  354. p++;
  355. continue;
  356. }
  357. start = p;
  358. while (p < MAX_PAGES && !page_is_used(p))
  359. p++;
  360. if (count < 32) {
  361. UINT64 len = (UINT64)(p - start) * PAGE_SIZE_;
  362. entries[count].base = (UINT64)start * PAGE_SIZE_;
  363. entries[count].length = len;
  364. entries[count].type = 1; /* E820_RAM */
  365. entries[count].attrs = 0;
  366. count++;
  367. total += len;
  368. }
  369. }
  370. hdr[0] = 0xE820E820u;
  371. hdr[1] = count;
  372. com1_puts("nebos-efi: E820 ");
  373. com1_dec(count);
  374. com1_puts(" entries, ");
  375. com1_dec((UINT32)(total >> 20));
  376. com1_puts(" MB usable\n");
  377. }
  378. /* ---- boot structures --------------------------------------------------- */
  379. /* Copy the embedded rootfs pack to the top of RAM (below the 512 MB
  380. * identity window and above the 1..9 MB VGA shadow reserve), matching the
  381. * legacy stage's placement. Returns the address, or 0 when it does not fit.
  382. * Runs after ExitBootServices, so only plain memory accesses. */
  383. static UINT32 copy_pack(UINT32 size)
  384. {
  385. volatile UINT32 *map = (volatile UINT32 *)E820_ADDR;
  386. volatile struct e820_entry_disk *entries =
  387. (volatile struct e820_entry_disk *)(E820_ADDR + 8);
  388. UINT32 need, count, i, best = 0;
  389. UINT32 p;
  390. if (size == 0 || size > 0x400000u)
  391. return 0;
  392. need = (size + PAGE_SIZE_ - 1) & ~(PAGE_SIZE_ - 1);
  393. if (map[0] != 0xE820E820u)
  394. return 0;
  395. count = map[1];
  396. if (count > 32)
  397. count = 32;
  398. for (i = 0; i < count; i++) {
  399. UINT64 base = entries[i].base;
  400. UINT64 end = base + entries[i].length;
  401. UINT32 a;
  402. if (entries[i].type != 1 || base >= MAX_PHYS)
  403. continue;
  404. if (end > MAX_PHYS)
  405. end = MAX_PHYS;
  406. if (end < base + need)
  407. continue;
  408. a = (UINT32)(end - need) & ~(PAGE_SIZE_ - 1);
  409. if (a < 0x900000u || a <= best)
  410. continue;
  411. best = a;
  412. }
  413. if (!best)
  414. return 0;
  415. for (p = 0; p < size; p++)
  416. ((UINT8 *)(UINTN)best)[p] = pack_image_start[p];
  417. return best;
  418. }
  419. static void write_bootinfo(UINT32 rsdp, int have_fb, int rgbx,
  420. UINT32 pack_addr, UINT32 pack_size)
  421. {
  422. volatile struct boot_info *bi =
  423. (volatile struct boot_info *)BOOT_INFO_ADDR;
  424. UINT32 words[BOOT_INFO_WORDS];
  425. unsigned int i;
  426. UINT32 sum = 0;
  427. __asm__ volatile ("" : "+r"(bi));
  428. for (i = 0; i < BOOT_INFO_WORDS; i++)
  429. words[i] = 0;
  430. words[0] = BOOT_INFO_MAGIC;
  431. words[1] = BOOT_INFO_VERSION;
  432. words[3] = rsdp;
  433. words[4] = BOOTINFO_FLAG_EFI |
  434. ((have_fb && rgbx) ? BOOTINFO_FLAG_RGBX : 0);
  435. words[5] = pack_addr;
  436. words[6] = pack_size;
  437. for (i = 0; i < BOOT_INFO_WORDS; i++)
  438. sum += words[i];
  439. words[2] = 0u - sum; /* the sum over the block is zero */
  440. bi->magic = words[0];
  441. bi->version = words[1];
  442. bi->checksum = words[2];
  443. bi->rsdp = words[3];
  444. bi->flags = words[4];
  445. bi->pack_addr = words[5];
  446. bi->pack_size = words[6];
  447. for (i = 0; i < 5; i++)
  448. bi->reserved[i] = words[7 + i];
  449. }
  450. static void write_fbinfo(const struct fb_saved *fb)
  451. {
  452. volatile struct vga_boot_fb_info *info = VGA_BOOT_FB;
  453. __asm__ volatile ("" : "+r"(info));
  454. if (!fb->valid) {
  455. info->valid = 0;
  456. info->width = 0;
  457. info->height = 0;
  458. info->pitch = 0;
  459. info->bpp = 0;
  460. info->lfb_base = 0;
  461. info->vram_64k = 0;
  462. return;
  463. }
  464. info->valid = 1;
  465. info->width = (unsigned short)fb->width;
  466. info->height = (unsigned short)fb->height;
  467. info->pitch = (unsigned short)fb->pitch;
  468. info->bpp = (unsigned char)fb->bpp;
  469. info->lfb_base = (unsigned int)fb->base;
  470. {
  471. UINT64 blocks = fb->size >> 16;
  472. info->vram_64k = (unsigned short)(blocks > 0xFFFF ? 0xFFFF :
  473. blocks);
  474. }
  475. }
  476. static void copy_trampoline(void)
  477. {
  478. UINT32 n = (UINT32)(tramp_blob_end - tramp_blob_start);
  479. UINT8 *dst = (UINT8 *)(UINTN)TRAMP_ADDR;
  480. UINT32 i;
  481. if (n == 0 || n > TRAMP_MAX)
  482. fail_after("trampoline blob size invalid");
  483. __asm__ volatile ("" : "+r"(dst));
  484. for (i = 0; i < n; i++)
  485. dst[i] = tramp_blob_start[i];
  486. com1_puts("nebos-efi: trampoline ");
  487. com1_dec(n);
  488. com1_puts(" bytes at 0x");
  489. com1_hex(TRAMP_ADDR, 3);
  490. com1_puts("\n");
  491. }
  492. /* ---- entry ------------------------------------------------------------- */
  493. EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
  494. {
  495. EFI_BOOT_SERVICES *bs;
  496. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
  497. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION info;
  498. struct fb_saved fb;
  499. EFI_PHYSICAL_ADDRESS staging = 0xFFFFFFFFull;
  500. UINTN map_size, map_key = 0, desc_size = 0;
  501. UINT32 desc_ver = 0;
  502. UINT32 rsdp;
  503. UINT32 kernel_size;
  504. UINT32 pack_addr = 0, pack_size;
  505. UINTN pages, i, tries;
  506. EFI_STATUS status;
  507. com1_init();
  508. com1_puts("nebos-efi: start\n");
  509. if (!st || !st->BootServices)
  510. fail_before(st, "no boot services");
  511. bs = st->BootServices;
  512. bs->SetWatchdogTimer(0, 0, 0, 0);
  513. kernel_size = (UINT32)(kernel_image_end - kernel_image_start);
  514. if (kernel_size == 0 || kernel_size > KERNEL_MAX)
  515. fail_before(st, "kernel image size invalid");
  516. pack_size = (UINT32)(pack_image_end - pack_image_start);
  517. com1_puts("nebos-efi: kernel ");
  518. com1_dec(kernel_size);
  519. com1_puts(" bytes, entry 0x");
  520. com1_hex((UINT64)KERNEL_ENTRY, 8);
  521. com1_puts(", rootfs ");
  522. com1_dec(pack_size);
  523. com1_puts(" bytes\n");
  524. /* Video: prefer BGRX (the byte order the kernel's shadow buffer uses);
  525. * RGBX is handled by a swizzling flush. */
  526. fb.valid = 0;
  527. fb.rgbx = 0;
  528. if (gop_init(st, &gop, &info)) {
  529. fb.valid = 1;
  530. fb.rgbx = (info.PixelFormat == EFI_PIXEL_RGBX);
  531. fb.width = info.HorizontalResolution;
  532. fb.height = info.VerticalResolution;
  533. fb.pitch = info.PixelsPerScanLine * 4;
  534. fb.bpp = 32;
  535. fb.base = gop->Mode->FrameBufferBase;
  536. fb.size = gop->Mode->FrameBufferSize;
  537. com1_puts("nebos-efi: GOP ");
  538. com1_dec(fb.width);
  539. com1_puts("x");
  540. com1_dec(fb.height);
  541. com1_puts(fb.rgbx ? " RGBX" : " BGRX");
  542. com1_puts(" pitch ");
  543. com1_dec(fb.pitch);
  544. com1_puts(" fb 0x");
  545. com1_hex(fb.base, 8);
  546. com1_puts("\n");
  547. } else {
  548. com1_puts("nebos-efi: no usable GOP mode, booting headless\n");
  549. }
  550. rsdp = find_rsdp(st);
  551. if (rsdp) {
  552. com1_puts("nebos-efi: ACPI RSDP 0x");
  553. com1_hex(rsdp, 8);
  554. com1_puts("\n");
  555. }
  556. /* Stage the kernel below 4 GB; the low-memory trampoline copies it to
  557. * 0x7E00 after ExitBootServices, when low memory is ours. */
  558. pages = (kernel_size + PAGE_SIZE_ - 1) / PAGE_SIZE_;
  559. status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
  560. pages, &staging);
  561. if (status || staging > 0xFFFFFFFFull || staging < 0x100000ull ||
  562. staging + kernel_size > 0x100000000ull)
  563. fail_before(st, "cannot allocate kernel staging buffer");
  564. for (i = 0; i < kernel_size; i++)
  565. ((UINT8 *)(UINTN)staging)[i] = kernel_image_start[i];
  566. com1_puts("nebos-efi: staged at 0x");
  567. com1_hex(staging, 8);
  568. com1_puts("\n");
  569. /* GetMemoryMap + ExitBootServices. A map-key change makes the call
  570. * fail; fetch a fresh map and retry. */
  571. for (tries = 0; tries < 4; tries++) {
  572. map_size = sizeof(memmap_buf);
  573. status = bs->GetMemoryMap(&map_size,
  574. (EFI_MEMORY_DESCRIPTOR *)memmap_buf,
  575. &map_key, &desc_size, &desc_ver);
  576. if (status) {
  577. com1_puts("nebos-efi: GetMemoryMap status 0x");
  578. com1_hex(status, 16);
  579. com1_puts("\n");
  580. fail_before(st, "GetMemoryMap failed");
  581. }
  582. status = bs->ExitBootServices(image, map_key);
  583. if (!status)
  584. break;
  585. }
  586. if (status)
  587. fail_before(st, "ExitBootServices failed");
  588. /* From here on no firmware services are allowed. The firmware IDT and
  589. * timer are still live until the kernel installs its own, so keep
  590. * interrupts off. */
  591. __asm__ volatile ("cli");
  592. com1_puts("nebos-efi: boot services exited\n");
  593. if (!desc_size || map_size < desc_size)
  594. fail_after("memory map is empty");
  595. build_e820((EFI_MEMORY_DESCRIPTOR *)memmap_buf, map_size, desc_size,
  596. &fb);
  597. pack_addr = copy_pack(pack_size);
  598. com1_puts("nebos-efi: rootfs at 0x");
  599. com1_hex(pack_addr, 8);
  600. com1_puts("\n");
  601. write_bootinfo(rsdp, fb.valid, fb.rgbx, pack_addr,
  602. pack_addr ? pack_size : 0);
  603. write_fbinfo(&fb);
  604. copy_trampoline();
  605. com1_puts("nebos-efi: jumping to kernel\n");
  606. ((void (*)(UINT32, UINT32, UINT32))(UINTN)TRAMP_ADDR)
  607. ((UINT32)staging, kernel_size, (UINT32)KERNEL_ENTRY);
  608. /* The trampoline never returns. */
  609. halt_forever();
  610. return EFI_SUCCESS;
  611. }