main.c 23 KB

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