main.c 18 KB

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