main.c 16 KB

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