openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

main.c (3261B)


      1 /* Copyright 1999, Sidney Cammeresi. All rights reserved.
      2 ** Distributed under the terms of the OpenBLT License
      3 */
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include <errno.h>
      9 #include <ctype.h>
     10 #include <blt/syscall.h>
     11 #include <blt/namer.h>
     12 #include <blt/os.h>
     13 #include <blt/blkdev.h>
     14 #include <blt/disk.h>
     15 #include "ide-int.h"
     16 
     17 void ide_btochs (int block, ide_dev_t *dev, int *cyl, int *head, int *sect)
     18 {
     19 	*cyl = block / (dev->hwdev->heads * dev->hwdev->sectors);
     20 	block %= dev->hwdev->heads * dev->hwdev->sectors;
     21 	*head = block / dev->hwdev->sectors;
     22 	block %= dev->hwdev->sectors;
     23 	*sect = block;
     24 }
     25 
     26 void ide_main (int *ready)
     27 {
     28 	char *c, dev[3];
     29 	int a, b, i, port, txnlen, reslen, bus, device, offset;
     30 	msg_hdr_t mh;
     31 	blktxn_t *txn;
     32 	blkres_t *res;
     33 	disk_t *disk;
     34 
     35 	if (!ide_probe_devices ())
     36 	{
     37 		printf ("ide: no devices found; exiting.\n");
     38 		os_terminate (1);
     39 	}
     40 
     41 	port = port_create (0, "ide");
     42 	namer_register (port, "ide");
     43 	txn = malloc (txnlen = sizeof (blktxn_t) + 1024);
     44 	res = malloc (reslen = sizeof (blkres_t) + 1024);
     45 	*ready = 1;
     46 
     47 	for (;;)
     48 	{
     49 		mh.src = 0;
     50 		mh.dst = port;
     51 		mh.data = txn;
     52 		mh.size = txnlen;
     53 		old_port_recv (&mh);
     54 
     55 		switch (txn->cmd)
     56 		{
     57 			case BLK_CMD_OPEN:
     58 				c = (char *) (txn + 1);
     59 				if (!isdigit (c[0]) || !isdigit (c[2]) || (c[1] != '/') ||
     60 						(c[3] != '/'))
     61 				{
     62 					res->status = ENOENT;
     63 					break;
     64 				}
     65 				bus = c[0] - '0';
     66 				device = c[2] - '0';
     67 				if ((bus >= total_busses) || ((device != 0) && (device != 1)))
     68 					res->status = ENOENT;
     69 				else if (ide_dev[bus * 2 + device] == NULL)
     70 					res->status = ENOENT;
     71 				else if (!strcmp (c + 4, "raw"))
     72 				{
     73 					res->status = 0;
     74 					res->data[0] = 512;
     75 					res->data[1] = (bus * 2 + device) | (0xff << 8);
     76 					mh.size = sizeof (blkres_t);
     77 				}
     78 				else if (isdigit (c[4]) && !c[5])
     79 				{
     80 					res->status = 0;
     81 					res->data[0] = 512;
     82 					res->data[1] = (bus * 2 + device) | ((c[4] - '0' + 1) << 8);
     83 					mh.size = sizeof (blkres_t);
     84 				}
     85 				else if (isdigit (c[4]) && c[5])
     86 				{
     87 					res->status = 0;
     88 					res->data[0] = 512;
     89 					res->data[1] = (bus * 2 + device) | ((c[4] - '0' + 1) <<
     90 						8) | ((c[5] - 'a' + 1) << 16);
     91 					mh.size = sizeof (blkres_t);
     92 				}
     93 				else
     94 					res->status = ENOENT;
     95 				break;
     96 
     97 			case BLK_CMD_READ:
     98 				if ((txn->device & 0xff) > (total_busses * 2))
     99 				{
    100 					res->status = ENOENT;
    101 					break;
    102 				}
    103 				offset = 0;
    104 				if ((txn->device >> 8))
    105 				{
    106 					a = ((txn->device & 0x0000ff00) >> 8);
    107 					b = ((txn->device & 0x00ff0000) >> 16);
    108 					dev[0] = a ? a - 1 + '0' : 0;
    109 					dev[1] = b ? b - 1 + 'a' : 0;
    110 					dev[2] = 0;
    111 					disk = ide_dev[txn->device & 0xff]->disk;
    112 					for (i = 0; i < disk->numparts; i++)
    113 						if (!strcmp (disk_partition_name (disk, i), dev))
    114 							offset = disk_partition_start (disk, i);
    115 					txn->device &= 0xff;
    116 				}
    117 				res->status = ide_dev[txn->device]->read (txn->device / 2,
    118 					txn->device % 2, res + 1, txn->block + offset);
    119 				mh.size = res->status ? sizeof (blkres_t) : sizeof (blkres_t) +
    120 					512;
    121 				break;
    122 		}
    123 
    124 		mh.dst = mh.src;
    125 		mh.src = port;
    126 		mh.data = res;
    127 		old_port_send (&mh);
    128 	}
    129 }
    130 
    131 int main (void)
    132 {
    133 	volatile int ready;
    134 
    135 	ready = 0;
    136 	thr_create (ide_main, (void *) &ready, "ide");
    137 	while (!ready) ;
    138 	return 0;
    139 }
    140