Exemplo n.º 1
0
static PedGeometry*
_generic_apfs_probe (PedGeometry* geom, uint32_t kind)
{
	uint32_t *block;
	PedSector root;
	struct PartitionBlock * part;
	uint32_t blocksize = 1, reserved = 2;

	PED_ASSERT (geom != NULL);
	PED_ASSERT (geom->dev != NULL);
	if (geom->dev->sector_size != 512)
		return NULL;

	/* Finds the blocksize and reserved values of the partition block */
	if (!(part = ped_malloc (PED_SECTOR_SIZE_DEFAULT*blocksize))) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Failed to allocate partition block\n"), __func__);
		goto error_part;
	}
	if (amiga_find_part(geom, part) != NULL) {
		reserved = PED_BE32_TO_CPU (part->de_Reserved);
		blocksize = PED_BE32_TO_CPU (part->de_SizeBlock)
			* PED_BE32_TO_CPU (part->de_SectorPerBlock) / 128;
	}
	free (part);

	/* Test boot block */
	if (!(block = ped_malloc (PED_SECTOR_SIZE_DEFAULT*blocksize))) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Failed to allocate block\n"), __func__);
		goto error_block;
	}
	if (!ped_device_read (geom->dev, block, geom->start, blocksize)) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Couldn't read boot block %llu\n"), __func__, geom->start);
		goto error;
	}
	if (PED_BE32_TO_CPU (block[0]) != kind) {
		goto error;
	}

	/* Find and test the root block */
	root = geom->start+reserved*blocksize;
	if (!ped_device_read (geom->dev, block, root, blocksize)) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Couldn't read root block %llu\n"), __func__, root);
		goto error;
	}
	if (_apfs_probe_root(block, blocksize, kind) == 1) {
		free(block);
		return ped_geometry_duplicate (geom);
	}

error:
	free (block);
