Index: uspace/lib/gpt/Makefile
===================================================================
--- uspace/lib/gpt/Makefile	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/gpt/Makefile	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -28,5 +28,5 @@
 
 USPACE_PREFIX = ../..
-EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX)
+EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX) -I$(LIBMBR_PREFIX)
 LIBRARY = libgpt
 
Index: uspace/lib/gpt/global.c
===================================================================
--- uspace/lib/gpt/global.c	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/gpt/global.c	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -40,4 +40,8 @@
 };
 
+const uint8_t revision[4] = {
+	00, 00, 01, 00
+};
+
 const struct partition_type gpt_ptypes[] = {
 	{ "Unused entry",					"00000000" "0000" "0000" "0000000000000000" }, /* 0 */
@@ -55,9 +59,9 @@
 	{ "HP-UX Data",						"75894C1E" "3AEB" "11D3" "B7C17B03A0000000" },
 	{ "HP-UX Service",					"E2A1E728" "32E3" "11D6" "A6827B03A0000000" },
-	{ "Linux filesystem data",			"EBD0A0A2" "B9E5" "4433" "87C068B6B72699C7" },
 	{ "Linux filesystem data",			"0FC63DAF" "8483" "4772" "8E793D69D8477DE4" },
 	{ "Linux RAID",						"A19D880F" "05FC" "4D3B" "A006743F0F84911E" },
 	{ "Linux Swap",						"0657FD6D" "A4AB" "43C4" "84E50933C84B4F4F" },
 	{ "Linux LVM",						"E6D6D379" "F507" "44C2" "A23C238F2A3DF928" },
+	{ "Linux filesystem data",			"933AC7E1" "2EB4" "4F13" "B8440E14E2AEF915" },
 	{ "Linux Reserved",					"8DA63339" "0007" "60C0" "C436083AC8230908" },
 	{ "FreeBSD Boot",					"83BD6B9D" "7F41" "11DC" "BE0B001560B84F0F" }, /* 20 */
Index: uspace/lib/gpt/gpt.h
===================================================================
--- uspace/lib/gpt/gpt.h	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/gpt/gpt.h	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -50,5 +50,5 @@
 typedef struct {
 	uint8_t  efi_signature[8];
-	uint32_t revision;
+	uint8_t  revision[4];
 	uint32_t header_size;
 	uint32_t header_crc32;
Index: uspace/lib/gpt/libgpt.c
===================================================================
--- uspace/lib/gpt/libgpt.c	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/gpt/libgpt.c	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -49,4 +49,5 @@
 #include <mem.h>
 #include <sys/typefmt.h>
+#include <mbr.h>
 
 
@@ -112,5 +113,12 @@
 	}
 	
+	/* Enter some sane defaults. */
 	memset(gpt->header, 0, final_size);
+	memcpy(gpt->header->efi_signature, efi_signature, 8);
+	memcpy(gpt->header->revision, revision, 4);
+	gpt->header->header_size = host2uint32_t_le(final_size);
+	gpt->header->entry_lba = host2uint64_t_le((uint64_t) 2);
+	gpt->header->entry_size = host2uint32_t_le(sizeof(gpt_entry_t));
+	
 	
 	return gpt;
@@ -207,4 +215,6 @@
 	uint64_t tmp;
 	
+	gpt_set_random_uuid(label->gpt->header->disk_guid);
+	
 	/* Prepare the backup header */
 	label->gpt->header->alternate_lba = label->gpt->header->my_lba;
@@ -243,8 +253,21 @@
 	/* Write to main GPT header location */
 	rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
+	if (rc != EOK)
+		return rc;
+	
+	/* Write Protective MBR */
+	br_block_t mbr;
+	memset(&mbr, 0, 512);
+	memset(mbr.pte[0].first_chs, 1, 3);
+	mbr.pte[0].ptype = 0xEE;
+	memset(mbr.pte[0].last_chs, 0xFF, 3);
+	mbr.pte[0].first_lba = host2uint32_t_le(1);
+	mbr.pte[0].length = 0xFFFFFFFF;
+	mbr.signature = host2uint16_t_le(BR_SIGNATURE);
+	
+	rc = block_write_direct(dev_handle, 0, 1, &mbr);
 	block_fini(dev_handle);
 	if (rc != EOK)
 		return rc;
-	
 	
 	return 0;
@@ -343,9 +366,4 @@
 	int rc;
 	size_t b_size;
-	uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
-	size_t fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
-	
-	if (e_size != sizeof(gpt_entry_t))
-		return ENOTSUP;
 	
 	/* comm_size of 4096 is ignored */
@@ -363,4 +381,18 @@
 		goto fail;
 	
+	/* When we're creating a new label from scratch, we need to fill 
+	 * the header with sensible defaults. */
+	if (label->gpt == NULL) {
+		label->gpt = gpt_alloc_header(b_size);
+	}
+	
+	printf("test1.0\n");
+	uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
+	printf("test1.025\n");
+	size_t fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
+	printf("test1.05\n");
+	if (e_size != sizeof(gpt_entry_t))
+		return ENOTSUP;
+
 	label->gpt->header->fillries = host2uint32_t_le(fillries);
 	uint64_t arr_blocks = (fillries * sizeof(gpt_entry_t)) / b_size;
@@ -368,5 +400,5 @@
 	label->gpt->header->first_usable_lba = host2uint64_t_le(gpt_space);
 	label->gpt->header->last_usable_lba = host2uint64_t_le(n_blocks - gpt_space - 1);
-	
+	printf("test1.5\n");
 	/* Perform checks */
 	gpt_part_foreach (label, p) {
@@ -395,10 +427,10 @@
 		}
 	}
-	
+	printf("test1.6\n");
 	label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
 	                               (uint8_t *) label->parts->part_array,
 	                               fillries * e_size));
 	
