示例#1
0
int main(void)
{
    uint8_t msglen = 0;
    uint8_t msg[BUS_MAXRECVMSGLEN];
    uint16_t sender = 0;
    uint16_t module_id = 0;

    io_initialize();
    clk_initialize();
    scomm_initialize_uart1(&g_serial_phy);

    //register_set_u16(MOD_eReg_ModuleID, 0x0003);
    register_get(MOD_eReg_ModuleID, 0, &module_id);
    bus_configure(&g_bus, module_id);
    bus_initialize(&g_bus, 0);// initialize bus on UART 0

    sei();
    LED_STATUS_OFF;
    LED_ERROR_OFF;
    clk_timer_start(&g_LED_timer, CLOCK_MS_2_TICKS(1000));

    while (1) {
        // check for message and read it
        if (bus_get_message(&g_bus)) {
            bgw_forward_bus_msg(&g_bus, &g_serial_phy);
            if (bus_read_message(&g_bus, &sender, &msglen, msg)) {
                interpret_message(sender, msglen, msg);
            }
        }
        bgw_forward_serial_msg(&g_bus, &g_serial_phy);

        if (clk_timer_is_elapsed(&g_LED_timer)) {
        	// cyclic reset of error LED
            LED_ERROR_OFF;
            LED_STATUS_TOGGLE;
            clk_timer_start(&g_LED_timer, CLOCK_MS_2_TICKS(1000));
        }
    }
    return 0;
}
示例#2
0
文件: main.cpp 项目: stmobo/Omamori
void test_process_1() {   
    kprintf("Initializing serial logging.\n");
    initialize_serial();
    
    logger_initialize();
    kprintf("Initialized kernel logger process.\n");
    
    terminal_writestring("Initializing ACPI.\n");
    initialize_acpi();

    kprintf("Initializing APICs.\n");
	logger_flush_buffer();
	initialize_apics();
	//logger_flush_buffer();
	//kprintf("Done initializing APICs.\n");
	//logger_flush_buffer();

	/*
	unsigned long long int last_ticked = get_sys_time_counter();
	kprintf("Current system timer = %llu.\n", last_ticked);
	logger_flush_buffer();
	while(true) {
		if( last_ticked != get_sys_time_counter() ) {
			kprintf("Tick.\nCurrent system timer = %llu.\n", last_ticked);
			logger_flush_buffer();
			last_ticked = get_sys_time_counter();
		}
	}
	*/

	//while(true) { asm volatile("pause"); };

    kprintf("Starting kernel worker thread.\n");
    k_work::start();
    
    kprintf("Initializing PS/2 controller.\n");
    ps2_controller_init();
    
    kprintf("Initializing PS/2 keyboard.\n");
    ps2_keyboard_initialize();
    
    kprintf("Initializing I/O.\n");
    io_initialize();
    
    kprintf("Initializing PCI.\n");
    //pci_initialize();
    pci_check_all_buses();

    pci_get_int_routing();
    //while(true) { asm volatile("pause"); };

    kprintf("Initializing ATA storage.\n");
    ata::initialize();
    
    io_detect_disk( io_get_disk( 1 ) );

    kprintf("Scheduling work...\n");
    logger_flush_buffer();
    k_work::work* wk = k_work::schedule( &k_worker_thread_test );
	kprintf("Test work function returned: %u\n", wk->wait());

    uint32_t child_pid = fork();
    if( child_pid == -1 ) {
        kprintf("Whoops, something went wrong with fork!");
    } else if( child_pid == 0 ) {
        kprintf("Hello from (child) process %u!\n", (unsigned int)process_current->id);
        /*
        set_message_listen_status( "keypress", true );
        while(true) {
            unique_ptr<ps2_keypress> kp;
            kp = ps2_keyboard_get_keystroke();
            if( !kp->released ) {
                if( kp->key == KEY_CurUp ) {
                    kprintf("Process %u: Up!\n", process_current->id);
                } else if( kp->key == KEY_CurDown ) {
                    kprintf("Process %u: Down!\n", process_current->id);
                /*} else if( kp->key == KEY_Enter ) {
                    kprintf("Process %u: Forcing ATA cache flush...\n", process_current->id);
                    ata_do_cache_flush(); *//*
                }
            }
        }
        */
        /*
		unsigned long long int last_ticked = 0;
		while(true) {
			if(last_ticked+1000 <= get_sys_time_counter()) {
				kprintf("Tick.");
				last_ticked = get_sys_time_counter();
			}
		}
		*/

    } else {
        kprintf("Hello from (parent) process %u!\n", process_current->id);
        kprintf("Starting lua interpretation.\n");
        lua_State *st = luaL_newstate();
        //kprintf("Opened state..\n");
        
        luaL_openlibs( st );
        //kprintf("Opened libs..\n");
        lua_register( st, "writeout", lua_writeout_proxy );
        //kprintf("Registered writeout..\n");
        int stat = luaL_loadstring( st, "writeout(\"hello from lua version \",_VERSION,\"!\") return 0xC0DE" );
        //kprintf("Loaded test program..\n");
        
        if( stat == 0 ) { // was it loaded successfully?
            //kprintf("Performing call..\n");
            stat = lua_pcall( st, 0, 1, 0 );
            if( stat == 0 ) { // get return values
                int retval = lua_tonumber(st, -1);
                kprintf("Test program returned successfully, returned value: 0x%x\n", retval);
                lua_pop(st, 1);
            }
        }
        if( stat != 0 ) { // was there an error?
            const char *err = lua_tostring(st, -1);
            kprintf("lua-error: %s\n", err);
            lua_pop(st, 1);
        }
        lua_close(st);
        //kprintf("Closed state..\n");
        
        //kprintf("Initializing AHCI.\n");
        //ahci_initialize();

        // format partition 1 as FAT (using our own code):
        //fat32_do_format( 1 );
        
        const char* test_file_name = "test2.txt";
        char *test_file_data = "Hello world! This is a test file!";

        fat_fs::fat_fs f( 1 );
		iso9660::iso9660_fs f2(2);
		device_manager::dev_fs f3;
		vfs::vfs_root = f.base;

		vfs::vfs_status fop_stat = vfs::mount(&f2, (unsigned char*)(const_cast<char*>("/cd")));
		if( fop_stat != vfs::vfs_status::ok ) {
			kprintf("FS mount failed with error: %s.\n", vfs::status_description(fop_stat));
		} else {
			kprintf("FS mount succeeded.\n");
		}

		fop_stat = vfs::mount(&f3, (unsigned char*)(const_cast<char*>("/dev")));
		if( fop_stat != vfs::vfs_status::ok ) {
			kprintf("FS mount failed with error: %s.\n", vfs::status_description(fop_stat));
		} else {
			kprintf("FS mount succeeded.\n");
		}

		while(true) {
			kprintf(">");
			logger_flush_buffer();

			unsigned int len;
			char* line = ps2_keyboard_readline( &len );

			unsigned int cmd_len = 0;
			unsigned int arg1_start = 0;
			unsigned int arg1_end = 0;
			unsigned int arg2_start = 0;

			bool found_cmd = false;
			bool found_arg1 = false;
			bool stopped_arg1 = false;
			bool found_arg2 = false;

			for(unsigned int i=0;i<len;i++) {
				if(line[i] == ' ') {
					if( !found_cmd ) {
						cmd_len = i;
						found_cmd = true;
					} else if( found_arg1 && !stopped_arg1 ) {
						arg1_end = i;
						stopped_arg1 = true;
					}
				} else {
					if( found_cmd && !found_arg1 ) {
						arg1_start = i;
						found_arg1 = true;
					} else if( found_arg1 && stopped_arg1 ) {
						arg2_start = i;
						found_arg2 = true;
						break;
					}
				}
			}


			if( found_cmd ) {
				char* cmd = NULL;
				char* arg1 = NULL;
				char* arg2 = NULL;
				cmd = (char*)kmalloc( cmd_len+1 );

				for(unsigned int i=0;i<cmd_len;i++) {
					cmd[i] = line[i];
				}
				cmd[cmd_len] = '\0';

				if( found_arg1 ) {
					if( stopped_arg1 ) {
						arg1 = (char*)kmalloc( (arg1_end - arg1_start)+1 );

						for(unsigned int i=arg1_start;i<arg1_end;i++) {
							arg1[ i - arg1_start ] = line[i];
						}
						arg1[ arg1_end - arg1_start ] = '\0';

						if( found_arg2 ) {
							arg2 = (char*)kmalloc( (len - arg2_start)+1 );
							for(unsigned int i=arg2_start;i<len;i++) {
								arg2[i-arg2_start] = line[i];
							}
							arg2[len-arg2_start] = '\0';
						}
					} else {
						arg1 = (char*)kmalloc( (len - arg1_start)+1 );
						for(unsigned int i=arg1_start;i<len;i++) {
							arg1[ i - arg1_start ] = line[i];
						}
						arg1[ len - arg1_start ] = '\0';
					}
				}

				if( arg1 != NULL ) {
					if( strcmp( cmd, const_cast<char*>("ls") ) || strcmp( cmd, const_cast<char*>("list") ) ) {
						vector<vfs_node*> dls;
						vfs::vfs_status fop_stat = vfs::list_directory( (unsigned char*)arg1, &dls );
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS list failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS list succeeded.\n");
						}
						kprintf( "Directory listing:\n" );
						for( unsigned int i=0;i<dls.count();i++) {
							kprintf( "%s\n", dls[i]->name );
						}
					} else if( (arg2 != NULL) && strcmp( cmd, const_cast<char*>("write") ) ) {
						vfs::vfs_status fop_stat = vfs::write_file( (unsigned char*)arg1, (void*)arg2, strlen(arg2)+1);
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS write failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS write succeeded.\n");
						}
					} else if( strcmp( cmd, const_cast<char*>("read") ) ) {
						vfs_node* node = NULL;
						vfs::vfs_status fop_stat = vfs::get_file_info( (unsigned char*)arg1, &node );
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS get info failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS get info succeeded.\n");
						}

						if( node != NULL ) {
							if( node->type != vfs_node_types::file ) {
								kprintf("FS read failed with error: incorrect type\n");
								continue;
							}
						}

						vfs_file* fn = (vfs_file*)node;
						void* buffer = kmalloc( fn->size );

						fop_stat = vfs::read_file( (unsigned char*)arg1, buffer );
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS read failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS read succeeded.\n");
						}

						kprintf("file data: %s\n", (unsigned char*)buffer );
					} else if( strcmp( cmd, const_cast<char*>("delete") ) || strcmp( cmd, const_cast<char*>("rm") ) ) {
						vfs::vfs_status fop_stat = vfs::delete_file( (unsigned char*)arg1 );
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS delete failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS delete succeeded.\n");
						}
					} else if( ( arg2 != NULL ) && (strcmp( cmd, const_cast<char*>("copy") ) || strcmp( cmd, const_cast<char*>("cp") ) ) ) {
						vfs::vfs_status fop_stat = vfs::copy_file( (unsigned char*)arg2, (unsigned char*)arg1 );
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS copy failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS copy succeeded.\n");
						}
					} else if( ( arg2 != NULL ) && (strcmp( cmd, const_cast<char*>("move") ) || strcmp( cmd, const_cast<char*>("mv") ) ) ) {
						vfs::vfs_status fop_stat = vfs::move_file( (unsigned char*)arg2, (unsigned char*)arg1 );
						if( fop_stat != vfs::vfs_status::ok ) {
							kprintf("FS move failed with error: %s\n", vfs::status_description(fop_stat));
						} else {
							kprintf("FS move succeeded.\n");
						}
					}
				}
			}
		}

		/*

        vfs_directory *root = f.base;
        vfs_file *test_file = NULL;
        kprintf( "Directory listing:\n" );
        for( unsigned int i=0;i<root->files.count();i++) {
            vfs_node *fn = root->files[i];
            kprintf( "* %u - %s\n", i, fn->name );
            if( strcmp(const_cast<char*>(test_file_name), (char*)(fn->name), 0) ) {
            	test_file = new vfs_file(const_cast<vfs_node*>(fn));
            }
        }

        if(test_file == NULL) {
        	test_file = f.create_file( (unsigned char*)(test_file_name), f.base );
			kprintf("test file created\n");
        	test_file->size = strlen(test_file_data)+1;

        	f.write_file(test_file, (void*)test_file_data, strlen(test_file_data)+1);
        	kprintf("File written out to %s\n", test_file_name);
        } else {
        	//f.write_file(test_file, (void*)test_file_data, strlen(test_file_data)+1);
			//kprintf("File written out to %s\n", test_file_name);

			void *readback = kmalloc(strlen(test_file_data)+1);
			f.read_file(test_file, readback);

			char* readback_data = (char*)readback;
			kprintf("Readback data: %s (%#p)\n", readback_data, readback);
        }


		unsigned char f2_mountpoint[] = { '/', 'c', 'd', '\0' };
		root = f2.base;
		kprintf( "Directory listing (ISO 9660):\n" );
		for( unsigned int i=0;i<root->files.count();i++) {
			vfs_node *fn = root->files[i];
			kprintf( "* %u - %s\n", i, fn->name );
		}

		vfs::vfs_status fop_stat = vfs::mount(&f2, f2_mountpoint);
		if( fop_stat != vfs::vfs_status::ok ) {
			kprintf("FS mount failed with error: %s.\n", vfs::status_description(fop_stat));
		} else {
			kprintf("FS mount succeeded.\n");
		}

		vector<vfs_node*> dls;
		fop_stat = vfs::list_directory( (unsigned char*)(const_cast<char*>("/")), &dls );
		if( fop_stat != vfs::vfs_status::ok ) {
			kprintf("FS list failed with error: %s\n", vfs::status_description(fop_stat));
		} else {
			kprintf("FS list succeeded.\n");
		}

		kprintf( "Directory listing (VFS root):\n" );
		for( unsigned int i=0;i<dls.count();i++) {
			kprintf("* %u - %s\n", i, dls[i]->name);
		}

		dls.clear();
		fop_stat = vfs::list_directory( (unsigned char*)(const_cast<char*>("/cd")), &dls );
		if( fop_stat != vfs::vfs_status::ok ) {
			kprintf("FS list failed with error: %s\n", vfs::status_description(fop_stat));
		} else {
			kprintf("FS list succeeded.\n");
		}

		kprintf( "Directory listing (VFS, /cd):\n" );
		for( unsigned int i=0;i<dls.count();i++) {
			kprintf("* %u - %s\n", i, dls[i]->name);
		}

        logger_flush_buffer();
		while(true) { asm volatile("pause"); }

		*/

        /*
        f.write_file(test_file, (void*)test_file_data, strlen(test_file_data)+1);

		kprintf("File written out to %s\n", test_file_name);
		logger_flush_buffer();
		system_halt;
		*/

        /*
        test_file = f.create_file( (unsigned char*)(test_file_name), f.base );
		kprintf("test file created\n");
		logger_flush_buffer();
		system_halt;
		*/

        /*
        logger_flush_buffer();
		system_halt;

        if( test_file == NULL ) {
        	test_file = f.create_file( (unsigned char*)(test_file_name), f.base );
        	kprintf("test file created\n");
        	logger_flush_buffer();
			system_halt;
        }

        f.write_file(test_file, (void*)test_file_data, strlen(test_file_data)+1);

        kprintf("File written out to %s\n", test_file_name);
        logger_flush_buffer();
		system_halt;

        void *readback = kmalloc(strlen(test_file_data)+1);
        f.read_file(test_file, readback);

        char* readback_data = (char*)readback;
        kprintf("Readback data: %s\n", readback_data);
        logger_flush_buffer();
		system_halt;
		*/

        /*

        fat32_fs f2(1);
        root = f2.root_dir_vfs;
        kprintf( "Directory listing:\n" );
		for( unsigned int i=0;i<root->files.count();i++) {
			vfs_node *fn = root->files[i];
			kprintf( "* %u - %s\n", i, fn->name );
		}
        
		logger_flush_buffer();
		system_halt;
		*/

        // hacked-together raw disk function:
        /*
        while( true ) {
            unsigned int len = 0;
            kprintf("sector_n > ");
            char* sector_str = ps2_keyboard_readline(&len);
            if( len > 0 ) {
                uint32_t sector = atoi( sector_str );
                void *buf = kmalloc(512);
                io_read_partition( 1, buf, sector*512, 512 );
                uint8_t *buf2 = (uint8_t*)buf;
                for( int i=0;i<512;i+=16 ) {
                    kprintf("%#02x: ", i);
                    for( int j=0;j<16;j++ ) {
                        kprintf("%02x ", buf2[i+j]);
                    }
                    kprintf("\n");
                    set_message_listen_status( "keypress", true );
                    while(true) {
                        unique_ptr<ps2_keypress> kp;
                        kp = ps2_keyboard_get_keystroke();
                        if( !kp->released ) {
                            if( kp->key == KEY_Enter ) {
                                break;
                            }
                        }
                    }
                }
                kfree(buf);
            }
            kfree(sector_str);
        }
        */
        // hacked-together ls:
        /*
        kprintf("Reading partition 1 as FAT32...");
        fat32_fs *fs = new fat32_fs( 1 );
        kprintf("Reading root directory...\n");
        vfs_directory *root = fs->read_directory(0);
        for( int i=0;i<root->files.count();i++ ) {
            kprintf("* %u - %s\n", i, root->files[i]->name);
        }
        */
    }
}