main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. /* The synthesized E820 map covers RAM up to 4 GB: the kernel identity-maps
  21. * the first MAX_PHYS (lowmem) and uses the rest as highmem for ring-3
  22. * pages. The rootfs buffer must still be lowmem. */
  23. #define MAP_LIMIT ((UINT64)MM_HIGH_LIMIT)
  24. #define MAP_PAGES ((UINT32)(MAP_LIMIT / PAGE_SIZE_))
  25. #define E820_ADDR 0x1000u
  26. /* The kernel image is linked to live at 0x7E00. Its file image must stay
  27. * below the legacy pack staging gap at 0x60000 (kernel.ld asserts
  28. * __bss_start <= 0x60000); KERNEL_MAX is a generous sanity bound. */
  29. #define KERNEL_DEST 0x7E00u
  30. #define KERNEL_MAX 0x70000u
  31. /* Trampoline copy address: below the kernel, same low-memory slot kexec uses
  32. * (trampoline at 0x800, flat GDT inside the blob, stack at 0x7C00). */
  33. #define TRAMP_ADDR 0x800u
  34. #define TRAMP_MAX 0x800u
  35. extern unsigned char kernel_image_start[];
  36. extern unsigned char kernel_image_end[];
  37. extern unsigned char pack_image_start[];
  38. extern unsigned char pack_image_end[];
  39. extern unsigned char tramp_blob_start[];
  40. extern unsigned char tramp_blob_end[];
  41. /* GetMemoryMap buffer. A firmware map is normally a few KB; 64 KB is very
  42. * generous and keeps the retry loop simple. */
  43. static UINT8 memmap_buf[65536] __attribute__((aligned(8)));
  44. /* One bit per 4 KB page below 4 GB (128 KB). Starts
  45. * all-used; only EfiConventional/BootServices memory is freed. EfiLoader*
  46. * (this image and the staging buffer) stays reserved, which is safe and
  47. * costs only a few hundred KB. */
  48. static UINT8 page_used[MAP_PAGES / 8];
  49. struct fb_saved {
  50. int valid;
  51. int rgbx;
  52. UINT32 width;
  53. UINT32 height;
  54. UINT32 pitch;
  55. UINT32 bpp;
  56. UINT64 base;
  57. UINT64 size;
  58. };
  59. struct e820_entry_disk {
  60. UINT64 base;
  61. UINT64 length;
  62. UINT32 type;
  63. UINT32 attrs;
  64. } __attribute__((packed));
  65. /* ---- serial console (COM1, 115200 8N1) --------------------------------- */
  66. static inline void outb(UINT16 port, UINT8 v)
  67. {
  68. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  69. }
  70. static inline UINT8 inb(UINT16 port)
  71. {
  72. UINT8 v;
  73. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  74. return v;
  75. }
  76. static void com1_init(void)
  77. {
  78. outb(0x3F9, 0x00); /* IER */
  79. outb(0x3FB, 0x80); /* LCR: DLAB */
  80. outb(0x3F8, 0x01); /* DLL: 115200 */
  81. outb(0x3F9, 0x00); /* DLM */
  82. outb(0x3FB, 0x03); /* LCR: 8N1 */
  83. outb(0x3FA, 0xC7); /* FIFO */
  84. outb(0x3FC, 0x0B); /* MCR: DTR/RTS/OUT2 */
  85. }
  86. static void com1_putc(char c)
  87. {
  88. unsigned int guard = 100000;
  89. while (!(inb(0x3FD) & 0x20) && guard--)
  90. ;
  91. outb(0x3F8, (UINT8)c);
  92. }
  93. static void com1_puts(const char *s)
  94. {
  95. while (*s) {
  96. if (*s == '\n')
  97. com1_putc('\r');
  98. com1_putc(*s++);
  99. }
  100. }
  101. static void com1_hex(UINT64 v, unsigned int digits)
  102. {
  103. static const char h[] = "0123456789ABCDEF";
  104. unsigned int i;
  105. for (i = digits; i > 0; i--)
  106. com1_putc(h[(v >> (4 * (i - 1))) & 0xF]);
  107. }
  108. static void com1_dec(UINT32 v)
  109. {
  110. char buf[11];
  111. unsigned int i = 0;
  112. if (v == 0) {
  113. com1_putc('0');
  114. return;
  115. }
  116. while (v) {
  117. buf[i++] = (char)('0' + v % 10);
  118. v /= 10;
  119. }
  120. while (i)
  121. com1_putc(buf[--i]);
  122. }
  123. static void conout_puts(EFI_SYSTEM_TABLE *st, const char *s)
  124. {
  125. CHAR16 buf[128];
  126. unsigned int i = 0;
  127. if (!st || !st->ConOut || !st->ConOut->OutputString)
  128. return;
  129. while (*s) {
  130. if (*s == '\n')
  131. buf[i++] = '\r';
  132. buf[i++] = (CHAR16)(UINT8)*s++;
  133. if (i >= 126) {
  134. buf[i] = 0;
  135. st->ConOut->OutputString(st->ConOut, buf);
  136. i = 0;
  137. }
  138. }
  139. if (i) {
  140. buf[i] = 0;
  141. st->ConOut->OutputString(st->ConOut, buf);
  142. }
  143. }
  144. static void halt_forever(void) __attribute__((noreturn));
  145. static void halt_forever(void)
  146. {
  147. for (;;)
  148. __asm__ volatile ("cli; hlt");
  149. }
  150. static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
  151. __attribute__((noreturn));
  152. static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
  153. {
  154. com1_puts("nebos-efi: ");
  155. com1_puts(msg);
  156. com1_puts("\n");
  157. conout_puts(st, "NebOS EFI loader: ");
  158. conout_puts(st, msg);
  159. conout_puts(st, "\n");
  160. halt_forever();
  161. }
  162. static void fail_after(const char *msg) __attribute__((noreturn));
  163. static void fail_after(const char *msg)
  164. {
  165. com1_puts("nebos-efi: ");
  166. com1_puts(msg);
  167. com1_puts("\n");
  168. halt_forever();
  169. }
  170. /* ---- helpers ----------------------------------------------------------- */
  171. static int guid_eq(const EFI_GUID *a, const EFI_GUID *b)
  172. {
  173. const UINT8 *pa = (const UINT8 *)a;
  174. const UINT8 *pb = (const UINT8 *)b;
  175. unsigned int i;
  176. for (i = 0; i < 16; i++)
  177. if (pa[i] != pb[i])
  178. return 0;
  179. return 1;
  180. }
  181. static UINT32 find_rsdp(EFI_SYSTEM_TABLE *st)
  182. {
  183. UINTN i;
  184. if (!st->ConfigurationTable)
  185. return 0;
  186. for (i = 0; i < st->NumberOfTableEntries; i++) {
  187. EFI_CONFIGURATION_TABLE *ct = &st->ConfigurationTable[i];
  188. UINT64 addr;
  189. if (!guid_eq(&ct->VendorGuid, &efi_acpi20_guid) &&
  190. !guid_eq(&ct->VendorGuid, &efi_acpi10_guid))
  191. continue;
  192. addr = (UINT64)(UINTN)ct->VendorTable;
  193. if (addr && addr <= 0xFFFFFFFFull)
  194. return (UINT32)addr;
  195. }
  196. return 0;
  197. }
  198. /* ---- GOP --------------------------------------------------------------- */
  199. static int gop_init(EFI_SYSTEM_TABLE *st, EFI_GRAPHICS_OUTPUT_PROTOCOL **out_gop,
  200. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *out_info)
  201. {
  202. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
  203. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
  204. EFI_STATUS status;
  205. UINTN size;
  206. UINT32 i, best = 0, best_score = 0;
  207. UINT64 fb_base, fb_size;
  208. status = st->BootServices->LocateProtocol((EFI_GUID *)&efi_gop_guid,
  209. 0, (void **)&gop);
  210. if (status || !gop || !gop->Mode)
  211. return 0;
  212. for (i = 0; i < gop->Mode->MaxMode; i++) {
  213. UINT64 bytes;
  214. UINT32 score;
  215. status = gop->QueryMode(gop, i, &size, &info);
  216. if (status || !info)
  217. continue;
  218. if (info->PixelFormat != EFI_PIXEL_BGRX &&
  219. info->PixelFormat != EFI_PIXEL_RGBX)
  220. continue;
  221. if (info->HorizontalResolution < 320 ||
  222. info->VerticalResolution < 200)
  223. continue;
  224. if (info->HorizontalResolution > 1920 ||
  225. info->VerticalResolution > 65535)
  226. continue;
  227. if (info->PixelsPerScanLine < info->HorizontalResolution)
  228. continue;
  229. if (info->PixelsPerScanLine > 0x3FFF) /* pitch must fit u16 */
  230. continue;
  231. bytes = (UINT64)info->PixelsPerScanLine * 4 *
  232. info->VerticalResolution;
  233. if (bytes > 8ull * 1024 * 1024)
  234. continue;
  235. score = info->HorizontalResolution * info->VerticalResolution;
  236. if (info->PixelFormat == EFI_PIXEL_BGRX)
  237. score++; /* prefer BGRX on equal area */
  238. if (score > best_score) {
  239. best_score = score;
  240. best = i;
  241. }
  242. }
  243. if (!best_score)
  244. return 0;
  245. status = gop->SetMode(gop, best);
  246. if (status)
  247. return 0;
  248. info = gop->Mode->Info;
  249. if (!info)
  250. return 0;
  251. if (info->PixelFormat != EFI_PIXEL_BGRX &&
  252. info->PixelFormat != EFI_PIXEL_RGBX)
  253. return 0;
  254. if (info->HorizontalResolution < 320 ||
  255. info->HorizontalResolution > 1920 ||
  256. info->VerticalResolution < 200 ||
  257. info->VerticalResolution > 65535 ||
  258. info->PixelsPerScanLine < info->HorizontalResolution ||
  259. info->PixelsPerScanLine > 0x3FFF)
  260. return 0;
  261. {
  262. UINT64 bytes = (UINT64)info->PixelsPerScanLine * 4 *
  263. info->VerticalResolution;
  264. if (bytes > 8ull * 1024 * 1024)
  265. return 0;
  266. }
  267. fb_base = gop->Mode->FrameBufferBase;
  268. fb_size = gop->Mode->FrameBufferSize;
  269. if (!fb_base || fb_base > 0xFFFFFFFFull)
  270. return 0;
  271. if (fb_base + fb_size > 0x100000000ull)
  272. return 0;
  273. out_info->Version = info->Version;
  274. out_info->HorizontalResolution = info->HorizontalResolution;
  275. out_info->VerticalResolution = info->VerticalResolution;
  276. out_info->PixelFormat = info->PixelFormat;
  277. out_info->PixelInformation.RedMask = info->PixelInformation.RedMask;
  278. out_info->PixelInformation.GreenMask = info->PixelInformation.GreenMask;
  279. out_info->PixelInformation.BlueMask = info->PixelInformation.BlueMask;
  280. out_info->PixelInformation.ReservedMask =
  281. info->PixelInformation.ReservedMask;
  282. out_info->PixelsPerScanLine = info->PixelsPerScanLine;
  283. *out_gop = gop;
  284. return 1;
  285. }
  286. /* ---- E820 conversion --------------------------------------------------- */
  287. static inline int page_is_used(UINT32 p)
  288. {
  289. return (page_used[p >> 3] >> (p & 7)) & 1;
  290. }
  291. static inline void page_set_used(UINT32 p)
  292. {
  293. page_used[p >> 3] |= (UINT8)(1u << (p & 7));
  294. }
  295. static inline void page_set_free(UINT32 p)
  296. {
  297. page_used[p >> 3] &= (UINT8)~(1u << (p & 7));
  298. }
  299. /* Free only pages fully inside [base, base+len). */
  300. static void free_range(UINT64 base, UINT64 len)
  301. {
  302. UINT64 end;
  303. UINT32 p0, p1, p;
  304. if (base >= MAP_LIMIT)
  305. return;
  306. end = base + len;
  307. if (end > MAP_LIMIT)
  308. end = MAP_LIMIT;
  309. p0 = (UINT32)((base + PAGE_SIZE_ - 1) / PAGE_SIZE_);
  310. p1 = (UINT32)(end / PAGE_SIZE_);
  311. for (p = p0; p < p1; p++)
  312. page_set_free(p);
  313. }
  314. /* Reserve every page touched by [base, base+len). */
  315. static void use_range(UINT64 base, UINT64 len)
  316. {
  317. UINT64 end;
  318. UINT32 p0, p1, p;
  319. if (base >= MAP_LIMIT)
  320. return;
  321. end = base + len;
  322. if (end > MAP_LIMIT)
  323. end = MAP_LIMIT;
  324. p0 = (UINT32)(base / PAGE_SIZE_);
  325. p1 = (UINT32)((end + PAGE_SIZE_ - 1) / PAGE_SIZE_);
  326. for (p = p0; p < p1; p++)
  327. page_set_used(p);
  328. }
  329. static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
  330. UINTN desc_size, const struct fb_saved *fb)
  331. {
  332. volatile UINT32 *hdr = (volatile UINT32 *)E820_ADDR;
  333. volatile struct e820_entry_disk *entries =
  334. (volatile struct e820_entry_disk *)(E820_ADDR + 8);
  335. UINTN n = desc_size ? map_size / desc_size : 0;
  336. __asm__ volatile ("" : "+r"(hdr), "+r"(entries));
  337. UINTN i;
  338. UINT32 p, count = 0;
  339. UINT64 total = 0;
  340. for (i = 0; i < sizeof(page_used); i++)
  341. page_used[i] = 0xFF;
  342. for (i = 0; i < n; i++) {
  343. EFI_MEMORY_DESCRIPTOR *d =
  344. (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)map + i * desc_size);
  345. UINT64 len = d->NumberOfPages * PAGE_SIZE_;
  346. if (d->Type == EFI_CONVENTIONAL_MEMORY ||
  347. d->Type == EFI_BOOT_SERVICES_CODE ||
  348. d->Type == EFI_BOOT_SERVICES_DATA)
  349. free_range(d->PhysicalStart, len);
  350. }
  351. /* The scanout is MMIO, not RAM; make sure it can never be handed out
  352. * even when its BAR happens to sit below 4 GB next to RAM. */
  353. if (fb->valid)
  354. use_range(fb->base, fb->size);
  355. for (p = 0; p < MAP_PAGES; ) {
  356. UINT32 start;
  357. if (page_is_used(p)) {
  358. p++;
  359. continue;
  360. }
  361. start = p;
  362. while (p < MAP_PAGES && !page_is_used(p))
  363. p++;
  364. if (count < 32) {
  365. UINT64 len = (UINT64)(p - start) * PAGE_SIZE_;
  366. entries[count].base = (UINT64)start * PAGE_SIZE_;
  367. entries[count].length = len;
  368. entries[count].type = 1; /* E820_RAM */
  369. entries[count].attrs = 0;
  370. count++;
  371. total += len;
  372. }
  373. }
  374. hdr[0] = 0xE820E820u;
  375. hdr[1] = count;
  376. com1_puts("nebos-efi: E820 ");
  377. com1_dec(count);
  378. com1_puts(" entries, ");
  379. com1_dec((UINT32)(total >> 20));
  380. com1_puts(" MB usable\n");
  381. }
  382. /* ---- boot structures --------------------------------------------------- */
  383. /* The rootfs pack used to be copied to the top of usable RAM after
  384. * ExitBootServices. That walks into firmware code pages that OVMF maps
  385. * read-only (BootServicesCode is still listed as free memory), so the copy
  386. * faulted once the pack grew. Allocate a loader-data buffer instead:
  387. * firmware-owned pages are always writable to the loader. */
  388. static void write_bootinfo(UINT32 rsdp, int have_fb, int rgbx,
  389. UINT32 pack_addr, UINT32 pack_size)
  390. {
  391. volatile struct boot_info *bi =
  392. (volatile struct boot_info *)BOOT_INFO_ADDR;
  393. UINT32 words[BOOT_INFO_WORDS];
  394. unsigned int i;
  395. UINT32 sum = 0;
  396. __asm__ volatile ("" : "+r"(bi));
  397. for (i = 0; i < BOOT_INFO_WORDS; i++)
  398. words[i] = 0;
  399. words[0] = BOOT_INFO_MAGIC;
  400. words[1] = BOOT_INFO_VERSION;
  401. words[3] = rsdp;
  402. words[4] = BOOTINFO_FLAG_EFI |
  403. ((have_fb && rgbx) ? BOOTINFO_FLAG_RGBX : 0);
  404. words[5] = pack_addr;
  405. words[6] = pack_size;
  406. for (i = 0; i < BOOT_INFO_WORDS; i++)
  407. sum += words[i];
  408. words[2] = 0u - sum; /* the sum over the block is zero */
  409. bi->magic = words[0];
  410. bi->version = words[1];
  411. bi->checksum = words[2];
  412. bi->rsdp = words[3];
  413. bi->flags = words[4];
  414. bi->pack_addr = words[5];
  415. bi->pack_size = words[6];
  416. for (i = 0; i < 5; i++)
  417. bi->reserved[i] = words[7 + i];
  418. }
  419. static void write_fbinfo(const struct fb_saved *fb)
  420. {
  421. volatile struct vga_boot_fb_info *info = VGA_BOOT_FB;
  422. __asm__ volatile ("" : "+r"(info));
  423. if (!fb->valid) {
  424. info->valid = 0;
  425. info->width = 0;
  426. info->height = 0;
  427. info->pitch = 0;
  428. info->bpp = 0;
  429. info->lfb_base = 0;
  430. info->vram_64k = 0;
  431. return;
  432. }
  433. info->valid = 1;
  434. info->width = (unsigned short)fb->width;
  435. info->height = (unsigned short)fb->height;
  436. info->pitch = (unsigned short)fb->pitch;
  437. info->bpp = (unsigned char)fb->bpp;
  438. info->lfb_base = (unsigned int)fb->base;
  439. {
  440. /* GOP reports the mode buffer size, not total VRAM. Round up
  441. * so the kernel's VBE-era height clamp (vram_64k * 64K /
  442. * pitch) cannot shave rows off a 1920x1080 frame. */
  443. UINT64 blocks = (fb->size + 0xFFFFull) >> 16;
  444. info->vram_64k = (unsigned short)(blocks > 0xFFFF ? 0xFFFF :
  445. blocks);
  446. }
  447. }
  448. static void copy_trampoline(void)
  449. {
  450. UINT32 n = (UINT32)(tramp_blob_end - tramp_blob_start);
  451. UINT8 *dst = (UINT8 *)(UINTN)TRAMP_ADDR;
  452. UINT32 i;
  453. if (n == 0 || n > TRAMP_MAX)
  454. fail_after("trampoline blob size invalid");
  455. __asm__ volatile ("" : "+r"(dst));
  456. for (i = 0; i < n; i++)
  457. dst[i] = tramp_blob_start[i];
  458. com1_puts("nebos-efi: trampoline ");
  459. com1_dec(n);
  460. com1_puts(" bytes at 0x");
  461. com1_hex(TRAMP_ADDR, 3);
  462. com1_puts("\n");
  463. }
  464. /* ---- disk boot image ---------------------------------------------------- */
  465. static UINT8 io_buf[512] __attribute__((aligned(4096)));
  466. static UINT32 rd16(const UINT8 *p)
  467. {
  468. return (UINT32)p[0] | ((UINT32)p[1] << 8);
  469. }
  470. static UINT32 rd32(const UINT8 *p)
  471. {
  472. return (UINT32)p[0] | ((UINT32)p[1] << 8) |
  473. ((UINT32)p[2] << 16) | ((UINT32)p[3] << 24);
  474. }
  475. static int disk_read(EFI_BLOCK_IO_PROTOCOL *bio, UINT64 lba, void *buf,
  476. UINT32 bytes)
  477. {
  478. if (!bio || !bio->Media || !bytes || (bytes & 511u))
  479. return -1;
  480. if (bio->Media->BlockSize != 512)
  481. return -1;
  482. if (lba > bio->Media->LastBlock)
  483. return -1;
  484. if ((UINT64)(bytes / 512u) > bio->Media->LastBlock - lba + 1)
  485. return -1;
  486. if (bio->ReadBlocks(bio, bio->Media->MediaId, lba, bytes, buf))
  487. return -1;
  488. return 0;
  489. }
  490. /* The ESP is a partition; the kernel and rootfs live at absolute disk LBAs,
  491. * so strip the HD node and locate the parent whole-disk Block I/O handle. */
  492. static EFI_BLOCK_IO_PROTOCOL *disk_block_io(EFI_HANDLE image,
  493. EFI_BOOT_SERVICES *bs)
  494. {
  495. static UINT8 path[512];
  496. EFI_LOADED_IMAGE_PROTOCOL *li = 0;
  497. EFI_DEVICE_PATH_PROTOCOL *dp = 0, *p, *last = 0, *tp;
  498. EFI_BLOCK_IO_PROTOCOL *bio = 0;
  499. EFI_HANDLE handle = 0;
  500. UINTN len = 0, total = 0;
  501. UINT32 i;
  502. EFI_STATUS st;
  503. if (bs->HandleProtocol(image, (EFI_GUID *)&efi_loaded_image_guid,
  504. (void **)&li) || !li || !li->DeviceHandle)
  505. return 0;
  506. if (bs->HandleProtocol(li->DeviceHandle,
  507. (EFI_GUID *)&efi_device_path_guid,
  508. (void **)&dp) || !dp)
  509. return 0;
  510. p = dp;
  511. for (;;) {
  512. UINTN nl = (UINTN)p->Length[0] | ((UINTN)p->Length[1] << 8);
  513. if (p->Type == 0x7F && p->SubType == 0xFF)
  514. break;
  515. if (nl < 4 || total + nl > sizeof(path))
  516. return 0;
  517. last = p;
  518. total += nl;
  519. p = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)p + nl);
  520. }
  521. if (!last || last->Type != 0x04 || last->SubType != 0x01)
  522. return 0;
  523. len = (UINTN)((UINT8 *)last - (UINT8 *)dp);
  524. if (len + 4 > sizeof(path))
  525. return 0;
  526. for (i = 0; i < len; i++)
  527. path[i] = ((UINT8 *)dp)[i];
  528. path[len] = 0x7F;
  529. path[len + 1] = 0xFF;
  530. path[len + 2] = 4;
  531. path[len + 3] = 0;
  532. tp = (EFI_DEVICE_PATH_PROTOCOL *)path;
  533. st = bs->LocateDevicePath((EFI_GUID *)&efi_block_io_guid, &tp, &handle);
  534. if (st || !handle)
  535. return 0;
  536. if (bs->HandleProtocol(handle, (EFI_GUID *)&efi_block_io_guid,
  537. (void **)&bio) || !bio || !bio->Media)
  538. return 0;
  539. if (bio->Media->LogicalPartition || bio->Media->BlockSize != 512)
  540. return 0;
  541. return bio;
  542. }
  543. /* Read P1's boot sector and P2's NBROOT1 header. Returns the kernel size in
  544. * bytes and the pack size, or 0 when this is not a NebOS layout. */
  545. static int disk_sizes(EFI_BLOCK_IO_PROTOCOL *bio, UINT32 *kernel_lba,
  546. UINT32 *kernel_bytes, UINT32 *pack_lba,
  547. UINT32 *pack_bytes)
  548. {
  549. UINT32 ks, kl, pl, pb;
  550. if (disk_read(bio, 63, io_buf, 512) != 0)
  551. return 0;
  552. if (io_buf[510] != 0x55 || io_buf[511] != 0xAA)
  553. return 0;
  554. if (io_buf[3] != 'N' || io_buf[4] != 'E' || io_buf[5] != 'B' ||
  555. io_buf[6] != 'O' || io_buf[7] != 'S')
  556. return 0;
  557. ks = rd16(io_buf + 0x3E);
  558. kl = rd16(io_buf + 0x40);
  559. pl = rd32(io_buf + 0x42);
  560. if (!ks || kl < 1 || pl <= kl || kl + ks > pl)
  561. return 0;
  562. if ((UINT64)ks * 512u > KERNEL_MAX)
  563. return 0;
  564. if (disk_read(bio, pl, io_buf, 512) != 0)
  565. return 0;
  566. if (io_buf[0] != 'N' || io_buf[1] != 'B' || io_buf[2] != 'R' ||
  567. io_buf[3] != 'O' || io_buf[4] != 'O' || io_buf[5] != 'T' ||
  568. io_buf[6] != '1' || rd32(io_buf + 8) != 1)
  569. return 0;
  570. pb = rd32(io_buf + 12);
  571. if (!pb || pb > 0x800000u || !(rd32(io_buf + 20) & 0x1u))
  572. return 0;
  573. *kernel_lba = kl;
  574. *kernel_bytes = ks * 512u;
  575. *pack_lba = pl;
  576. *pack_bytes = pb;
  577. return 1;
  578. }
  579. /* ---- entry ------------------------------------------------------------- */
  580. EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
  581. {
  582. EFI_BOOT_SERVICES *bs;
  583. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
  584. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION info;
  585. struct fb_saved fb;
  586. EFI_PHYSICAL_ADDRESS staging = 0xFFFFFFFFull;
  587. EFI_PHYSICAL_ADDRESS pack_buf = 0xFFFFFFFFull;
  588. UINTN map_size, map_key = 0, desc_size = 0;
  589. UINT32 desc_ver = 0;
  590. UINT32 rsdp;
  591. EFI_BLOCK_IO_PROTOCOL *bio;
  592. UINT32 kernel_size, pack_size;
  593. UINT32 embed_ksize, embed_psize, disk_ksize = 0, disk_psize = 0;
  594. UINT32 disk_kl = 0, disk_pl = 0, alloc_ksize, alloc_psize;
  595. UINT32 pack_addr = 0;
  596. int disk_ok = 0, from_disk = 0;
  597. UINTN pages, i, tries;
  598. EFI_STATUS status;
  599. com1_init();
  600. com1_puts("nebos-efi: start\n");
  601. if (!st || !st->BootServices)
  602. fail_before(st, "no boot services");
  603. bs = st->BootServices;
  604. bs->SetWatchdogTimer(0, 0, 0, 0);
  605. embed_ksize = (UINT32)(kernel_image_end - kernel_image_start);
  606. embed_psize = (UINT32)(pack_image_end - pack_image_start);
  607. if (embed_ksize == 0 || embed_ksize > KERNEL_MAX)
  608. fail_before(st, "kernel image size invalid");
  609. if (embed_psize == 0 || embed_psize > 0x400000u) /* PACK_MAX */
  610. fail_before(st, "rootfs image size invalid");
  611. kernel_size = embed_ksize;
  612. pack_size = embed_psize;
  613. com1_puts("nebos-efi: embedded kernel ");
  614. com1_dec(embed_ksize);
  615. com1_puts(" bytes, entry 0x");
  616. com1_hex((UINT64)KERNEL_ENTRY, 8);
  617. com1_puts(", rootfs ");
  618. com1_dec(embed_psize);
  619. com1_puts(" bytes\n");
  620. /* Video: prefer BGRX (the byte order the kernel's shadow buffer uses);
  621. * RGBX is handled by a swizzling flush. */
  622. fb.valid = 0;
  623. fb.rgbx = 0;
  624. if (gop_init(st, &gop, &info)) {
  625. fb.valid = 1;
  626. fb.rgbx = (info.PixelFormat == EFI_PIXEL_RGBX);
  627. fb.width = info.HorizontalResolution;
  628. fb.height = info.VerticalResolution;
  629. fb.pitch = info.PixelsPerScanLine * 4;
  630. fb.bpp = 32;
  631. fb.base = gop->Mode->FrameBufferBase;
  632. fb.size = gop->Mode->FrameBufferSize;
  633. com1_puts("nebos-efi: GOP ");
  634. com1_dec(fb.width);
  635. com1_puts("x");
  636. com1_dec(fb.height);
  637. com1_puts(fb.rgbx ? " RGBX" : " BGRX");
  638. com1_puts(" pitch ");
  639. com1_dec(fb.pitch);
  640. com1_puts(" fb 0x");
  641. com1_hex(fb.base, 8);
  642. com1_puts("\n");
  643. } else {
  644. com1_puts("nebos-efi: no usable GOP mode, booting headless\n");
  645. conout_puts(st,
  646. "NebOS EFI loader: no usable GOP mode, booting headless\n");
  647. }
  648. rsdp = find_rsdp(st);
  649. if (rsdp) {
  650. com1_puts("nebos-efi: ACPI RSDP 0x");
  651. com1_hex(rsdp, 8);
  652. com1_puts("\n");
  653. }
  654. /* Prefer the installed P1/P2 image so persistent updates and `install`
  655. * take effect for both boot paths. A missing/corrupt disk image falls
  656. * back to the kernel/rootfs embedded in BOOTX64.EFI. */
  657. bio = disk_block_io(image, bs);
  658. if (bio && disk_sizes(bio, &disk_kl, &disk_ksize, &disk_pl,
  659. &disk_psize)) {
  660. disk_ok = 1;
  661. com1_puts("nebos-efi: disk image: kernel ");
  662. com1_dec(disk_ksize);
  663. com1_puts(" bytes at lba ");
  664. com1_dec(disk_kl);
  665. com1_puts(", rootfs ");
  666. com1_dec(disk_psize);
  667. com1_puts(" bytes at lba ");
  668. com1_dec(disk_pl);
  669. com1_puts("\n");
  670. } else {
  671. com1_puts("nebos-efi: no usable disk image; using embedded\n");
  672. }
  673. alloc_ksize = disk_ok && disk_ksize > embed_ksize ?
  674. disk_ksize : embed_ksize;
  675. alloc_psize = disk_ok && disk_psize > embed_psize ?
  676. disk_psize : embed_psize;
  677. alloc_psize = (alloc_psize + 511u) & ~511u;
  678. /* Stage the kernel below 4 GB; the low-memory trampoline copies it to
  679. * 0x7E00 after ExitBootServices, when low memory is ours. */
  680. pages = (alloc_ksize + PAGE_SIZE_ - 1) / PAGE_SIZE_;
  681. status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
  682. pages, &staging);
  683. if (status || staging > 0xFFFFFFFFull || staging < 0x100000ull ||
  684. staging + alloc_ksize > 0x100000000ull)
  685. fail_before(st, "cannot allocate kernel staging buffer");
  686. /* Rootfs pack: allocate a writable loader-data buffer below the
  687. * kernel's 1 GB identity window (and above the low 1 MB it
  688. * reserves). It must be below MAX_PHYS: the kernel only
  689. * identity-maps that much and ramfs_init() reads it after paging is
  690. * enabled. */
  691. pages = (alloc_psize + PAGE_SIZE_ - 1) / PAGE_SIZE_;
  692. pack_buf = MAX_PHYS;
  693. status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
  694. pages, &pack_buf);
  695. if (status || pack_buf < 0x100000ull ||
  696. pack_buf + alloc_psize > MAX_PHYS)
  697. fail_before(st, "cannot allocate rootfs buffer");
  698. if (disk_ok) {
  699. UINT32 pread = (disk_psize + 511u) & ~511u;
  700. if (disk_read(bio, disk_kl, (void *)(UINTN)staging,
  701. disk_ksize) == 0 &&
  702. pread <= alloc_psize &&
  703. disk_read(bio, disk_pl + 1u, (void *)(UINTN)pack_buf,
  704. pread) == 0) {
  705. kernel_size = disk_ksize;
  706. pack_size = disk_psize;
  707. from_disk = 1;
  708. com1_puts("nebos-efi: kernel + rootfs loaded from disk\n");
  709. } else {
  710. com1_puts("nebos-efi: disk read failed; using embedded\n");
  711. }
  712. }
  713. if (!from_disk) {
  714. for (i = 0; i < embed_ksize; i++)
  715. ((UINT8 *)(UINTN)staging)[i] = kernel_image_start[i];
  716. for (i = 0; i < embed_psize; i++)
  717. ((UINT8 *)(UINTN)pack_buf)[i] = pack_image_start[i];
  718. kernel_size = embed_ksize;
  719. pack_size = embed_psize;
  720. com1_puts("nebos-efi: using embedded kernel + rootfs\n");
  721. }
  722. com1_puts("nebos-efi: staged at 0x");
  723. com1_hex(staging, 8);
  724. com1_puts(", rootfs at 0x");
  725. com1_hex(pack_buf, 8);
  726. com1_puts("\n");
  727. /* GetMemoryMap + ExitBootServices. A map-key change makes the call
  728. * fail; fetch a fresh map and retry. */
  729. for (tries = 0; tries < 4; tries++) {
  730. map_size = sizeof(memmap_buf);
  731. status = bs->GetMemoryMap(&map_size,
  732. (EFI_MEMORY_DESCRIPTOR *)memmap_buf,
  733. &map_key, &desc_size, &desc_ver);
  734. if (status) {
  735. com1_puts("nebos-efi: GetMemoryMap status 0x");
  736. com1_hex(status, 16);
  737. com1_puts("\n");
  738. fail_before(st, "GetMemoryMap failed");
  739. }
  740. status = bs->ExitBootServices(image, map_key);
  741. if (!status)
  742. break;
  743. }
  744. if (status)
  745. fail_before(st, "ExitBootServices failed");
  746. /* From here on no firmware services are allowed. The firmware IDT and
  747. * timer are still live until the kernel installs its own, so keep
  748. * interrupts off. */
  749. __asm__ volatile ("cli");
  750. com1_puts("nebos-efi: boot services exited\n");
  751. if (!desc_size || map_size < desc_size)
  752. fail_after("memory map is empty");
  753. build_e820((EFI_MEMORY_DESCRIPTOR *)memmap_buf, map_size, desc_size,
  754. &fb);
  755. pack_addr = (UINT32)pack_buf;
  756. com1_puts("nebos-efi: rootfs at 0x");
  757. com1_hex(pack_addr, 8);
  758. com1_puts("\n");
  759. write_bootinfo(rsdp, fb.valid, fb.rgbx, pack_addr,
  760. pack_addr ? pack_size : 0);
  761. write_fbinfo(&fb);
  762. copy_trampoline();
  763. com1_puts("nebos-efi: jumping to kernel\n");
  764. ((void (*)(UINT32, UINT32, UINT32))(UINTN)TRAMP_ADDR)
  765. ((UINT32)staging, kernel_size, (UINT32)KERNEL_ENTRY);
  766. /* The trampoline never returns. */
  767. halt_forever();
  768. return EFI_SUCCESS;
  769. }