Back to index

plt-scheme  4.2.1
oskglue.inc
Go to the documentation of this file.
00001 #if defined(OSKIT) && !defined(OSKIT_TEST) && !KNIT
00002 
00003 #  include <oskit/clientos.h>
00004 #  include <oskit/dev/osenv.h>
00005 # include <oskit/fs/memfs.h> 
00006 # include <oskit/dev/clock.h> 
00007 # include <oskit/c/sys/time.h> 
00008 # include <oskit/x86/pc/dev.h>
00009 # ifdef USE_OSKIT_CONSOLE
00010 #  include <oskit/x86/pc/direct_cons.h> 
00011 # else
00012 #  include <oskit/dev/freebsd.h> 
00013 # endif
00014 
00015 static oskit_osenv_t *osenv;
00016 
00017 void start_clock()
00018 {
00019 # define LOCAL_TO_GMT(t) /* (t)->tv_sec += secondswest */
00020   oskit_timespec_t time;
00021   /* use fdev's default clock device */
00022   oskit_clock_t *clock = oskit_clock_init();
00023   
00024   oskit_rtc_get(&time);   /* read rtc */
00025   LOCAL_TO_GMT(&time);            /* adjust for local time */
00026   oskit_clock_settime(clock, &time); /* set time */
00027   
00028   oskit_register(&oskit_clock_iid, (void *) clock);
00029 }
00030 
00031 void start_memfs()
00032 {
00033   oskit_error_t err;
00034   oskit_filesystem_t *fs;
00035   oskit_dir_t *root;
00036   oskit_fsnamespace_t *fsnamespace;
00037 
00038 # define CHECK(what, f) \
00039   if ((err = f)) { printf("in-memory filesystem init error at " what ": %x\n", err); return; }
00040 
00041   CHECK("memfs", oskit_memfs_init(osenv, &fs));
00042   CHECK("getroot", oskit_filesystem_getroot(fs, &root));
00043   CHECK("fsnamespace", oskit_create_fsnamespace(root, root, &fsnamespace)); 
00044   CHECK("setfsnamespace",oskit_clientos_setfsnamespace(fsnamespace));
00045 
00046 #undef CHECK
00047 }
00048 
00049 oskit_error_t fs_gettime(struct oskit_timespec *tsp)
00050 {
00051   struct timeval now;
00052   gettimeofday(&now, NULL);
00053 
00054   tsp->tv_sec = now.tv_sec;
00055   tsp->tv_nsec = now.tv_usec * 1000;
00056 
00057   return 0;
00058 }
00059 
00060 int gethostname(char *s, int len)
00061 {
00062   strncpy(s, "mzscheme-machine", len);
00063   return 0;
00064 }
00065 
00066 static void reset_console_input(void)
00067 {
00068   /* So the "press any key to reboot" works: */
00069   direct_cons_set_flags(0);
00070 }
00071 
00072 /*    *********** OSKIT filesystem/ethernet START *****************    */
00073 # ifdef OSK_FILESYSTEMS_AND_ETHERNET
00074 
00075 #  include <oskit/c/fs.h>
00076 #  include <oskit/dev/dev.h>
00077 #  include <oskit/fs/filesystem.h>
00078 #  include <oskit/fs/dir.h>
00079 #  include <oskit/diskpart/diskpart.h>
00080 #  include <oskit/fs/linux.h>
00081 #  include <oskit/dev/linux.h>
00082 #  include <oskit/principal.h>
00083 #  include <oskit/net/freebsd.h>
00084 
00085 static oskit_principal_t *cur_principal;
00086 static oskit_filesystem_t *main_fs;
00087 
00088 static void unmount(void)
00089 {
00090   if (main_fs) {
00091     printf(">> Flushing and unmounting filesystem\n");
00092     fflush(NULL);
00093     oskit_clientos_setfsnamespace(NULL);
00094     oskit_filesystem_sync(main_fs, 1);
00095     oskit_filesystem_release(main_fs);
00096     main_fs = NULL;
00097   }
00098 }
00099 
00100 int start_linux_fs(char *diskname, char *partname, int net)
00101 {
00102   oskit_error_t err;
00103   oskit_identity_t id;
00104   oskit_blkio_t *disk;
00105   oskit_blkio_t *part;
00106   oskit_filesystem_t *fs;
00107   oskit_dir_t *root;
00108   oskit_fsnamespace_t *fsnamespace;
00109 # define MAX_PARTS 30
00110   diskpart_t part_array[MAX_PARTS];
00111   int num_parts;
00112 
00113 # define CHECK(what, f) \
00114   if ((err = f)) { printf("filesystem init error at " what ": %x\n", err); return 0; }
00115 
00116   printf(">> Initializing devices\n");
00117   oskit_dev_init(osenv);
00118   oskit_linux_init_osenv(osenv);
00119   oskit_linux_init_ide();
00120   if (net)
00121     oskit_linux_init_net();
00122   oskit_linux_init_scsi();
00123   printf(">> Probing devices\n");
00124   oskit_dev_probe();
00125   printf(">> Filesystem initialization\n");
00126   CHECK("fsinit", fs_linux_init());
00127 
00128   id.uid = 0;
00129   id.gid = 0;
00130   id.ngroups = 0;
00131   id.groups = 0;
00132   printf(">> Making principal\n");
00133   CHECK("makeprincipal", oskit_principal_create(&id, &cur_principal));
00134 
00135   printf(">> Opening disk %s\n", diskname);
00136   CHECK("diskopen", oskit_linux_block_open(diskname, OSKIT_DEV_OPEN_ALL, &disk));
00137 
00138   printf(">> Reading partitions\n");
00139   num_parts = diskpart_blkio_get_partition(disk, part_array, MAX_PARTS);
00140   printf(">> Found %d partitions, looking for %s\n", num_parts, partname);
00141   if (diskpart_blkio_lookup_bsd_string(part_array, partname, disk, &part) == 0) {
00142     printf("can't find partition %s\n", partname);
00143     return 0;
00144   }
00145 
00146   oskit_blkio_release(disk); /* (Partition has a ref.) */
00147 
00148   printf(">> Mounting filesystem\n");
00149   CHECK("mount", fs_linux_mount(part, 0, &fs));
00150 
00151   printf(">> Getting root\n");
00152   CHECK("getroot", oskit_filesystem_getroot(fs, &root));
00153   CHECK("fsnamespace", oskit_create_fsnamespace(root, root, &fsnamespace)); 
00154   CHECK("setfsnamespace",oskit_clientos_setfsnamespace(fsnamespace));
00155   
00156   /* fs has root: */
00157   oskit_dir_release(root);
00158   /* clientos has namespace: */
00159   oskit_fsnamespace_release(fsnamespace);
00160 
00161   main_fs = fs;
00162   atexit(unmount);
00163 
00164   return 1;
00165 # undef CHECK
00166 }
00167 
00168 static int start_freebsd_enet(char *addr, char *mask, char *gate, int fs)
00169 {
00170   oskit_socket_factory_t *factory;
00171   struct oskit_freebsd_net_ether_if *eif;
00172   oskit_error_t err;
00173 # define CHECK(what, f) \
00174   if ((err = f)) { printf("ethernet init error at " what ": %x\n", err); return 0; }
00175 
00176   if (!fs) {
00177     /* Otherwise, fs initialization does this: */
00178     printf(">> Initializing devices\n");
00179     oskit_dev_init(osenv);
00180     oskit_linux_init_osenv(osenv);
00181     oskit_linux_init_net();
00182     printf(">> Probing devices\n");
00183     oskit_dev_probe();
00184   }
00185 
00186   printf(">> Initializing ethernet\n");
00187   CHECK("socket creator", oskit_freebsd_net_init(osenv, &factory));
00188   oskit_register(&oskit_socket_factory_iid, (void *)factory);
00189 
00190   printf(">> Finding ethernet device\n");
00191   CHECK("open", oskit_freebsd_net_open_first_ether_if(&eif));
00192 
00193   printf(">> Configuring ethernet: %s %s %s\n", addr, mask, gate);
00194   CHECK("ifconfig", oskit_freebsd_net_ifconfig(eif, "eth0", addr, mask));
00195   CHECK("gateway", oskit_freebsd_net_add_default_route(gate));
00196 
00197   return 1;
00198 # undef CHECK
00199 }
00200 
00201 oskit_error_t oskit_get_call_context(const struct oskit_guid *iid, void **out_if)
00202 {
00203   if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
00204       memcmp(iid, &oskit_principal_iid, sizeof(*iid)) == 0) {
00205     *out_if = cur_principal;
00206     oskit_principal_addref(cur_principal);
00207     return 0;
00208   }
00209   
00210   *out_if = 0;
00211   return OSKIT_E_NOINTERFACE;
00212 }
00213 
00214 # endif
00215 /*    *********** OSKIT filesystem/ethernet END *****************    */
00216 
00217 #ifndef OSKIT_TEST
00218 
00219 static void oskit_prepare(int *_argc, char ***_argv)
00220 {
00221   int argc = *_argc;
00222   char **argv = *_argv;
00223 
00224   oskit_clientos_init();
00225   osenv = oskit_osenv_create_default();
00226   oskit_register(&oskit_osenv_iid, (void *)osenv);
00227   start_clock();
00228   oskit_init_libc();
00229 
00230 # ifdef OSK_FILESYSTEMS_AND_ETHERNET
00231   {
00232     /* To hardwire a disk or network configuration, set
00233        `fs' or `net' to 1 and set the initial values of the
00234        corresponding configuration variables: */
00235     int fs = 0;
00236     char *disk = "hda";
00237     char *partition = "f";
00238     int net = 0;
00239     char *addr = "155.99.212.55";
00240     char *mask = "255.255.255.0";
00241     char *gate = "155.99.212.126";
00242 
00243     while (1) {
00244       if (argc > 1) {
00245        if (!strcmp(argv[1], "--fs")) {
00246          if (argc >= 4) {
00247            fs = 1;
00248            disk = argv[2];
00249            partition = argv[3];
00250            memmove(argv + 1, argv + 4, (argc - 4) * sizeof(char **));
00251            argc -= 3;
00252          } else {
00253            argc = 1;
00254            printf("--fs requires two arguments: <disk> <partition>\n");
00255            break;
00256          }
00257        } else if (!strcmp(argv[1], "--net")) {
00258          if (argc >= 5) {
00259            net = 1;
00260            addr = argv[2];
00261            mask = argv[3];
00262            gate = argv[4];
00263            memmove(argv + 1, argv + 5, (argc - 5) * sizeof(char **));
00264            argc -= 4;
00265          } else {
00266            argc = 1;
00267            printf("--net requires three arguments: <address> <netmask> <gateway>\n");
00268            break;
00269          }
00270        } else
00271          break;
00272       } else
00273        break;
00274     }
00275 
00276     if (fs) {
00277       if (!start_linux_fs(disk, partition, net)) {
00278        printf("Disk filesystem init failed; using in-memory filesystem.\n");
00279        start_memfs();
00280       }
00281     } else {
00282       printf("No disk or partition specified; using in-memory filesystem.\n");
00283       start_memfs();
00284     }
00285   
00286     if (!net || !start_freebsd_enet(addr, mask, gate, fs))
00287       printf("No ethernet; TCP connections will fail.\n"); 
00288   }
00289 # else
00290   {
00291     start_memfs();
00292   }
00293 # endif
00294 
00295 # ifdef USE_OSKIT_CONSOLE
00296   /* We talk to console directly */
00297   direct_cons_set_flags(DC_NONBLOCK | DC_RAW);
00298   atexit(reset_console_input);
00299 # else
00300   /* C library handles console; needs liboskit_freebsd_dev.a. */
00301   /* (Initialization here conflicts with OSK_FILESYSTEMS_AND_ETHERNET). */
00302   oskit_dev_init();
00303   oskit_freebsd_init_sc();
00304   oskit_dev_probe();
00305   oskit_console_init();
00306 # endif
00307 
00308   *_argc = argc;
00309   *_argv = argv;
00310 }
00311 
00312 #endif
00313 
00314 #endif /* OSKIT */