move msi-related functionality to generic isrm
[lunaix-os.git] / lunaix-os / kernel / lunad.c
index 99cd066927dc9e8b9fc6f19d556f9b077204d064..b808022ab1f8f4ee10d1c34587bbd90a050b0a45 100644 (file)
@@ -2,7 +2,6 @@
 #include <lunaix/exec.h>
 #include <lunaix/foptions.h>
 #include <lunaix/fs.h>
 #include <lunaix/exec.h>
 #include <lunaix/foptions.h>
 #include <lunaix/fs.h>
-#include <lunaix/fs/probe_boot.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
@@ -10,6 +9,7 @@
 #include <lunaix/owloysius.h>
 #include <lunaix/sched.h>
 #include <lunaix/kpreempt.h>
 #include <lunaix/owloysius.h>
 #include <lunaix/sched.h>
 #include <lunaix/kpreempt.h>
+#include <lunaix/kcmd.h>
 
 #include <klibc/string.h>
 
 
 #include <klibc/string.h>
 
@@ -21,16 +21,33 @@ init_platform();
 int
 mount_bootmedium()
 {
 int
 mount_bootmedium()
 {
-    struct v_dnode* dnode;
     int errno = 0;
     int errno = 0;
-    struct device* dev = probe_boot_medium();
+    char* rootfs;
+    struct v_dnode* dn;
+    struct device* dev;
+
+    if (!kcmd_get_option("rootfs", &rootfs)) {
+        ERROR("no rootfs.");
+        return 0;
+    }
+
+    if ((errno = vfs_walk(NULL, rootfs, &dn, NULL, 0))) {
+        ERROR("%s: no such file (%d)", rootfs, errno);
+        return 0;
+    }
+    
+    dev = resolve_device(dn->inode->data);
     if (!dev) {
     if (!dev) {
-        ERROR("fail to acquire device. (%d)", errno);
+        ERROR("%s: not a device", rootfs);
         return 0;
     }
 
         return 0;
     }
 
-    if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
-        ERROR("fail to mount boot medium. (%d)", errno);
+    // unmount the /dev to put old root fs in clear
+    must_success(vfs_unmount("/dev"));
+
+    // re-mount the root fs with our device.
+    if ((errno = vfs_mount_root(NULL, dev))) {
+        ERROR("mount root failed: %s (%d)", rootfs, errno);
         return 0;
     }
 
         return 0;
     }
 
@@ -41,8 +58,12 @@ int
 exec_initd()
 {
     int errno = 0;
 exec_initd()
 {
     int errno = 0;
+    const char* argv[] = { "/init", 0 };
+    const char* envp[] = { 0 };
+
+    kcmd_get_option("init", (char**)&argv[0]);
 
 
-    if ((errno = exec_kexecve("/mnt/lunaix-os/usr/bin/init", NULL, NULL))) {
+    if ((errno = exec_kexecve(argv[0], argv, envp))) {
         goto fail;
     }
 
         goto fail;
     }
 
@@ -56,9 +77,9 @@ fail:
 static void
 lunad_do_usr() {
     // No, these are not preemptive
 static void
 lunad_do_usr() {
     // No, these are not preemptive
-    cpu_disable_interrupt();
-    
-    if (!mount_bootmedium() || !exec_initd()) {
+    no_preemption();
+
+    if (!exec_initd()) {
         fail("failed to initd");
     }
 }
         fail("failed to initd");
     }
 }
@@ -71,15 +92,9 @@ lunad_do_usr() {
  * 同时,该进程也负责fork出我们的init进程。
  *
  */
  * 同时,该进程也负责fork出我们的init进程。
  *
  */
-void _preemptible
+void
 lunad_main()
 {
 lunad_main()
 {
-    /*
-     * We must defer boot code/data cleaning to here, after we successfully
-     * escape that area
-     */
-    boot_cleanup();
-
     spawn_kthread((ptr_t)init_platform);
 
     /*
     spawn_kthread((ptr_t)init_platform);
 
     /*
@@ -93,26 +108,32 @@ lunad_main()
         thread (which is preemptive!)
     */
 
         thread (which is preemptive!)
     */
 
-    cpu_enable_interrupt();
+    set_preemption();
     while (1)
     {
         cleanup_detached_threads();
     while (1)
     {
         cleanup_detached_threads();
-        sched_pass();
+        yield_current();
     }
 }
 
 void
 init_platform()
     }
 }
 
 void
 init_platform()
-{
+{    
     device_postboot_load();
     invoke_init_function(on_postboot);
 
     twifs_register_plugins();
 
     device_postboot_load();
     invoke_init_function(on_postboot);
 
     twifs_register_plugins();
 
+    if (!mount_bootmedium()) {
+        ERROR("failed to boot");
+        goto exit;
+    }
+
     // FIXME Re-design needed!!
     // sdbg_init();
     
     assert(!spawn_process(NULL, (ptr_t)lunad_do_usr, true));
 
     // FIXME Re-design needed!!
     // sdbg_init();
     
     assert(!spawn_process(NULL, (ptr_t)lunad_do_usr, true));
 
+exit:
     exit_thread(NULL);
 }
\ No newline at end of file
     exit_thread(NULL);
 }
\ No newline at end of file