Blame SOURCES/0067-libparted-Fix-MacOS-boot-support.patch

003ee8
From 526f3ee2afbe7cd90d282915b7b580d4f356c4eb Mon Sep 17 00:00:00 2001
003ee8
From: Laurent Vivier <laurent@vivier.eu>
003ee8
Date: Fri, 9 Dec 2016 15:10:53 +0100
003ee8
Subject: [PATCH 67/75] libparted: Fix MacOS boot support
003ee8
003ee8
boot_region_length (or BootSize in the MacOS dialect) is the length
003ee8
of the driver code in the driver partition. This length is used
003ee8
to compute the checksum of the driver.
003ee8
003ee8
libparted updates this value by setting the whole size of the partition
003ee8
without computing the checksum of the driver using this size.
003ee8
003ee8
As the checksum is wrong, the driver is broken and cannot be loaded
003ee8
by the MacOS ROM, and thus the disk is not bootable anymore.
003ee8
003ee8
Moreover, parted try to update the driver list and makes it disappear.
003ee8
003ee8
As parted is not able to insert a driver in a partition,
003ee8
the driver is generally inserted by the Apple HD Tool,
003ee8
this patch removes the line updating the driver size.
003ee8
003ee8
We also simplify the driver list scan and fix endianess use.
003ee8
003ee8
This has been tested... and it works, now.
003ee8
003ee8
I have updated a bootable disk with parted on x86_64 and
003ee8
then been able to boot it (again) on a Mac LC III.
003ee8
003ee8
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
003ee8
Signed-off-by: Brian C. Lane <bcl@redhat.com>
003ee8
---
003ee8
 libparted/labels/mac.c | 41 ++++++++++++++++++++---------------------
003ee8
 1 file changed, 20 insertions(+), 21 deletions(-)
