Index: uspace/app/hdisk/func_gpt.c
===================================================================
--- uspace/app/hdisk/func_gpt.c	(revision 6e8e4e191b06e73e7414d8e4538ce949d1bd24f9)
+++ uspace/app/hdisk/func_gpt.c	(revision 1c8bfe8be1ae2ca042fcbe7b5cc365f259f3a422)
@@ -34,4 +34,5 @@
 
 #include <stdio.h>
+#include <str.h>
 #include <errno.h>
 #include <str_error.h>
@@ -60,25 +61,28 @@
 }
 
-int add_gpt_part(label_t *this, tinput_t * in)
-{
-	gpt_part_t * p = gpt_alloc_partition(this->data.gpt->parts);
+int add_gpt_part(label_t *this, tinput_t *in)
+{
+	gpt_part_t * p = gpt_get_partition(this->data.gpt);
 	if (p == NULL) {
 		return ENOMEM;
 	}
-
+	
 	return set_gpt_partition(in, p);
 }
 
-int delete_gpt_part(label_t *this, tinput_t * in)
-{
+int delete_gpt_part(label_t *this, tinput_t *in)
+{
+	int rc;
 	size_t idx;
-
+	
 	printf("Number of the partition to delete (counted from 0): ");
 	idx = get_input_size_t(in);
-
-	if (gpt_remove_partition(this->data.gpt->parts, idx) == -1) {
+	
+	rc = gpt_remove_partition(this->data.gpt, idx);
+	if (rc != EOK) {
 		printf("Warning: running low on memory, not resizing...\n");
-	}
-
+		return rc;
+	}
+	
 	return EOK;
 }
@@ -86,4 +90,5 @@
 int destroy_gpt_label(label_t *this)
 {
+	gpt_free_label(this->data.gpt);
 	return EOK;
 }
@@ -91,6 +96,5 @@
 int new_gpt_label(label_t *this)
 {
-	this->data.gpt->gpt = gpt_alloc_gpt_header();
-	this->data.gpt->parts = gpt_alloc_partitions();
+	this->data.gpt = gpt_alloc_label();
 	return EOK;
 }
@@ -104,17 +108,20 @@
 	size_t i = 0;
 	
-	gpt_part_foreach(this->data.gpt->parts, iter) {
+	gpt_part_foreach(this->data.gpt, iter) {
+		i++;
 		//FIXMEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-		if (gpt_get_part_type(iter) == 62)
+		if (gpt_get_part_type(iter) == GPT_PTE_UNUSED)
 			continue;
+		
+		if (i % 20 == 0)
+			printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
 		
 		//printf("\t%10u %10u %10u %3d\n", iter->start_addr, iter->start_addr + iter->length,
 		//		iter->length, gpt_get_part_type(iter), gpt_get_part_name(iter));
-		printf("%3u\t%10llu %10llu %10llu %3d %s\n", i, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
+		printf("%3u  %10llu %10llu %10llu    %3d %s\n", i-1, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
 				gpt_get_end_lba(iter) - gpt_get_start_lba(iter), gpt_get_part_type(iter),
 				gpt_get_part_name(iter));
-		i++;
-	}
-
+	}
+	
 	//return rc;
 	return EOK;
@@ -123,4 +130,18 @@
 int read_gpt_parts(label_t *this, service_id_t dev_handle)
 {
+	int rc;
+	
+	rc = gpt_read_header(this->data.gpt, dev_handle);
+	if (rc != EOK) {
+		printf("Error: Reading header failed: %d (%s)\n", rc, str_error(rc));
+		return rc;
+	}
+	
+	rc = gpt_read_partitions(this->data.gpt);
+	if (rc != EOK) {
+		printf("Error: Reading partitions failed: %d (%s)\n", rc, str_error(rc));
+		return rc;
+	}
+	
 	return EOK;
 }
@@ -129,21 +150,21 @@
 {
 	int rc;
-
-	rc = gpt_write_partitions(this->data.gpt->parts, this->data.gpt->gpt, dev_handle);
+	
+	rc = gpt_write_partitions(this->data.gpt, dev_handle);
 	if (rc != EOK) {
 		printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
 		return rc;
 	}
-
-	rc = gpt_write_gpt_header(this->data.gpt->gpt, dev_handle);
-	if (rc != EOK) {
-		printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
-		return rc;
-	}
-
-	return EOK;
-}
-
-int extra_gpt_funcs(label_t *this, tinput_t * in, service_id_t dev_handle)
+	
+	rc = gpt_write_header(this->data.gpt, dev_handle);
+	if (rc != EOK) {
+		printf("Error: Writing header failed: %d (%s)\n", rc, str_error(rc));
+		return rc;
+	}
+	
+	return EOK;
+}
+
+int extra_gpt_funcs(label_t *this, tinput_t *in, service_id_t dev_handle)
 {
 	printf("Not implemented.\n");
@@ -151,28 +172,35 @@
 }
 
-static int set_gpt_partition(tinput_t * in, gpt_part_t * p)
-{
-	//int rc;
-
+static int set_gpt_partition(tinput_t *in, gpt_part_t *p)
+{
+	int rc;
+	
 	uint64_t sa, ea;
-
+	
 	printf("Set starting address (number): ");
 	sa = get_input_uint64(in);
-
+	
 	printf("Set end addres (number): ");
 	ea = get_input_uint64(in);
-
+	
 	if (ea <= sa) {
 		printf("Invalid value.\n");
 		return EINVAL;
 	}
-
-
-	//p->start_addr = sa;
+	
 	gpt_set_start_lba(p, sa);
-	//p->length = ea - sa;
 	gpt_set_end_lba(p, ea);
-
-	return EOK;
-}
-
+	
+	
+	char *name;
+	rc = get_input_line(in, &name);
+	if (rc != EOK) {
+		printf("Error reading name: %d (%s)\n", rc, str_error(rc));
+		return rc;
+	}
+	
+	gpt_set_part_name(p, name, str_size(name));
+	
+	return EOK;
+}
+
Index: uspace/lib/gpt/global.c
===================================================================
--- uspace/lib/gpt/global.c	(revision 6e8e4e191b06e73e7414d8e4538ce949d1bd24f9)
+++ uspace/lib/gpt/global.c	(revision 1c8bfe8be1ae2ca042fcbe7b5cc365f259f3a422)
@@ -41,66 +41,69 @@
 
 const struct partition_type gpt_ptypes[] = {
-	{ "Unused entry",					"00000000-0000-0000-0000-000000000000" },
-	{ "MBR partition scheme",			"024DEE41-33E7-11D3-9D69-0008C781F39F" },
-	{ "EFI System",						"C12A7328-F81F-11D2-BA4B-00A0C93EC93B" },
-	{ "BIOS Boot",						"21686148-6449-6E6F-744E-656564454649" },
-	{ "Windows Reserved",				"E3C9E316-0B5C-4DB8-817D-F92DF00215AE" },
-	{ "Windows Basic data",				"EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" },
-	{ "Windows LDM metadata", 			"5808C8AA-7E8F-42E0-85D2-E1E90434CFB3" },
-	{ "Windows LDM data", 				"AF9B60A0-1431-4F62-BC68-3311714A69AD" },
-	{ "Windows Recovery Environment",	"DE94BBA4-06D1-4D40-A16A-BFD50179D6AC" },
-	{ "Windows IBM GPFS",				"37AFFC90-EF7D-4E96-91C3-2D7AE055B174" },
-	{ "Windows Cluster metadata",		"DB97DBA9-0840-4BAE-97F0-FFB9A327C7E1" },
-	{ "HP-UX Data",						"75894C1E-3AEB-11D3-B7C1-7B03A0000000" },
-	{ "HP-UX Service",					"E2A1E728-32E3-11D6-A682-7B03A0000000" },
-	{ "Linux filesystem data",			"EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" },
-	{ "Linux filesystem data",			"0FC63DAF-8483-4772-8E79-3D69D8477DE4" },
-	{ "Linux RAID",						"A19D880F-05FC-4D3B-A006-743F0F84911E" },
-	{ "Linux Swap",						"0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" },
-	{ "Linux LVM",						"E6D6D379-F507-44C2-A23C-238F2A3DF928" },
-	{ "Linux Reserved",					"8DA63339-0007-60C0-C436-083AC8230908" },
-	{ "FreeBSD Boot",					"83BD6B9D-7F41-11DC-BE0B-001560B84F0F" },
-	{ "FreeBSD Data",					"516E7CB4-6ECF-11D6-8FF8-00022D09712B" },
-	{ "FreeBSD Swap",					"516E7CB5-6ECF-11D6-8FF8-00022D09712B" },
-	{ "FreeBSD UFS", 					"516E7CB6-6ECF-11D6-8FF8-00022D09712B" },
-	{ "FreeBSD Vinum VM",				"516E7CB8-6ECF-11D6-8FF8-00022D09712B" },
-	{ "FreeBSD ZFS",					"516E7CBA-6ECF-11D6-8FF8-00022D09712B" },
-	{ "Mac OS X HFS+",					"48465300-0000-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X UFS",					"55465300-0000-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X ZFS",					"6A898CC3-1DD2-11B2-99A6-080020736631" },
-	{ "Mac OS X RAID",					"52414944-0000-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X RAID, offline",			"52414944-5F4F-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X Boot",					"426F6F74-0000-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X Label",					"4C616265-6C00-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X TV Recovery",			"5265636F-7665-11AA-AA11-00306543ECAC" },
-	{ "Mac OS X Core Storage",			"53746F72-6167-11AA-AA11-00306543ECAC" },
-	{ "Solaris Boot",					"6A82CB45-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Root",					"6A85CF4D-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Swap",					"6A87C46F-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Backup",					"6A8B642B-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris /usr",					"6A898CC3-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris /var",					"6A8EF2E9-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris /home",					"6A90BA39-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Alternate sector",		"6A9283A5-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Reserved",				"6A945A3B-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Reserved",				"6A9630D1-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Reserved",				"6A980767-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Reserved",				"6A96237F-1DD2-11B2-99A6-080020736631" },
-	{ "Solaris Reserved",				"6A8D2AC7-1DD2-11B2-99A6-080020736631" },
-	{ "NetBSD Swap",					"49F48D32-B10E-11DC-B99B-0019D1879648" },
-	{ "NetBSD FFS",						"49F48D5A-B10E-11DC-B99B-0019D1879648" },
-	{ "NetBSD LFS",						"49F48D82-B10E-11DC-B99B-0019D1879648" },
-	{ "NetBSD RAID",					"49F48DAA-B10E-11DC-B99B-0019D1879648" },
-	{ "NetBSD Concatenated",			"2DB519C4-B10F-11DC-B99B-0019D1879648" },
-	{ "NetBSD Encrypted",				"2DB519EC-B10F-11DC-B99B-0019D1879648" },
-	{ "ChromeOS ChromeOS kernel",		"FE3A2A5D-4F32-41A7-B725-ACCC3285A309" },
-	{ "ChromeOS rootfs",				"3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC" },
-	{ "ChromeOS future use",			"2E0A753D-9E48-43B0-8337-B15192CB1B5E" },
-	{ "MidnightBSD Boot",				"85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7" },
-	{ "MidnightBSD Data",				"85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7" },
-	{ "MidnightBSD Swap",				"85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7" },
-	{ "MidnightBSD UFS",				"0394Ef8B-237E-11E1-B4B3-E89A8F7FC3A7" },
-	{ "MidnightBSD Vinum VM",			"85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7" },
-	{ "MidnightBSD ZFS",				"85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7" },
-	{ "Uknown", NULL}	// keep this as the last one! gpt_get_part_type depends on it!
+	{ "Unused entry",					"00000000" "0000" "0000" "0000" "000000000000" },
+	{ "MBR partition scheme",			"024DEE41" "33E7" "11D3" "9D69" "0008C781F39F" },
+	{ "EFI System",						"C12A7328" "F81F" "11D2" "BA4B" "00A0C93EC93B" },
+	{ "BIOS Boot",						"21686148" "6449" "6E6F" "744E" "656564454649" },
+	{ "Windows Reserved",				"E3C9E316" "0B5C" "4DB8" "817D" "F92DF00215AE" },
+	{ "Windows Basic data",				"EBD0A0A2" "B9E5" "4433" "87C0" "68B6B72699C7" },
+	{ "Windows LDM metadata", 			"5808C8AA" "7E8F" "42E0" "85D2" "E1E90434CFB3" },
+	{ "Windows LDM data", 				"AF9B60A0" "1431" "4F62" "BC68" "3311714A69AD" },
+	{ "Windows Recovery Environment",	"DE94BBA4" "06D1" "4D40" "A16A" "BFD50179D6AC" },
+	{ "Windows IBM GPFS",				"37AFFC90" "EF7D" "4E96" "91C3" "2D7AE055B174" },
+	{ "Windows Cluster metadata",		"DB97DBA9" "0840" "4BAE" "97F0" "FFB9A327C7E1" },
+	{ "HP-UX Data",						"75894C1E" "3AEB" "11D3" "B7C1" "7B03A0000000" },
+	{ "HP-UX Service",					"E2A1E728" "32E3" "11D6" "A682" "7B03A0000000" },
+	{ "Linux filesystem data",			"EBD0A0A2" "B9E5" "4433" "87C0" "68B6B72699C7" },
+	{ "Linux filesystem data",			"0FC63DAF" "8483" "4772" "8E79" "3D69D8477DE4" },
+	{ "Linux RAID",						"A19D880F" "05FC" "4D3B" "A006" "743F0F84911E" },
+	{ "Linux Swap",						"0657FD6D" "A4AB" "43C4" "84E5" "0933C84B4F4F" },
+	{ "Linux LVM",						"E6D6D379" "F507" "44C2" "A23C" "238F2A3DF928" },
+	{ "Linux Reserved",					"8DA63339" "0007" "60C0" "C436" "083AC8230908" },
+	{ "FreeBSD Boot",					"83BD6B9D" "7F41" "11DC" "BE0B" "001560B84F0F" },
+	{ "FreeBSD Data",					"516E7CB4" "6ECF" "11D6" "8FF8" "00022D09712B" },
+	{ "FreeBSD Swap",					"516E7CB5" "6ECF" "11D6" "8FF8" "00022D09712B" },
+	{ "FreeBSD UFS", 					"516E7CB6" "6ECF" "11D6" "8FF8" "00022D09712B" },
+	{ "FreeBSD Vinum VM",				"516E7CB8" "6ECF" "11D6" "8FF8" "00022D09712B" },
+	{ "FreeBSD ZFS",					"516E7CBA" "6ECF" "11D6" "8FF8" "00022D09712B" },
+	{ "Mac OS X HFS+",					"48465300" "0000" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X UFS",					"55465300" "0000" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X ZFS",					"6A898CC3" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Mac OS X RAID",					"52414944" "0000" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X RAID, offline",			"52414944" "5F4F" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X Boot",					"426F6F74" "0000" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X Label",					"4C616265" "6C00" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X TV Recovery",			"5265636F" "7665" "11AA" "AA11" "00306543ECAC" },
+	{ "Mac OS X Core Storage",			"53746F72" "6167" "11AA" "AA11" "00306543ECAC" },
+	{ "Solaris Boot",					"6A82CB45" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Root",					"6A85CF4D" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Swap",					"6A87C46F" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Backup",					"6A8B642B" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris /usr",					"6A898CC3" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris /var",					"6A8EF2E9" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris /home",					"6A90BA39" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Alternate sector",		"6A9283A5" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Reserved",				"6A945A3B" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Reserved",				"6A9630D1" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Reserved",				"6A980767" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Reserved",				"6A96237F" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "Solaris Reserved",				"6A8D2AC7" "1DD2" "11B2" "99A6" "080020736631" },
+	{ "NetBSD Swap",					"49F48D32" "B10E" "11DC" "B99B" "0019D1879648" },
+	{ "NetBSD FFS",						"49F48D5A" "B10E" "11DC" "B99B" "0019D1879648" },
+	{ "NetBSD LFS",						"49F48D82" "B10E" "11DC" "B99B" "0019D1879648" },
+	{ "NetBSD RAID",					"49F48DAA" "B10E" "11DC" "B99B" "0019D1879648" },
+	{ "NetBSD Concatenated",			"2DB519C4" "B10F" "11DC" "B99B" "0019D1879648" },
+	{ "NetBSD Encrypted",				"2DB519EC" "B10F" "11DC" "B99B" "0019D1879648" },
+	{ "ChromeOS ChromeOS kernel",		"FE3A2A5D" "4F32" "41A7" "B725" "ACCC3285A309" },
+	{ "ChromeOS rootfs",				"3CB8E202" "3B7E" "47DD" "8A3C" "7FF2A13CFCEC" },
+	{ "ChromeOS future use",			"2E0A753D" "9E48" "43B0" "8337" "B15192CB1B5E" },
+	{ "MidnightBSD Boot",				"85D5E45E" "237C" "11E1" "B4B3" "E89A8F7FC3A7" },
+	{ "MidnightBSD Data",				"85D5E45A" "237C" "11E1" "B4B3" "E89A8F7FC3A7" },
+	{ "MidnightBSD Swap",				"85D5E45B" "237C" "11E1" "B4B3" "E89A8F7FC3A7" },
+	{ "MidnightBSD UFS",				"0394Ef8B" "237E" "11E1" "B4B3" "E89A8F7FC3A7" },
+	{ "MidnightBSD Vinum VM",			"85D5E45C" "237C" "11E1" "B4B3" "E89A8F7FC3A7" },
+	{ "MidnightBSD ZFS",				"85D5E45D" "237C" "11E1" "B4B3" "E89A8F7FC3A7" },
+	{ "Uknown", NULL} /* keep this as the last one! gpt_get_part_type depends on it! */
 };
+
+
+
Index: uspace/lib/gpt/libgpt.c
===================================================================
--- uspace/lib/gpt/libgpt.c	(revision 6e8e4e191b06e73e7414d8e4538ce949d1bd24f9)
+++ uspace/lib/gpt/libgpt.c	(revision 1c8bfe8be1ae2ca042fcbe7b5cc365f259f3a422)
@@ -51,23 +51,73 @@
 #include "libgpt.h"
 
-static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t * header);
+static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t *header);
 static gpt_partitions_t * alloc_part_array(uint32_t num);
-static int extend_part_array(gpt_partitions_t * p);
-static int reduce_part_array(gpt_partitions_t * p);
+static int extend_part_array(gpt_partitions_t *);
+static int reduce_part_array(gpt_partitions_t *);
 static long long nearest_larger_int(double a);
-static int gpt_memcmp(const void * a, const void * b, size_t len);
+static uint8_t get_byte(const char *);
+
+/** Allocate memory for gpt label */
+gpt_label_t * gpt_alloc_label(void)
+{
+	gpt_label_t *label = malloc(sizeof(gpt_label_t));
+	if (label == NULL)
+		return NULL;
+	
+	label->gpt = NULL;
+	label->parts = NULL;
+	label->device = 0;
+	
+	return label;
+}
+
+/** Free gpt_label_t structure */
+void gpt_free_label(gpt_label_t *label)
+{
+	if (label->gpt != NULL)
+		gpt_free_gpt(label->gpt);
+	
+	if (label->parts != NULL)
+		gpt_free_partitions(label->parts);
+	
+	free(label);
+}
 
 /** Allocate memory for gpt header */
-gpt_t * gpt_alloc_gpt_header(void)
-{
-	return malloc(sizeof(gpt_t));
+gpt_t * gpt_alloc_header(size_t size)
+{
+	gpt_t *gpt = malloc(sizeof(gpt_t));
+	if (gpt == NULL)
+		return NULL;
+	
+	// We might need only sizeof(gpt_header_t),
+	// but we should follow specs and have
+	// zeroes through all the rest of the block
+	size_t final_size = size > sizeof(gpt_header_t) ? size : sizeof(gpt_header_t);
+	gpt->header = malloc(final_size);
+	if (gpt->header == NULL) {
+		free(gpt);
+		return NULL;
+	}
+	
+	memset(gpt->header, 0, final_size);
+	
+	return gpt;
+}
+
+/** free() GPT header including gpt->header_lba */
+void gpt_free_gpt(gpt_t *gpt)
+{
+	free(gpt->header);
+	free(gpt);
 }
 
 /** Read GPT from specific device
- * @param	dev_handle	device to read GPT from
- *
- * @return				GPT record on success, NULL on error
- */
-gpt_t * gpt_read_gpt_header(service_id_t dev_handle)
+ * @param label        label structure to fill
+ * @param dev_handle   device to read GPT from
+ *
+ * @return             EOK on success, errorcode on error
+ */
+int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
 {
 	int rc;
@@ -76,71 +126,58 @@
 	rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
 	if (rc != EOK)
-		return NULL;
+		return rc;
 	
 	rc = block_get_bsize(dev_handle, &b_size);
-	if (rc != EOK) {
-		errno = rc;
-		return NULL;
-	}
-	
-	gpt_t * gpt = malloc(sizeof(gpt_t));
-	if (gpt == NULL) {
-		errno = ENOMEM;
-		return NULL;
-	}
-
-	gpt->raw_data = malloc(b_size);	// We might need only sizeof(gpt_header_t),
-	if (gpt == NULL) {				// but we should follow specs and have
-		free(gpt);					// zeroes through all the rest of the block
-		errno = ENOMEM;
-		return NULL;
-	}
-	
-	
-	rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, gpt->raw_data);
+	if (rc != EOK)
+		return rc;
+	
+	if (label->gpt == NULL) {
+		label->gpt = gpt_alloc_header(b_size);
+		if (label->gpt == NULL)
+			return ENOMEM;
+	}
+	
+	rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, label->gpt->header);
 	if (rc == EBADCHECKSUM || rc == EINVAL) {
 		aoff64_t n_blocks;
 		rc = block_get_nblocks(dev_handle, &n_blocks);
-		if (rc != EOK) {
-			errno = rc;
+		if (rc != EOK)
 			goto fail;
-		}
-
-		rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, gpt->raw_data);
-		if (rc == EBADCHECKSUM || rc == EINVAL) {
-			errno = rc;
+
+		rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
+		if (rc == EBADCHECKSUM || rc == EINVAL)
 			goto fail;
-		}
-	}
-	
-	gpt->device = dev_handle;
+	}
+	
+	label->device = dev_handle;
 	block_fini(dev_handle);
