Add home partition support

- Split partition table into 4G and 8G versions, that define a home
partition.
- Generate real UUIDs for partitions, and convert fstab to use them
instead of fixed block device IDs.
- Update the flash script to choose a partition table, in the same way
we choose BL2.
- Add a -H flag, to control erasing -- if specified, we erase the whole
mmc0, otherwise, we only flash over existing partitions.

Change-Id: I7e7db994c5484c577977059003a5c3a9d857f60b
diff --git a/flash.sh b/flash.sh
index 1d7cbf5..d74367b 100755
--- a/flash.sh
+++ b/flash.sh
@@ -17,6 +17,7 @@
 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 FASTBOOT_CMD="$(which fastboot)"
 DRAM_SIZE=2G
+EMMC_SIZE=8G
 
 function die {
   echo "$@" >/dev/stderr
@@ -29,10 +30,13 @@
 
 function flash_partitions {
   local files_dir="$1"
+  local overwrite_home="$2"
 
-  try ${FASTBOOT_CMD} erase mmc0
-  try ${FASTBOOT_CMD} erase mmc0boot0
-  try ${FASTBOOT_CMD} erase mmc0boot1
+  if [[ ! -z "${overwrite_home}" ]]; then
+    try ${FASTBOOT_CMD} erase mmc0
+    try ${FASTBOOT_CMD} erase mmc0boot0
+    try ${FASTBOOT_CMD} erase mmc0boot1
+  fi
   try ${FASTBOOT_CMD} flash mmc0 "${files_dir}/partition-table.img"
   try ${FASTBOOT_CMD} flash mmc0boot0 "${files_dir}/bl2.img"
   try ${FASTBOOT_CMD} flash mmc0boot1 "${files_dir}/u-boot-env.bin"
@@ -58,13 +62,19 @@
     ln -sf ${file} "${link_dir}"
   done
 
-  # BL2 needs to be handled separatedly for now.
+  # BL2 and partition tables need to be handled separatedly for now.
   if [[ -f "${files_dir}/bl2_${DRAM_SIZE}.img" ]]; then
     ln -sf "${files_dir}/bl2_${DRAM_SIZE}.img" "${link_dir}/bl2.img"
   else
     echo "bl2_${DRAM_SIZE}.img is missing!"
     found_all_files=""
   fi
+  if [[ -f "${files_dir}/partition-table_${EMMC_SIZE}.img" ]]; then
+    ln -sf "${files_dir}/partition-table_${EMMC_SIZE}.img" "${link_dir}/partition-table.img"
+  else
+    echo "partition-table_${EMMC_SIZE}.img is missing!"
+    found_all_files=""
+  fi
 
   for file in "${files[@]}"; do
     if [[ ! -f "${link_dir}/${file}" ]]; then
@@ -122,7 +132,8 @@
   local serial_number             # -s <serial>
   local files_dir                 # -d <files_dir>
   local lk_fastboot               # -l
-  local args=$(getopt hlous:d:r: $*)
+  local overwrite_home            # -H
+  local args=$(getopt hHlous:d:r: $*)
   set -- $args
 
   for i; do
@@ -152,6 +163,12 @@
 
       -o)  # old board
         DRAM_SIZE=1G
+        EMMC_SIZE=4G
+        shift 1
+        ;;
+
+      -H)  # overwrite home
+        overwrite_home=true
         shift 1
         ;;
 
@@ -195,7 +212,7 @@
     detect_device_or_die "${detect_retries}" "${serial_number}"
   fi
 
-  flash_partitions "${link_dir}"
+  flash_partitions "${link_dir}" "${overwrite_home}"
   try ${FASTBOOT_CMD} reboot
 
   echo "Flash completed."
diff --git a/fstab.emmc b/fstab.emmc
index 1ba4fec..9957a31 100644
--- a/fstab.emmc
+++ b/fstab.emmc
@@ -1,3 +1,4 @@
-/dev/mmcblk0p3 / ext4 noatime,defaults 0 1
-/dev/mmcblk0p2 /boot ext2 noatime,defaults 0 0
+PARTUUID=02f36a4f-4562-46b2-bbec-bfc1682f9e92 / ext4 noatime,defaults 0 1
+PARTUUID=b3c678b9-3004-4e30-87dc-ed33fbf4dbfd /home ext4 noatime,nosuid,nodev,defaults,x-systemd.makefs 0 3
+PARTUUID=0f94c91b-1d90-492f-ab86-6952303b214e /boot ext2 noatime,defaults 0 0
 tmpfs /var/log tmpfs defaults 0 0
