gonzui


Format: Advanced Search

tkernel_2/monitor/cmdsvc/src/boot.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:  *      boot.c
   17:  *
   18:  *       boot processing
   19:  */
   20: 
   21: #include "cmdsvc.h"
   22: 
   23: /*
   24:  * boot information
   25:  *       Information passed to primary boot loader (PBOOT).
   26:  *      This address (&bootinfo) is passed to the primary boot loader and is directly referenced.
   27:  */
   28: LOCAL BootInfo  bootInfo;
   29: 
   30: /*
   31:  * Loading of the primary bootloader (PBOOT).
   32:  *      return value     boot partition number
   33:  */
   34: LOCAL W loadPBoot( const UB *devnm, DISKCB **dcb_p )
   35: {
   36:         W      retry = 2;
   37:         W      pno;
   38:         DISKCB *dcb;
   39:         ER     err;
   40: 
   41:         while ( --retry >= 0 ) {
   42: 
   43:                 /* opening of a disk device */
   44:                 pno = openDisk(devnm, &dcb);
   45:                 if ( pno < E_OK ) {
   46:                         err = pno;
   47:                         if ( err == E_IO ) continue; /* retry */
   48:                         return err;
   49:                 }
   50: 
   51:                 /* If there is no partition specification in the device name, we assume the boot partition. */
   52:                 if ( pno == 0 ) pno = dcb->boot;
   53: 
   54:                 /* read the boot block inside the target partiion */
   55:                 err = (*dcb->rwdisk)(dcb, dcb->part[pno].sblk, 1,
   56:                                                 PBootAddr, FALSE);
   57:                 if ( err < E_OK ) {
   58:                         if ( err == E_IO ) continue; /* retry */
   59:                         return err;
   60:                 }
   61: 
   62:                 /* check the signature in the boot block */
   63:                 if ( *(UH*)(PBootAddr + 510) != BootSignature ) return E_BOOT;
   64: 
   65:                 *dcb_p = dcb;
   66:                 return pno;
   67:         }
   68: 
   69:         return E_IO;
   70: }
   71: 
   72: /*
   73:  * disk boot
   74:  *       devnm   device name (possibly with the partition number)
   75:  *               if it is NULL, the standard search order is used to look for a bootable device.
   76:  *       return value error code
   77:  */
   78: EXPORT ER bootDisk( const UB *devnm )
   79: {
   80:         DISKCB *dcb;
   81:         W      pno, i, c;
   82:         ER     err;
   83: 
   84:         if ( devnm != NULL ) {
   85:                 /* boot from the specified device */
   86:                 pno = loadPBoot(devnm, &dcb);
   87:                 if ( pno < E_OK ) return pno;
   88:         } else {
   89:                 /* Boot using the standard boot order */
   90:                 pno = E_BOOT;
   91:                 for ( i = 0;; i++ ) {
   92:                         devnm = bootDevice(i);
   93:                         if ( devnm == NULL ) break; /* end is seen */
   94: 
   95:                         pno = loadPBoot(devnm, &dcb);
   96:                         if ( pno >= 0 ) break;
   97:                 }
   98:         }
   99:         if ( pno >= 0 ) {
  100:                 /* Length of the device name without the partition number */
  101:                 i = strlen(devnm);
  102:                 c = devnm[i - 1];
  103:                 if ( i >= 2 && c >= '0' && c <= '3' ) --i;
  104: 
  105:                 /* Set boot information */
  106:                 strncpy(bootInfo.devnm, devnm, L_DEVNM);
  107:                 bootInfo.devnm[i] = '\0'; /* erase partition number */
  108:                 bootInfo.part  = pno - 1;
  109:                 bootInfo.start = dcb->part[pno].sblk;
  110:                 bootInfo.secsz = dcb->blksz;
  111: 
  112:                 /* prepare for primary boot execution */
  113:                 setUpBoot(PBootAddr, &bootInfo);
  114: 
  115:         } else {
  116:                 /* if boot from disk fails
  117:                    try invoking ROM kernel */
  118:                 err = bootROM();
  119:                 if ( err < E_OK ) {
  120:                         if ( err != E_ABORT ) err = pno;
  121:                         return err;
  122:                 }
  123:         }
  124: 
  125:         return E_OK;
  126: }