* A bit of rewording and refactoring to make it a little easier to
follow and remove any duplicate code. No (intended) functional change. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27396 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -45,7 +45,6 @@ PartitionMapWriter::PartitionMapWriter(int deviceFD, off_t sessionOffset,
|
|||||||
: fDeviceFD(deviceFD),
|
: fDeviceFD(deviceFD),
|
||||||
fSessionOffset(sessionOffset),
|
fSessionOffset(sessionOffset),
|
||||||
fSessionSize(sessionSize),
|
fSessionSize(sessionSize),
|
||||||
fPTS(NULL),
|
|
||||||
fMap(NULL)
|
fMap(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -82,19 +81,19 @@ PartitionMapWriter::WriteMBR(const PartitionMap *map, bool clearSectors)
|
|||||||
if (clearSectors)
|
if (clearSectors)
|
||||||
memset(sector, 0, SECTOR_SIZE);
|
memset(sector, 0, SECTOR_SIZE);
|
||||||
else
|
else
|
||||||
error = _ReadPTS(0, pts);
|
error = _ReadSector(0, pts);
|
||||||
|
|
||||||
if (error == B_OK) {
|
if (error == B_OK) {
|
||||||
error = _WritePrimary(pts);
|
error = _WritePrimary(pts);
|
||||||
if (error == B_OK)
|
if (error == B_OK)
|
||||||
error = _WriteSector(0, sector);
|
error = _WriteSector(0, pts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the second sector, if desired. We do that to make the partition
|
// Clear the second sector, if desired. We do that to make the partition
|
||||||
// unrecognizable by BFS.
|
// unrecognizable by BFS.
|
||||||
if (error == B_OK && clearSectors) {
|
if (error == B_OK && clearSectors) {
|
||||||
memset(sector, 0, SECTOR_SIZE);
|
memset(sector, 0, SECTOR_SIZE);
|
||||||
error = _WriteSector(SECTOR_SIZE, sector);
|
error = _WriteSector(SECTOR_SIZE, pts);
|
||||||
}
|
}
|
||||||
|
|
||||||
fMap = NULL;
|
fMap = NULL;
|
||||||
@@ -108,35 +107,34 @@ PartitionMapWriter::WriteMBR(const PartitionMap *map, bool clearSectors)
|
|||||||
disk.
|
disk.
|
||||||
|
|
||||||
This function ensures that the connection of the following linked list
|
This function ensures that the connection of the following linked list
|
||||||
of logical partitions will be correct. It do nothing with the connection of
|
of logical partitions will be correct. It does nothing with the connection
|
||||||
previous logical partitions (call this function on previous logical
|
of previous logical partitions (call this function on previous logical
|
||||||
partition to ensure it).
|
partition to ensure it).
|
||||||
|
|
||||||
\param pts Pointer to \c partition_table_sector.
|
\param pts Pointer to \c partition_table_sector.
|
||||||
\param partition Pointer to the logical partition.
|
\param partition Pointer to the logical partition.
|
||||||
*/
|
*/
|
||||||
status_t
|
status_t
|
||||||
PartitionMapWriter::WriteLogical(partition_table_sector *pts,
|
PartitionMapWriter::WriteLogical(partition_table_sector* pts,
|
||||||
const LogicalPartition *partition)
|
const LogicalPartition* partition)
|
||||||
{
|
{
|
||||||
status_t error = (partition ? B_OK : B_BAD_VALUE);
|
if (partition == NULL)
|
||||||
if (error == B_OK) {
|
return B_BAD_VALUE;
|
||||||
if (pts) {
|
|
||||||
error = _WriteExtended(pts, partition, partition->Next());
|
partition_table_sector _pts;
|
||||||
if (error == B_OK)
|
if (pts == NULL) {
|
||||||
error = _WriteSector(partition->PTSOffset(), pts);
|
// no PTS given, use stack based PTS and read from disk first
|
||||||
} else {
|
pts = &_pts;
|
||||||
partition_table_sector _pts;
|
status_t error = _ReadSector(partition->PTSOffset(), pts);
|
||||||
pts = &_pts;
|
if (error != B_OK)
|
||||||
error = _ReadPTS(partition->PTSOffset(), pts);
|
return error;
|
||||||
if (error == B_OK) {
|
|
||||||
error = _WriteExtended(pts, partition, partition->Next());
|
|
||||||
if (error == B_OK)
|
|
||||||
error = _WriteSector(partition->PTSOffset(), pts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return error;
|
|
||||||
|
status_t error = _WriteExtended(pts, partition, partition->Next());
|
||||||
|
if (error != B_OK)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
return _WriteSector(partition->PTSOffset(), pts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteExtendedHead
|
// WriteExtendedHead
|
||||||
@@ -146,40 +144,41 @@ PartitionMapWriter::WriteLogical(partition_table_sector *pts,
|
|||||||
|
|
||||||
Writes the head of linked list describing logical partitions.
|
Writes the head of linked list describing logical partitions.
|
||||||
|
|
||||||
If the \a first_partition is not specified, it only initializes EBR and the
|
If the \a firstPartition is not specified, it only initializes EBR and the
|
||||||
linked list contains no logical partitions.
|
linked list contains no logical partitions.
|
||||||
|
|
||||||
\param pts Pointer to \c partition_table_sector.
|
\param pts Pointer to \c partition_table_sector.
|
||||||
\param first_partition Pointer to the first logical partition.
|
\param firstPartition Pointer to the first logical partition.
|
||||||
*/
|
*/
|
||||||
status_t
|
status_t
|
||||||
PartitionMapWriter::WriteExtendedHead(partition_table_sector *pts,
|
PartitionMapWriter::WriteExtendedHead(partition_table_sector* pts,
|
||||||
const LogicalPartition *first_partition)
|
const LogicalPartition* firstPartition)
|
||||||
{
|
{
|
||||||
LogicalPartition partition;
|
LogicalPartition partition;
|
||||||
if (first_partition)
|
if (firstPartition != NULL)
|
||||||
partition.SetPrimaryPartition(first_partition->GetPrimaryPartition());
|
partition.SetPrimaryPartition(firstPartition->GetPrimaryPartition());
|
||||||
status_t error = B_OK;
|
|
||||||
if (pts) {
|
partition_table_sector _pts;
|
||||||
error = _WriteExtended(pts, &partition, first_partition);
|
if (pts == NULL) {
|
||||||
if (error == B_OK)
|
// no PTS given, use stack based PTS and read from disk first
|
||||||
error = _WriteSector(0, pts);
|
|
||||||
} else {
|
|
||||||
partition_table_sector _pts;
|
|
||||||
pts = &_pts;
|
pts = &_pts;
|
||||||
error = _ReadPTS(0, pts);
|
status_t error = _ReadSector(0, pts);
|
||||||
if (error == B_OK) {
|
if (error != B_OK)
|
||||||
error = _WriteExtended(pts, &partition, first_partition);
|
return error;
|
||||||
if (error == B_OK)
|
|
||||||
error = _WriteSector(0, pts);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return error;
|
|
||||||
|
status_t error = _WriteExtended(pts, &partition, firstPartition);
|
||||||
|
if (error != B_OK)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
return _WriteSector(0, pts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #pragma mark - fill a PTS in memory
|
||||||
|
|
||||||
// _WritePrimary
|
// _WritePrimary
|
||||||
status_t
|
status_t
|
||||||
PartitionMapWriter::_WritePrimary(partition_table_sector *pts)
|
PartitionMapWriter::_WritePrimary(partition_table_sector* pts)
|
||||||
{
|
{
|
||||||
if (pts == NULL)
|
if (pts == NULL)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
@@ -210,7 +209,7 @@ status_t
|
|||||||
PartitionMapWriter::_WriteExtended(partition_table_sector *pts,
|
PartitionMapWriter::_WriteExtended(partition_table_sector *pts,
|
||||||
const LogicalPartition *partition, const LogicalPartition *next)
|
const LogicalPartition *partition, const LogicalPartition *next)
|
||||||
{
|
{
|
||||||
if (!pts || !partition)
|
if (pts == NULL || partition == NULL)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
// write the signature
|
// write the signature
|
||||||
@@ -252,58 +251,68 @@ PartitionMapWriter::_WriteExtended(partition_table_sector *pts,
|
|||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// _ReadPTS
|
// #pragma mark - to/from disk
|
||||||
|
|
||||||
|
// _ReadSector
|
||||||
/*! \brief Reads the sector from the disk.
|
/*! \brief Reads the sector from the disk.
|
||||||
*/
|
*/
|
||||||
status_t
|
status_t
|
||||||
PartitionMapWriter::_ReadPTS(off_t offset, partition_table_sector *pts)
|
PartitionMapWriter::_ReadSector(off_t offset, partition_table_sector* pts)
|
||||||
{
|
{
|
||||||
status_t error = B_OK;
|
|
||||||
if (!pts)
|
|
||||||
pts = fPTS;
|
|
||||||
int32 toRead = sizeof(partition_table_sector);
|
int32 toRead = sizeof(partition_table_sector);
|
||||||
|
// same as SECTOR_SIZE actually
|
||||||
|
|
||||||
// check the offset
|
// check the offset
|
||||||
if (offset < 0 || offset + toRead > fSessionSize) {
|
if (offset < 0 || offset + toRead > fSessionSize) {
|
||||||
error = B_BAD_VALUE;
|
TRACE(("intel: _ReadSector(): bad offset: %Ld\n", offset));
|
||||||
TRACE(("intel: _ReadPTS(): bad offset: %Ld\n", offset));
|
return B_BAD_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
// read
|
// read
|
||||||
} else if (read_pos(fDeviceFD, fSessionOffset + offset, pts, toRead)
|
offset += fSessionOffset;
|
||||||
!= toRead) {
|
if (read_pos(fDeviceFD, offset, pts, toRead) != toRead) {
|
||||||
#ifndef _BOOT_MODE
|
#ifndef _BOOT_MODE
|
||||||
error = errno;
|
status_t error = errno;
|
||||||
if (error == B_OK)
|
if (error == B_OK)
|
||||||
error = B_IO_ERROR;
|
error = B_IO_ERROR;
|
||||||
#else
|
#else
|
||||||
error = B_IO_ERROR;
|
status_t error = B_IO_ERROR;
|
||||||
#endif
|
#endif
|
||||||
TRACE(("intel: _ReadPTS(): reading the PTS failed: %lx\n", error));
|
TRACE(("intel: _ReadSector(): reading the PTS failed: %lx\n", error));
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
return error;
|
|
||||||
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// _WriteSector
|
// _WriteSector
|
||||||
/*! \brief Writes the sector to the disk.
|
/*! \brief Writes the sector to the disk.
|
||||||
*/
|
*/
|
||||||
status_t
|
status_t
|
||||||
PartitionMapWriter::_WriteSector(off_t offset, const void* pts)
|
PartitionMapWriter::_WriteSector(off_t offset,
|
||||||
|
const partition_table_sector* pts)
|
||||||
{
|
{
|
||||||
status_t error = B_OK;
|
int32 toWrite = sizeof(partition_table_sector);
|
||||||
|
// same as SECTOR_SIZE actually
|
||||||
int32 toWrite = SECTOR_SIZE;
|
|
||||||
|
|
||||||
// check the offset
|
// check the offset
|
||||||
if (offset < 0 || offset + toWrite > fSessionSize) {
|
if (offset < 0 || offset + toWrite > fSessionSize) {
|
||||||
error = B_BAD_VALUE;
|
|
||||||
TRACE(("intel: _WriteSector(): bad offset: %Ld\n", offset));
|
TRACE(("intel: _WriteSector(): bad offset: %Ld\n", offset));
|
||||||
|
return B_BAD_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += fSessionOffset;
|
||||||
|
|
||||||
// write
|
// write
|
||||||
} else if (write_pos(fDeviceFD, fSessionOffset + offset, pts, toWrite)
|
if (write_pos(fDeviceFD, offset, pts, toWrite) != toWrite) {
|
||||||
!= toWrite) {
|
status_t error = errno;
|
||||||
error = errno;
|
|
||||||
if (error == B_OK)
|
if (error == B_OK)
|
||||||
error = B_IO_ERROR;
|
error = B_IO_ERROR;
|
||||||
|
|
||||||
TRACE(("intel: _WriteSector(): writing the PTS failed: %lx\n", error));
|
TRACE(("intel: _WriteSector(): writing the PTS failed: %lx\n", error));
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
return error;
|
|
||||||
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,28 +35,34 @@ struct partition_table_sector;
|
|||||||
*/
|
*/
|
||||||
class PartitionMapWriter {
|
class PartitionMapWriter {
|
||||||
public:
|
public:
|
||||||
PartitionMapWriter(int deviceFD, off_t sessionOffset, off_t sessionSize);
|
PartitionMapWriter(int deviceFD,
|
||||||
~PartitionMapWriter();
|
off_t sessionOffset, off_t sessionSize);
|
||||||
|
~PartitionMapWriter();
|
||||||
|
|
||||||
status_t WriteMBR(const PartitionMap *map, bool clearSectors);
|
status_t WriteMBR(const PartitionMap* map,
|
||||||
status_t WriteLogical(partition_table_sector *pts,
|
bool clearSectors);
|
||||||
const LogicalPartition *partition);
|
status_t WriteLogical(partition_table_sector* pts,
|
||||||
status_t WriteExtendedHead(partition_table_sector *pts,
|
const LogicalPartition* partition);
|
||||||
const LogicalPartition *first_partition);
|
status_t WriteExtendedHead(partition_table_sector* pts,
|
||||||
|
const LogicalPartition* firstPartition);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _WritePrimary(partition_table_sector *pts);
|
status_t _WritePrimary(partition_table_sector* pts);
|
||||||
status_t _WriteExtended(partition_table_sector *pts,
|
status_t _WriteExtended(partition_table_sector* pts,
|
||||||
const LogicalPartition *partition, const LogicalPartition *next);
|
const LogicalPartition* partition,
|
||||||
status_t _ReadPTS(off_t offset, partition_table_sector *pts = NULL);
|
const LogicalPartition* next);
|
||||||
status_t _WriteSector(off_t offset, const void* pts = NULL);
|
|
||||||
|
status_t _ReadSector(off_t offset,
|
||||||
|
partition_table_sector* pts);
|
||||||
|
status_t _WriteSector(off_t offset,
|
||||||
|
const partition_table_sector* pts);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int fDeviceFD;
|
int fDeviceFD;
|
||||||
off_t fSessionOffset;
|
off_t fSessionOffset;
|
||||||
off_t fSessionSize;
|
off_t fSessionSize;
|
||||||
partition_table_sector *fPTS; // while writing
|
|
||||||
const PartitionMap *fMap;
|
const PartitionMap* fMap; // while writing
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PARTITION_MAP_WRITER_H
|
#endif // PARTITION_MAP_WRITER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user