main.c 18 KB

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