-	return gpt;
+	return EOK;
 	
 fail:
 	block_fini(dev_handle);
-	gpt_free_gpt(gpt);
-	return NULL;
+	gpt_free_gpt(label->gpt);
+	label->gpt = NULL;
+	return rc;
 }
 
 /** Write GPT header to device
- * @param header		GPT header to be written
- * @param dev_handle	device handle to write the data to
- *
- * @return				0 on success, libblock error code otherwise
- *
- * Note: Firstly write partitions (if changed), then gpt header.
- */
-int gpt_write_gpt_header(gpt_t * gpt, service_id_t dev_handle)
+ * @param label        GPT label header to be written
+ * @param dev_handle   device handle to write the data to
+ *
+ * @return             EOK on success, libblock error code otherwise
+ *
+ * Note: Firstly write partitions (if modified), then gpt header.
+ */
+int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
 {
 	int rc;
 	size_t b_size;
 
-	gpt->raw_data->header_crc32 = 0;
-	gpt->raw_data->header_crc32 = compute_crc32((uint8_t *) gpt->raw_data,
-					uint32_t_le2host(gpt->raw_data->header_size));
+	label->gpt->header->header_crc32 = 0;
+	label->gpt->header->header_crc32 = compute_crc32((uint8_t *) label->gpt->header,
+					uint32_t_le2host(label->gpt->header->header_size));
 
 	rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
-	if (rc != EOK)
+	if (rc != EOK && rc != EEXIST)
 		return rc;
 
@@ -150,17 +187,20 @@
 
 	/* Write to main GPT header location */
-	rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, gpt->raw_data);
-	if (rc != EOK)
+	rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
+	if (rc != EOK) {
 		block_fini(dev_handle);
 		return rc;
+	}
 
 	aoff64_t n_blocks;
 	rc = block_get_nblocks(dev_handle, &n_blocks);
