From cbd923db57f8c7b26d77adb118c01a3cbf70b507 Mon Sep 17 00:00:00 2001
From: Francis Rowe <info@gluglug.org.uk>
Date: Sat, 10 Jan 2015 22:58:32 -0500
Subject: ich9deblob: re-factor main function

---
(limited to 'resources/utilities/ich9deblob')

diff --git a/resources/utilities/ich9deblob/src/ich9deblob.c b/resources/utilities/ich9deblob/src/ich9deblob.c
index d025a64..18b5c81 100644
--- a/resources/utilities/ich9deblob/src/ich9deblob.c
+++ b/resources/utilities/ich9deblob/src/ich9deblob.c
@@ -49,37 +49,18 @@
 
 int main()
 {
-	/*
-	 * descriptor region. Will have an actual descriptor struct mapped to it (from the factory.rom dump)
-	 * and then it will be modified (deblobbed) to remove the ME/AMT
-	 */
-	uint8_t factoryDescriptorBuffer[DESCRIPTORREGIONSIZE];
-	struct DESCRIPTORREGIONRECORD factoryDescriptorStruct;
-	struct DESCRIPTORREGIONRECORD deblobbedDescriptorStruct;
+	struct DESCRIPTORREGIONRECORD descriptorStruct;
+	uint8_t* descriptorBuffer = (uint8_t*)&descriptorStruct;
 	
-	/* 
-	 * gbe region. Well have actual gbe buffer mapped to it (from the factory.rom dump)
-	 * and then it will be modified to correct the main region
-	 */
-	uint8_t factoryGbeBuffer8k[GBEREGIONSIZE_8K];
-	struct GBEREGIONRECORD_8K factoryGbeStruct8k;
-	struct GBEREGIONRECORD_8K deblobbedGbeStruct8k;
-	
-	/*
-	 * Used to store the location of the Gbe
-	 * region inside the factory.rom image.
-	 */
-	uint32_t factoryGbeRegionStart;
+	struct GBEREGIONRECORD_8K gbeStruct8k;
+	uint8_t* gbeBuffer8k = (uint8_t*)&gbeStruct8k;
+	uint32_t gbeRegionStart;
 	
-	/* names of the files that this utility will handle */
-	char* factoryRomFilename = "factory.rom";                       /* user-supplied factory.bin dump (original firmware) */
-	char* deblobbedDescriptorFilename = "deblobbed_descriptor.bin"; /* descriptor+gbe: to be dd'd to beginning of a libreboot image */
+	char* romFilename = "factory.rom";
+	char* descriptorGbeFilename = "deblobbed_descriptor.bin";
 	
-	/* Used when reading the factory.rom to extract descriptor/gbe regions */
 	unsigned int bufferLength;
-	
-	/* For storing the size of the factory.rom dump in bytes */
-	unsigned int factoryRomSize;
+	unsigned int romSize;
 	
 	/*
 	 * ------------------------------------------------------------------
@@ -87,7 +68,7 @@ int main()
 	 * ------------------------------------------------------------------
 	 */
 
-	if (systemOrCompilerIncompatible(factoryDescriptorStruct, factoryGbeStruct8k)) return 1;
+	if (systemOrCompilerIncompatible(descriptorStruct, gbeStruct8k)) return 1;
 	/* If true, fail with error message */
 	
 	/*
@@ -95,92 +76,55 @@ int main()
 	 * Extract the descriptor and gbe regions from the factory.rom dump
 	 * ------------------------------------------------------------------
 	 */
-	FILE* fileStream = NULL;
-	fileStream = fopen(factoryRomFilename, "rb"); /* open factory.rom */
-	if (NULL == fileStream)
+	FILE* fp = NULL;
+	fp = fopen(romFilename, "rb"); /* open factory.rom */
+	if (NULL == fp)
 	{
-		printf("\nerror: could not open factory.rom\n");
-		fclose(fileStream);
+		printf("\nerror: could not open %s\n", romFilename);
+		fclose(fp);
 		return 1;
 	}
-	printf("\nfactory.rom opened successfully\n");
+	printf("\n%s opened successfully\n", romFilename);
 	
 	/* 
 	 * Get the descriptor region dump from the factory.rom
 	 * (goes in factoryDescriptorBuffer variable)
 	 */
-	bufferLength = fread(factoryDescriptorBuffer, 1, DESCRIPTORREGIONSIZE, fileStream);
+	bufferLength = fread(descriptorBuffer, 1, DESCRIPTORREGIONSIZE, fp);
 	if (DESCRIPTORREGIONSIZE != bufferLength) // 
 	{
-		printf("\nerror: could not read descriptor from factory.rom (%i) bytes read\n", bufferLength);
-		fclose(fileStream);
+		printf("\nerror: could not read descriptor from %s (%i) bytes read\n", romFilename, bufferLength);
+		fclose(fp);
 		return 1;
 	}
 	printf("\ndescriptor region read successfully\n");
-	/*
-	 * copy descriptor buffer into descriptor struct memory
-	 * factoryDescriptorStruct is an instance of a struct that actually
-	 * defines the locations of all these variables in the descriptor,
-	 * as defined in the datasheets. This allows us to map the extracted
-	 * descriptor over the struct so that it can then be modified
-	 * for libreboot's purpose
-	 */
-	memcpy(&factoryDescriptorStruct, &factoryDescriptorBuffer, DESCRIPTORREGIONSIZE);
-	/*
-	 * ^ the above is just for reference if needed. The modifications will be made here:
-	 */
-	memcpy(&deblobbedDescriptorStruct, &factoryDescriptorBuffer, DESCRIPTORREGIONSIZE);
 	
-	/*
-	 * Get the gbe region dump from the factory.rom
-	 */
-
-	/*
-	 * get original GBe region location
-	 * (it will be moved to the beginning of the flash, after the descriptor region)
-	 * note for example, factoryGbeRegionStart is set to <<FLREGIONBITSHIFT of actual address (in C). this is how the addresses
-	 * are stored in the descriptor.
-	 */
-	factoryGbeRegionStart = factoryDescriptorStruct.regionSection.flReg3.BASE << FLREGIONBITSHIFT;
+	gbeRegionStart = descriptorStruct.regionSection.flReg3.BASE << FLREGIONBITSHIFT;
 
 	/*
 	 * Set offset so that we can read the data from
 	 * the gbe region
 	 */
-	fseek(fileStream, factoryGbeRegionStart, SEEK_SET);
+	fseek(fp, gbeRegionStart, SEEK_SET);
 	/* Read the gbe data from the factory.rom and put it in factoryGbeBuffer8k */
-	bufferLength = fread(factoryGbeBuffer8k, 1, GBEREGIONSIZE_8K, fileStream);
+	bufferLength = fread(gbeBuffer8k, 1, GBEREGIONSIZE_8K, fp);
 	if (GBEREGIONSIZE_8K != bufferLength)
 	{
-		printf("\nerror: could not read GBe region from factory.rom (%i) bytes read\n", bufferLength);
-		fclose(fileStream);
+		printf("\nerror: could not read GBe region from %s (%i) bytes read\n", romFilename, bufferLength);
+		fclose(fp);
 		return 1;
 	}
 	printf("\ngbe (8KiB) region read successfully\n");
-	/*
-	 * copy gbe buffer into gbe struct memory
-	 * factoryGbeStruct8k is an instance of a struct that actually
-	 * defines the locations of all these variables in the gbe,
-	 * as defined in the datasheets. This allows us to map the extracted
-	 * gbe over the struct so that it can then be modified
-	 * for libreboot's purpose
-	 */
-	memcpy(&factoryGbeStruct8k, &factoryGbeBuffer8k, GBEREGIONSIZE_8K);
-	/*
-	 * the original factoryGbeStruct8k is only reference. Changes go here:
-	 */
-	memcpy(&deblobbedGbeStruct8k, &factoryGbeBuffer8k, GBEREGIONSIZE_8K);
-
-	/* 
-	 * Get size of ROM image
-	 * This is needed for relocating the BIOS region (per descriptor)
-	 */
-	fseek(fileStream, 0L, SEEK_END);
-	factoryRomSize = ftell(fileStream);
 
-	printf("\nfactory.rom size: [%i] bytes\n", factoryRomSize);
+	fseek(fp, 0L, SEEK_END);
+	romSize = ftell(fp);
+	printf("\n%s size: [%i] bytes\n", romFilename, romSize);
 
-	fclose(fileStream);
+	fclose(fp);
+	
+	/* Debugging (before modification) */
+	printDescriptorRegionLocations(descriptorStruct, "Original");
+	printGbeChecksumDataFromStruct8k(gbeStruct8k, "Original");
 	
 	/*
 	 * ------------------------------------------------------------------
@@ -188,29 +132,15 @@ int main()
 	 * ------------------------------------------------------------------
 	 */
 
-	/*
-	 * Disable the ME and Platform regions. Put Gbe at the beginning (after descriptor). 
-	 * Also, extend the BIOS region to fill the ROM image (after descriptor+gbe).
-	 */
-	deblobbedDescriptorStruct = deblobbedDescriptorStructFromFactory(factoryDescriptorStruct, factoryRomSize);
-	/*
-	 * Debugging: show region locations in the 
-	 * original descriptor and the modified one
-	 */
-	printDescriptorRegionLocations(factoryDescriptorStruct, "Original");
-	printDescriptorRegionLocations(deblobbedDescriptorStruct, "Modified");
+	/* Delete the ME/Platform regions, place Gbe after the descriptor, resize BIOS region to fill the gap */
+	descriptorStruct = deblobbedDescriptorStructFromFactory(descriptorStruct, romSize);
 
-	/*
-	 * Modify the Gbe region (see function for details)
-	 */
-	deblobbedGbeStruct8k = deblobbedGbeStructFromFactory(factoryGbeStruct8k);
-	/*
-	 * Debugging: show difference between Gbe checksums in the
-	 * original gbe and the modified one
-	 */
-	printf("\n");
-	printGbeChecksumDataFromStruct8k(factoryGbeStruct8k, "Original");
-	printGbeChecksumDataFromStruct8k(deblobbedGbeStruct8k, "Modified");
+	/* Modify the Gbe region (see function for details) */
+	gbeStruct8k = deblobbedGbeStructFromFactory(gbeStruct8k);
+
+	/* Debugging (after modifying the descriptor and gbe regions) */
+	printDescriptorRegionLocations(descriptorStruct, "Modified");
+	printGbeChecksumDataFromStruct8k(gbeStruct8k, "Modified");
 
 	/*
 	 * ------------------------------------------------------------------
@@ -219,7 +149,7 @@ int main()
 	 */
 
 	printf("\n");
-	if (notCreatedDescriptorGbeFile(deblobbedDescriptorStruct, deblobbedGbeStruct8k, deblobbedDescriptorFilename)) {
+	if (notCreatedDescriptorGbeFile(descriptorStruct, gbeStruct8k, descriptorGbeFilename)) {
 		return 1;
 	}
 	
@@ -228,23 +158,25 @@ int main()
 	 * Generate ich9gen data (C code that will recreate the deblobbed descriptor+gbe from scratch)
 	 * ------------------------------------------------------------------
 	 */
-	/* Code for generating the Gbe struct */
-	/* mkgbe.h */
-	if (notCreatedHFileForGbeCFile("mkgbe.h", "mkgbe.c")) {
-		return 1;
-	} /* and now mkgbe.c */
-	if (notCreatedCFileFromGbeStruct4k(deblobbedGbeStruct8k.backup, "mkgbe.c", "mkgbe.h")) {
-		return 1;
-	}
 	/* Code for generating the Descriptor struct */
 	/* mkdescriptor.h */
 	if (notCreatedHFileForDescriptorCFile("mkdescriptor.h", "mkdescriptor.c")) {
 		return 1;
 	} /* and now mkdescriptor.c */
-	if (notCreatedCFileFromDescriptorStruct(deblobbedDescriptorStruct, "mkdescriptor.c", "mkdescriptor.h")) {
+	if (notCreatedCFileFromDescriptorStruct(descriptorStruct, "mkdescriptor.c", "mkdescriptor.h")) {
+		return 1;
+	}
+	
+	/* Code for generating the Gbe struct */
+	/* mkgbe.h */
+	if (notCreatedHFileForGbeCFile("mkgbe.h", "mkgbe.c")) {
+		return 1;
+	} /* and now mkgbe.c */
+	if (notCreatedCFileFromGbeStruct4k(gbeStruct8k.backup, "mkgbe.c", "mkgbe.h")) {
 		return 1;
 	}
-	printf("The modified descriptor and gbe regions have also been dumped as src files: mkgbe.c, mkgbe.h, mkdescriptor.c, mkdescriptor.h\n");
+	
+	printf("The modified descriptor and gbe regions have also been dumped as src files: mkdescriptor.c, mkdescriptor.h, mkgbe.c, mkgbe.h\n");
 	printf("To use these in ich9gen, place them in src/ich9gen/ and re-build ich9gen.\n\n");
 
 	return 0;
--
cgit v0.9.1