/*! @brief Adds a command to the MPX shell. * */ void add_command( /*! [in] The command name that will be made available in the shell. */ char *name, /*! [in] The C function which will implement the shell command. */ void (*function)(int argc, char *argv[]) ) { /* Temporary variable for iterating through the list of commands. */ struct mpx_command *this_command; /* Allocate space for the new command structure. */ struct mpx_command *new_command = (struct mpx_command *)sys_alloc_mem(sizeof(struct mpx_command)); new_command->name = (char *)sys_alloc_mem(MAX_ARG_LEN+1); /*! @bug This function doesn't check for failure to * allocate memory for the new command struct. */ /* Initialize the structure. */ strcpy( new_command->name, name ); new_command->function = function; new_command->next = NULL; /* Insert the new command into the linked-list of commands. */ this_command = list_head; if ( this_command == NULL ) { list_head = new_command; } else { while ( this_command->next != NULL ){ this_command = this_command->next; } this_command->next = new_command; } }
int date(char* argstr) { date_rec* date_p; int err = 0, d, m, y; char *tokenPtr; char *months[12] = {"January","February","March","April","May","June","July", "August","September","October","November","December"}; char *day = sys_alloc_mem(2); char *month = sys_alloc_mem(2); char *year = sys_alloc_mem(4); tokenPtr = strtok(argstr," "); // tokenize those args if(tokenPtr == NULL){ sys_get_date(date_p); printf("%s %d %d\n", months[date_p->month], date_p->day, date_p->year); return err; } while (tokenPtr != NULL) { if(!strcmp(tokenPtr, "-s\0")){ tokenPtr = strtok(NULL, " "); // next token plz if(strlen(tokenPtr) != 8){ // if it ain't 8, we know they f****d up printf("Invalid date string.\n"); break; } // grab those strings strncpy(year, tokenPtr, 4); strncpy(month, tokenPtr + 4, 2); strncpy(day, tokenPtr + 6, 2); // leading zeros? eh, let's make em ints anyway... m = atoi(month)-1; d = atoi(day); y = atoi(year); // are they even valid? if(validDate(d,m,y)){ date_p->day = d; date_p->month = m; date_p->year = y; err = sys_set_date(date_p); printf("Changing system date to %s %d %d\n", months[m], d, y); } else{ printf("Invalid date string.\n"); break; } } if(!strcmp(tokenPtr, "-h\0")){ printf("Usage of date:\n\tdate -s yyyymmdd\tChange the system date to the date specified.\n"); } tokenPtr = strtok(NULL, " "); } return err; }
//Allocates the memory for the PCB PCB *allocatePCB(char* processName){ PCB *thePcb; thePcb = sys_alloc_mem(sizeof(PCB)); if(strcmp(processName,"COMMHAND")==0){ thePcb->stackBottom = (unsigned char*)sys_alloc_mem(4096); }else{ thePcb->stackBottom = (unsigned char*)sys_alloc_mem(1024); } thePcb->stack = thePcb->stackBottom+SYS_STACK_SIZE-sizeof(context); return thePcb; }
void getdate() { char *encodedweekday = getweekday(); serial_print(toweekday(encodedweekday)); serial_print(" "); char *encodedmonth = getmonth(); serial_print(tomonth(encodedmonth)); serial_print(" "); serial_print(getmonthday()); serial_print(" "); char *encodedyear = getyear(); char *year = sys_alloc_mem(5); year[0] = '2'; year[1] = '0'; year[2] = encodedyear[0]; year[3] = encodedyear[1]; year[4] = '\0'; serial_println(year); serial_println(""); sys_free_mem(encodedyear); sys_free_mem(encodedweekday); sys_free_mem(encodedmonth); sys_free_mem(year); }
PCB *PCB_alloc () { // Allocate the PCB PCB *ret; ret = (PCB *)sys_alloc_mem(sizeof(PCB)); // ... and return it. return ret; }
PCB *PCB_alloc () { // Allocate the PCB PCB *ret; ret = (PCB *)sys_alloc_mem(sizeof(PCB)); // Set pstack_top to the appropriate value ret->pstack_top = ret->pstack + SYS_STACK_SIZE - sizeof(context); // ... and return it. return ret; }
void load (char *filename, int priority){ PCB *pcb; int SYS_CHECK_ERR = 0; int SYS_LOAD_ERR = 0; int PCB_CREATE_ERR = 0; int pclass = 0; int state = 0; int len_p; int offset_p; int memory_size; unsigned char load_address; unsigned char execution_address; if( name == NULL ) { // No name argument printf("No name supplied.\n"); return PCB_CREATE_ERR; } else if( strlen(name) > 8 ) { // Name too long printf("Specified process name too long.\n"); return PCB_CREATE_ERR; } else if ( !(priority >= -128 && priority <= 127) ) { printf("Invalid priority.\n"); return PCB_CREATE_ERR; } else { SYS_CHECK_ERR = sys_check_program("\0", filename, *len_p, *offset_p); if( !SYS_CHECK_ERR == 0 ) { printf("Program either doesn't exist or is invalid.\n"); return SYS_CHECK_ERR; } else { memory_size = len_p; load_address = (unsigned char*)sys_alloc_mem(memory_size); execution_address = load_address + (unsigned char*)offset_p // We've gotten what we needed and now we can apply it to a PCB. pcb = PCB_setup(filename, priority, pclass); // Place it in appropriate queue PCB_CREATE_ERR = PCB_insert(PRIORITY_QUEUES[state], temp, state); pcb->loadaddr = load_address; pcb->execaddr = execution_address context cp* = (context *) pcb->stack_top; } } }
void getdate() { outb(0x70, 0x06); unsigned char beforeconv = inb(0x71); char *afterconv = sys_alloc_mem(5); fromBCD(beforeconv, afterconv); char *weekday = toweekday(afterconv); serial_print(weekday); serial_print(" "); outb(0x70, 0x08); beforeconv = inb(0x71); fromBCD(beforeconv, afterconv); char *month = tomonth(afterconv); serial_print(month); serial_print(" "); outb(0x70, 0x07); beforeconv = inb(0x71); fromBCD(beforeconv, afterconv); serial_print(afterconv); serial_print(" "); outb(0x70, 0x09); beforeconv = inb(0x71); fromBCD(beforeconv, afterconv); char *year = sys_alloc_mem(5); year[0] = '2'; year[1] = '0'; year[2] = afterconv[0]; year[3] = afterconv[1]; year[4] = '\0'; serial_println(year); serial_println(""); sys_free_mem(afterconv); sys_free_mem(weekday); sys_free_mem(month); sys_free_mem(year); }
PCBDLL *PCBDLL_alloc () { // Allocate the PCBDLL PCBDLL *ret; ret = (PCBDLL *)sys_alloc_mem(sizeof(PCBDLL)); // We have a dummy header node, so let's set everything to null ret->prev = NULL; ret->next = NULL; ret->contents = NULL; // Return a pointer to the PCBDLL return ret; }
int PCB_insert ( PCBDLL *queue, PCB *proc, int newstate ) { // Initialize the error code and exit flag. int err = 0; int lbreak = 0; // Initialize two PCBDLL nodes: the new process and an iterator PCBDLL *newproc; PCBDLL *iter = queue; // First check to see if the PCB to insert is NULL, we don't want that if ( proc == NULL ) { err = 1; return err; } // Create the new PCBDLL structure for the new process newproc = (PCBDLL *)sys_alloc_mem(sizeof(PCBDLL)); newproc->contents = proc; // Find where to insert the PCB while ( iter->next != NULL && lbreak != 1 ) { if ( ((iter->next)->contents)->priority > proc->priority ) { iter = iter->next; } else { lbreak = 1; } } // Update the pointers accordingly newproc->next = iter->next; newproc->prev = iter; if ( iter->next != NULL ) { (iter->next)->prev = newproc; } iter->next = newproc; // Update the state of the process proc->state = newstate; // Return the error code. return err; }
char* read(int size){ //declare variables int lc, max = size; char* input = sys_alloc_mem(max); if(input == 0) write("can't allocate memory!"); sys_req(READ, TERMINAL, input, &max); //remove trailing '\n' if input contains it. lc = strlen(input)-1; if(input[lc] == '\n') input[lc] = '\0'; return input; }
void gettime() { outb(0x70, 0x04); unsigned char bcdtime = inb(0x71); char *convtime = sys_alloc_mem(5); fromBCD(bcdtime, convtime); serial_print(convtime); serial_print(":"); outb(0x70, 0x02); bcdtime = inb(0x71); fromBCD(bcdtime, convtime); serial_print(convtime); serial_print(":"); outb(0x70, 0x00); bcdtime = inb(0x71); fromBCD(bcdtime, convtime); serial_println(convtime); serial_println(""); sys_free_mem(convtime); }
void setdate() { serial_println("What day of the week is it?"); char *response = polling(); char *bcdweekday = fromweekday(response); unsigned char portnum = 0x06; //weekday port writetoRTC(portnum, bcdweekday); serial_println("What month is it?"); response = polling(); char *bcdmonth = frommonth(response); portnum = 0x08; //month port writetoRTC(portnum, bcdmonth); serial_println("What day of the month is it?"); response = polling(); portnum = 0x07; //month day port writetoRTC(portnum, response); serial_println("What year is it?"); response = polling(); char *bcdyear = sys_alloc_mem(2); if (strlen(response) != 4 || atoi(response) < 2000) { bcdyear[0] = '\0'; } else { bcdyear[0] = response[2]; bcdyear[1] = response[3]; } portnum = 0x09; writetoRTC(portnum, bcdyear); serial_print("Time successfully changed to -> "); getdate(); sys_free_mem(response); sys_free_mem(bcdweekday); sys_free_mem(bcdmonth); sys_free_mem(bcdyear); }
void IOScheduler(){ if(param_ptr->device_id==TERMINAL||param_ptr->device_id==COM_PORT){ IOD *Descriptor = sys_alloc_mem(sizeof(IOD)); Descriptor->ppt = COP; strcpy(Descriptor->PCBName,COP->processName); Descriptor->buffer = param_ptr->buf_addr; Descriptor->count = param_ptr->count_addr; Descriptor->requestType = param_ptr->op_code; if(param_ptr->device_id==TERMINAL){ if(TerminalQueue-> count == 0){ TerminalQueue->head = Descriptor; TerminalQueue->tail = Descriptor; TerminalQueue->count = 1; ProcessIORequest(1); }else{ TerminalQueue->tail->next = Descriptor; TerminalQueue->tail = TerminalQueue->tail->next; TerminalQueue->count++; } }else if(param_ptr->device_id==COM_PORT){ if(Com_PortQueue-> count == 0){ Com_PortQueue->head = Descriptor; Com_PortQueue->tail = Descriptor; Com_PortQueue->count = 1; ProcessIORequest(2); }else{ Com_PortQueue->tail->next = Descriptor; Com_PortQueue->tail = Com_PortQueue->tail->next; Com_PortQueue->count++; } } COP->state = 0; insertPCB(COP); //blockPCB(COP->processName); } }
// loads a program into memory void loadProgram(int argc, char *argv[]){ //name,fileName,priority,path // sets up variables static int count; MEMDSC *tempMem; unsigned char temptop; char *dir, *name, *filename; int size,offset,priority; tcontext *tempContext; unsigned int *tempCS,*tempIP; STACKDSC *temp; // initializes values int err = 0; PCB *newPCB = allocate_PCB(); tempMem=newPCB->memdsc; dir = (char*) sys_alloc_mem(30 * sizeof(char)); name = (char*) sys_alloc_mem(30 * sizeof(char)); filename = (char*) sys_alloc_mem(30 * sizeof(char)); strcpy(dir,argv[4]); strcpy(name,argv[1]); strcpy(filename,argv[2]); priority = atoi(argv[3]); err = sys_check_program(dir,filename,&size,&offset); if((argc==5)||(127<=priority<=-128)&&( err==0)){ //checks for validity /* if( count == ZERO ){ //If first process allocate queue rQueue = (ROOT*) sys_alloc_mem(sizeof(ROOT)); wsQueue = (ROOT*) sys_alloc_mem(sizeof(ROOT)); } */ setup_PCB(newPCB,name,APPLICATION,SUSPENDED_READY,priority); // sets up the adressess newPCB->memdsc->loadADDR= sys_alloc_mem(size); newPCB->memdsc->execADDR=newPCB->memdsc->loadADDR + offset;// is this the correct address? //make sure all registers are properly set newPCB -> stackdsc-> top = newPCB -> stackdsc-> base + STACKSIZE - sizeof(tcontext); tempContext = (tcontext *) (newPCB -> stackdsc-> top); tempContext ->ES = _ES; tempContext ->DS = _DS; tempContext ->CS = FP_SEG(newPCB->memdsc->execADDR); tempContext ->IP = FP_OFF(newPCB->memdsc->execADDR); tempContext ->FLAGS = 0x200; // load the program into memory err = sys_load_program(newPCB->memdsc->loadADDR,size,dir,filename); insert_PCB(newPCB); // put pcb into a queue count++;//Update the number of times the function has run. } else{ printf("Wrong or invalid arguments entered."); } }
void CreateIOCB(){ TerminalQueue = sys_alloc_mem(sizeof(IOCB)); Com_PortQueue = sys_alloc_mem(sizeof(IOCB)); trm_open(&TerminalQueue->event_flag); com_open(&Com_PortQueue->event_flag,1200); }