Improve sdcard partitioning process

-Improve code for partitioning sdcards
-Allow user to select a device for partitioning (must be removable)
-Use sgdisk to partition sdcards
-Set default sizes for ext and swap to 0
-Change increments for ext to 256MB and swap to 64MB

Note: sgdisk is included in 6.0. I have included a static prebuilt
sgdisk for trees that do not have sgdisk, however the prebuilt
sgdisk is a decent bit larger than the old parted binary. The old
parted binary is quite old at this point and we only have it for
armv7a. sgdisk should be maintained by AOSP and can be built from
source so it should work across architectures.

Change-Id: Ib80882d9b5776e5e9358b11340fba392e6f1ae09
diff --git a/Android.mk b/Android.mk
index 4414973..0ac82ba 100644
--- a/Android.mk
+++ b/Android.mk
@@ -374,7 +374,11 @@
     LOCAL_ADDITIONAL_DEPENDENCIES += mkexfatfs fsckexfat
 endif
 ifeq ($(BOARD_HAS_NO_REAL_SDCARD),)
-    LOCAL_ADDITIONAL_DEPENDENCIES += parted
+    ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 22; echo $$?),0)
+        LOCAL_ADDITIONAL_DEPENDENCIES += sgdisk
+    else
+        LOCAL_ADDITIONAL_DEPENDENCIES += sgdisk_static
+    endif
 endif
 ifneq ($(TW_EXCLUDE_ENCRYPTED_BACKUPS), true)
     LOCAL_ADDITIONAL_DEPENDENCIES += openaes ../openaes/LICENSE
diff --git a/data.cpp b/data.cpp
index 5cca3a1..c945f2a 100644
--- a/data.cpp
+++ b/data.cpp
@@ -794,8 +794,8 @@
 	mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
 	mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
 	mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
-	mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
-	mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
+	mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("0", 1)));
+	mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("0", 1)));
 	mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
 	mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT,M3.2.0,M11.1.0", 1)));
 	mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
diff --git a/gui/action.cpp b/gui/action.cpp
index 656c687..e13d15c 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -792,25 +792,27 @@
 	return 0;
 }
 