diff --git a/partition-table.json b/partition-table.json
deleted file mode 100644
index d34088a..0000000
--- a/partition-table.json
+++ /dev/null
@@ -1,31 +0,0 @@
-{
-  "settings": {
-    "disk_alignment": "512 KiB",
-    "disk_guid": "85b83f23-51c4-4403-8bac-906ff5800f4e",
-    "disk_size": "3728 MiB"
-  },
-  "partitions": [
-    {
-      "label": "bootloaders",
-      "size": "4 MiB",
-      "guid": "595059e1-7b18-4e71-98f8-86b0ef75fe34",
-      "type_guid": "brillo_boot",
-      "position": 1
-    },
-    {
-      "label": "boot",
-      "size": "128 MiB",
-      "guid": "595059e1-7b18-4e71-98f8-86b0ef75fe34",
-      "type_guid": "brillo_boot",
-      "position": 2
-    },
-    {
-      "label": "rootfs",
-      "size": "3.5 GiB",
-      "grow": true,
-      "guid": "70672ec3-5eee-49ff-b3b1-eb1fbd406bf5",
-      "type_guid": "brillo_system",
-      "position": 3
-    }
-  ]
-}
diff --git a/partition-table.mk b/partition-table.mk
index 6d9812d..3debbc6 100644
--- a/partition-table.mk
+++ b/partition-table.mk
@@ -18,10 +18,13 @@
 
 include $(ROOTDIR)/build/preamble.mk
 
-partition-table: $(PRODUCT_OUT)/partition-table.img
+partition-table: $(PRODUCT_OUT)/partition-table_8G.img $(PRODUCT_OUT)/partition-table_4G.img
 BPTTOOL := $(ROOTDIR)/tools/bpt/bpttool
 
-$(PRODUCT_OUT)/partition-table.img: $(ROOTDIR)/board/partition-table.json | out-dirs
+$(PRODUCT_OUT)/partition-table_8G.img: $(ROOTDIR)/board/partition-table_8G.json | out-dirs
+	$(BPTTOOL) make_table --input $< --output_gpt $@
+
+$(PRODUCT_OUT)/partition-table_4G.img: $(ROOTDIR)/board/partition-table_4G.json | out-dirs
 	$(BPTTOOL) make_table --input $< --output_gpt $@
 
 targets::
diff --git a/partition-table_4G.json b/partition-table_4G.json
new file mode 100644
index 0000000..01825fb
--- /dev/null
+++ b/partition-table_4G.json
@@ -0,0 +1,38 @@
+{
+  "settings": {
+    "disk_alignment": "512 KiB",
+    "disk_guid": "85b83f23-51c4-4403-8bac-906ff5800f4e",
+    "disk_size": "3728 MiB"
+  },
+  "partitions": [
+    {
+      "label": "bootloaders",
+      "size": "4 MiB",
+      "guid": "897d7224-9d8b-4c1c-a201-fd14d63bff4e",
+      "type_guid": "brillo_boot",
+      "position": 1
+    },
+    {
+      "label": "boot",
+      "size": "128 MiB",
+      "guid": "0f94c91b-1d90-492f-ab86-6952303b214e",
+      "type_guid": "brillo_boot",
+      "position": 2
+    },
+    {
+      "label": "home",
+      "size": "128 MiB",
+      "guid": "b3c678b9-3004-4e30-87dc-ed33fbf4dbfd",
+      "type_guid": "brillo_userdata",
+      "position": 3
+    },
+    {
+      "label": "rootfs",
+      "size": "3.4 GiB",
+      "grow": true,
+      "guid": "02f36a4f-4562-46b2-bbec-bfc1682f9e92",
+      "type_guid": "brillo_system",
+      "position": 4
+    }
+  ]
+}
diff --git a/partition-table_8G.json b/partition-table_8G.json
new file mode 100644
index 0000000..e89178a
--- /dev/null
+++ b/partition-table_8G.json
@@ -0,0 +1,38 @@
+{
+  "settings": {
+    "disk_alignment": "512 KiB",
+    "disk_guid": "534a56f4-400f-4e75-af83-d5e9621bdb42",
+    "disk_size": "7456 MiB"
+  },
+  "partitions": [
+    {
+      "label": "bootloaders",
+      "size": "4 MiB",
+      "guid": "897d7224-9d8b-4c1c-a201-fd14d63bff4e",
+      "type_guid": "brillo_boot",
+      "position": 1
+    },
+    {
+      "label": "boot",
+      "size": "128 MiB",
+      "guid": "0f94c91b-1d90-492f-ab86-6952303b214e",
+      "type_guid": "brillo_boot",
+      "position": 2
+    },
+    {
+      "label": "home",
+      "size": "2048 MiB",
+      "guid": "b3c678b9-3004-4e30-87dc-ed33fbf4dbfd",
+      "type_guid": "brillo_userdata",
+      "position": 3
+    },
+    {
+      "label": "rootfs",
+      "size": "3.5 GiB",
+      "grow": true,
+      "guid": "02f36a4f-4562-46b2-bbec-bfc1682f9e92",
+      "type_guid": "brillo_system",
+      "position": 4
+    }
+  ]
+}