Home /Modules/operating-system/SLBootLoaderEmitter.sleela

SLBootLoaderEmitter.sleela

lib/operating-system/os-creator/SLBootLoaderEmitter.sleela · 135 line(s) · sleela

<< lib/operating-system/os-creator/SLBootLoaderEmitter.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLBootLoaderEmitter — the OS Creator(TM) emitter for the
     boot-loader piece. Given an SLOSModel it produces the C and C++ source for a
     boot loader shaped like the model's loader (GRUB for Linux, BOOTMGR for
     Windows, boot.efi for macOS): it reads its own configuration, presents a
     boot menu, selects the default entry, and hands off to the kernel loader.
     It also emits a loader.conf menu file (grub.cfg / BCD / com.apple.Boot-
     style) carrying the entry, the kernel command line, and the timeout.
   
   This is piece #3 of the chain. Output is String concatenation on OP_ADD.
   
   Family: operating-system. OS Creator(TM) piece emitter; consumes SLOSModel, fills SLOSPiece. >>
#sleela 2.5
material;
class SLBootLoaderEmitter {
  < ---- 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 = 10869;
  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("Boot Loader", "boot/loader", "bootloader");

    String loader = m.bootloader().loaderName();
    String cmdline = m.kernelSpec().commandLine();

    String c = "/* bootloader.c - " + loader + "-shaped boot loader, OS Creator(TM) for " + m.name() + " */\n";
    c = c + "#include <stdint.h>\n\n";
    c = c + "extern int load_config(const char *path);\n";
    c = c + "extern int default_entry(void);\n";
    c = c + "extern int handoff_kernel_loader(const char *cmdline);\n\n";
    c = c + "int bootloader_main(void) {\n";
    c = c + "    if (load_config(\"loader.conf\") != 0) return 1;\n";
    c = c + "    int entry = default_entry();\n";
    c = c + "    if (entry < 0) return 2;\n";
    c = c + "    /* Hand control to the kernel loader with the model command line. */\n";
    c = c + "    return handoff_kernel_loader(\"" + cmdline + "\");\n";
    c = c + "}\n";
    p.setC(c);

    String cpp = "// bootloader.cpp - " + loader + "-shaped boot loader (C++ surface) for " + m.name() + "\n";
    cpp = cpp + "#include <string>\n#include <vector>\nnamespace oscreator {\n";
    cpp = cpp + "class BootLoader {\npublic:\n";
    cpp = cpp + "  std::string kind    = \"" + loader + "\";\n";
    cpp = cpp + "  std::string cmdline = \"" + cmdline + "\";\n";
    cpp = cpp + "  int timeout         = " + m.bootloader().menuTimeout() + ";\n";
    cpp = cpp + "  std::vector<std::string> entries = {\"" + m.name() + " " + m.osVersion() + "\"};\n";
    cpp = cpp + "  int run() {\n";
    cpp = cpp + "    if (!loadConfig()) return 1;\n";
    cpp = cpp + "    int e = defaultEntry();\n";
    cpp = cpp + "    if (e < 0) return 2;\n";
    cpp = cpp + "    return handoffKernelLoader(cmdline);\n";
    cpp = cpp + "  }\n";
    cpp = cpp + "private:\n  bool loadConfig();\n  int defaultEntry();\n  int handoffKernelLoader(const std::string&);\n";
    cpp = cpp + "};\n} // namespace oscreator\n";
    p.setCpp(cpp);

    < Loader menu file in the loader's native idiom. >
    String conf = loaderMenu(loader, m);
    p.setConfig("loader.conf", conf);

    p.setReadme("# Boot Loader piece\n\nA `" + loader + "`-shaped loader (`bootloader.c`/`bootloader.cpp`): "
      + "read `loader.conf`, select the default entry, hand off to the kernel loader "
      + "(`../kernel-loader`).\n");
    return p;
  }

  String loaderMenu(String loader, SLOSModel m) {
    String title = m.name() + " " + m.osVersion();
    String cmdline = m.kernelSpec().commandLine();
    if (loader == "GRUB") {
      String g = "# grub.cfg (loader.conf) - generated by OS Creator(TM)\n";
      g = g + "set timeout=" + m.bootloader().menuTimeout() + "\n";
      g = g + "set default=0\n";
      g = g + "menuentry \"" + title + "\" {\n";
      g = g + "    linux /boot/vmlinuz " + cmdline + "\n";
      g = g + "    initrd /boot/initrd.img\n";
      g = g + "}\n";
      return g;
    }
    if (loader == "boot.efi") {
      String a = "# com.apple.Boot.plist-style (loader.conf) - OS Creator(TM)\n";
      a = a + "Kernel = /System/Library/Kernels/kernel\n";
      a = a + "Kernel Flags = " + cmdline + "\n";
      a = a + "Timeout = " + m.bootloader().menuTimeout() + "\n";
      return a;
    }
    < BOOTMGR / BCD-style. >
    String b = "# BCD-style boot entry (loader.conf) - OS Creator(TM)\n";
    b = b + "description = " + title + "\n";
    b = b + "path = \\\\Windows\\\\system32\\\\winload.efi\n";
    b = b + "osdevice = partition=C:\n";
    b = b + "timeout = " + m.bootloader().menuTimeout() + "\n";
    return b;
  }
}