cros_generate_update_payload: Don't do sudo when calling cgpt.

cros_generate_update_payload was using partoffset and partsize from
chromeos-common.sh which call sudo before running cgpt to detect the
offset and size of a partition. This is ok in the context of
chromeos-common.sh, since they are often called over a block device,
but in cros_generate_update_payload the images are regular files.

This patch stops including chromeos-common.sh and uses its own simpler
version of these functions that don't call sudo.

BUG=None
TEST=Ran cros_generate_update_payload as a regular user.

Change-Id: I57d16460ee411e0f7670d7e16d611718c83be2df
Reviewed-on: https://chromium-review.googlesource.com/284703
Trybot-Ready: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/host/cros_generate_update_payload b/host/cros_generate_update_payload
index 0ece22a..ae4286b 100755
--- a/host/cros_generate_update_payload
+++ b/host/cros_generate_update_payload
@@ -31,12 +31,6 @@
 find_common_sh
 . "${SCRIPT_ROOT}/common.sh" || exit 1
 
-# Load functions and constants for chromeos-common.
-. "${SRC_ROOT}/platform2/installer/share/chromeos-common.sh" &> /dev/null || \
-. "/usr/share/misc/chromeos-common.sh" &> /dev/null || \
-. "./chromeos-common.sh" || \
-  die "Unable to load chromeos-common.sh"
-
 DEFINE_string image "" "The image that should be sent to clients."
 DEFINE_string src_image "" "Optional: a source image. If specified, this makes\
  a delta update."
@@ -93,6 +87,23 @@
 DST_KERNEL=""
 DST_ROOT=""
 
+# We include cgpt in the au-generator.zip, so we can call it directly here. We
+# don't use chromeos-common.sh versions because we need to not run them as root.
+
+# Usage: gpt_part_offset <image> <partition_number>
+# Return the start sector number of the partition number |partition_number| in
+# the GPT image |image|.
+gpt_part_offset() {
+  cgpt show -b -i "$2" "$1"
+}
+
+# Usage: gpt_part_size <image> <partition_number>
+# Return the size of the partition number |partition_number| in the GPT image
+#|image|, in number of sectors.
+gpt_part_size() {
+  cgpt show -s -i "$2" "$1"
+}
+
 cleanup() {
   local err=""
 
@@ -144,8 +155,8 @@
 
   # Keep `local` decl split from assignment so return code is checked.
   local offset length
-  offset=$(partoffset "${filename}" ${partition})  # 512-byte sectors
-  length=$(partsize "${filename}" ${partition})  # 512-byte sectors
+  offset=$(gpt_part_offset "${filename}" ${partition})  # 512-byte sectors
+  length=$(gpt_part_size "${filename}" ${partition})  # 512-byte sectors
   dd if="${filename}" of="${temp_file}" bs=8M iflag=skip_bytes,count_bytes \
       count="$(( length * 512 ))" skip="$(( offset * 512 ))" status=none
 }
@@ -199,8 +210,6 @@
   assert_inside_chroot
 fi
 
-locate_gpt
-
 if [[ "${FLAGS_extract}" -eq "${FLAGS_TRUE}" ]]; then
   if [[ -n "${FLAGS_src_image}" ]]; then
     SRC_KERN_PATH="${FLAGS_src_kern_path:-old_kern.dat}"
@@ -287,7 +296,8 @@
 fi
 
 # Add partition size. Only *required* for minor_version=1.
-DST_ROOT_PARTITION_SECTORS=$(partsize "${FLAGS_image}" "${ROOTFS_PART_NUM}")
+DST_ROOT_PARTITION_SECTORS=$(gpt_part_size "${FLAGS_image}" \
+  "${ROOTFS_PART_NUM}")
 if [[ -n "${DST_ROOT_PARTITION_SECTORS}" ]]; then
   DST_ROOT_PARTITION_SIZE=$(( DST_ROOT_PARTITION_SECTORS * 512 ))
   echo "Using rootfs partition size: ${DST_ROOT_PARTITION_SIZE}";