003ee8
003ee8
diff --git a/libparted/labels/mac.c b/libparted/labels/mac.c
003ee8
index d8da941..fa4e43f 100644
003ee8
--- a/libparted/labels/mac.c
003ee8
+++ b/libparted/labels/mac.c
003ee8
@@ -411,14 +411,14 @@ _rawpart_has_driver (const MacRawPartition* raw_part, MacDiskData* mac_disk_data
003ee8
 {
003ee8
 	MacDeviceDriver *driverlist;
003ee8
 	uint16_t i;
003ee8
-	uint32_t driver_bs, driver_be, part_be;
003ee8
+	uint32_t start_block, block_count;
003ee8
 
003ee8
+	start_block = PED_BE32_TO_CPU(raw_part->start_block);
003ee8
+	block_count = PED_BE32_TO_CPU(raw_part->block_count);
003ee8
 	driverlist = &mac_disk_data->driverlist[0];
003ee8
 	for (i = 0; i < mac_disk_data->driver_count; i++) {
003ee8
-		driver_bs = driverlist->block;
003ee8
-		driver_be = driver_bs + driverlist->size;
003ee8
-		part_be = raw_part->start_block + raw_part->block_count;
003ee8
-		if (driver_bs >= raw_part->start_block && driver_be <= part_be)
003ee8
+		if (start_block == PED_BE32_TO_CPU(driverlist->block) &&
003ee8
+                    block_count == PED_BE16_TO_CPU(driverlist->size))
003ee8
 			return 1;
003ee8
 		driverlist++;
003ee8
 	}
003ee8
@@ -751,11 +751,12 @@ mac_read (PedDisk* disk)
003ee8
 	if (!ped_disk_delete_all (disk))
003ee8
 		goto error;
003ee8
 
003ee8
-	if (raw_disk->driver_count && raw_disk->driver_count < 62) {
003ee8
+	if (PED_BE16_TO_CPU(raw_disk->driver_count) &&
003ee8
+            PED_BE16_TO_CPU(raw_disk->driver_count) < 62) {
003ee8
 		memcpy(&mac_disk_data->driverlist[0], &raw_disk->driverlist[0],
003ee8
 				sizeof(mac_disk_data->driverlist));
003ee8
-		mac_disk_data->driver_count = raw_disk->driver_count;
003ee8
-		mac_disk_data->block_size = raw_disk->block_size;
003ee8
+		mac_disk_data->driver_count = PED_BE16_TO_CPU(raw_disk->driver_count);
003ee8
+		mac_disk_data->block_size = PED_BE16_TO_CPU(raw_disk->block_size);
003ee8
 	}
003ee8
 
003ee8
 	/* If _disk_analyse_block_size has increased the sector_size,
003ee8
@@ -877,17 +878,16 @@ static void
003ee8
 _update_driver_count (MacRawPartition* part_map_entry,
003ee8
 		      MacDiskData *mac_driverdata, const MacDiskData* mac_disk_data)
003ee8
 {
003ee8
-	uint16_t	i, count_orig, count_cur;
003ee8
-	uint32_t	driver_bs, driver_be, part_be;
003ee8
-
003ee8
-	count_cur = mac_driverdata->driver_count;
003ee8
-	count_orig = mac_disk_data->driver_count;
003ee8
-	for (i = 0; i < count_orig; i++) {
003ee8
-		driver_bs = mac_disk_data->driverlist[i].block;
003ee8
-		driver_be = driver_bs + mac_disk_data->driverlist[i].size;
003ee8
-		part_be = part_map_entry->start_block + part_map_entry->block_count;
003ee8
-		if (driver_bs >= part_map_entry->start_block
003ee8
-				&& driver_be <= part_be) {
003ee8
+	uint16_t	i;
003ee8
+	uint32_t	start_block, block_count;
003ee8
+
003ee8
+	start_block = PED_BE32_TO_CPU(part_map_entry->start_block);
003ee8
+	block_count = PED_BE32_TO_CPU(part_map_entry->block_count);
003ee8
+
003ee8
+	for (i = 0; i < mac_disk_data->driver_count; i++) {
003ee8
+		if (start_block == PED_BE32_TO_CPU(mac_disk_data->driverlist[i].block) &&
003ee8
+		    block_count == PED_BE16_TO_CPU(mac_disk_data->driverlist[i].size)) {
003ee8
+		        uint16_t count_cur = mac_driverdata->driver_count;
003ee8
 			mac_driverdata->driverlist[count_cur].block
003ee8
 				= mac_disk_data->driverlist[i].block;
003ee8
 			mac_driverdata->driverlist[count_cur].size
003ee8
@@ -934,7 +934,6 @@ _generate_raw_part (PedDisk* disk, PedPartition* part,
003ee8
 	strncpy (part_map_entry->type, mac_part_data->system_name, 32);
003ee8
 
003ee8
 	if (mac_part_data->is_driver) {
003ee8
-		mac_part_data->boot_region_length = part->geom.length;
003ee8
 		if (mac_part_data->has_driver)
003ee8
 			_update_driver_count(part_map_entry, mac_driverdata,
003ee8
 					mac_disk_data);
003ee8
@@ -1042,7 +1041,7 @@ write_block_zero (PedDisk* disk, MacDiskData* mac_driverdata)
003ee8
 	raw_disk->block_size = PED_CPU_TO_BE16 (dev->sector_size);
003ee8
 	raw_disk->block_count = PED_CPU_TO_BE32 (dev->length);
003ee8
 
003ee8
-	raw_disk->driver_count = mac_driverdata->driver_count;
003ee8
+	raw_disk->driver_count = PED_CPU_TO_BE16(mac_driverdata->driver_count);
003ee8
 	memcpy(&raw_disk->driverlist[0], &mac_driverdata->driverlist[0],
003ee8
 			sizeof(raw_disk->driverlist));
003ee8
 
003ee8
-- 
003ee8
2.9.3
003ee8