gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/sysdsk/src/mem.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 T-Engine Forum at 2014/09/10.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: 
   16: 
   17: /*
   18:  *      mem.c        System disk driver
   19:  *
   20:  *      Memory disk access processing
   21:  */
   22: 
   23: #include "sdisk.h"
   24: 
   25: /*
   26:  *      Abort processing
   27:  */
   28: EXPORT  void     memAbort(DrvTab *drv)
   29: {
   30:         /* Execute nothing because it does not enter a wait status in memory disk */
   31: }
   32: 
   33: /*
   34:  *      Reading/Writing of memory block
   35:  */
   36: LOCAL   ER        memRWblks(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write)
   37: {
   38:         void*  madr;
   39: 
   40:         /* Convert to byte unit */
   41:         blk *= drv->SecSize;
   42:         cnt *= drv->SecSize;
   43: 
   44:         /* Writing/Reading to memory */
   45:         madr = (void*)(drv->IOB + blk);        /* Address is logical address */
   46:         if (write) {
   47:                 if (mptr != NULL) MEMCPY(madr, mptr, cnt);
   48:                 else            MEMSET(madr, 0, cnt);
   49:         } else {
   50:                 MEMCPY(mptr, madr, cnt);
   51:         }
   52:         return E_OK;
   53: }
   54: 
   55: /*
   56:  *      Reading/Writing to disk block
   57:  */
   58: EXPORT  INT      memReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write)
   59: {
   60:         ER     er;
   61: 
   62:         /* The validity of  "blk" and "cnt" is already checked */
   63: 
   64:         /* Reading /Writing of memory block  */
   65:         er = memRWblks(drv, blk, cnt, mptr, write);
   66:         if (er >= E_OK) return cnt;
   67:         return er;
   68: }
   69: 
   70: /*
   71:  *      Format processing
   72:  */
   73: EXPORT  ER       memFormat(DrvTab *drv, DiskFormat *fmt, W dcnt)
   74: {
   75:         ER     er;
   76:         W      blk, cnt;
   77: 
   78:         if (*fmt == DiskFmt_MEMINIT) {
   79: 
   80:                 if (dcnt < sizeof(DiskFormat) + sizeof(W) * 2)
   81:                         {er = E_PAR; goto E_EXIT;}
   82: 
   83:                 fmt++;
   84:                 blk = ((W*)fmt)[0];   /* Block size     */
   85:                 cnt = ((W*)fmt)[1];   /* Total number of blocks         */
   86:                 if (cnt < 0 || blk < 512 || blk > 8192 || (blk % 512) != 0)
   87:                         {er = E_PAR; goto E_EXIT;}
   88: 
   89:                 if (drv->IOB != 0) {
   90:                         Kfree((void*)drv->IOB);
   91:                         drv->IOB = 0;
   92:                 }
   93:                 er = E_OK;
   94:                 if (cnt > 0) {
   95:                         if ((drv->IOB = (UW)Kcalloc(blk, cnt)) == 0) {
   96:                                 er = E_NOMEM;
   97:                                 cnt = 0;
   98:                         }
   99:                 }
  100:                 drv->MiscPar = cnt * blk;
  101:                 drv->SecSize = blk;
  102:                 drv->nXSec = drv->nSec = (cnt < 0x10000) ? cnt : 0xffff;
  103:                 drv->s.SUnit[0].EndBlock = cnt;
  104: 
  105:         } else if (*fmt == DiskFmt_MEM) {
  106:                 /* Start block/The number of blocks */
  107:                 blk = drv->s.SUnit[/*drv->CurSUnit*/ 0].StartBlock;
  108:                 cnt = drv->s.SUnit[/*drv->CurSUnit*/ 0].EndBlock - blk;
  109: 
  110:                 /* Initialize the number of valid units in the case of physical unit */
  111:                 /* if (drv->CurSUnit == 0) drv->nSUnit = 0; */
  112: 
  113:                 /* Write 0 into memory block */
  114:                 er = memRWblks(drv, blk, cnt, NULL, TRUE);
  115:         } else {
  116:                 er = E_PAR;
  117:         }
  118:         if (er >= E_OK) return E_OK;
  119: 
  120: E_EXIT:
  121:         return er;
  122: }
  123: 
  124: /*
  125:  *      Reading of disk information
  126:  */
  127: EXPORT  ER       memIdentify(DrvTab *drv, BOOL check)
  128: {
  129:         W      sz;
  130: 
  131:         /* Media shall be always existent */
  132:         drv->MediaOK = TRUE;
  133: 
  134:         if (check) return E_OK;
  135: 
  136:         sz = drv->MiscPar;                     /* The total memory size  */
  137:         sz /= drv->SecSize;                    /* The total number of sectors   */
  138: 
  139:         /* Set the information */
  140:         drv->nXSec = drv->nSec = (sz < 0x10000) ? sz : 0xffff;
  141:         drv->nXHead = drv->nHead = 1;          /* Dummy        */
  142:         drv->nXCyl = drv->nCyl = 1;            /* Dummy  */
  143: 
  144:         /* Set the physical unit */
  145:         drv->s.SUnit[0].SystemId = DSID_NONE;
  146:         drv->s.SUnit[0].StartBlock = 0;
  147:         drv->s.SUnit[0].EndBlock = sz;         /* The total number of sectors */
  148:         drv->s.SUnit[0].OpenCnt = 0;
  149:         return E_OK;
  150: }
  151: /*
  152:  *      Disk initialization
  153:  */
  154: EXPORT  ER       memDiskInit(DrvTab *drv)
  155: {
  156:         /* Subunit does not exist in memory disk */
  157:         drv->nSUnit = 0;
  158:         return E_OK;
  159: }
  160: /*
  161:  *      Various processings
  162:  */
  163: EXPORT  ER       memMisc(DrvTab *drv, W cmd)
  164: {
  165: #if     0
  166:         switch(cmd) {
  167:         case DC_TSKINIT:       /* Task initialization processing    (Top drv)        */
  168:         case DC_TSKTMO:                /* Task time-out processing          (Top drv)        */
  169:         case DC_DRVCHG:                /* Drive change processing         (Target drv)  */
  170:         case DC_OPEN:          /* Open processing                   (Target drv)   */
  171:         case DC_CLOSE:         /* Close processing                 (Target drv) */
  172:         case DC_CLOSEALL:      /* Close processing               (Target drv)        */
  173:         }
  174: #endif
  175:         return E_OK;
  176: }