gonzui


Format: Advanced Search

tkernel_2/monitor/driver/memdisk/src/memdsk.cbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: 
   15: /*
   16:  *      memdsk.c
   17:  *
   18:  *       memory disk
   19:  */
   20: 
   21: #include <tmonitor.h>
   22: #include <tm/tmonitor.h>
   23: #include <sys/rominfo.h>
   24: 
   25: /*
   26:  * memory disk information
   27:  *       Use the ROM disk information in ROM info as the starting point.
   28:  */
   29: typedef struct {
   30:         UW     rd_type;    /* disk type */
   31:         UW     rd_blksz;   /* disk block size */
   32:         UW     rd_saddr;   /* disk start address */
   33:         UW     rd_eaddr;   /* disk end address */
   34: } MDINFO;
   35: 
   36: /*
   37:  * disk types
   38:  */
   39: #define ROMDISK         1
   40: 
   41: /* ------------------------------------------------------------------------ */
   42: 
   43: /*
   44:  * submodule extended SVC function
   45:  *       fno     function code
   46:  *       p1-p3 parameter(s)
   47:  *       er_p    returns error code
   48:  *       return value      if the function is handled, returns TRUE.
   49:  *               if not, returns FALSE
   50:  */
   51: EXPORT BOOL memDiskSVC( W fno, W p1, W p2, W p3, W *er_p )
   52: {
   53:         MDINFO *mdi;
   54:         UW     type;
   55:         W      er, n, v;
   56: 
   57:         switch ( fno ) {
   58:           case TMEF_RDAINFO:   /* ROM disk information */
   59:                 mdi = (MDINFO*)&ROMInfo->rd_type;
   60:                 type = ROMDISK;
   61:                 goto info;
   62: 
   63:           info:
   64:                 if ( mdi->rd_blksz > 0 && mdi->rd_type == type ) {
   65:                         v = (UW)mdi;
   66:                         n = writeMem(p1, &v, sizeof(W), sizeof(W));
   67:                         er = ( n == sizeof(W) )? E_OK: E_MACV;
   68:                 } else {
   69:                         er = E_NOEXS;
   70:                 }
   71:                 break;
   72: 
   73:           default:
   74:                 return FALSE; /* unsupported */
   75:         }
   76: 
   77:         *er_p = er;
   78:         return TRUE;
   79: }
   80: 
   81: /* ------------------------------------------------------------------------ */
   82: 
   83: /*
   84:  * I/O functions
   85:  *       blk     start block number
   86:  *               this is not a block number within a partition, but
   87:  *               it is a disk-wide block number unique inside the whole disk.
   88:  *       nblk    number of blocks
   89:  *       buf     buffer (* )
   90:  *       wrt     FALSE : read
   91:  *               TRUE  : write
   92:  *       return value error code
   93:  *       argument marked with (* ) may be an address specified from external sources.
   94:  */
   95: LOCAL ER rwdisk( DISKCB *dcb, W blk, W nblk, void *buf, BOOL wrt )
   96: {
   97:         MDINFO *mdi = (MDINFO*)dcb->info;
   98:         W      sz, asz;
   99:         void   *adr;
  100: 
  101:         if ( dcb->blksz <= 0 ) return E_NOEXS;  /* not yet initialized */
  102: 
  103:         adr = (void*)(mdi->rd_saddr + blk * dcb->blksz);
  104:         sz = nblk * dcb->blksz;
  105: 
  106:         if ( wrt ) {
  107:                 /* write */
  108:                 if ( mdi->rd_type == ROMDISK ) return E_RONLY;
  109:                 asz = readMem((UW)buf, adr, sz, 1);
  110:         } else {
  111:                 /* read */
  112:                 asz = writeMem((UW)buf, adr, sz, 1);
  113:         }
  114:         if ( asz < sz ) return E_IO;
  115: 
  116:         return E_OK;
  117: }
  118: 
  119: /*
  120:  * initialization processing
  121:  *       disk drive that is supported by the initialization by CFGDISK
  122:  *       disk drive is initialized and DISKCB is set up.
  123:  *       DISKCB is given in 0-cleared state initially. Subsequently,
  124:  *       DISKCB returned in the previous call is passed.
  125:  *       I/O function receives this DISKCB.
  126:  *
  127:  *       In principle, this function is called every time an I/O processing is performed.
  128:  *       Hence, there is no need to perform hardware initialization on the second call and afterward.
  129:  *       but whether the hardware status remains as it was the last time the initialization took place is not guaranteed,
  130:  *     so,
  131:  *      it is desirable to initialize hardware from time to as necessary.
  132:  *     If we perform re-initialization, DISKCB is to be re-initialized.
  133:  */
  134: EXPORT ER initMemDisk( DISKCB *dcb, const CFGDISK *cfg )
  135: {
  136:         MDINFO *mdi;
  137:         UW     type;
  138: 
  139:         if ( dcb->blksz > 0 ) return E_OK;  /* already initialized  */
  140: 
  141:         /* select the target disk */
  142:         switch ( cfg->info ) {
  143:           case 0:      /* ROM disk */
  144:                 mdi = (MDINFO*)&ROMInfo->rd_type;
  145:                 type = ROMDISK;
  146:                 break;
  147: 
  148:           default:
  149:                 return E_PAR;
  150:         }
  151: 
  152:         if ( mdi->rd_blksz <= 0 || mdi->rd_type != type ) return E_NOEXS;
  153: 
  154:         /* set up disk drive control block (DISKCB) */
  155:         memset(dcb, 0, sizeof(DISKCB));
  156:         dcb->info         = (UW)mdi;
  157:         dcb->blksz        = mdi->rd_blksz;
  158:         dcb->part[0].sblk = 0;
  159:         dcb->part[0].nblk = (mdi->rd_eaddr - mdi->rd_saddr) / mdi->rd_blksz;
  160:         dcb->rwdisk       = rwdisk;
  161: 
  162:         return E_OK;
  163: }