-int GUIAction::checkpartitionlist(std::string arg __unused)
+int GUIAction::checkpartitionlist(std::string arg)
 {
-	string Wipe_List, wipe_path;
+	string List, part_path;
 	int count = 0;
 
-	DataManager::GetValue("tw_wipe_list", Wipe_List);
-	LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
-	if (!Wipe_List.empty()) {
-		size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
-		while (end_pos != string::npos && start_pos < Wipe_List.size()) {
-			wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
-			LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
-			if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
+	if (arg.empty())
+		arg = "tw_wipe_list";
+	DataManager::GetValue(arg, List);
+	LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
+	if (!List.empty()) {
+		size_t start_pos = 0, end_pos = List.find(";", start_pos);
+		while (end_pos != string::npos && start_pos < List.size()) {
+			part_path = List.substr(start_pos, end_pos - start_pos);
+			LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
+			if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
 				// Do nothing
 			} else {
 				count++;
 			}
 			start_pos = end_pos + 1;
-			end_pos = Wipe_List.find(";", start_pos);
+			end_pos = List.find(";", start_pos);
 		}
 		DataManager::SetValue("tw_check_partition_list", count);
 	} else {
@@ -819,29 +821,32 @@
 		return 0;
 }
 
-int GUIAction::getpartitiondetails(std::string arg __unused)
+int GUIAction::getpartitiondetails(std::string arg)
 {
-	string Wipe_List, wipe_path;
+	string List, part_path;
 	int count = 0;
 
-	DataManager::GetValue("tw_wipe_list", Wipe_List);
-	LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
-	if (!Wipe_List.empty()) {
-		size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
-		while (end_pos != string::npos && start_pos < Wipe_List.size()) {
-			wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
-			LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
-			if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
+	if (arg.empty())
+		arg = "tw_wipe_list";
+	DataManager::GetValue(arg, List);
+	LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
+	if (!List.empty()) {
+		size_t start_pos = 0, end_pos = List.find(";", start_pos);
+		part_path = List;
+		while (end_pos != string::npos && start_pos < List.size()) {
+			part_path = List.substr(start_pos, end_pos - start_pos);
+			LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
+			if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
 				// Do nothing
 			} else {
-				DataManager::SetValue("tw_partition_path", wipe_path);
+				DataManager::SetValue("tw_partition_path", part_path);
 				break;
 			}
 			start_pos = end_pos + 1;
-			end_pos = Wipe_List.find(";", start_pos);
+			end_pos = List.find(";", start_pos);
 		}
-		if (!wipe_path.empty()) {
-			TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
+		if (!part_path.empty()) {
+			TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
 			if (Part) {
 				unsigned long long mb = 1048576;
 
@@ -881,12 +886,14 @@
 					DataManager::SetValue("tw_partition_ext", 0);
 				return 0;
 			} else {
-				LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
+				LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
 			}
 		}
 	}
 	DataManager::SetValue("tw_partition_name", "");
 	DataManager::SetValue("tw_partition_file_system", "");
+	// Set this to 0 to prevent trying to partition this device, just in case
+	DataManager::SetValue("tw_partition_removable", 0);
 	return 0;
 }
 
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index 6c91d06..57fef40 100755
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -1068,7 +1068,7 @@
 				<placement x="%col1_x_left%" y="%row15a_y%"/>
 				<text>{@repair_change_btn=Repair or Change File System}</text>
 				<actions>
-					<action function="checkpartitionlist"/>
+					<action function="checkpartitionlist">tw_wipe_list</action>
 					<action function="page">checkpartitionlist</action>
 				</actions>
 			</button>
@@ -1179,7 +1179,7 @@
 				<condition var1="tw_check_partition_list" op="=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=0</action>
-					<action function="getpartitiondetails"/>
+					<action function="getpartitiondetails">tw_wipe_list</action>
 					<action function="page">partitionoptions</action>
 				</actions>
 			</action>
@@ -1188,7 +1188,6 @@
 				<condition var1="tw_check_partition_list" op="!=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=1</action>
-					<action function="set">tw_wipe_list=</action>
 					<action function="page">advancedwipe</action>
 				</actions>
 			</action>
@@ -1271,6 +1270,7 @@
 					<action function="set">tw_action_param=%tw_partition_mount_point%</action>
 					<action function="set">tw_has_action2=1</action>
 					<action function="set">tw_action2=getpartitiondetails</action>
+					<action function="set">tw_action2_param=tw_wipe_list</action>
 					<action function="set">tw_text1={@resize_confirm=Resize %tw_partition_name%?}</action>
 					<action function="set">tw_text2=</action>
 					<action function="set">tw_action_text1={@resizing=Resizing...}</action>
@@ -1320,7 +1320,7 @@
 				<condition var1="tw_check_partition_list" op="=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=0</action>
-					<action function="getpartitiondetails"/>
+					<action function="getpartitiondetails">tw_wipe_list</action>
 					<action function="page">selectfilesystem</action>
 				</actions>
 			</action>
@@ -3091,7 +3091,10 @@
 				<condition var1="tw_allow_partition_sdcard" var2="1"/>
 				<placement x="%col1_x_left%" y="%row6a_y%"/>
 				<text>{@part_sd_btn=Partition SD Card}</text>
-				<action function="page">partsdcard</action>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
 			</button>
 
 			<button style="main_button">
@@ -3151,6 +3154,69 @@
 			</action>
 		</page>
 
+		<page name="partsdcardsel">
+			<template name="page"/>
+
+			<text style="text_l">
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@advanced_hdr=Advanced}</text>
+			</text>
+
+			<text style="text_m">
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@part_sd_hdr=Partition SD Card}</text>
+			</text>
+
+			<partitionlist style="partitionlist_storage">
+				<text>{@sel_storage_list=Select Storage}</text>
+				<data name="tw_storage_path"/>
+				<listtype name="storage"/>
+			</partitionlist>
+
+			<button style="button_third_width">
+				<placement x="%dialog_button_x%" y="%row13_y%"/>
+				<text>{@ok_btn=OK}</text>
+				<actions>
+					<action function="getpartitiondetails">tw_storage_path</action>
+					<action function="page">partsdcardcheck</action>
+				</actions>
+			</button>
+
+			<text style="text_m_fail">
+				<condition var1="partitionlisterror" var2="1"/>
+				<placement x="%center_x%" y="%row17_y%" placement="5"/>
+				<text>{@invalid_partsd_sel=You must select a removable device}</text>
+			</text>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">advanced</action>
+			</action>
+		</page>
+
+		<page name="partsdcardcheck">
+			<action>
+				<condition var1="tw_partition_removable" op="=" var2="1"/>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcard</action>
+				</actions>
+			</action>
+
+			<action>
+				<condition var1="tw_partition_removable" op="!=" var2="1"/>
+				<actions>
+					<action function="set">partitionlisterror=1</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
+			</action>
+		</page>
+
 		<page name="partsdcard">
 			<template name="page"/>
 
@@ -3187,13 +3253,13 @@
 			<button style="button_quarter_width">
 				<placement x="%btn4_col3_x_left%" y="%row5_y%"/>
 				<text>{@part_sd_m=-}</text>
-				<action function="addsubtract">tw_sdext_size-128</action>
+				<action function="addsubtract">tw_sdext_size-256</action>
 			</button>
 
 			<button style="button_quarter_width">
 				<placement x="%btn4_col2_x_right%" y="%row5_y%"/>
 				<text>{@part_sd_p=+}</text>
-				<action function="addsubtract">tw_sdext_size+128</action>
+				<action function="addsubtract">tw_sdext_size+256</action>
 			</button>
 
 			<text style="text_m_accent">
@@ -3209,13 +3275,13 @@
 			<button style="button_quarter_width">
 				<placement x="%btn4_col3_x_left%" y="%row9_y%"/>
 				<text>{@part_sd_m=-}</text>
-				<action function="addsubtract">tw_swap_size-32</action>
+				<action function="addsubtract">tw_swap_size-64</action>
 			</button>
 
 			<button style="button_quarter_width">
 				<placement x="%btn4_col2_x_right%" y="%row9_y%"/>
 				<text>{@part_sd_p=+}</text>
-				<action function="addsubtract">tw_swap_size+32</action>
+				<action function="addsubtract">tw_swap_size+64</action>
 			</button>
 
 			<text style="text_m_accent">
@@ -3263,7 +3329,7 @@
 
 			<action>
 				<touch key="back"/>
-				<action function="page">advanced</action>
+				<action function="page">partsdcardsel</action>
 			</action>
 		</page>
 
@@ -4353,7 +4419,6 @@
 
 			<slider style="slider_centered">
 				<text>{@swipe_to_sideload=Swipe to Start Sideload}</text>
-				<action function="page">partsdcardaction</action>
 				<actions>
 					<action function="set">tw_back=advanced</action>
 					<action function="set">tw_action=adbsideload</action>
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index 9669e70..c77d3a2 100755
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -379,6 +379,7 @@
 		<string name="inject_twrp_complete">TWRP Injection Complete</string>
 		<string name="swipe_to_confirm">Swipe to Confirm</string>
 		<string name="part_sd_hdr">Partition SD Card</string>
+		<string name="invalid_partsd_sel">You must select a removable device</string>
 		<string name="part_sd_lose">You will lose all files on your SD card!</string>
 		<string name="part_sd_undo">This action cannot be undone!</string>
 		<string name="part_sd_ext_sz">EXT Size:</string>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index 251be75..d93a273 100755
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -1020,7 +1020,7 @@
 				<placement x="%indent%" y="%row18a_y%"/>
 				<text>{@repair_change_btn=Repair or Change File System}</text>
 				<actions>
-					<action function="checkpartitionlist"/>
+					<action function="checkpartitionlist">tw_wipe_list</action>
 					<action function="page">checkpartitionlist</action>
 				</actions>
 			</button>
@@ -1136,7 +1136,7 @@
 				<condition var1="tw_check_partition_list" op="=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=0</action>
-					<action function="getpartitiondetails"/>
+					<action function="getpartitiondetails">tw_wipe_list</action>
 					<action function="page">partitionoptions</action>
 				</actions>
 			</action>
@@ -1145,7 +1145,6 @@
 				<condition var1="tw_check_partition_list" op="!=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=1</action>
-					<action function="set">tw_wipe_list=</action>
 					<action function="page">advancedwipe</action>
 				</actions>
 			</action>
@@ -1228,6 +1227,7 @@
 					<action function="set">tw_action_param=%tw_partition_mount_point%</action>
 					<action function="set">tw_has_action2=1</action>
 					<action function="set">tw_action2=getpartitiondetails</action>
+					<action function="set">tw_action2_param=tw_wipe_list</action>
 					<action function="set">tw_text1={@resize_confirm=Resize %tw_partition_name%?}</action>
 					<action function="set">tw_text2=</action>
 					<action function="set">tw_action_text1={@resizing=Resizing...}</action>
@@ -1277,7 +1277,7 @@
 				<condition var1="tw_check_partition_list" op="=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=0</action>
-					<action function="getpartitiondetails"/>
+					<action function="getpartitiondetails">tw_wipe_list</action>
 					<action function="page">selectfilesystem</action>
 				</actions>
 			</action>
@@ -3161,7 +3161,10 @@
 				<condition var1="tw_allow_partition_sdcard" var2="1"/>
 				<placement x="%indent%" y="%row8_y%"/>
 				<text>{@part_sd_btn=Partition SD Card}</text>
-				<action function="page">partsdcard</action>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
 			</button>
 
 			<button style="main_button">
@@ -3221,6 +3224,69 @@
 			</action>
 		</page>
 
+		<page name="partsdcardsel">
+			<template name="page"/>
+
+			<text style="text_l">
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@advanced_hdr=Advanced}</text>
+			</text>
+
+			<text style="text_m">
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@part_sd_hdr=Partition SD Card}</text>
+			</text>
+
+			<partitionlist style="partitionlist_storage">
+				<text>{@sel_storage_list=Select Storage}</text>
+				<data name="tw_storage_path"/>
+				<listtype name="storage"/>
+			</partitionlist>
+
+			<text style="text_m_fail">
+				<condition var1="partitionlisterror" var2="1"/>
+				<placement x="%center_x%" y="%row17_y%" placement="5"/>
+				<text>{@invalid_partsd_sel=You must select a removable device}</text>
+			</text>
+
+			<button style="button_third_width">
+				<placement x="%dialog_button_x%" y="%row14_y%"/>
+				<text>{@ok_btn=OK}</text>
+				<actions>
+					<action function="getpartitiondetails">tw_storage_path</action>
+					<action function="page">partsdcardcheck</action>
+				</actions>
+			</button>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">advanced</action>
+			</action>
+		</page>
+
+		<page name="partsdcardcheck">
+			<action>
+				<condition var1="tw_partition_removable" op="=" var2="1"/>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcard</action>
+				</actions>
+			</action>
+
+			<action>
+				<condition var1="tw_partition_removable" op="!=" var2="1"/>
+				<actions>
+					<action function="set">partitionlisterror=1</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
+			</action>
+		</page>
+
 		<page name="partsdcard">
 			<template name="page"/>
 