-	if (rc != EOK)
-		return rc;
+	if (rc != EOK) {
+		block_fini(dev_handle);
+		return rc;
+	}
 
 	/* Write to backup GPT header location */
 	//FIXME: those idiots thought it would be cool to have these fields in reverse order...
-	rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, gpt->raw_data);
+	rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
 	block_fini(dev_handle);
 	if (rc != EOK)
@@ -171,5 +211,5 @@
 
 /** Alloc partition array */
-gpt_partitions_t *	gpt_alloc_partitions()
+gpt_partitions_t * gpt_alloc_partitions()
 {
 	return alloc_part_array(128);
@@ -177,22 +217,21 @@
 
 /** Parse partitions from GPT
- * @param gpt	GPT to be parsed
- *
- * @return 		partition linked list pointer or NULL on error
- * 				error code is stored in errno
- */
-gpt_partitions_t * gpt_read_partitions(gpt_t * gpt)
+ * @param label   GPT label to be parsed
+ *
+ * @return        EOK on success, errorcode otherwise
+ */
+int gpt_read_partitions(gpt_label_t *label)
 {
 	int rc;
 	unsigned int i;
-	gpt_partitions_t * res;
-	uint32_t fill = uint32_t_le2host(gpt->raw_data->fillries);
-	uint32_t ent_size = uint32_t_le2host(gpt->raw_data->entry_size);
-	uint64_t ent_lba = uint64_t_le2host(gpt->raw_data->entry_lba);
-
-	res = alloc_part_array(fill);
-	if (res == NULL) {
-		//errno = ENOMEM; // already set in alloc_part_array()
-		return NULL;
+	uint32_t fill = uint32_t_le2host(label->gpt->header->fillries);
+	uint32_t ent_size = uint32_t_le2host(label->gpt->header->entry_size);
+	uint64_t ent_lba = uint64_t_le2host(label->gpt->header->entry_lba);
+	
+	if (label->parts == NULL) {
+		label->parts = alloc_part_array(fill);
+		if (label->parts == NULL) {
+			return ENOMEM;
+		}
 	}
 
@@ -200,18 +239,12 @@
 	 *  - we don't need more bytes
 	 *  - the size of GPT partition entry can be different to 128 bytes */
-	rc = block_init(EXCHANGE_SERIALIZE, gpt->device, sizeof(gpt_entry_t));
-	if (rc != EOK) {
-		gpt_free_partitions(res);
-		errno = rc;
-		return NULL;
-	}
+	rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
+	if (rc != EOK)
+		goto fail;
 
 	size_t block_size;
-	rc = block_get_bsize(gpt->device, &block_size);
-	if (rc != EOK) {
-		gpt_free_partitions(res);
-		errno = rc;
-		return NULL;
-	}
+	rc = block_get_bsize(label->device, &block_size);
+	if (rc != EOK)
+		goto fail;
 
 	//size_t bufpos = 0;
@@ -226,14 +259,11 @@
 	for (i = 0; i < fill; ++i) {
 		//FIXME: this does bypass cache...
-		rc = block_read_bytes_direct(gpt->device, pos, sizeof(gpt_entry_t), res->part_array + i);
+		rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
 		//FIXME: but seqread() is just too complex...
 		//rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
 		pos += ent_size;
 
-		if (rc != EOK) {
-			gpt_free_partitions(res);
-			errno = rc;
-			return NULL;
-		}
+		if (rc != EOK)
+			goto fail;
 	}
 
@@ -243,131 +273,200 @@
 	 * on all of the partition entry array.
 	 */
-	uint32_t crc = compute_crc32((uint8_t *) res->part_array, res->fill * sizeof(gpt_entry_t));
-
-	if(uint32_t_le2host(gpt->raw_data->pe_array_crc32) != crc)
+	uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array, label->parts->fill * sizeof(gpt_entry_t));
+
+	if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
 	{
-		gpt_free_partitions(res);
-		errno = EBADCHECKSUM;
-		return NULL;
-	}
-
-	return res;
+		rc = EBADCHECKSUM;
+		goto fail;
+	}
+
+	return EOK;
+	
+fail:
+	gpt_free_partitions(label->parts);
+	label->parts = NULL;
+	return rc;
 }
 
 /** Write GPT and partitions to device
- * @param parts			partition list to be written
- * @param header		GPT header belonging to the 'parts' partitions
- * @param dev_handle	device to write the data to
- *
- * @return				returns EOK on succes, specific error code otherwise
- */
-int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * gpt, service_id_t dev_handle)
+ * @param label        label to write
+ * @param dev_handle   device to write the data to
+ *
+ * @return             returns EOK on succes, errorcode otherwise
+ */
+int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
 {
 	int rc;
 	size_t b_size;
-
-	gpt->raw_data->pe_array_crc32 = compute_crc32((uint8_t *) parts->part_array, parts->fill * gpt->raw_data->entry_size);
-
-	rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
-	if (rc != EOK)
-		return rc;
-
+	uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
+	size_t fill = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
+	
+	label->gpt->header->pe_array_crc32 = compute_crc32(
+	                               (uint8_t *) label->parts->part_array,
+	                               fill * e_size);
+	
+	/* comm_size of 4096 is ignored */
+	rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
+	if (rc != EOK && rc != EEXIST)
+		return rc;
+	
 	rc = block_get_bsize(dev_handle, &b_size);
 	if (rc != EOK)
-		return rc;
-
-	/* Write to main GPT partition array location */
-	rc = block_write_direct(dev_handle, uint64_t_le2host(gpt->raw_data->entry_lba),
-			nearest_larger_int((uint64_t_le2host(gpt->raw_data->entry_size) * parts->fill) / b_size),
-			parts->part_array);
-	if (rc != EOK)
-		block_fini(dev_handle);
-		return rc;
-
+		goto fail;
+	
 	aoff64_t n_blocks;
 	rc = block_get_nblocks(dev_handle, &n_blocks);
 	if (rc != EOK)
-		return rc;
-
+		goto fail;
+	
 	/* Write to backup GPT partition array location */
 	//rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, header->raw_data);
+	if (rc != EOK)
+		goto fail;
+	
+	/* Write to main GPT partition array location */
+	rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
+			nearest_larger_int((uint64_t_le2host(label->gpt->header->entry_size) * label->parts->fill) / b_size),
+			label->parts->part_array);
+	if (rc != EOK)
+		goto fail;
+	
+	return gpt_write_header(label, dev_handle);
+	
+fail:
 	block_fini(dev_handle);
