Add -u to usage message.
[pintos-anon] / src / threads / init.c
index 2684d17d5023621594e89bc35a2e647da20178a1..49b26a9bae3b636daf3bdea790c3b19b7367ab70 100644 (file)
@@ -1,4 +1,5 @@
 #include "threads/init.h"
+#include <console.h>
 #include <debug.h>
 #include <limits.h>
 #include <random.h>
@@ -18,6 +19,7 @@
 #include "threads/mmu.h"
 #include "threads/paging.h"
 #include "threads/palloc.h"
+#include "threads/test.h"
 #include "threads/thread.h"
 #ifdef USERPROG
 #include "userprog/exception.h"
@@ -44,27 +46,32 @@ static bool format_filesys;
 static char *initial_program;
 #endif
 
+/* Power off after running requested actions? */
+static bool power_off;
+
 static void ram_init (void);
 static void argv_init (void);
+static void do_power_off (void);
 
 int main (void) NO_RETURN;
 
 int
 main (void)
 {
-  /* Needed by printf(), so initialize them very early. */
+  /* Initialize everything needed for printf() first. */
   ram_init ();
+  thread_init ();
   vga_init ();
-  serial_init (1);
+  serial_init_poll ();
+  console_init ();
 
   /* Greet user. */
-  printf ("Booting cnachos86 with %'d kB RAM...\n", ram_pages * 4);
+  printf ("Pintos booting with %'zd kB RAM...\n", ram_pages * (PGSIZE / 1024));
 
   /* Parse command line. */
   argv_init ();
 
   /* Initialize memory system, segments, paging. */
-  thread_init ();
   palloc_init ();
   paging_init ();
 #ifdef USERPROG
@@ -73,7 +80,7 @@ main (void)
 #endif
   malloc_init ();
 
-  /* Set random seed if not already done. */
+  /* Set random seed if argv_init() didn't. */
   random_init (0);
 
   /* Initialize interrupt handlers. */
@@ -87,7 +94,7 @@ main (void)
 
   /* Start thread scheduler and enable interrupts. */
   thread_start ();
-  serial_init (2);
+  serial_init_queue ();
 
 #ifdef FILESYS
   /* Initialize filesystem. */
@@ -98,9 +105,6 @@ main (void)
 
   printf ("Boot complete.\n");
   
-  for (;;) 
-    putchar (serial_getc ());
-
 #ifdef USERPROG
   /* Run a user program. */
   if (initial_program != NULL)
@@ -108,8 +112,13 @@ main (void)
       printf ("\nExecuting '%s':\n", initial_program);
       thread_execute (initial_program); 
     }
+#else
+  test ();
 #endif
 
+  if (power_off) 
+    do_power_off ();
+
   /* Terminate this thread. */
   thread_exit ();
 }
@@ -136,22 +145,26 @@ static void
 argv_init (void) 
 {
   char *cmd_line, *pos;
-  char *argv[LOADER_CMD_LINE_LEN / 2 + 1];
+  char *argv[LOADER_CMD_LINE_LEN / 2 + 2];
   int argc = 0;
   int i;
 
   /* The command line is made up of null terminated strings
      followed by an empty string.  Break it up into words. */
   cmd_line = pos = ptov (LOADER_CMD_LINE);
+  printf ("Kernel command line:");
   while (pos < cmd_line + LOADER_CMD_LINE_LEN)
     {
       ASSERT (argc < LOADER_CMD_LINE_LEN / 2);
       if (*pos == '\0')
         break;
       argv[argc++] = pos;
+      printf (" %s", pos);
       pos = strchr (pos, '\0') + 1;
     }
+  printf ("\n");
   argv[argc] = "";
+  argv[argc + 1] = "";
 
   /* Parse the words. */
   for (i = 0; i < argc; i++)
@@ -159,6 +172,8 @@ argv_init (void)
       random_init (atoi (argv[++i]));
     else if (!strcmp (argv[i], "-d")) 
       debug_enable (argv[++i]);
+    else if (!strcmp (argv[i], "-q"))
+      power_off = true;
 #ifdef USERPROG
     else if (!strcmp (argv[i], "-ex")) 
       initial_program = argv[++i];
@@ -166,8 +181,13 @@ argv_init (void)
 #ifdef FILESYS
   else if (!strcmp (argv[i], "-f"))
       format_filesys = true;
-    else if (!strcmp (argv[i], "-cp")) 
-      fsutil_copy_arg = argv[++i];
+    else if (!strcmp (argv[i], "-ci")) 
+      {
+        fsutil_copyin_file = argv[++i]; 
+        fsutil_copyin_size = atoi (argv[++i]); 
+      }
+    else if (!strcmp (argv[i], "-co"))
+      fsutil_copyout_file = argv[++i];
     else if (!strcmp (argv[i], "-p")) 
       fsutil_print_file = argv[++i];
     else if (!strcmp (argv[i], "-r"))
@@ -188,15 +208,36 @@ argv_init (void)
 #endif
 #ifdef FILESYS
           " -f                  Format the filesystem disk (hdb or hd0:1).\n"
-          " -cp FILENAME:SIZE   Copy SIZE bytes from the scratch disk (hdc\n"
+          " -ci FILENAME SIZE   Copy SIZE bytes from the scratch disk (hdc\n"
           "                     or hd1:0) into the filesystem as FILENAME\n"
+          " -co FILENAME        Copy FILENAME to the scratch disk, with\n"
+          "                     size at start of sector 0 and data afterward\n"
           " -p FILENAME         Print the contents of FILENAME\n"
           " -r FILENAME         Delete FILENAME\n"
           " -ls                 List the files in the filesystem\n"
           " -D                  Dump complete filesystem contents\n"
 #endif
+          " -q                  Power off after doing requested actions.\n"
+          " -u                  Print this help message and power off.\n"
           );
+        do_power_off ();
       }
     else 
-      PANIC ("unknown option `%s'", argv[i]);
+      PANIC ("unknown option `%s' (use -u for help)", argv[i]);
+}
+
+void
+do_power_off (void) 
+{
+  const char s[] = "Shutdown";
+  const char *p;
+
+#ifdef FILESYS
+  filesys_done ();
+#endif
+
+  printf ("Powering off...\n");
+  for (p = s; *p != '\0'; p++)
+    outb (0x8900, *p);
+  for (;;);
 }