@@ -3257,13 +3323,13 @@
 			<button style="button_quarter_width">
 				<placement x="%indent%" y="%row6_y%"/>
 				<text>{@part_sd_m=-}</text>
-				<action function="addsubtract">tw_sdext_size-128</action>
+				<action function="addsubtract">tw_sdext_size-256</action>
 			</button>
 
 			<button style="button_quarter_width">
 				<placement x="%btn4_col4_x%" y="%row6_y%"/>
 				<text>{@part_sd_p=+}</text>
-				<action function="addsubtract">tw_sdext_size+128</action>
+				<action function="addsubtract">tw_sdext_size+256</action>
 			</button>
 
 			<text style="text_m_accent">
@@ -3279,13 +3345,13 @@
 			<button style="button_quarter_width">
 				<placement x="%indent%" y="%row10_y%"/>
 				<text>{@part_sd_m=-}</text>
-				<action function="addsubtract">tw_swap_size-32</action>
+				<action function="addsubtract">tw_swap_size-64</action>
 			</button>
 
 			<button style="button_quarter_width">
 				<placement x="%btn4_col4_x%" y="%row10_y%"/>
 				<text>{@part_sd_p=+}</text>
-				<action function="addsubtract">tw_swap_size+32</action>
+				<action function="addsubtract">tw_swap_size+64</action>
 			</button>
 
 			<text style="text_m_accent">