-	if (rc != EOK)
-		return rc;
-
-
-	return gpt_write_gpt_header(gpt, dev_handle);
+	return rc;
 }
 
 /** Alloc new partition
  *
- * @param parts		partition table to carry new partition
- *
- * @return			returns pointer to the new partition or NULL on ENOMEM
- *
- * Note: use either gpt_alloc_partition or gpt_add_partition. The first
- * returns a pointer to write your data to, the second copies the data
- * (and does not free the memory).
- */
-gpt_part_t * gpt_alloc_partition(gpt_partitions_t * parts)
-{
-	if (parts->fill == parts->arr_size) {
-		if (extend_part_array(parts) == -1)
-			return NULL;
-	}
-
-	return parts->part_array + parts->fill++;
+ * @return        returns pointer to the new partition or NULL
+ *
+ * Note: use either gpt_alloc_partition or gpt_get_partition.
+ * This returns a memory block (zero-filled) and needs gpt_add_partition()
+ * to be called to insert it into a partition array.
+ * Requires you to call gpt_free_partition afterwards.
+ */
+gpt_part_t * gpt_alloc_partition(void)
+{
+	gpt_part_t *p = malloc(sizeof(gpt_part_t));
+	if (p == NULL)
+		return NULL;
+	
+	memset(p, 0, sizeof(gpt_part_t));
+	
+	return p;
+}
+
+/** Alloc new partition already inside the label
+ *
+ * @param label   label to carry new partition
+ *
+ * @return        returns pointer to the new partition or NULL on ENOMEM
+ *
+ * Note: use either gpt_alloc_partition or gpt_get_partition.
+ * This one returns a pointer to the first empty structure already
+ * inside the array, so don't call gpt_add_partition() afterwards.
+ * This is the one you will usually want.
+ */
+gpt_part_t * gpt_get_partition(gpt_label_t *label)
+{
+	gpt_part_t *p;
+	
+	/* Find the first empty entry */
+	do {
+		if (label->parts->fill == label->parts->arr_size) {
+			if (extend_part_array(label->parts) == -1)
+				return NULL;
+		}
+		
+		p = label->parts->part_array + label->parts->fill++;
+		
+	} while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
+	
+	return p;
+}
+
+/** Get partition already inside the label
+ *
+ * @param label   label to carrying the partition
+ * @param idx     index of the partition
+ *
+ * @return        returns pointer to the partition
+ *                or NULL when out of range
+ *
+ * Note: For new partitions use either gpt_alloc_partition or
+ * gpt_get_partition unless you want a partition at a specific place.
+ * This returns a pointer to a structure already inside the array,
+ * so don't call gpt_add_partition() afterwards.
+ * This function is handy when you want to change already existing
+ * partition or to simply write somewhere in the middle. This works only
+ * for indexes smaller than either 128 or the actual number of filled
+ * entries.
+ */
+gpt_part_t * gpt_get_partition_at(gpt_label_t *label, size_t idx)
+{
+	return NULL;
+	
+	if (idx >= GPT_MIN_PART_NUM && idx >= label->parts->fill)
+		return NULL;
+	
+	return label->parts->part_array + idx;
 }
 
 /** Copy partition into partition array
  *
- * @param parts			target partition array
+ * @param parts			target label
  * @param partition		source partition to copy
  *
  * @return 				-1 on error, 0 otherwise
  *
- * Note: use either gpt_alloc_partition or gpt_add_partition. The first
- * returns a pointer to write your data to, the second copies the data
- * (and does not free the memory).
- */
-int gpt_add_partition(gpt_partitions_t * parts, gpt_part_t * partition)
-{
-	if (parts->fill == parts->arr_size) {
-		if (extend_part_array(parts) == -1)
+ * Note: for use with gpt_alloc_partition() only. You will get
+ * duplicates with gpt_get_partition().
+ */
+int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
+{
+	if (label->parts->fill == label->parts->arr_size) {
+		if (extend_part_array(label->parts) == -1)
 			return ENOMEM;
 	}
-	extend_part_array(parts);
-	return EOK;;
+	
+	memcpy(label->parts->part_array + label->parts->fill++,
+	       partition, sizeof(gpt_part_t));
+	
+	return EOK;
 }
 
 /** Remove partition from array
- *
- * @param idx		index of the partition to remove
- *
- * @return			-1 on error, 0 otherwise
+ * @param label   label to remove from
+ * @param idx     index of the partition to remove
+ *
+ * @return        EOK on success, ENOMEM on array reduction failure
  *
  * Note: even if it fails, the partition still gets removed. Only
  * reducing the array failed.
  */
-int gpt_remove_partition(gpt_partitions_t * parts, size_t idx)
-{
-	if (idx != parts->fill - 1) {
-		memcpy(parts->part_array + idx, parts->part_array + parts->fill - 1, sizeof(gpt_entry_t));
-		parts->fill -= 1;
-	}
-
-	if (parts->fill < (parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
-		if (reduce_part_array(parts) == -1)
-			return -1;
-	}
-
-	return 0;
-}
-
-/** free() GPT header including gpt->header_lba */
-void gpt_free_gpt(gpt_t * gpt)
-{
-	free(gpt->raw_data);
-	free(gpt);
+int gpt_remove_partition(gpt_label_t *label, size_t idx)
+{
+	if (idx >= label->parts->fill)
+		return EINVAL;
+	
+	/* FIXME! 
+	 * If we allow blank spots, we break the array. If we have more than
+	 * 128 partitions in the array and then remove something from
+	 * the first 128 partitions, we would forget to write the last one.*/
+	memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
+	
+	label->parts->fill -= 1;
+	
+	/* FIXME!
+	 * We cannot reduce the array so simply. We may have some partitions
+	 * there since we allow blank spots.*/
+	if (label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
+		if (reduce_part_array(label->parts) == ENOMEM)
+			return ENOMEM;
+	}
+
+	return EOK;
 }
 
@@ -388,9 +487,28 @@
 {
 	size_t i;
+	
 	for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
-		if (gpt_memcmp(p->part_type, gpt_ptypes[i].guid, 16) == 0) {
-			break;
-		}
-	}
+		if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
+			p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
+			p->part_type[1] == get_byte(gpt_ptypes[i].guid +4) &&
+			p->part_type[0] == get_byte(gpt_ptypes[i].guid +6) &&
+			
+			p->part_type[5] == get_byte(gpt_ptypes[i].guid +8) &&
+			p->part_type[4] == get_byte(gpt_ptypes[i].guid +10) &&
+			
+			p->part_type[7] == get_byte(gpt_ptypes[i].guid +12) &&
+			p->part_type[6] == get_byte(gpt_ptypes[i].guid +14) &&
+			
+			p->part_type[8] == get_byte(gpt_ptypes[i].guid +16) &&
+			p->part_type[9] == get_byte(gpt_ptypes[i].guid +18) &&
+			p->part_type[10] == get_byte(gpt_ptypes[i].guid +20) &&
+			p->part_type[11] == get_byte(gpt_ptypes[i].guid +22) &&
+			p->part_type[12] == get_byte(gpt_ptypes[i].guid +24) &&
+			p->part_type[13] == get_byte(gpt_ptypes[i].guid +26) &&
+			p->part_type[14] == get_byte(gpt_ptypes[i].guid +28) &&
+			p->part_type[15] == get_byte(gpt_ptypes[i].guid +30))
+				break;
+	}
+	
 	return i;
 }
@@ -449,5 +567,5 @@
 }
 
-
+/** Get partition name */
 unsigned char * gpt_get_part_name(gpt_part_t * p)
 {
@@ -456,5 +574,5 @@
 
 /** Copy partition name */
-void gpt_set_part_name(gpt_part_t * p, char * name[], size_t length)
+void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
 {
 	if (length >= 72)
@@ -561,9 +679,8 @@
 	if(p->arr_size > GPT_MIN_PART_NUM) {
 		unsigned int nsize = p->arr_size / 2;
+		nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
 		gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
-		if(tmp == NULL) {
-			errno = ENOMEM;
-			return -1;
-		}
+		if(tmp == NULL)
+			return ENOMEM;
 
 		memcpy(tmp, p->part_array, p->fill < nsize ? p->fill  : nsize);
@@ -586,21 +703,14 @@
 }
 
-static int gpt_memcmp(const void * a, const void * b, size_t len)
-{
-	size_t i;
-	int diff;
-	const unsigned char * x = a;
-	const unsigned char * y = b;
-
-	for (i = 0; i < len; i++) {
-		diff = (int)*(x++) - (int)*(y++);
-		if (diff != 0) {
-			return diff;
-		}
-	}
-	return 0;
-}
-
-
-
-
+static uint8_t get_byte(const char * c)
+{
+	uint8_t val = 0;
+	char hex[3] = {*c, *(c+1), 0};
+	
+	errno = str_uint8_t(hex, NULL, 16, false, &val);
+	return val;
+}
+
+
+
+
Index: uspace/lib/gpt/libgpt.h
===================================================================
--- uspace/lib/gpt/libgpt.h	(revision 6e8e4e191b06e73e7414d8e4538ce949d1bd24f9)
+++ uspace/lib/gpt/libgpt.h	(revision 1c8bfe8be1ae2ca042fcbe7b5cc365f259f3a422)
@@ -53,4 +53,7 @@
 #define GPT_IGNORE_FILL_NUM 10
 
+/** Unused partition entry */
+#define GPT_PTE_UNUSED 0
+
 /** GPT header signature ("EFI PART" in ASCII) */
 extern const uint8_t efi_signature[8];
@@ -86,9 +89,6 @@
 typedef struct {
 	/** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
-	 * See gpt_read_gpt_header() to know why. */
-	gpt_header_t * raw_data;
-	/** Device where the data are from */
-	service_id_t device;
-	/** Linked list of partitions (initially NULL) */
+	 * See gpt_alloc_header() to know why. */
+	gpt_header_t *header;
 } gpt_t;
 
@@ -123,51 +123,56 @@
 	size_t arr_size;
 	/** Resizable partition array */
-	gpt_entry_t * part_array;
+	gpt_entry_t *part_array;
 } gpt_partitions_t;
 
 
 typedef struct gpt_table {
-	gpt_t * gpt;
-	gpt_partitions_t * parts;
+	gpt_t *gpt;
+	gpt_partitions_t *parts;
+	service_id_t device;
 } gpt_label_t;
 
 struct partition_type {
-	const char * desc;
-	const char * guid;
+	const char *desc;
+	const char *guid;
 };
 
 extern const struct partition_type gpt_ptypes[];
 
+extern gpt_label_t * gpt_alloc_label(void);
+extern void gpt_free_label(gpt_label_t *);
 
-extern gpt_t * gpt_alloc_gpt_header(void);
-extern gpt_t * gpt_read_gpt_header(service_id_t dev_handle);
-extern int     gpt_write_gpt_header(gpt_t * header, service_id_t dev_handle);
+extern gpt_t * gpt_alloc_header(size_t);
+extern int     gpt_read_header(gpt_label_t *, service_id_t);
+extern int     gpt_write_header(gpt_label_t *, service_id_t);
 
 extern gpt_partitions_t * gpt_alloc_partitions(void);
-extern gpt_partitions_t * gpt_read_partitions(gpt_t * gpt);
-extern int             gpt_write_partitions(gpt_partitions_t * parts, gpt_t * header, service_id_t dev_handle);
-extern gpt_part_t *    gpt_alloc_partition (gpt_partitions_t * parts);
-extern int             gpt_add_partition   (gpt_partitions_t * parts, gpt_part_t * partition);
-extern int             gpt_remove_partition(gpt_partitions_t * parts, size_t idx);
+extern int             gpt_read_partitions (gpt_label_t *);
+extern int             gpt_write_partitions(gpt_label_t *, service_id_t);
+extern gpt_part_t *    gpt_alloc_partition (void);
+extern gpt_part_t *    gpt_get_partition   (gpt_label_t *);
+extern gpt_part_t *    gpt_get_partition_at(gpt_label_t *, size_t);
+extern int             gpt_add_partition   (gpt_label_t *, gpt_part_t *);
+extern int             gpt_remove_partition(gpt_label_t *, size_t);
 
-extern size_t          gpt_get_part_type(gpt_part_t * p);
-extern void            gpt_set_part_type(gpt_part_t * p, size_t type);
-extern void            gpt_set_start_lba(gpt_part_t * p, uint64_t start);
-extern uint64_t        gpt_get_start_lba(gpt_part_t * p);
-extern void            gpt_set_end_lba  (gpt_part_t * p, uint64_t end);
-extern uint64_t        gpt_get_end_lba  (gpt_part_t * p);
-extern unsigned char * gpt_get_part_name(gpt_part_t * p);
-extern void            gpt_set_part_name(gpt_part_t * p, char * name[], size_t length);
-extern bool            gpt_get_flag     (gpt_part_t * p, GPT_ATTR flag);
-extern void            gpt_set_flag     (gpt_part_t * p, GPT_ATTR flag, bool value);
+extern size_t          gpt_get_part_type(gpt_part_t *);
+extern void            gpt_set_part_type(gpt_part_t *, size_t);
+extern void            gpt_set_start_lba(gpt_part_t *, uint64_t);
+extern uint64_t        gpt_get_start_lba(gpt_part_t *);
+extern void            gpt_set_end_lba  (gpt_part_t *, uint64_t);
+extern uint64_t        gpt_get_end_lba  (gpt_part_t *);
+extern unsigned char * gpt_get_part_name(gpt_part_t *);
+extern void            gpt_set_part_name(gpt_part_t *, char *, size_t);
+extern bool            gpt_get_flag     (gpt_part_t *, GPT_ATTR);
+extern void            gpt_set_flag     (gpt_part_t *, GPT_ATTR, bool);
 
 
 
-#define gpt_part_foreach(parts, iterator) \
-		for(gpt_part_t * iterator = (parts)->part_array; \
-		    iterator < (parts)->part_array + (parts)->fill; ++iterator)
+#define gpt_part_foreach(label, iterator) \
+		for(gpt_part_t * iterator = (label)->parts->part_array; \
+		    iterator < (label)->parts->part_array + (label)->parts->fill; ++iterator)
 
-extern void gpt_free_gpt(gpt_t * gpt);
-extern void gpt_free_partitions(gpt_partitions_t * parts);
+extern void gpt_free_gpt(gpt_t *);
+extern void gpt_free_partitions(gpt_partitions_t *);
 
 #endif
Index: uspace/lib/mbr/libmbr.c
===================================================================
--- uspace/lib/mbr/libmbr.c	(revision 6e8e4e191b06e73e7414d8e4538ce949d1bd24f9)
+++ uspace/lib/mbr/libmbr.c	(revision 1c8bfe8be1ae2ca042fcbe7b5cc365f259f3a422)
@@ -94,8 +94,5 @@
  */
 int mbr_read_mbr(mbr_label_t *label, service_id_t dev_handle)
-{
-	if (label == NULL)
-		return EINVAL;
-	
+{	
 	int rc;
 	
@@ -195,5 +192,4 @@
 		
 		rc_ext = decode_part(&(label->mbr->raw_data.pte[i]), p, 0);
-		printf("p: %d %u %u\n", rc_ext, p->start_addr, p->length);
 		mbr_set_flag(p, ST_LOGIC, false);
 		rc = mbr_add_partition(label, p);
@@ -207,5 +203,4 @@
 		if (rc_ext) {
 			ext = p;
-			printf("ext: %u %u\n", p->start_addr, p->length);
 			label->parts->l_extended = &p->link;
 		}
@@ -231,4 +226,10 @@
 int mbr_write_partitions(mbr_label_t *label, service_id_t dev_handle)
 {
+	if (label->parts == NULL)
+		return EOK;
+	
+	if (label->mbr == NULL)
+		label->mbr = mbr_alloc_mbr();
+	
 	int i = 0;
 	int rc;
@@ -248,5 +249,4 @@
 	for (i = 0; i < N_PRIMARY; i++) {
 		p = list_get_instance(l, mbr_part_t, link);	
-		printf("status: %hu\n", p->status);
 		encode_part(p, &(label->mbr->raw_data.pte[i]), 0, false);
 		l = l->next;