-	
+	printf("test1.7\n");
 	/* Write to backup GPT partition array location */
 	rc = block_write_direct(dev_handle, n_blocks - arr_blocks - 1, 
@@ -406,5 +438,5 @@
 	if (rc != EOK)
 		goto fail;
-	
+	printf("test1.8\n");
 	/* Write to main GPT partition array location */
 	rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
@@ -412,5 +444,5 @@
 	if (rc != EOK)
 		goto fail;
-	
+	printf("test1.9\n");
 	return gpt_write_header(label, dev_handle);
 	
@@ -594,4 +626,9 @@
 	
 	for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
+		//printf("%x =?= %x\n", p->part_type[3], get_byte(gpt_ptypes[i].guid +0));
+		//printf("%x =?= %x\n", p->part_type[2], get_byte(gpt_ptypes[i].guid +2));
+		//printf("%x =?= %x\n", p->part_type[1], get_byte(gpt_ptypes[i].guid +4));
+		//printf("%x =?= %x\n", p->part_type[0], get_byte(gpt_ptypes[i].guid +6));
+		//getchar();
 		if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
 			p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
@@ -627,23 +664,23 @@
 {
 	/* Beware: first 3 blocks are byteswapped! */
-	p->part_type[3] = gpt_ptypes[type].guid[0];
-	p->part_type[2] = gpt_ptypes[type].guid[1];
-	p->part_type[1] = gpt_ptypes[type].guid[2];
-	p->part_type[0] = gpt_ptypes[type].guid[3];
-
-	p->part_type[5] = gpt_ptypes[type].guid[4];
-	p->part_type[4] = gpt_ptypes[type].guid[5];
-
-	p->part_type[7] = gpt_ptypes[type].guid[6];
-	p->part_type[6] = gpt_ptypes[type].guid[7];
-
-	p->part_type[8] = gpt_ptypes[type].guid[8];
-	p->part_type[9] = gpt_ptypes[type].guid[9];
-	p->part_type[10] = gpt_ptypes[type].guid[10];
-	p->part_type[11] = gpt_ptypes[type].guid[11];
-	p->part_type[12] = gpt_ptypes[type].guid[12];
-	p->part_type[13] = gpt_ptypes[type].guid[13];
-	p->part_type[14] = gpt_ptypes[type].guid[14];
-	p->part_type[15] = gpt_ptypes[type].guid[15];
+	p->part_type[3] = get_byte(gpt_ptypes[type].guid +0);
+	p->part_type[2] = get_byte(gpt_ptypes[type].guid +2);
+	p->part_type[1] = get_byte(gpt_ptypes[type].guid +4);
+	p->part_type[0] = get_byte(gpt_ptypes[type].guid +6);
+	                
+	p->part_type[5] = get_byte(gpt_ptypes[type].guid +8);
+	p->part_type[4] = get_byte(gpt_ptypes[type].guid +10);
+	                
+	p->part_type[7] = get_byte(gpt_ptypes[type].guid +12);
+	p->part_type[6] = get_byte(gpt_ptypes[type].guid +14);
+	                
+	p->part_type[8] = get_byte(gpt_ptypes[type].guid +16);
+	p->part_type[9] = get_byte(gpt_ptypes[type].guid +18);
+	p->part_type[10] = get_byte(gpt_ptypes[type].guid +20);
+	p->part_type[11] = get_byte(gpt_ptypes[type].guid +22);
+	p->part_type[12] = get_byte(gpt_ptypes[type].guid +24);
+	p->part_type[13] = get_byte(gpt_ptypes[type].guid +26);
+	p->part_type[14] = get_byte(gpt_ptypes[type].guid +28);
+	p->part_type[15] = get_byte(gpt_ptypes[type].guid +30);
 }
 
Index: uspace/lib/gpt/libgpt.h
===================================================================
--- uspace/lib/gpt/libgpt.h	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/gpt/libgpt.h	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -60,4 +60,5 @@
 /** GPT header signature ("EFI PART" in ASCII) */
 extern const uint8_t efi_signature[8];
+extern const uint8_t revision[4];
 
 typedef struct {
Index: uspace/lib/mbr/libmbr.c
===================================================================
--- uspace/lib/mbr/libmbr.c	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/mbr/libmbr.c	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -70,4 +70,9 @@
 }
 
+void mbr_set_device(mbr_label_t *label, service_id_t dev_handle)
+{
+	label->device = dev_handle;
+}
+
 /** Free mbr_label_t structure */
 void mbr_free_label(mbr_label_t *label)
@@ -253,4 +258,6 @@
 	}
 	
+	label->mbr->raw_data.signature = host2uint16_t_le(BR_SIGNATURE);
+	
 	/* Writing MBR */
 	rc = block_write_direct(dev_handle, 0, 1, &(label->mbr->raw_data));
@@ -271,5 +278,5 @@
 	 * and implementation. If you don't have to change it, don't. Other
 	 * designs have been tried, this came out as the least horror with
-	 * as much power over it as you can get. Thanks. */
+	 * as much power over it as you can get. */
 	
 	/* Encoding and writing first logical partition */
Index: uspace/lib/mbr/libmbr.h
===================================================================
--- uspace/lib/mbr/libmbr.h	(revision 61ab4a95bac7c7181a8130596eb9465807121d80)
+++ uspace/lib/mbr/libmbr.h	(revision 8c95dff74a31d0b98e3e8fb115185a07e64c2ac2)
@@ -120,4 +120,5 @@
 /* Alloc complete label structure */
 extern mbr_label_t * mbr_alloc_label(void);
+extern void mbr_set_device(mbr_label_t *, service_id_t);
 
 /* Read/Write MBR header.