@@ -3333,7 +3399,10 @@
 
 			<action>
 				<touch key="back"/>
-				<action function="page">advanced</action>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
 			</action>
 		</page>
 
@@ -4242,7 +4311,6 @@
 
 			<slider>
 				<text>{@swipe_to_sideload=Swipe to Start Sideload}</text>
-				<action function="page">partsdcardaction</action>
 				<actions>
 					<action function="set">tw_back=advanced</action>
 					<action function="set">tw_action=adbsideload</action>
diff --git a/gui/theme/common/watch.xml b/gui/theme/common/watch.xml
index 7d343dd..6ed0b83 100755
--- a/gui/theme/common/watch.xml
+++ b/gui/theme/common/watch.xml
@@ -1422,7 +1422,7 @@
 				<condition var1="tw_check_partition_list" op="=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=0</action>
-					<action function="getpartitiondetails"/>
+					<action function="getpartitiondetails">tw_wipe_list</action>
 					<action function="page">partitionoptions</action>
 				</actions>
 			</action>
@@ -1431,7 +1431,6 @@
 				<condition var1="tw_check_partition_list" op="!=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=1</action>
-					<action function="set">tw_wipe_list=</action>
 					<action function="page">partitionoptions_select</action>
 				</actions>
 			</action>
@@ -1471,7 +1470,7 @@
 				<placement x="%btn4_col4_x%" y="%row11_y%"/>
 				<highlight color="%highlight_color%"/>
 				<image resource="q_btn_arrow_right"/><actions>
