Mercurial > sos > sos
annotate src/os/shell.c @ 128:ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Thu, 11 Aug 2011 20:58:46 -0400 |
parents | de391a7d85b1 |
children | 86ec817aa4f4 |
rev | line source |
---|---|
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
1 #include <std.h> |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
2 #include <string.h> |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
3 #include <stdio.h> |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
4 #include <error.h> |
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
5 #include <die.h> |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
6 #include <os/fs.h> |
115
4473e746fe5a
Restructured the include directory somewhat
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
112
diff
changeset
|
7 #include <os/elf.h> |
4473e746fe5a
Restructured the include directory somewhat
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
112
diff
changeset
|
8 #include <os/psw.h> |
4473e746fe5a
Restructured the include directory somewhat
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
112
diff
changeset
|
9 #include <os/svc.h> |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
10 #include <os/scall.h> |
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
11 #include <os/pcb.h> |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
12 |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
13 #define PROGRAM_STACK_START 0x400000 - 160 //this is correct, ignore the documentation |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
14 #define KERNEL_STACK_START 0x380000 - 160 //this is correct, ignore the documentation |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
15 |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
16 void start(u64 __memsize) |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
17 { |
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
18 char buffer[128]; |
128
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
19 char *c, *track; //TODO rememember if there was any reason I used b and c as variable names... |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
20 ErrCode err; |
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
21 init_all(__memsize); |
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
22 if(isError(init_fs(0x100, __memsize))) die(); |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
23 set_svc_handler(svc_handler, (void*)KERNEL_STACK_START); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
24 |
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
25 sPrint("SOS online!\n"); |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
26 while(1) { |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
27 sPrint("$ "); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
28 sGet(buffer, 78); |
128
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
29 c = strtok_r(buffer, " ", &track); |
125
e2396f625db8
Started working on argc, argv
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
123
diff
changeset
|
30 |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
31 u32 fid; |
125
e2396f625db8
Started working on argc, argv
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
123
diff
changeset
|
32 if(isError(err = lookupFile(c, &fid))) { |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
33 if(errCode(err) == NOTFILE) sPrint("ERROR: command not found\n"); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
34 continue; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
35 } else { |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
36 u32 fileSize; |
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
37 if(isError(getFileSize(fid, &fileSize))) { //get the length of the file |
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
38 sPrint("ERROR: could not read file size\n"); |
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
39 continue; |
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
40 } |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
41 void *data = malloc(fileSize); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
42 if(!data) continue; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
43 if(isError(getFileData(fid, data))) goto loopCont; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
44 Elf64_Ehdr *eHdr = data; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
45 if(eHdr->e_ident[EI_MAG0] != ELFMAG0 || eHdr->e_ident[EI_MAG1] != ELFMAG1 || |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
46 eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3) |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
47 goto loopCont; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
48 if(eHdr->e_ident[EI_CLASS] != ELFCLASS64) goto loopCont; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
49 int i; |
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
50 for(i = 0; i < eHdr->e_phnum; i++) { //go through all the headers |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
51 Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr->e_phoff) + i); |
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset
|
52 if(pHdr->p_type == PT_LOAD) { //these segments get loaded into the memory |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
53 void *fromLoc = data + pHdr->p_offset; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
54 void *toLoc = (void*)pHdr->p_vaddr; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
55 memcpy(toLoc, fromLoc, pHdr->p_filesz); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
56 size_t diff = pHdr->p_memsz - pHdr->p_filesz; |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
57 if(diff) memset(toLoc + pHdr->p_filesz, 0, diff); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
58 } |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
59 } |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
60 |
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
61 PCB pcb; |
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
62 memset(&pcb, 0, sizeof(pcb)); |
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
63 pcb.registers[15] = PROGRAM_STACK_START; |
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
64 pcb.psw.p = 1; |
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
65 pcb.psw.ea = 1; |
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
66 pcb.psw.ba = 1; |
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
67 pcb.psw.ptr = eHdr->e_entry; |
128
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
68 |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
69 char *clps[10]; //for now, only accept ten values |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
70 char **argv = clps; |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
71 pcb.registers[3] = (u64)argv; //TODO where should this actually be stored?? |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
72 |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
73 while((*argv++ = strtok_r(NULL, " ", &track))) { //there was another item passed to the program |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
74 pcb.registers[2]++; //this starts at 0 |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
75 if(pcb.registers[2] == 10) break; //TODO find a better way to deal with this |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
76 } |
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset
|
77 |
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset
|
78 swapcontext_pcb(&shellPCB, &pcb); |
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
79 loopCont: |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
80 free(data); |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
81 } |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
82 } |
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset
|
83 } |