Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.2KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <termios.h>
  5. #include "../emul.h"
  6. #include "shell-bin.h"
  7. /* Collapse OS shell with filesystem
  8. *
  9. * On startup, if "cfsin" directory exists, it packs it as a afke block device
  10. * and loads it in. Upon halting, unpcks the contents of that block device in
  11. * "cfsout" directory.
  12. *
  13. * Memory layout:
  14. *
  15. * 0x0000 - 0x3fff: ROM code from shell.asm
  16. * 0x4000 - 0x4fff: Kernel memory
  17. * 0x5000 - 0xffff: Userspace
  18. *
  19. * I/O Ports:
  20. *
  21. * 0 - stdin / stdout
  22. * 1 - Filesystem blockdev data read/write. Reads and write data to the address
  23. * previously selected through port 2
  24. */
  25. //#define DEBUG
  26. #define MAX_FSDEV_SIZE 0x20000
  27. // in sync with glue.asm
  28. #define RAMSTART 0x2000
  29. #define STDIO_PORT 0x00
  30. #define FS_DATA_PORT 0x01
  31. // Controls what address (24bit) the data port returns. To select an address,
  32. // this port has to be written to 3 times, starting with the MSB.
  33. // Reading this port returns an out-of-bounds indicator. Meaning:
  34. // 0 means addr is within bounds
  35. // 1 means that we're equal to fsdev size (error for reading, ok for writing)
  36. // 2 means more than fsdev size (always invalid)
  37. // 3 means incomplete addr setting
  38. #define FS_ADDR_PORT 0x02
  39. static uint8_t fsdev[MAX_FSDEV_SIZE] = {0};
  40. static uint32_t fsdev_ptr = 0;
  41. // 0 = idle, 1 = received MSB (of 24bit addr), 2 = received middle addr
  42. static int fsdev_addr_lvl = 0;
  43. static int running;
  44. static uint8_t iord_stdio()
  45. {
  46. int c = getchar();
  47. if (c == EOF) {
  48. running = 0;
  49. }
  50. return (uint8_t)c;
  51. }
  52. static uint8_t iord_fsdata()
  53. {
  54. if (fsdev_addr_lvl != 0) {
  55. fprintf(stderr, "Reading FSDEV in the middle of an addr op (%d)\n", fsdev_ptr);
  56. return 0;
  57. }
  58. if (fsdev_ptr < MAX_FSDEV_SIZE) {
  59. #ifdef DEBUG
  60. fprintf(stderr, "Reading FSDEV at offset %d\n", fsdev_ptr);
  61. #endif
  62. return fsdev[fsdev_ptr];
  63. } else {
  64. fprintf(stderr, "Out of bounds FSDEV read at %d\n", fsdev_ptr);
  65. return 0;
  66. }
  67. }
  68. static uint8_t iord_fsaddr()
  69. {
  70. if (fsdev_addr_lvl != 0) {
  71. return 3;
  72. } else if (fsdev_ptr >= MAX_FSDEV_SIZE) {
  73. fprintf(stderr, "Out of bounds FSDEV addr request at %d / %d\n", fsdev_ptr, MAX_FSDEV_SIZE);
  74. return 2;
  75. } else {
  76. return 0;
  77. }
  78. }
  79. static void iowr_stdio(uint8_t val)
  80. {
  81. if (val == 0x04) { // CTRL+D
  82. running = 0;
  83. } else {
  84. putchar(val);
  85. }
  86. }
  87. static void iowr_fsdata(uint8_t val)
  88. {
  89. if (fsdev_addr_lvl != 0) {
  90. fprintf(stderr, "Writing to FSDEV in the middle of an addr op (%d)\n", fsdev_ptr);
  91. return;
  92. }
  93. if (fsdev_ptr < MAX_FSDEV_SIZE) {
  94. #ifdef DEBUG
  95. fprintf(stderr, "Writing to FSDEV (%d)\n", fsdev_ptr);
  96. #endif
  97. fsdev[fsdev_ptr] = val;
  98. } else {
  99. fprintf(stderr, "Out of bounds FSDEV write at %d\n", fsdev_ptr);
  100. }
  101. }
  102. static void iowr_fsaddr(uint8_t val)
  103. {
  104. if (fsdev_addr_lvl == 0) {
  105. fsdev_ptr = val << 16;
  106. fsdev_addr_lvl = 1;
  107. } else if (fsdev_addr_lvl == 1) {
  108. fsdev_ptr |= val << 8;
  109. fsdev_addr_lvl = 2;
  110. } else {
  111. fsdev_ptr |= val;
  112. fsdev_addr_lvl = 0;
  113. }
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. FILE *fp = NULL;
  118. while (1) {
  119. int c = getopt(argc, argv, "f:");
  120. if (c < 0) {
  121. break;
  122. }
  123. switch (c) {
  124. case 'f':
  125. fp = fopen(optarg, "r");
  126. if (fp == NULL) {
  127. fprintf(stderr, "Can't open %s\n", optarg);
  128. return 1;
  129. }
  130. break;
  131. default:
  132. fprintf(stderr, "Usage: shell [-f fsdev]\n");
  133. return 1;
  134. }
  135. }
  136. // Setup fs blockdev
  137. if (fp == NULL) {
  138. fp = popen("../cfspack/cfspack cfsin", "r");
  139. if (fp == NULL) {
  140. fprintf(stderr, "Can't initialize filesystem. Leaving blank.\n");
  141. }
  142. }
  143. if (fp != NULL) {
  144. fprintf(stderr, "Initializing filesystem\n");
  145. int i = 0;
  146. int c;
  147. while ((c = fgetc(fp)) != EOF && i < MAX_FSDEV_SIZE) {
  148. fsdev[i++] = c & 0xff;
  149. }
  150. if (i == MAX_FSDEV_SIZE) {
  151. fprintf(stderr, "Filesytem image too large.\n");
  152. return 1;
  153. }
  154. pclose(fp);
  155. }
  156. bool tty = isatty(fileno(stdin));
  157. struct termios termInfo;
  158. if (tty) {
  159. // Turn echo off: the shell takes care of its own echoing.
  160. if (tcgetattr(0, &termInfo) == -1) {
  161. printf("Can't setup terminal.\n");
  162. return 1;
  163. }
  164. termInfo.c_lflag &= ~ECHO;
  165. termInfo.c_lflag &= ~ICANON;
  166. tcsetattr(0, TCSAFLUSH, &termInfo);
  167. }
  168. Machine *m = emul_init();
  169. m->ramstart = RAMSTART;
  170. m->iord[STDIO_PORT] = iord_stdio;
  171. m->iord[FS_DATA_PORT] = iord_fsdata;
  172. m->iord[FS_ADDR_PORT] = iord_fsaddr;
  173. m->iowr[STDIO_PORT] = iowr_stdio;
  174. m->iowr[FS_DATA_PORT] = iowr_fsdata;
  175. m->iowr[FS_ADDR_PORT] = iowr_fsaddr;
  176. // initialize memory
  177. for (int i=0; i<sizeof(KERNEL); i++) {
  178. m->mem[i] = KERNEL[i];
  179. }
  180. // Run!
  181. running = 1;
  182. while (running && emul_step());
  183. if (tty) {
  184. printf("Done!\n");
  185. termInfo.c_lflag |= ECHO;
  186. termInfo.c_lflag |= ICANON;
  187. tcsetattr(0, TCSAFLUSH, &termInfo);
  188. emul_printdebug();
  189. }
  190. return 0;
  191. }