error_block:
error_part:
	return NULL;
}
Exemplo n.º 2
0
Arquivo: loop.c Projeto: Excito/parted
static int
loop_write (const PedDisk* disk)
{
	size_t buflen = disk->dev->sector_size;
	char *buf = ped_malloc (buflen);
	if (buf == NULL)
		return 0;

	if (ped_disk_get_partition (disk, 1)) {
		if (!ped_device_read (disk->dev, buf, 0, 1)) {
			free (buf);
			return 0;
		}
		if (strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE)) != 0) {
			free (buf);
			return 1;
                }
		memset (buf, 0, strlen (LOOP_SIGNATURE));
		return ped_device_write (disk->dev, buf, 0, 1);
	}

	memset (buf, 0, buflen);
	strcpy (buf, LOOP_SIGNATURE);

        int write_ok = ped_device_write (disk->dev, buf, 0, 1);
        free (buf);
	return write_ok;
}
Exemplo n.º 3
0
bool LibPartedDevice::readSectors(void* buffer, qint64 offset, qint64 numSectors)
{
    if (!isExclusive())
        return false;

    return ped_device_read(pedDevice(), buffer, offset, numSectors);
}
Exemplo n.º 4
0
static struct AmigaBlock *
_amiga_read_block (const PedDevice *dev, struct AmigaBlock *blk,
                   PedSector block, struct AmigaIds *ids)
{
	if (!ped_device_read (dev, blk, block, 1))
		return NULL;
	if (ids && !_amiga_id_in_list(PED_BE32_TO_CPU(blk->amiga_ID), ids))
		return NULL;
	if (_amiga_checksum (blk) != 0) {
		switch (ped_exception_throw(PED_EXCEPTION_ERROR,
			PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE | PED_EXCEPTION_CANCEL,
			_("%s : Bad checksum on block %llu of type %s."),
			__func__, block, _amiga_block_id(PED_BE32_TO_CPU(blk->amiga_ID))))
		{
			case PED_EXCEPTION_CANCEL :
				return NULL;
			case PED_EXCEPTION_FIX :
				_amiga_calculate_checksum(AMIGA(blk));
				if (!ped_device_write ((PedDevice*)dev, blk, block, 1))
					return NULL;
			case PED_EXCEPTION_IGNORE :
			case PED_EXCEPTION_UNHANDLED :
			default :
				return blk;
		}
	}
	return blk;
}
Exemplo n.º 5
0
PyObject *py_ped_device_read(PyObject *s, PyObject *args) {
    PyObject *ret = NULL;
    PedSector start, count;
    PedDevice *device = NULL;
    char *out_buf = NULL;

    if (!PyArg_ParseTuple(args, "LL", &start, &count)) {
        return NULL;
    }

    device = _ped_Device2PedDevice(s);
    if (device == NULL) {
        return NULL;
    }

    if (!device->open_count) {
        PyErr_Format(IOException, "Device %s is not open.", device->path);
        return NULL;
    }

    if (device->external_mode) {
        PyErr_Format(IOException, "Device %s is already open for external access.", device->path);
        return NULL;
    }

    if ((out_buf = malloc(device->sector_size * count)) == NULL) {
        return PyErr_NoMemory();
    }

    if (ped_device_read(device, out_buf, start, count) == 0) {
        if (partedExnRaised) {
            partedExnRaised = 0;

            if (!PyErr_ExceptionMatches(PartedException) &&
                !PyErr_ExceptionMatches(PyExc_NotImplementedError))
                PyErr_SetString(IOException, partedExnMessage);
        }
        else
            PyErr_Format(IOException, "Could not read from device %s", device->path);

        free(out_buf);
        return NULL;
    }

    ret = PyUnicode_FromString(out_buf);
    free(out_buf);

    return ret;
}
Exemplo n.º 6
0
static int
pc98_probe (const PedDevice *dev)
{
	PC98RawTable		part_table;
	int			empty;
	const PC98RawPartition*	p;

	PED_ASSERT (dev != NULL);

        if (dev->sector_size != 512)
                return 0;

	if (!ped_device_read (dev, &part_table, 0, 2))
		return 0;

	/* check magic */
	if (!pc98_check_magic (&part_table))
		return 0;

	/* check consistency */
	empty = 1;
	for (p = part_table.partitions;
	     p < part_table.partitions + MAX_PART_COUNT;
	     p++)
	{
		if (p->mid == 0 && p->sid == 0)
			continue;
		empty = 0;
		if (!check_partition_consistency (dev, p))
			return 0;
	}

	/* check boot loader */
	if (pc98_check_ipl_signature (&part_table))
		return 1;
	else if (part_table.boot_code[0])	/* invalid boot loader */
		return 0;

	/* Not to mistake msdos disk map for PC-9800's empty disk map  */
	if (empty)
		return 0;

	return 1;
}
Exemplo n.º 7
0
Arquivo: pc98.c Projeto: Excito/parted
static int
pc98_probe (const PedDevice *dev)
{
	PC98RawTable		part_table;

	PED_ASSERT (dev != NULL);

        if (dev->sector_size != 512)
                return 0;

	if (!ped_device_read (dev, &part_table, 0, 2))
		return 0;

	/* check magic */
	if (!pc98_check_magic (&part_table))
		return 0;

	/* check for boot loader signatures */
	return pc98_check_ipl_signature (&part_table);
}
Exemplo n.º 8
0
/** Reads a given number of sectors from the Device into the given buffer.

	Note that @p readOffset must be greater or equal than zero.

	@param buffer the buffer to store the read sectors in
	@param readOffset the offset to begin reading
	@param numSectors the number of sector to read

	@return true if successful
*/
bool CopySourceDevice::readSectors(void* buffer, qint64 readOffset, qint64 numSectors)
{
	Q_ASSERT(readOffset >= 0);
	return ped_device_read(m_PedDevice, buffer, readOffset, numSectors);
}
Exemplo n.º 9
0
Arquivo: pc98.c Projeto: Excito/parted
static int
read_table (PedDisk* disk)
{
	int			i;
	PC98RawTable		table;
	PedConstraint*		constraint_any;

	PED_ASSERT (disk != NULL);
	PED_ASSERT (disk->dev != NULL);

	constraint_any = ped_constraint_any (disk->dev);

	if (!ped_device_read (disk->dev, (void*) &table, 0, 2))
		goto error;

	if (!pc98_check_magic(&table)) {
		if (ped_exception_throw (
			PED_EXCEPTION_ERROR, PED_EXCEPTION_IGNORE_CANCEL,
			_("Invalid partition table on %s."),
			disk->dev->path))
			goto error;
	}

	for (i = 0; i < MAX_PART_COUNT; i++) {
		PC98RawPartition*	raw_part;
		PedPartition*		part;
		PC98PartitionData*	pc98_data;
		PedSector		part_start;
		PedSector		part_end;

		raw_part = &table.partitions [i];

		if (is_unused_partition(raw_part))
			continue;

		part_start = legacy_start (disk, raw_part);
		part_end   = legacy_end (disk, raw_part);

		part = ped_partition_new (disk, PED_PARTITION_NORMAL,
                                          NULL, part_start, part_end);
		if (!part)
			goto error;
		pc98_data = part->disk_specific;
		PED_ASSERT (pc98_data != NULL);

		pc98_data->system = (raw_part->mid << 8) | raw_part->sid;
		pc98_data->boot = GET_BIT(raw_part->mid, 7);
		pc98_data->hidden = !GET_BIT(raw_part->sid, 7);

		ped_partition_set_name (part, raw_part->name);

		pc98_data->ipl_sector = chs_to_sector (
			disk->dev,
			PED_LE16_TO_CPU(raw_part->ipl_cyl),
			raw_part->ipl_head,
			raw_part->ipl_sect);

		/* hack */
		if (pc98_data->ipl_sector == part->geom.start)
			pc98_data->ipl_sector = 0;

		part->num = i + 1;

		if (!ped_disk_add_partition (disk, part, constraint_any))
			goto error;

		if (part->geom.start != part_start
		    || part->geom.end != part_end) {
			ped_exception_throw (
				PED_EXCEPTION_NO_FEATURE,
				PED_EXCEPTION_CANCEL,
				_("Partition %d isn't aligned to cylinder "
				  "boundaries.  This is still unsupported."),
				part->num);
			goto error;
		}

		part->fs_type = ped_file_system_probe (&part->geom);
	}

	ped_constraint_destroy (constraint_any);
	return 1;

error:
	ped_disk_delete_all (disk);
	ped_constraint_destroy (constraint_any);
	return 0;
}
Exemplo n.º 10
0
/* We have already allocated a rdb, we are now reading it from the disk */
struct PartitionBlock *
amiga_find_part (PedGeometry *geom, struct PartitionBlock *part)
{
	struct RigidDiskBlock *rdb;
	uint32_t partblock;
	uint32_t partlist[AMIGA_MAX_PARTITIONS];
	int i;

	PED_ASSERT(geom!= NULL);
	PED_ASSERT(geom->dev!= NULL);

	if (!(rdb = ped_malloc (PED_SECTOR_SIZE_DEFAULT))) {
		switch (ped_exception_throw(PED_EXCEPTION_ERROR,
			PED_EXCEPTION_CANCEL,
			_("%s : Failed to allocate disk_specific rdb block\n"), __func__))
		{
			case PED_EXCEPTION_CANCEL :
			case PED_EXCEPTION_UNHANDLED :
			default :
				return NULL;
		}
	}
	if (_amiga_find_rdb (geom->dev, rdb) == AMIGA_RDB_NOT_FOUND) {
		switch (ped_exception_throw(PED_EXCEPTION_ERROR,
			PED_EXCEPTION_CANCEL,
			_("%s : Didn't find rdb block, should never happen\n"), __func__))
		{
			case PED_EXCEPTION_CANCEL :
			case PED_EXCEPTION_UNHANDLED :
			default :
				free(rdb);
				return NULL;
		}
	}

	/* We initialize the hardblock free list to detect loops */
	for (i = 0; i < AMIGA_MAX_PARTITIONS; i++) partlist[i] = IDNAME_FREE;

	for (i = 1, partblock = PED_BE32_TO_CPU(rdb->rdb_PartitionList);
		i < AMIGA_MAX_PARTITIONS && partblock != IDNAME_FREE;
		i++, partblock = PED_BE32_TO_CPU(part->pb_Next))
	{
		PedSector start, end;
		PedSector cylblocks;

		/* Let's look for loops in the partition table */
		if (_amiga_loop_check(partblock, partlist, i)) {
			free (rdb);
			return NULL;
		}
		/* Let's read a partition block to get its geometry*/
		if (!ped_device_read (geom->dev, part, (PedSector)partblock, 1)) {
			switch (ped_exception_throw(PED_EXCEPTION_ERROR,
				PED_EXCEPTION_CANCEL,
				_("%s : Failed to read partition block %llu\n"),
				__func__, (PedSector)partblock))
			{
				case PED_EXCEPTION_CANCEL :
				case PED_EXCEPTION_UNHANDLED :
				default :
					free(rdb);
					return NULL;
			}
		}

		/* Current block is not a Partition Block */
		if (part->pb_ID != IDNAME_PARTITION) {
			free (rdb);
			return NULL;
		}

		/* Calculate the geometry of the partition */
		cylblocks = ((PedSector) PED_BE32_TO_CPU (part->de_Surfaces)) *
			((PedSector) PED_BE32_TO_CPU (part->de_BlocksPerTrack));
		start = ((PedSector) PED_BE32_TO_CPU (part->de_LowCyl)) * cylblocks;
		end = ((((PedSector) PED_BE32_TO_CPU (part->de_HighCyl))+1) * (cylblocks))-1;

		/* And check if it is the one we are searching for */
		if (start == geom->start && end == geom->end) {
			free (rdb);
			return part;
		}
	}

	free (rdb);
	return NULL;
}
Exemplo n.º 11
0
void MParted::MParted_Core::scan(MParted::Devices & devices) {
    QMutexLocker locker(&mutex);

    // Clean up first
    devices.clear();

    // Initialise
    QStringList devicePaths;
    ped_device_probe_all();

    pedDevice = NULL;
    while ((pedDevice = ped_device_get_next(pedDevice))) {
        // Only add this device if we can read the first sector (which means it's a real device)
        char * buf = static_cast<char *>(malloc(pedDevice->sector_size));
        if (buf) {
            // Confirming device
            if (ped_device_open(pedDevice)) {
                //Devices with sector sizes of 512 bytes and higher are supported
                if (ped_device_read(pedDevice, buf, 0, 1))
                    devicePaths.append(Utils::charToString(pedDevice->path));

                ped_device_close(pedDevice) ;
            }
            free(buf);
        }
    }

    closeDeviceAndDisk();

    // Sort list, remove duplicates and empty entries
    devicePaths.removeDuplicates();
    devicePaths.removeAll("");
    devicePaths.sort();




    for (int i = 0; i < devicePaths.size(); i++) {
        const QString devicePath = devicePaths.at(i);

        // Open device and disk
        if (!openDeviceAndDisk(devicePath, false))
            continue;

        // Add to list and save pointer
        devices.append(MParted::Device());
        MParted::Device &device = devices.last();

        //device info..
        device.path = devicePath;
        device.model 	=	Utils::charToString(pedDevice->model);
        device.length 	=	pedDevice->length;
        device.sector_size	=	pedDevice->sector_size;
        device.heads 	=	pedDevice->bios_geom.heads;
        device.sectors 	=	pedDevice->bios_geom.sectors;
        device.cylinders	=	pedDevice->bios_geom.cylinders;
        device.cylsize 	=	device.heads * device.sectors;
        device.removable = isDeviceRemovable(device);


        //make sure cylsize is at least 1 MiB
        if (device.cylsize < (MEBIBYTE / device.sector_size))
            device.cylsize = (MEBIBYTE / device.sector_size);

        //normal harddisk
        if (pedDisk) {
            device.unkownPartitionTable = false;
            device.diskType =	Utils::charToString(pedDisk->type->name);
            device.maxPrimaryPartitions = ped_disk_get_max_primary_partition_count(pedDisk);

            // Scan partitions
            scanDevicePartitions(device);
        }
        //harddisk without disklabel
        else {
            device.unkownPartitionTable = true;
            device.diskType = QObject::tr("unrecognized");
            device.maxPrimaryPartitions = -1;
        }

        // Clean up
        closeDeviceAndDisk();
    }
}