update_engine: Add support for parsing the size of a squashfs.

This patch extends the utils::GetFilesystemSize() function to support
squashfs (in addition to the existing ext3 support). The filesystem
type is detected automatically.

squashfs doesn't define a block size for the physical device that it
relies on. There's a definition of block size for the data before
compression that doesn't affect the block size used for the underlying
device. When creating the squashfs, it is by default padded to 4 KiB,
so we assume 4 KiB as the block size. This is also the minimum block
size required by verity to compute hashes in all supported archs.

BUG=chromium:430956
TEST=Unittest added.

Change-Id: Ia683a27c094e25952e8d665535dc6219c849bdd0
Reviewed-on: https://chromium-review.googlesource.com/228906
Reviewed-by: Gaurav Shah <gauravsh@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/utils.h b/utils.h
index 2c0af3e..19bb9d6 100644
--- a/utils.h
+++ b/utils.h
@@ -191,7 +191,7 @@
                      unsigned long flags);  // NOLINT(runtime/int)
 bool UnmountFilesystem(const std::string& mountpoint);
 
-// Returns the block count and the block byte size of the ext3 file system on
+// Returns the block count and the block byte size of the file system on
 // |device| (which may be a real device or a path to a filesystem image) or on
 // an opened file descriptor |fd|. The actual file-system size is |block_count|
 // * |block_size| bytes. Returns true on success, false otherwise.
@@ -202,6 +202,23 @@
                              int* out_block_count,
                              int* out_block_size);
 
+// Determines the block count and block size of the ext3 fs. At least 2048 bytes
+// are required to parse the first superblock. Returns whether the buffer
+// contains a valid ext3 filesystem and the values were parsed.
+bool GetExt3Size(const uint8_t* buffer, size_t buffer_size,
+                 int* out_block_count,
+                 int* out_block_size);
+
+// Determines the block count and block size of the squashfs v4 fs. At least 96
+// bytes are required to parse the header of the filesystem. Since squashfs
+// doesn't define a physical block size, a value of 4096 is used for the block
+// size, which is the default padding when creating the filesystem.
+// Returns whether the buffer contains a valid squashfs v4 header and the size
+// was parsed. Only little endian squashfs is supported.
+bool GetSquashfs4Size(const uint8_t* buffer, size_t buffer_size,
+                      int* out_block_count,
+                      int* out_block_size);
+
 // Returns the path of the passed |command| on the board. This uses the
 // environment variable SYSROOT to determine the path to the command on the
 // board instead of the path on the running environment.
@@ -643,6 +660,4 @@
     }                                                                          \
   } while (0)
 
-
-
 #endif  // UPDATE_ENGINE_UTILS_H_