?? usrfdiskpartlib.c
字號(hào):
/* usrFdiskPartLib.c - FDISK-style partition handler *//* Copyright 1997-2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------02p,10dec01,jkf fixed typo in 02n checkin that broke 4 partition systems.02o,09nov01,jkf SPR#71633, dont set errno when DevCreate is called w/BLK_DEV02n,04oct01,rip SPR#66973, computation overflows with large capacity disks SPR#30850, CBIO_RESET may be needed on device's first reading02m,20sep01,jkf SPR#69031, common code for both AE & 5.x.02l,03mar01,jkf SPR#62415, use sector size from BLK_DEV.02k,26dec99,jkf T3 KHEAP_ALLOC, fixed usrFdiskPartCreate to work w/BLK_DEV02j,31aug99,jkf changes for new CBIO API.02i,31jul99,jkf changed usrFdiskPartCreate. SPR#2828502h,31jul99,jkf reentrancy overhaul. SPR#2827802g,31jul99,jkf fixed blind resetting cbio in show routine. SPR#2827902f,31jul99,jkf fixed bugs introduced in 02a adaptation: useFdiskPartParse offset calculation fixed, SPR#2828002e,31jul99,jkf T2 merge, tidiness & spelling.02d,07dec98,lrn partition table creation routine (SPR#21977), terse Show02c,15sep98,lrn enabled SHOW by default, assume non-partitioned disk if all partitions are nil02b,02jul98,lrn DosFs 2.0 pre-release02a,21jun98,lrn adopted from rev 01c by jkf.01a,01sep97,jkf adapted from dosPartLibAta.c.*//*DESCRIPTION:This module is provided is source code to accommodate variouscustomizations of partition table handling, resulting fromvariations in the partition table format in a particular configuration.It is intended for use with dpartCbio partition manager.This code supports both mounting MSDOS file systems anddisplaying partition tables written by MSDOS FDISK.exe orby any other MSDOS FDISK.exe compatible partitioning software. The first partition table is contained within a hard drivesMaster Boot Record (MBR) sector, which is defined as sectorone, cylinder zero, head zero or logical block address zero.The mounting and displaying routines within this code will first parse the MBR partition tables entries (defined below)and also recursively parse any "extended" partition tables,which may reside within another sector further into the hard disk. MSDOS file systems within extended partitions are knownto those familiar with the MSDOS FDISK.exe utility as "Logical drives within the extended partition".Here is a picture showing the layout of a single disk containingmultiple MSDOS file systems:.CS +---------------------------------------------------------+ |<---------------------The entire disk------------------->| |M | |B<---C:---> | |R /---- First extended partition--------------\| | E<---D:---><-Rest of the ext part------------>| |P x | |A t E<---E:--->E<Rest of the ext part->| |R x x | |T t t<---------F:---------->| +---------------------------------------------------------+ (Ext == extended partition sector) C: is a primary partiion D:, E:, and F: are logical drives within the extended partition..CEA MS-DOS partition table resides within one sector on a harddisk. There is always one in the first sector of a hard diskpartitioned with FDISK.exe. There first partition table maycontain references to "extended" partition tables residing onother sectors if there are multiple partitions. The first sector of the disk is the starting point. Partition tablesare of the format:.CSOffset from the beginning of the sector Description------------- ------------------------- 0x1be Partition 1 table entry (16 bytes) 0x1ce Partition 2 table entry (16 bytes) 0x1de Partition 3 table entry (16 bytes) 0x1ee Partition 4 table entry (16 bytes) 0x1fe Signature (0x55aa, 2 bytes).CEIndividual MSDOS partition table entries are of the format:.CSOffset Size Description------ ---- ------------------------------ 0x0 8 bits boot type 0x1 8 bits beginning sector head value 0x2 8 bits beginning sector (2 high bits of cylinder#) 0x3 8 bits beginning cylinder# (low order bits of cylinder#) 0x4 8 bits system indicator 0x5 8 bits ending sector head value 0x6 8 bits ending sector (2 high bits of cylinder#) 0x7 8 bits ending cylinder# (low order bits of cylinder#) 0x8 32 bits number of sectors preceding the partition 0xc 32 bits number of sectors in the partition.CEThe Cylinder, Head and Sector values herein are not used,instead the 32-bit partition offset and size (also known as LBAaddresses) are used exclusively to determine partition geometry.If a non-partitioned disk is detected, in which case the 0'th blockis a DosFs boot block rather then an MBR, the entire disk will beconfigured as partition 0, so that disks formatted with VxWorks anddisks formatted on MS-DOS or Windows can be accepted interchangeably.The usrFdiskPartCreate() will create a partition table with up to fourpartitions, which can be later used with usrFdiskPartRead() anddpartCbio to manage a partitioned disk on VxWorks.However, it can not be guaranteed that this partition table can be usedon another system due to several BIOS specific paramaters in the bootarea. If interchangeability via removable disks is a requirement, partition tables should be created and volumes should be formatted on the other system with which the data is to be interchanged/CAUTIONThe partition decode function is recursive, up to the maximumnumber of partitions expected, which is no more then 24.Sufficient stack space needs to be provided via taskSpawn() to accommodate the recursion level.SEE ALSO: dpartCbio*//* includes */#include "vxWorks.h"#include "private/dosFsVerP.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "tickLib.h"#include "dosFsLib.h"#include "usrFdiskPartLib.h"#include "private/dosFsLibP.h" /* for byte swapping macros */#include "dpartCbio.h"#include "cbioLib.h"/* defines *//* may be undefine the following to conserve memory space */#define INCLUDE_PART_SHOW /* include the show function by default *//* declarations *//* locals */LOCAL STATUS useFdiskPartParse ( CBIO_DEV_ID dev, /* device from which to read blocks */ CBIO_PARAMS * pCbioParams, PART_TABLE_ENTRY *pPartTab, /* table where to fill results */ int nPart, /* # of entries in <pPartTable> */ ULONG startBlock, /* where to expect part table */ ULONG extStartBlock /* Offset to extended partition */ );#ifdef INCLUDE_PART_SHOWSTATUS usrFdiskPartShow ( CBIO_DEV_ID cbioDev, /* device CBIO handle */ block_t extPartOffset, /* user should pass zero */ block_t currentOffset, /* user should pass zero */ int extPartLevel /* user should pass zero */ );LOCAL const struct partType /* Some partition type values & names. */ { /* Only MSDOS are used in this code. */ const UINT8 partTypeNum; const char *partTypeName; } partNames[] = { {0x00, "Empty (NULL) Partition"}, {0x01, "MSDOS Partition 12-bit FAT"}, {0x02, "XENIX / (slash) Partition"}, {0x03, "XENIX /usr Partition"}, {0x04, "MSDOS 16-bit FAT <32M Partition"}, {0x05, "MSDOS Extended Partition"}, {0x06, "MSDOS 16-bit FAT >=32M Partition"}, {0x07, "HPFS / NTFS Partition"}, {0x08, "AIX boot or SplitDrive Partition"}, {0x09, "AIX data or Coherent Partition"}, {0x0a, "OS/2 Boot Manager Partition"}, {0x0b, "Win95 FAT32 Partition"}, {0x0c, "Win95 FAT32 (LBA) Partition"}, {0x0e, "Win95 FAT16 (LBA) Partition"}, {0x0f, "Win95 Extended (LBA) Partition"}, {0x10, "OPUS Partition"}, {0x11, "Hidden DOS FAT12 Partition"}, {0x12, "Compaq diagnostics Partition"}, {0x14, "Hidden DOS FAT16 Partition"}, {0x16, "Hidden DOS FAT16 (big) Partition"}, {0x17, "Hidden HPFS/NTFS Partition"}, {0x18, "AST Windows swapfile Partition"}, {0x24, "NEC DOS Partition"}, {0x3c, "PartitionMagic recovery Partition"}, {0x40, "Venix 80286 Partition"}, {0x41, "Linux/MINIX (shared with DRDOS) Partition"}, {0x42, "SFS or Linux swap part (shared with DRDOS)"}, {0x43, "Linux native (shared with DRDOS) Partition"}, {0x50, "DM (disk manager) Partition"}, {0x51, "DM6 Aux1 (or Novell) Partition"}, {0x52, "CP/M or Microport SysV/AT Partition"}, {0x53, "DM6 Aux3 Partition"}, {0x54, "DM6 Partition"}, {0x55, "EZ-Drive (disk manager) Partition"}, {0x56, "Golden Bow (disk manager) Partition"}, {0x5c, "Priam Edisk (disk manager) Partition"}, {0x61, "SpeedStor Partition"}, {0x63, "GNU HURD or Mach or Sys V/386 (ISC UNIX)"}, {0x64, "Novell Netware 286 Partition"}, {0x65, "Novell Netware 386 Partition"}, {0x70, "DiskSecure Multi-Boot Partition"}, {0x75, "PC/IX Partition"}, {0x77, "QNX4.x Partition"}, {0x78, "QNX4.x 2nd part Partition"}, {0x79, "QNX4.x 3rd part Partition"}, {0x80, "MINIX until 1.4a Partition"}, {0x81, "MINIX / old Linux Partition"}, {0x82, "Linux swap Partition"}, {0x83, "Linux native Partition"}, {0x84, "OS/2 hidden C: drive Partition"}, {0x85, "Linux extended Partition"}, {0x86, "NTFS volume set Partition"}, {0x87, "NTFS volume set Partition"}, {0x93, "Amoeba Partition"}, {0x94, "Amoeba BBT Partition"}, {0xa0, "IBM Thinkpad hibernation Partition"}, {0xa5, "BSD/386 Partition"}, {0xa7, "NeXTSTEP 486 Partition"}, {0xb7, "BSDI fs Partition"}, {0xb8, "BSDI swap Partition"}, {0xc1, "DRDOS/sec (FAT-12) Partition"}, {0xc4, "DRDOS/sec (FAT-16, < 32M) Partition"}, {0xc6, "DRDOS/sec (FAT-16, >= 32M) Partition"}, {0xc7, "Syrinx Partition"}, {0xdb, "CP/M-Concurrent CP/M-Concurrent DOS-CTOS"}, {0xe1, "DOS access-SpeedStor 12-bit FAT ext."}, {0xe3, "DOS R/O or SpeedStor Partition"}, {0xe4, "SpeedStor 16-bit FAT Ext Part. < 1024 cyl."}, {0xf1, "SpeedStor Partition"}, {0xf2, "DOS 3.3+ secondary Partition"}, {0xf4, "SpeedStor large partition Partition"}, {0xfe, "SpeedStor >1024 cyl. or LANstep Partition"}, {0xff, "Xenix Bad Block Table Partition"}, };#endif /* INCLUDE_PART_SHOW *//******************************************************************************* useFdiskPartParse - parse partitions on given disk** This routine is not intended to be user callable.* * This routine parses all existing partition tables on a disk.* It adds partition node data entries to a table which it has been * passed for any partition which contains a* file system mountable by dosFsLib(). The size (in sectors)* of the partition and the absolute offset (in sectors) from the* start of the drive are also stored, since that is what VxWorks* CBIO device create routines need to overlay partitions. * * The partition table must appear to be valid when checked with* 0x55aa signature. The partition table in the Master Boot Record* will be parsed first. If there are any extended partitions found,* a (recursive) call to itself is made to parse the extended* partition(s) in the new sector(s). Recursive functions may use a lot* of stack space. Developer should beware of stack overflow.** RETURNS: ERROR or a (positive) number of partitions decoded and filled.**/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 = KHEAP_ALLOC(pCbioParams->bytesPerBlk)) == NULL) { printErr ("usrFdiskPartParse: Error allocating sector buffer.\n"); return (ERROR); } bzero( (char *)secBuf, pCbioParams->bytesPerBlk); /* Get current sector containing part table into buffer */ stat = cbioBlkRW( dev, startBlock, 1, (addr_t)secBuf, CBIO_READ, NULL ); /* * SPR#30850. Certain devices when accessed for the first time report * Error erroneously. Once the first read fails, the device might then * work as expected after receiving a RESET. This fix checks for * that condition by forcing a RESET and then retrying the read. * If it passes after the RESET, then it continues as requested. If * it fails again, however, then it assumes that the Error condition will * continue in perpetuity and returns the error code as before. */ if( stat == ERROR ) { if(cbioIoctl(dev, CBIO_RESET, 0) == ERROR) { printErr ("usrFdiskPartParse: error issuing " "CBIO_RESET cmd to device %x, errno %x\n", dev, errno); printErr ("usrFdiskPartParse: device is not ready\n"); KHEAP_FREE(secBuf); return (ERROR); } } /* The reset terminated correctly. Now try the access again. */ stat = cbioBlkRW( dev, startBlock, 1, (addr_t)secBuf, CBIO_READ, NULL ); /* If still returning error, then assume not going to work. */ if( stat == ERROR ) { printErr ("usrFdiskPartParse: error reading "
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -