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

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