-					<action function="checkpartitionlist"/>
+					<action function="checkpartitionlist">tw_wipe_list</action>
 					<action function="page">checkpartitionlist</action>
 				</actions>
 			</button>
@@ -1562,6 +1561,7 @@
 					<action function="set">tw_action_param=%tw_partition_mount_point%</action>
 					<action function="set">tw_has_action2=1</action>
 					<action function="set">tw_action2=getpartitiondetails</action>
+					<action function="set">tw_action2_param=tw_wipe_list</action>
 					<action function="set">tw_text1={@resize_confirm=Resize %tw_partition_name%?}</action>
 					<action function="set">tw_text2=</action>
 					<action function="set">tw_action_text1={@resizing=Resizing...}</action>
@@ -1618,7 +1618,7 @@
 				<condition var1="tw_check_partition_list" op="=" var2="1"/>
 				<actions>
 					<action function="set">partitionlisterror=0</action>
-					<action function="getpartitiondetails"/>
+					<action function="getpartitiondetails">tw_wipe_list</action>
 					<action function="page">selectfilesystem</action>
 				</actions>
 			</action>
@@ -3896,7 +3896,10 @@
 				<condition var1="tw_allow_partition_sdcard" var2="1"/>
 				<placement x="%col1_x_right%" y="%row6_y%"/>
 				<text>{@part_sd_s_btn=SD Card}</text>
-				<action function="page">partsdcard</action>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
 			</button>
 
 			<button style="main_button">
@@ -3932,6 +3935,73 @@
 			</action>
 		</page>
 
+		<page name="partsdcardsel">
+			<template name="page"/>
+
+			<template name="statusbar"/>
+
+			<text style="text_m">
+				<placement x="%col1_x_left%" y="%row1_header_y%"/>
+				<text>{@advanced_hdr=Advanced} &gt; {@part_sd_hdr=Partition SD Card}</text>
+			</text>
+
+			<partitionlist style="partitionlist_headerless_rb">
+				<data name="tw_storage_path"/>
+				<listtype name="storage"/>
+			</partitionlist>
+
+			<text style="text_m_fail">
+				<condition var1="partitionlisterror" var2="1"/>
+				<placement x="%center_x%" y="%row9_y%" placement="5"/>
+				<text>{@invalid_partsd_sel=You must select a removable device}</text>
+			</text>
+
+			<button>
+				<placement x="%btn4_col2_x%" y="%row11_y%"/>
+				<highlight color="%highlight_color%"/>
+				<image resource="q_btn_refresh"/>
+				<action function="refreshsizes"/>
+			</button>
+
+			<button>
+				<placement x="%btn4_col3_x%" y="%row11_y%"/>
+				<highlight color="%highlight_color%"/>
+				<image resource="q_btn_accept"/>
+				<actions>
+					<action function="getpartitiondetails">tw_storage_path</action>
+					<action function="page">partsdcardcheck</action>
+				</actions>
+			</button>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">advanced</action>
+			</action>
+		</page>
+
+		<page name="partsdcardcheck">
+			<action>
+				<condition var1="tw_partition_removable" op="=" var2="1"/>
+				<actions>
+					<action function="set">partitionlisterror=0</action>
+					<action function="page">partsdcard</action>
+				</actions>
+			</action>
+
+			<action>
+				<condition var1="tw_partition_removable" op="!=" var2="1"/>
+				<actions>
+					<action function="set">partitionlisterror=1</action>
+					<action function="page">partsdcardsel</action>
+				</actions>
+			</action>
+		</page>
+
 		<page name="partsdcard">
 			<template name="page"/>
 
@@ -3965,13 +4035,13 @@
 			<button style="main_button_quarter_width">
 				<placement x="%indent%" y="%row6_y%"/>
 				<text>{@part_sd_m=-}</text>
-				<action function="addsubtract">tw_sdext_size-128</action>
+				<action function="addsubtract">tw_sdext_size-256</action>
 			</button>
 
 			<button style="main_button_quarter_width">
 				<placement x="%btn4_col4_x%" y="%row6_y%"/>
 				<text>{@part_sd_p=+}</text>
