⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usrfdiskpartlib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 4 页
字号:
*/LOCAL STATUS useFdiskPartParse    (    CBIO_DEV_ID dev,        	/* device from which to read blocks */    CBIO_PARAMS * pCbioParams, 	/* ptr to CBIO device parameters */    PART_TABLE_ENTRY *pPartTab, /* table where to fill results */    int nPart,               	/* # of entries in <pPartTable> */    ULONG startBlock,		/* where to expect part table, use zero */    ULONG extStartBlock		/* Offset to extended partition, use zero */    )    {    u_char * secBuf;		/* sector data we work on */    int i;                      /* used for loop through parts  */    int partOffset;		/* offset of part tab entry in block */    int tableIndex = 0;		/* where in table to write partition geo */    u_char partBootType, partSysType ;    STATUS stat;    /* allocate a local secBuf for the read sectors MBR/Part data */    if  ((secBuf = malloc (BYTES_PER_SECTOR)) == NULL)        {        printErr ("usrFdiskPartParse: Error allocating sector buffer.\n");        return (ERROR);        }    bzero( (char *)secBuf, BYTES_PER_SECTOR);    /* Get current sector containing part table into buffer */    stat = cbioBlkRW( dev, startBlock, 1, (addr_t)secBuf,				CBIO_READ, NULL );    if( stat == ERROR )        {        printErr ("usrFdiskPartLib error: "                   "Failed to read block %d\n", startBlock );	free (secBuf);        return (ERROR);        }    /* Check the validity of partition data with 0x55aa signature */    if ((secBuf[PART_SIG_ADRS  ] != PART_SIG_MSB) ||         (secBuf[PART_SIG_ADRS+1] != PART_SIG_LSB))        {	if( startBlock == 0)	    {	    printErr ("usrFdiskPartLib warning: " 		      "disk partition table is not initialized\n");	    printErr ("usrFdiskPartLib warning: "		     "use usrFdiskPartCreate( %#x, 1) to initialize the "		     "disk as a single partition\n", dev );	    pPartTab [ 0 ].nBlocks = pCbioParams->cbio_nBlocks;	    pPartTab [ 0 ].offset = 0;	    pPartTab [ 0 ].flags = 0xeeee ;	    free (secBuf);	    return(1);	    }        printErr ("usrFdiskPartLib error: "                   "Sector %d contains an invalid "                  "MSDOS partition signature 0x55aa.\n", startBlock);	free (secBuf);        return (ERROR);        }    /*     * Loop through all parts add mountable parts to table, will call     * itself recursively to parse extended partitions & logical     * drives in extended parts.  Recursive functions may use a lot     * of stack space. Developer should beware of stack overflow.     */    for (i=0 ; i<4 ; i++)        {	/* partition offset relative to DOS_BOOT_PART_TBL */	partOffset = DOS_BOOT_PART_TBL + i * 16 ;        /* if it is a bootable & non-extended partition, add first */	partBootType = secBuf[ partOffset + BOOT_TYPE_OFFSET] ;	partSysType  = secBuf[ partOffset + SYSTYPE_OFFSET] ;	/* XXX - for MS-DOS consistency, Bootable partitions should be first */        /* continue to next loop iteration if an empty partition */        if (partSysType == 0)            {            continue;            }	/* XXX - make this more logical, if not extended and if valid FS */        if (((partSysType != PART_TYPE_DOSEXT)      &&             (partSysType != PART_TYPE_WIN95_EXT))  &&                  ((partSysType == PART_TYPE_DOS4)     ||             (partSysType == PART_TYPE_WIN95_D4) ||             (partSysType == PART_TYPE_DOS3)     ||             (partSysType == PART_TYPE_DOS32)    ||             (partSysType == PART_TYPE_DOS32X)   ||             (partSysType == PART_TYPE_DOS12)))            {            /* Fill the node with the current partition tables data */ 	    pPartTab [ tableIndex ].nBlocks =		DISK_TO_VX_32( &secBuf[ partOffset + NSECTORS_TOTAL]) ;	    /* offset in part table is relative to table location */	    pPartTab [ tableIndex ].offset =		startBlock +		DISK_TO_VX_32( &secBuf[ partOffset + NSECTORS_OFFSET]) ;	    /* type of partition */	    pPartTab [ tableIndex ].flags =		partSysType | (partBootType << 8 ) ;	    tableIndex ++ ; /* next partition in next entry */            }        else if ((partSysType == PART_TYPE_DOSEXT) ||            (partSysType == PART_TYPE_WIN95_EXT))            {	    /* found an extended partition, call ourselves to parse.	     * For primary partitions, the Relative Sectors 	     * field represents the offset from the beginning 	     * of the disk to the beginning of the partition, 	     * counting by sectors. The Number of Sectors field 	     * represents the total number of sectors in the partition. 	     * Extended partition have the System ID byte in the 	     * Partition Table entry is set to 5.  Within the extended 	     * partition, you can create any number of logical drives. 	     * The primary partition points to an extended which can have	     * multiple "logical drives".	     * When you have an extended partition on the hard disk, 	     * the entry for that partition in the Partition Table 	     * (at the end of the Master Boot Record) points to the 	     * first disk sector in the extended partition.  	     * The first sector of each logical drive in an extended 	     * partition also has a Partition Table, with four entries 	     * (just like the others).  The first entry is for the 	     * current logical drive.  The second entry contains 	     * information about the next logical drive in the 	     * extended partition. Entries three and four are all 	     * zeroes.  This format repeats for every logical drive. 	     * The last logical drive has only its own partition entry 	     * listed. The entries for partitions 2-4 are all zeroes.  	     * The Partition Table entry is the only information 	     * on the first side of the first cylinder of each 	     * logical drive in the extended partition. The entry 	     * for partition 1 in each Partition Table contains the 	     * starting address for data on the current logical drive. 	     * And the entry for partition 2 is the address of the 	     * sector that contains the Partition Table for the next 	     * logical drive. 	     * 	     * The use of the Relative Sector and Total Sectors 	     * fields for logical drives in an extended partition 	     * is different than for primary partitions. 	     * 	     * For the partition 1 entry of each logical drive, the 	     * Relative Sectors field is the sector from the beginning 	     * of the logical drive that contains the Partition Boot 	     * Sector. The Total Sectors field is the number of sectors 	     * from the Partition Boot Sector to the end of the logical 	     * drive.   	     * 	     * For the partition 2 entry, the Relative Sectors field is 	     * the offset from the beginning of the extended partition 	     * to the sector containing the Partition Table for the logical 	     * drive defined in the Partition 2 entry. The Total Sectors 	     * field is the total size of the logical drive defined in 	     * the Partition 2 entry.	     */	    if (startBlock == 0) 	/* In the MBR, logical sector zero */		{  		extStartBlock = DISK_TO_VX_32			(&secBuf[ partOffset + NSECTORS_OFFSET]);			startBlock = extStartBlock; /* startBlock = extended start */		}	    else		/* In extended partition/logical drive */		{		startBlock = DISK_TO_VX_32			(&secBuf[ partOffset + NSECTORS_OFFSET]);			startBlock += extStartBlock;		}            /* Call ourselves to parse extended (sub) partition table */            stat = useFdiskPartParse (dev,	/* same device */			pCbioParams,		/* CBIO device parameters */			pPartTab + tableIndex,	/* next tab entry to fill */			nPart - tableIndex,	/* # left entries */			startBlock, 		/* where ext starts */			extStartBlock		/* where main ext starts */			);	    if( stat == ERROR )		{		free (secBuf);		return ERROR ;		}	    tableIndex += stat ;            }	else	    {	    printErr("usrFdiskPartLib: warning: "		    "invalid partition entry encountered "		    "block %d, entry %d, sys type %x, Skipped\n",		    startBlock, i, partSysType );	    }	/* check for max # of partitions */	if( tableIndex >= nPart )	    {      	    free (secBuf);	    return tableIndex ;            }        /* loop here to next part or end looping */        }    /*     * if signature is ok, but no partitions where found     * on the first level, assume the entire disk is     * one big file system     */    if (tableIndex == 0 && startBlock == 0)	{	pPartTab [ tableIndex ].nBlocks = pCbioParams->cbio_nBlocks;	pPartTab [ tableIndex ].offset = 0;	pPartTab [ tableIndex ].flags = 0x55aa ;	tableIndex ++;	}    free (secBuf);    return (tableIndex);    }/******************************************************************************* usrFdiskPartRead - read an FDISK-style partition table** This function will read and decode a PC formatted partition table* on a disk, and fill the appropriate partition table array with* the resulting geometry, which should be used by the dpartCbio* partition manager to access a partitioned disk with a shared disk* cache.** EXAMPLE:* The following example shows how a hard disk which is expected to have* up to two partitions might be configured, assuming the physical* level initialization resulted in the <blkIoDevId> handle:** .CS* devCbio = dcacheDevCreate( blkIoDevId, 0, 0x20000, "Hard Disk");* mainDevId = dpartDevCreate( devCbio, 2, usrFdiskPartRead )* dosFsDevCreate(  "/disk0a", dpartPartGet (mainDevId, 0), 0,0,0);* dosFsDevCreate(  "/disk0b", dpartPartGet (mainDevId, 1), 0,0,0);* .CE* * RETURNS: OK or ERROR if partition table is corrupt**/STATUS usrFdiskPartRead    (    CBIO_DEV_ID dev,        /* device from which to read blocks */    PART_TABLE_ENTRY *pPartTab, /* table where to fill results */    int nPart               /* # of entries in <pPartTable> */    )    {    CBIO_PARAMS cbioParams;    /* Get CBIO device parameters */    if (ERROR == cbioParamsGet (dev, &cbioParams))	{	return (ERROR);	}    /* Verify the device handle, possibly create wrapper */    if((dev = cbioDevVerify (dev, FALSE)) == NULL)	return (ERROR);    /* The main device has already been RESET before this is called */    if(TRUE == cbioRdyChgdGet(dev))	return (ERROR);    /* FDISK only works with 512 byte sectored disks, yuck ! */    if( cbioParams.cbio_bytesPerBlk != BYTES_PER_SECTOR )	{	printErr ("useFdiskPartParse error: sector size must be 512 bytes\n");	return (ERROR);	}    /* parse the boot partition and fill table with valid partitions */    if  ((useFdiskPartParse(dev, &cbioParams, pPartTab, nPart, 0, 0)) == ERROR)	{	printErr ("useFdiskPartParse error\n");	return (ERROR);	}        return (OK);    }/******************************************************************************* usrFdiskPartCreate - create an FDISK-like partition table on a disk** This function may be used to create a basic PC partition table.* Such partition table however is not intended to be compatible with* other operating systems, it is intended for disks connected to a* VxWorks target, but without the access to a PC which may be used to* create the partition table.** This function is capable of creating only one partition table - the* MBR, and will not create any Bootable or Extended partitions.* Therefore, 4 partitions are supported.** <dev> is a CBIO device handle for an entire disk, e.g. a handle* returned by dcacheDevCreate(), or if dpartCbio is used, it can be either* the Master partition manager handle, or the one of the 0th partition* if the disk does not contain a partition table at all.*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -