Merge "recovery: Add "boot-fastboot" command to BCB."
diff --git a/Android.bp b/Android.bp
index 53b74dc..c5fcd65 100644
--- a/Android.bp
+++ b/Android.bp
@@ -232,7 +232,7 @@
     required: [
         "e2fsdroid.recovery",
         "librecovery_ui_ext",
-        "mke2fs.conf",
+        "mke2fs.conf.recovery",
         "mke2fs.recovery",
         "recovery_deps",
     ],
diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp
index 248b469..c4f3176 100644
--- a/tests/component/updater_test.cpp
+++ b/tests/component/updater_test.cpp
@@ -56,8 +56,6 @@
 
 using PackageEntries = std::unordered_map<std::string, std::string>;
 
-static constexpr size_t kTransferListHeaderLines = 4;
-
 struct selabel_handle* sehandle = nullptr;
 
 static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
@@ -223,7 +221,7 @@
   // File not found.
   expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
 
-  std::string src_file = from_testdata_base("old.file");
+  std::string src_file = from_testdata_base("boot.img");
   std::string src_content;
   ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
   size_t src_size = src_content.size();
@@ -846,7 +844,8 @@
   };
 
   // "2\nstash " + block3_hash + " 2,2,3"
-  std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
+  std::string last_command_content =
+      "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2];
 
   RunBlockImageUpdate(false, entries, image_file_, "");
 
@@ -895,7 +894,8 @@
 
   ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
 
-  std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
+  std::string last_command_content =
+      "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines];
   ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
 
   RunBlockImageUpdate(false, entries, image_file_, "");
@@ -934,7 +934,8 @@
   ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
 
   // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
-  std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
+  std::string last_command_content =
+      "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2];
 
   // First run: expect the verification to succeed and the last_command_file is intact.
   ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
@@ -1129,16 +1130,17 @@
 
 INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
                         ::testing::Range(static_cast<size_t>(0),
-                                         g_transfer_list.size() - kTransferListHeaderLines));
+                                         g_transfer_list.size() -
+                                             TransferList::kTransferListHeaderLines));
 
 TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
   ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
 
   LOG(INFO) << "Interrupting at line " << index_ << " ("
-            << g_transfer_list[kTransferListHeaderLines + index_] << ")";
+            << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")";
 
   std::vector<std::string> transfer_list_copy{ g_transfer_list };
-  transfer_list_copy[kTransferListHeaderLines + index_] = "abort";
+  transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort";
 
   g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
 
@@ -1151,8 +1153,8 @@
   if (index_ == 0) {
     ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
   } else {
-    last_command_expected =
-        std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
+    last_command_expected = std::to_string(index_ - 1) + "\n" +
+                            g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1];
     std::string last_command_actual;
     ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
     ASSERT_EQ(last_command_expected, last_command_actual);
diff --git a/tests/testdata/new.file b/tests/testdata/new.file
deleted file mode 100644
index cdeb8fd..0000000
--- a/tests/testdata/new.file
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/old.file b/tests/testdata/old.file
deleted file mode 100644
index 166c873..0000000
--- a/tests/testdata/old.file
+++ /dev/null
Binary files differ
diff --git a/tests/unit/applypatch_test.cpp b/tests/unit/applypatch_test.cpp
index 0d4123b..fe00e1e 100644
--- a/tests/unit/applypatch_test.cpp
+++ b/tests/unit/applypatch_test.cpp
@@ -46,17 +46,17 @@
 class ApplyPatchTest : public ::testing::Test {
  protected:
   void SetUp() override {
-    old_file = from_testdata_base("old.file");
-    FileContents old_fc;
-    ASSERT_EQ(0, LoadFileContents(old_file, &old_fc));
-    old_sha1 = print_sha1(old_fc.sha1);
-    old_size = old_fc.data.size();
+    source_file = from_testdata_base("boot.img");
+    FileContents boot_fc;
+    ASSERT_EQ(0, LoadFileContents(source_file, &boot_fc));
+    source_size = boot_fc.data.size();
+    source_sha1 = print_sha1(boot_fc.sha1);
 
-    new_file = from_testdata_base("new.file");
-    FileContents new_fc;
-    ASSERT_EQ(0, LoadFileContents(new_file, &new_fc));
-    new_sha1 = print_sha1(new_fc.sha1);
-    new_size = new_fc.data.size();
+    target_file = from_testdata_base("recovery.img");
+    FileContents recovery_fc;
+    ASSERT_EQ(0, LoadFileContents(target_file, &recovery_fc));
+    target_size = recovery_fc.data.size();
+    target_sha1 = print_sha1(recovery_fc.sha1);
 
     srand(time(nullptr));
     bad_sha1_a = android::base::StringPrintf("%040x", rand());
@@ -66,78 +66,83 @@
     Paths::Get().set_cache_temp_source("/cache/saved.file");
   }
 
-  std::string old_file;
-  std::string old_sha1;
-  size_t old_size;
+  std::string source_file;
+  std::string source_sha1;
+  size_t source_size;
 
-  std::string new_file;
-  std::string new_sha1;
-  size_t new_size;
+  std::string target_file;
+  std::string target_sha1;
+  size_t target_size;
 
   std::string bad_sha1_a;
   std::string bad_sha1_b;
 };
 
 TEST_F(ApplyPatchTest, CheckMode) {
-  std::string partition = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
+  std::string partition =
+      "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + source_sha1;
   ASSERT_EQ(0, applypatch_check(partition, {}));
-  ASSERT_EQ(0, applypatch_check(partition, { old_sha1 }));
+  ASSERT_EQ(0, applypatch_check(partition, { source_sha1 }));
   ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, bad_sha1_b }));
-  ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, old_sha1, bad_sha1_b }));
+  ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, source_sha1, bad_sha1_b }));
 }
 
 TEST_F(ApplyPatchTest, CheckMode_NonEmmcTarget) {
-  ASSERT_NE(0, applypatch_check(old_file, {}));
-  ASSERT_NE(0, applypatch_check(old_file, { old_sha1 }));
-  ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, bad_sha1_b }));
-  ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, old_sha1, bad_sha1_b }));
+  ASSERT_NE(0, applypatch_check(source_file, {}));
+  ASSERT_NE(0, applypatch_check(source_file, { source_sha1 }));
+  ASSERT_NE(0, applypatch_check(source_file, { bad_sha1_a, bad_sha1_b }));
+  ASSERT_NE(0, applypatch_check(source_file, { bad_sha1_a, source_sha1, bad_sha1_b }));
 }
 
 TEST_F(ApplyPatchTest, CheckMode_EmmcTarget) {
-  // EMMC:old_file:size:sha1 should pass the check.
-  std::string src_file = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
+  // EMMC:source_file:size:sha1 should pass the check.
+  std::string src_file =
+      "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + source_sha1;
   ASSERT_EQ(0, applypatch_check(src_file, {}));
 
-  // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
-  src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
-             std::to_string(old_size + 1) + ":" + old_sha1;
+  // EMMC:source_file:(size-1):sha1:(size+1):sha1 should fail the check.
+  src_file = "EMMC:" + source_file + ":" + std::to_string(source_size - 1) + ":" + source_sha1 +
+             ":" + std::to_string(source_size + 1) + ":" + source_sha1;
   ASSERT_NE(0, applypatch_check(src_file, {}));
 
-  // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
-  src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
-             std::to_string(old_size) + ":" + old_sha1 + ":" + std::to_string(old_size + 1) + ":" +
-             old_sha1;
+  // EMMC:source_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
+  src_file = "EMMC:" + source_file + ":" + std::to_string(source_size - 1) + ":" + source_sha1 +
+             ":" + std::to_string(source_size) + ":" + source_sha1 + ":" +
+             std::to_string(source_size + 1) + ":" + source_sha1;
   ASSERT_EQ(0, applypatch_check(src_file, {}));
 
-  // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
-  src_file = "EMMC:" + old_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
-             std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
-             old_sha1;
+  // EMMC:source_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
+  src_file = "EMMC:" + source_file + ":" + std::to_string(source_size + 1) + ":" + source_sha1 +
+             ":" + std::to_string(source_size - 1) + ":" + source_sha1 + ":" +
+             std::to_string(source_size) + ":" + source_sha1;
   ASSERT_EQ(0, applypatch_check(src_file, {}));
 
-  // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
+  // EMMC:target_file:(size+1):source_sha1:(size-1):source_sha1:size:source_sha1:size:target_sha1
   // should pass the check.
-  src_file = "EMMC:" + new_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
-             std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
-             old_sha1 + ":" + std::to_string(new_size) + ":" + new_sha1;
+  src_file = "EMMC:" + target_file + ":" + std::to_string(source_size + 1) + ":" + source_sha1 +
+             ":" + std::to_string(source_size - 1) + ":" + source_sha1 + ":" +
+             std::to_string(source_size) + ":" + source_sha1 + ":" + std::to_string(target_size) +
+             ":" + target_sha1;
   ASSERT_EQ(0, applypatch_check(src_file, {}));
 }
 
 TEST_F(ApplyPatchTest, CheckMode_UseBackup) {
-  std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a;
-  ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 }));
+  std::string corrupted =
+      "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + bad_sha1_a;
+  ASSERT_NE(0, applypatch_check(corrupted, { source_sha1 }));
 
-  Paths::Get().set_cache_temp_source(old_file);
-  ASSERT_EQ(0, applypatch_check(corrupted, { old_sha1 }));
-  ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, old_sha1, bad_sha1_b }));
+  Paths::Get().set_cache_temp_source(source_file);
+  ASSERT_EQ(0, applypatch_check(corrupted, { source_sha1 }));
+  ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, source_sha1, bad_sha1_b }));
 }
 
 TEST_F(ApplyPatchTest, CheckMode_UseBackup_BothCorrupted) {
-  std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a;
+  std::string corrupted =
+      "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + bad_sha1_a;
   ASSERT_NE(0, applypatch_check(corrupted, {}));
-  ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 }));
+  ASSERT_NE(0, applypatch_check(corrupted, { source_sha1 }));
 
-  Paths::Get().set_cache_temp_source(old_file);
+  Paths::Get().set_cache_temp_source(source_file);
   ASSERT_NE(0, applypatch_check(corrupted, { bad_sha1_a, bad_sha1_b }));
 }
 
diff --git a/tests/unit/commands_test.cpp b/tests/unit/commands_test.cpp
index 5c69e07..8a54df7 100644
--- a/tests/unit/commands_test.cpp
+++ b/tests/unit/commands_test.cpp
@@ -17,6 +17,7 @@
 #include <algorithm>
 #include <string>
 
+#include <android-base/strings.h>
 #include <gtest/gtest.h>
 #include <openssl/sha.h>
 
@@ -496,3 +497,58 @@
   auto failing_stash_reader = [](const std::string&, std::vector<uint8_t>*) -> int { return -1; };
   ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, block_reader, failing_stash_reader));
 }
+
+TEST(TransferListTest, Parse) {
+  std::vector<std::string> input_lines{
+    "4",  // version
+    "2",  // total blocks
+    "1",  // max stashed entries
+    "1",  // max stashed blocks
+    "stash 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1",
+    "move 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1 1 2,0,1",
+  };
+
+  std::string err;
+  TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err);
+  ASSERT_TRUE(static_cast<bool>(transfer_list));
+  ASSERT_EQ(4, transfer_list.version());
+  ASSERT_EQ(2, transfer_list.total_blocks());
+  ASSERT_EQ(1, transfer_list.stash_max_entries());
+  ASSERT_EQ(1, transfer_list.stash_max_blocks());
+  ASSERT_EQ(2U, transfer_list.commands().size());
+  ASSERT_EQ(Command::Type::STASH, transfer_list.commands()[0].type());
+  ASSERT_EQ(Command::Type::MOVE, transfer_list.commands()[1].type());
+}
+
+TEST(TransferListTest, Parse_InvalidCommand) {
+  std::vector<std::string> input_lines{
+    "4",  // version
+    "2",  // total blocks
+    "1",  // max stashed entries
+    "1",  // max stashed blocks
+    "stash 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1",
+    "move 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1 1",
+  };
+
+  std::string err;
+  TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err);
+  ASSERT_FALSE(static_cast<bool>(transfer_list));
+}
+
+TEST(TransferListTest, Parse_ZeroTotalBlocks) {
+  std::vector<std::string> input_lines{
+    "4",  // version
+    "0",  // total blocks
+    "0",  // max stashed entries
+    "0",  // max stashed blocks
+  };
+
+  std::string err;
+  TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err);
+  ASSERT_TRUE(static_cast<bool>(transfer_list));
+  ASSERT_EQ(4, transfer_list.version());
+  ASSERT_EQ(0, transfer_list.total_blocks());
+  ASSERT_EQ(0, transfer_list.stash_max_entries());
+  ASSERT_EQ(0, transfer_list.stash_max_blocks());
+  ASSERT_TRUE(transfer_list.commands().empty());
+}
diff --git a/updater/commands.cpp b/updater/commands.cpp
index 0170863..aed6336 100644
--- a/updater/commands.cpp
+++ b/updater/commands.cpp
@@ -401,3 +401,54 @@
   }
   return os;
 }
+
+TransferList TransferList::Parse(const std::string& transfer_list_str, std::string* err) {
+  TransferList result{};
+
+  std::vector<std::string> lines = android::base::Split(transfer_list_str, "\n");
+  if (lines.size() < kTransferListHeaderLines) {
+    *err = android::base::StringPrintf("too few lines in the transfer list [%zu]", lines.size());
+    return TransferList{};
+  }
+
+  // First line in transfer list is the version number.
+  if (!android::base::ParseInt(lines[0], &result.version_, 3, 4)) {
+    *err = "unexpected transfer list version ["s + lines[0] + "]";
+    return TransferList{};
+  }
+
+  // Second line in transfer list is the total number of blocks we expect to write.
+  if (!android::base::ParseUint(lines[1], &result.total_blocks_)) {
+    *err = "unexpected block count ["s + lines[1] + "]";
+    return TransferList{};
+  }
+
+  // Third line is how many stash entries are needed simultaneously.
+  if (!android::base::ParseUint(lines[2], &result.stash_max_entries_)) {
+    return TransferList{};
+  }
+
+  // Fourth line is the maximum number of blocks that will be stashed simultaneously.
+  if (!android::base::ParseUint(lines[3], &result.stash_max_blocks_)) {
+    *err = "unexpected maximum stash blocks ["s + lines[3] + "]";
+    return TransferList{};
+  }
+
+  // Subsequent lines are all individual transfer commands.
+  for (size_t i = kTransferListHeaderLines; i < lines.size(); i++) {
+    const std::string& line = lines[i];
+    if (line.empty()) continue;
+
+    size_t cmdindex = i - kTransferListHeaderLines;
+    std::string parsing_error;
+    Command command = Command::Parse(line, cmdindex, &parsing_error);
+    if (!command) {
+      *err = android::base::StringPrintf("Failed to parse command %zu [%s]: %s", cmdindex,
+                                         line.c_str(), parsing_error.c_str());
+      return TransferList{};
+    }
+    result.commands_.push_back(command);
+  }
+
+  return result;
+}
diff --git a/updater/include/private/commands.h b/updater/include/private/commands.h
index 5212897..79f9154 100644
--- a/updater/include/private/commands.h
+++ b/updater/include/private/commands.h
@@ -406,3 +406,70 @@
 };
 
 std::ostream& operator<<(std::ostream& os, const Command& command);
+
+// TransferList represents the info for a transfer list, which is parsed from input text lines
+// containing commands to transfer data from one place to another on the target partition.
+//
+// The creator of the transfer list will guarantee that no block is read (i.e., used as the source
+// for a patch or move) after it has been written.
+//
+// The creator will guarantee that a given stash is loaded (with a stash command) before it's used
+// in a move/bsdiff/imgdiff command.
+//
+// Within one command the source and target ranges may overlap so in general we need to read the
+// entire source into memory before writing anything to the target blocks.
+//
+// All the patch data is concatenated into one patch_data file in the update package. It must be
+// stored uncompressed because we memory-map it in directly from the archive. (Since patches are
+// already compressed, we lose very little by not compressing their concatenation.)
+//
+// Commands that read data from the partition (i.e. move/bsdiff/imgdiff/stash) have one or more
+// additional hashes before the range parameters, which are used to check if the command has
+// already been completed and verify the integrity of the source data.
+class TransferList {
+ public:
+  // Number of header lines.
+  static constexpr size_t kTransferListHeaderLines = 4;
+
+  TransferList() = default;
+
+  // Parses the given input string and returns a TransferList object. Sets error message if any.
+  static TransferList Parse(const std::string& transfer_list_str, std::string* err);
+
+  int version() const {
+    return version_;
+  }
+
+  size_t total_blocks() const {
+    return total_blocks_;
+  }
+
+  size_t stash_max_entries() const {
+    return stash_max_entries_;
+  }
+
+  size_t stash_max_blocks() const {
+    return stash_max_blocks_;
+  }
+
+  const std::vector<Command>& commands() const {
+    return commands_;
+  }
+
+  // Returns whether the TransferList is valid.
+  constexpr explicit operator bool() const {
+    return version_ != 0;
+  }
+
+ private:
+  // BBOTA version.
+  int version_{ 0 };
+  // Total number of blocks to be written in this transfer.
+  size_t total_blocks_;
+  // Maximum number of stashes that exist at the same time.
+  size_t stash_max_entries_;
+  // Maximum number of blocks to be stashed.
+  size_t stash_max_blocks_;
+  // Commands in this transfer.
+  std::vector<Command> commands_;
+};