Add A/B implementation.

This CL add routines for working with A/B metadata, including A/B
selection and managing rollback indexes.

A/B metadata is stored in the 'misc' partition in the |slot_suffix|
field using a format private to libavb - see bootable/recovery/bootloader.h
for more details. A new set_ab_metadata sub-command has been added to
avbtool for initializing A/B metadata at build time.

A/B metadata integrity is provided by a simple magic marker and a CRC-32
checksum. If invalid A/B metadata is detected, the behavior is to reset
the A/B metadata to a known state where both slots are given seven boot
tries.

An implementation of the boot_control HAL using AVB-specific A/B
metadata is also provided.

Also factored out the test-side AvbOps into a FakeAvbOps class and put
it in its own file.

Saw a couple of references to things like "Brillo Boot Image" and the
like. Fixed these up.

This CL is based on work done by Kevin Chavez - see b/29072323 - during
his internship at Google.

BUG=31264229
TEST=New unit tests + all unit tests pass.
TEST=Manual testing of boot_control HAL using the bootctl command.

Change-Id: I594ea4173a051ecb72636058440372ff1ca5855b
diff --git a/test/fake_avb_ops.h b/test/fake_avb_ops.h
new file mode 100644
index 0000000..8513cae
--- /dev/null
+++ b/test/fake_avb_ops.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef FAKE_AVB_OPS_H_
+#define FAKE_AVB_OPS_H_
+
+#include <base/files/file_util.h>
+#include <string>
+#include <vector>
+
+#include "libavb.h"
+
+struct FakeAvbOpsC;
+typedef struct FakeAvbOpsC FakeAvbOpsC;
+
+class FakeAvbOps {
+ public:
+  FakeAvbOps();
+  ~FakeAvbOps();
+
+  AvbOps* avb_ops() { return (AvbOps*)avb_ops_; }
+
+  void set_partition_dir(const base::FilePath& partition_dir) {
+    partition_dir_ = partition_dir;
+  }
+
+  void set_expected_public_key(const std::string& expected_public_key) {
+    expected_public_key_ = expected_public_key;
+  }
+
+  void set_stored_rollback_indexes(
+      const std::vector<uint64_t>& stored_rollback_indexes) {
+    stored_rollback_indexes_ = stored_rollback_indexes;
+  }
+
+  std::vector<uint64_t> get_stored_rollback_indexes() {
+    return stored_rollback_indexes_;
+  }
+
+  void set_stored_is_device_unlocked(bool stored_is_device_unlocked) {
+    stored_is_device_unlocked_ = stored_is_device_unlocked;
+  }
+
+  AvbIOResult read_from_partition(const char* partition, int64_t offset,
+                                  size_t num_bytes, void* buffer,
+                                  size_t* out_num_read);
+
+  AvbIOResult write_to_partition(const char* partition, int64_t offset,
+                                 size_t num_bytes, const void* buffer);
+
+  int validate_vbmeta_public_key(AvbOps* ops, const uint8_t* public_key_data,
+                                 size_t public_key_length);
+
+  bool read_rollback_index(AvbOps* ops, size_t rollback_index_slot,
+                           uint64_t* out_rollback_index);
+
+  bool write_rollback_index(AvbOps* ops, size_t rollback_index_slot,
+                            uint64_t rollback_index);
+
+  bool read_is_device_unlocked(AvbOps* ops, bool* out_is_device_unlocked);
+
+  bool get_unique_guid_for_partition(AvbOps* ops, const char* partition,
+                                     char* guid_buf, size_t guid_buf_size);
+
+ private:
+  FakeAvbOpsC* avb_ops_;
+
+  base::FilePath partition_dir_;
+
+  std::string expected_public_key_;
+
+  std::vector<uint64_t> stored_rollback_indexes_;
+
+  bool stored_is_device_unlocked_;
+};
+
+#endif /* FAKE_AVB_OPS_H_ */