Home /Modules/operating-system/SLUEFIEmitter.sleela

SLUEFIEmitter.sleela

lib/operating-system/os-creator/SLUEFIEmitter.sleela · 118 line(s) · sleela

<< lib/operating-system/os-creator/SLUEFIEmitter.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLUEFIEmitter — the OS Creator(TM) emitter for the UEFI
     firmware piece. Given an SLOSModel it produces the C and C++ source for a
     UEFI application entry point that initialises boot services, locates the
     EFI System Partition, and chain-loads the model's boot loader, plus a
     uefi.conf recording the firmware vendor, UEFI revision, Secure Boot, and
     ESP label. The result is written to /os-development/firmware/uefi.
   
   This is piece #1 of the firmware -> boot -> kernel -> userspace -> desktop
   chain. Output is String concatenation on OP_ADD; no new VM opcode is used.
   
   Family: operating-system. OS Creator(TM) piece emitter; consumes SLOSModel, fills SLOSPiece. >>
#sleela 2.5
material;
class SLUEFIEmitter {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10942;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-10";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  SLOSPiece emit(SLOSModel m) {
    SLOSPiece p = new SLOSPiece();
    p.configure("UEFI", "firmware/uefi", "uefi");

    String loader = m.bootloader().loaderName();
    String fw = m.bootloader().firmwareName();

    String c = "/* uefi.c - UEFI firmware entry, generated by OS Creator(TM) for " + m.name() + " */\n";
    c = c + "#include <stdint.h>\n\n";
    c = c + "/* UEFI boot-services surface (modeled). */\n";
    c = c + "typedef struct { int (*LocateEsp)(const char **out);\n";
    c = c + "                 int (*LoadImage)(const char *path);\n";
    c = c + "                 int (*StartImage)(void); } EFI_BOOT_SERVICES;\n\n";
    c = c + "/* Entry invoked by firmware. Locate the ESP and chain-load the loader. */\n";
    c = c + "int efi_main(EFI_BOOT_SERVICES *bs) {\n";
    c = c + "    const char *esp = 0;\n";
    c = c + "    if (bs->LocateEsp(&esp) != 0) return 1;\n";
    c = c + "    /* Loader for this OS shape: " + loader + " */\n";
    c = c + "    if (bs->LoadImage(\"\\\\EFI\\\\BOOT\\\\" + loaderImage(loader) + "\") != 0) return 2;\n";
    c = c + "    return bs->StartImage();\n";
    c = c + "}\n";
    p.setC(c);

    String cpp = "// uefi.cpp - UEFI firmware entry (C++ surface) for " + m.name() + "\n";
    cpp = cpp + "#include <string>\nnamespace oscreator {\n";
    cpp = cpp + "class UefiFirmware {\npublic:\n";
    cpp = cpp + "  std::string vendor   = \"" + fw + "\";\n";
    cpp = cpp + "  std::string loader   = \"" + loader + "\";\n";
    cpp = cpp + "  bool secureBoot      = " + boolLit(m.bootloader().firmwareName() != "") + ";\n";
    cpp = cpp + "  int boot() {\n";
    cpp = cpp + "    if (!locateEsp()) return 1;\n";
    cpp = cpp + "    if (!loadImage(loader)) return 2;\n";
    cpp = cpp + "    return startImage();\n";
    cpp = cpp + "  }\n";
    cpp = cpp + "private:\n";
    cpp = cpp + "  bool locateEsp();\n  bool loadImage(const std::string&);\n  int startImage();\n";
    cpp = cpp + "};\n} // namespace oscreator\n";
    p.setCpp(cpp);

    String conf = "# uefi.conf - generated by OS Creator(TM)\n";
    conf = conf + "firmware = " + fw + "\n";
    conf = conf + "loader = " + loader + "\n";
    conf = conf + "esp_label = EFI System Partition\n";
    conf = conf + "secure_boot = true\n";
    p.setConfig("uefi.conf", conf);

    p.setReadme("# UEFI firmware piece\n\nUEFI application entry (`uefi.c`/`uefi.cpp`) that "
      + "locates the ESP and chain-loads `" + loader + "`. Build target of the firmware stage.\n");
    return p;
  }

  < Map a loader name to its EFI image filename. >
  String loaderImage(String loader) {
    if (loader == "BOOTMGR")  { return "BOOTMGFW.EFI"; }
    if (loader == "boot.efi") { return "BOOTX64.EFI"; }
    if (loader == "systemd-boot") { return "SYSTEMD-BOOTX64.EFI"; }
    return "GRUBX64.EFI";
  }
  String boolLit(boolean b) { if (b) { return "true"; } return "false"; }
}