-				<action function="addsubtract">tw_sdext_size+128</action>
+				<action function="addsubtract">tw_sdext_size+256</action>
 			</button>
 
 			<button>
@@ -3988,7 +4058,7 @@
 
 			<action>
 				<touch key="back"/>
-				<action function="page">advanced</action>
+				<action function="page">partsdcardsel</action>
 			</action>
 		</page>
 
@@ -4025,13 +4095,13 @@
 			<button style="main_button_quarter_width">
 				<placement x="%indent%" y="%row6_y%"/>
 				<text>{@part_sd_m=-}</text>
-				<action function="addsubtract">tw_swap_size-32</action>
+				<action function="addsubtract">tw_swap_size-64</action>
 			</button>
 
 			<button style="main_button_quarter_width">
 				<placement x="%btn4_col4_x%" y="%row6_y%"/>
 				<text>{@part_sd_p=+}</text>
-				<action function="addsubtract">tw_swap_size+32</action>
+				<action function="addsubtract">tw_swap_size+64</action>
 			</button>
 
 			<button>
@@ -5052,7 +5122,6 @@
 
 			<slider>
 				<text>{@swipe_sideload=   Start}</text>
-				<action function="page">partsdcardaction</action>
 				<actions>
 					<action function="set">tw_back=install_type</action>
 					<action function="set">tw_action=adbsideload</action>
diff --git a/partition.cpp b/partition.cpp
index 2f37d6f..a8de4a9 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -880,23 +880,9 @@
 }
 
 unsigned long long TWPartition::IOCTL_Get_Block_Size() {
-	unsigned long block_device_size;
-	int ret = 0;
-
 	Find_Actual_Block_Device();
-	int fd = open(Actual_Block_Device.c_str(), O_RDONLY);
-	if (fd < 0) {
-		LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", Actual_Block_Device.c_str(), strerror(errno));
-	} else {
-		ret = ioctl(fd, BLKGETSIZE, &block_device_size);
-		close(fd);
-		if (ret) {
-			LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno));
-		} else {
-			return (unsigned long long)(block_device_size) * 512LLU;
-		}
-	}
-	return 0;
+
+	return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
 }
 
 bool TWPartition::Find_Partition_Size(void) {
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index c1cd29f..4cd3e7d 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -30,6 +30,8 @@
 #include <iostream>
 #include <iomanip>
 #include <sys/wait.h>
+#include <linux/fs.h>
+#include <sys/mount.h>
 #include "variables.h"
 #include "twcommon.h"
 #include "partitions.hpp"
@@ -1658,20 +1660,20 @@
 
 int TWPartitionManager::Partition_SDCard(void) {
 	char mkdir_path[255], temp[255], line[512];
-	string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
+	string Storage_Path, Command, Device, fat_str, ext_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
 	int ext, swap, total_size = 0, fat_size;
-	FILE* fp;
 
 	gui_msg("start_partition_sd=Partitioning SD Card...");
-#ifdef TW_EXTERNAL_STORAGE_PATH
-	TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
-#else
-	TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
-#endif
+
+	// Locate and validate device to partition
+	TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
+
 	if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
 		gui_err("partition_sd_locate=Unable to locate device to partition.");
 		return false;
 	}
+
+	// Unmount everything
 	if (!SDCard->UnMount(true))
 		return false;
 	TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
@@ -1679,58 +1681,57 @@
 		if (!SDext->UnMount(true))
 			return false;
 	}
+	char* swappath = getenv("SWAPPATH");
+	if (swappath != NULL) {
+		LOGINFO("Unmounting swap at '%s'\n", swappath);
+		umount(swappath);
+	}
 
-	TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
-	Device = SDCard->Actual_Block_Device;
-	// Just use the root block device
-	Device.resize(strlen("/dev/block/mmcblkX"));
+	// Determine block device
+	if (SDCard->Alternate_Block_Device.empty()) {
+		SDCard->Find_Actual_Block_Device();
+		Device = SDCard->Actual_Block_Device;
+		// Just use the root block device
+		Device.resize(strlen("/dev/block/mmcblkX"));
+	} else {
+		Device = SDCard->Alternate_Block_Device;
+	}
 
 	// Find the size of the block device:
