13 #include "devices/kbd.h"
14 #include "devices/serial.h"
15 #include "devices/timer.h"
16 #include "devices/vga.h"
17 #include "lib/debug.h"
19 #include "lib/random.h"
21 #include "userprog/exception.h"
22 #include "userprog/gdt.h"
23 #include "userprog/syscall.h"
24 #include "userprog/tss.h"
27 #include "devices/disk.h"
28 #include "filesys/filesys.h"
29 #include "filesys/fsutil.h"
32 /* Amount of physical memory, in 4 kB pages. */
36 /* Format the filesystem? */
37 static bool format_filesys;
41 /* Initial program to run. */
42 static char *initial_program;
45 static void ram_init (void);
46 static void argv_init (void);
48 int main (void) NO_RETURN;
53 /* Needed by printk(), so initialize them very early. */
59 printk ("Booting cnachos86 with %'d kB RAM...\n", ram_pages * 4);
61 /* Parse command line. */
64 /* Initialize memory system, segments, paging. */
74 /* Set random seed if not already done. */
77 /* Initialize interrupt handlers. */
86 /* Start thread scheduler and enable interrupts. */
90 /* Initialize filesystem. */
92 filesys_init (format_filesys);
96 printk ("Boot complete.\n");
99 /* Run a user program. */
100 if (initial_program != NULL)
102 printk ("\nExecuting '%s':\n", initial_program);
103 thread_execute (initial_program);
107 /* Terminate this thread. */
111 /* Clear BSS and obtain RAM size from loader. */
115 /* The "BSS" is a segment that should be initialized to zeros.
116 It isn't actually stored on disk or zeroed by the kernel
117 loader, so we have to zero it ourselves.
119 The start and end of the BSS segment is recorded by the
120 linker as _start_bss and _end_bss. See kernel.lds. */
121 extern char _start_bss, _end_bss;
122 memset (&_start_bss, 0, &_end_bss - &_start_bss);
124 /* Get RAM size from loader. See loader.S. */
125 ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES);
128 /* Parses the command line. */
132 char *cmd_line, *pos;
133 char *argv[LOADER_CMD_LINE_LEN / 2 + 1];
137 /* The command line is made up of null terminated strings
138 followed by an empty string. Break it up into words. */
139 cmd_line = pos = ptov (LOADER_CMD_LINE);
140 while (pos < cmd_line + LOADER_CMD_LINE_LEN)
142 ASSERT (argc < LOADER_CMD_LINE_LEN / 2);
146 pos = strchr (pos, '\0') + 1;
150 /* Parse the words. */
151 for (i = 0; i < argc; i++)
152 if (!strcmp (argv[i], "-rs"))
153 random_init (atoi (argv[++i]));
154 else if (!strcmp (argv[i], "-d"))
155 debug_enable (argv[++i]);
157 else if (!strcmp (argv[i], "-ex"))
158 initial_program = argv[++i];
161 else if (!strcmp (argv[i], "-f"))
162 format_filesys = true;
163 else if (!strcmp (argv[i], "-cp"))
164 fsutil_copy_arg = argv[++i];
165 else if (!strcmp (argv[i], "-p"))
166 fsutil_print_file = argv[++i];
167 else if (!strcmp (argv[i], "-r"))
168 fsutil_remove_file = argv[++i];
169 else if (!strcmp (argv[i], "-ls"))
170 fsutil_list_files = true;
171 else if (!strcmp (argv[i], "-D"))
172 fsutil_dump_filesys = true;
174 else if (!strcmp (argv[i], "-u"))
178 " -rs SEED Seed random seed to SEED.\n"
179 " -d CLASS[,...] Enable the given classes of debug messages.\n"
181 " -ex 'PROG [ARG...]' Run PROG, passing the optional arguments.\n"
184 " -f Format the filesystem disk (hdb or hd0:1).\n"
185 " -cp FILENAME:SIZE Copy SIZE bytes from the scratch disk (hdc\n"
186 " or hd1:0) into the filesystem as FILENAME\n"
187 " -p FILENAME Print the contents of FILENAME\n"
188 " -r FILENAME Delete FILENAME\n"
189 " -ls List the files in the filesystem\n"
190 " -D Dump complete filesystem contents\n"
195 PANIC ("unknown option `%s'", argv[i]);