-	fp = fopen("/proc/partitions", "rt");
-	if (fp == NULL) {
-		gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")("/proc/partitions")(strerror(errno)));
-		return false;
-	}
-
-	while (fgets(line, sizeof(line), fp) != NULL)
-	{
-		unsigned long major, minor, blocks;
-		char device[512];
-		char tmpString[64];
-
-		if (strlen(line) < 7 || line[0] == 'm')	 continue;
-		sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
-
-		tmpdevice = "/dev/block/";
-		tmpdevice += device;
-		if (tmpdevice == Device) {
-			// Adjust block size to byte size
-			total_size = (int)(blocks * 1024ULL / 1000000LLU);
-			break;
-		}
-	}
-	fclose(fp);
+	total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576));
 
 	DataManager::GetValue("tw_sdext_size", ext);
 	DataManager::GetValue("tw_swap_size", swap);
 	DataManager::GetValue("tw_sdpart_file_system", ext_format);
 	fat_size = total_size - ext - swap;
-	LOGINFO("sd card block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
-	memset(temp, 0, sizeof(temp));
-	sprintf(temp, "%i", fat_size);
-	fat_str = temp;
-	memset(temp, 0, sizeof(temp));
-	sprintf(temp, "%i", fat_size + ext);
-	ext_str = temp;
-	memset(temp, 0, sizeof(temp));
-	sprintf(temp, "%i", fat_size + ext + swap);
-	swap_str = temp;
+	LOGINFO("sd card mount point %s block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", DataManager::GetCurrentStoragePath().c_str(), Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
+
+	// Determine partition sizes
+	if (swap == 0 && ext == 0) {
+		fat_str = "-0";
+	} else {
+		memset(temp, 0, sizeof(temp));
+		sprintf(temp, "%i", fat_size);
+		fat_str = temp;
+		fat_str += "MB";
+	}
+	if (swap == 0) {
+		ext_str = "-0";
+	} else {
+		memset(temp, 0, sizeof(temp));
+		sprintf(temp, "%i", ext);
+		ext_str = "+";
+		ext_str += temp;
+		ext_str += "MB";
+	}
+
 	if (ext + swap > total_size) {
 		gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
 		return false;
 	}
+
 	gui_msg("remove_part_table=Removing partition table...");
-	Command = "parted -s " + Device + " mklabel msdos";
+	Command = "sgdisk --zap-all " + Device;
 	LOGINFO("Command is: '%s'\n", Command.c_str());
 	if (TWFunc::Exec_Cmd(Command) != 0) {
 		gui_err("unable_rm_part=Unable to remove partition table.");
@@ -1738,7 +1739,7 @@
 		return false;
 	}
 	gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
-	Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
+	Command = "sgdisk  --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" " + Device;
 	LOGINFO("Command is: '%s'\n", Command.c_str());
 	if (TWFunc::Exec_Cmd(Command) != 0) {
 		gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
@@ -1746,7 +1747,7 @@
 	}
 	if (ext > 0) {
 		gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
-		Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
+		Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device;
 		LOGINFO("Command is: '%s'\n", Command.c_str());
 		if (TWFunc::Exec_Cmd(Command) != 0) {
 			gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
@@ -1756,7 +1757,7 @@
 	}
 	if (swap > 0) {
 		gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
-		Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
+		Command = "sgdisk --new=0:0:-0 --change-name=0:\"Linux swap\" --typecode=0:0657FD6D-A4AB-43C4-84E5-0933C84B4F4F " + Device;
 		LOGINFO("Command is: '%s'\n", Command.c_str());
 		if (TWFunc::Exec_Cmd(Command) != 0) {
 			gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
@@ -1764,38 +1765,52 @@
 			return false;
 		}
 	}
-	// recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
-#ifdef TW_EXTERNAL_STORAGE_PATH
-	Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
-	DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
-	memset(mkdir_path, 0, sizeof(mkdir_path));
-	sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
-#else
-	Mount_By_Path("/sdcard", 1);
-	strcpy(mkdir_path, "/sdcard/TWRP");
-#endif
-	mkdir(mkdir_path, 0777);
-	DataManager::Flush();
-#ifdef TW_EXTERNAL_STORAGE_PATH
-	DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
-	if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
-		DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
-#else
-	DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
-	if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
-		DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
-#endif
+
+	// Convert GPT to MBR
+	Command = "sgdisk --gpttombr " + Device;
+	if (TWFunc::Exec_Cmd(Command) != 0)
+		LOGINFO("Failed to covert partition GPT to MBR\n");
+
+	// Tell the kernel to rescan the partition table
+	int fd = open(Device.c_str(), O_RDONLY);
+	ioctl(fd, BLKRRPART, 0);
+	close(fd);
+
+	string format_device = Device;
+	if (Device.substr(0, 17) == "/dev/block/mmcblk")
+		format_device += "p";
+
+	// Format new partitions to proper file system
+	if (fat_size > 0) {
+		Command = "mkfs.fat " + format_device + "1";
+		TWFunc::Exec_Cmd(Command);
+	}
 	if (ext > 0) {
 		if (SDext == NULL) {
-			LOGERR("Unable to locate sd-ext partition.\n");
-			return false;
+			Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2";
+			gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
+			LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
+			TWFunc::Exec_Cmd(Command);
+		} else {
+			SDext->Wipe(ext_format);
 		}
-		Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
-		gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
-		LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
+	}
+	if (swap > 0) {
+		Command = "mkswap " + format_device;
+		if (ext > 0)
+			Command += "3";
+		else
+			Command += "2";
 		TWFunc::Exec_Cmd(Command);
 	}
 
+	// recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
+	if (SDCard->Mount(true)) {
+		string TWRP_Folder = SDCard->Mount_Point + "/TWRP";
+		mkdir(TWRP_Folder.c_str(), 0777);
+		DataManager::Flush();
+	}
+
 	Update_System_Details();
 	gui_msg("part_complete=Partitioning complete.");
 	return true;
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index df14bbd..8195425 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -201,6 +201,11 @@
     RELINK_SOURCE_FILES += $(TARGET_OUT_EXECUTABLES)/mkntfs
 endif
 endif
+ifeq ($(BOARD_HAS_NO_REAL_SDCARD),)
+    ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 22; echo $$?),0)
+        RELINK_SOURCE_FILES += $(TARGET_OUT_EXECUTABLES)/sgdisk
+    endif
+endif
 
 TWRP_AUTOGEN := $(intermediates)/teamwin
 
@@ -241,14 +246,25 @@
 include $(BUILD_PREBUILT)
 
 ifeq ($(BOARD_HAS_NO_REAL_SDCARD),)
+	ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+	    #prebuilt, static sgdisk
+	    include $(CLEAR_VARS)
+	    LOCAL_MODULE := sgdisk_static
+	    LOCAL_MODULE_STEM := sgdisk
+	    LOCAL_MODULE_TAGS := eng
+	    LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+	    LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+	    LOCAL_SRC_FILES := $(LOCAL_MODULE)
+	    include $(BUILD_PREBUILT)
+	endif
 	#parted
-	include $(CLEAR_VARS)
-	LOCAL_MODULE := parted
-	LOCAL_MODULE_TAGS := eng
-	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
-	LOCAL_SRC_FILES := $(LOCAL_MODULE)
-	include $(BUILD_PREBUILT)
+	#include $(CLEAR_VARS)
+	#LOCAL_MODULE := parted
+	#LOCAL_MODULE_TAGS := eng
+	#LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+	#LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+	#LOCAL_SRC_FILES := $(LOCAL_MODULE)
+	#include $(BUILD_PREBUILT)
 endif
 
 # copy license file for OpenAES
diff --git a/prebuilt/sgdisk_static b/prebuilt/sgdisk_static
new file mode 100755
index 0000000..eb8a129
--- /dev/null
+++ b/prebuilt/sgdisk_static
Binary files differ
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 6eb6cd5..def265c 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -1043,4 +1043,23 @@
 	}
 }
 
+unsigned long long TWFunc::IOCTL_Get_Block_Size(const char* block_device) {
+	unsigned long block_device_size;
+	int ret = 0;
+
+	int fd = open(block_device, O_RDONLY);
+	if (fd < 0) {
+		LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", block_device, strerror(errno));
+	} else {
+		ret = ioctl(fd, BLKGETSIZE, &block_device_size);
+		close(fd);
+		if (ret) {
+			LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno));
+		} else {
+			return (unsigned long long)(block_device_size) * 512LLU;
+		}
+	}
+	return 0;
+}
+
 #endif // ndef BUILD_TWRPTAR_MAIN
diff --git a/twrp-functions.hpp b/twrp-functions.hpp
index d49cd84..d72c916 100644
--- a/twrp-functions.hpp
+++ b/twrp-functions.hpp
@@ -85,6 +85,7 @@
 	static std::string to_string(unsigned long value); //convert ul to string
 	static void SetPerformanceMode(bool mode); // support recovery.perf.mode
 	static void Disable_Stock_Recovery_Replace(); // Disable stock ROMs from replacing TWRP with stock recovery
+	static unsigned long long IOCTL_Get_Block_Size(const char* block_device);
 
 private:
 	static void Copy_Log(string Source, string Destination);