Revert "[bootloader] Add ABR support"

This reverts commit 31620e85dadc5df71e687cf72ab671b24207b1dc.

Reason for revert: mkzedboot is failing

Original change's description:
> [bootloader] Add ABR support
> 
> Bug: 34464
> Bug: 46175
> Test: Manually tested zircon-a, recovery flow and looked at the
>       ABR info in the boot menu to match the expected value.
> 
> Change-Id: I6c5817a35a1d94e5db21c4725c8c45f6b65bf1f1
> Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/367447
> Commit-Queue: Puneetha Ramachandra <puneetha@google.com>
> Reviewed-by: Abdulla Kamar <abdulla@google.com>
> Testability-Review: Abdulla Kamar <abdulla@google.com>

TBR=abdulla@google.com,puneetha@google.com,rudymathu@google.com

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 34464, 46175
Change-Id: I3828ea4379fe57324868dbc5fd8317974badb7e9
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/404870
Reviewed-by: Puneetha Ramachandra <puneetha@google.com>
Commit-Queue: Puneetha Ramachandra <puneetha@google.com>
diff --git a/boards/qemu-x64.gni b/boards/qemu-x64.gni
index b16f420..6fcd668 100644
--- a/boards/qemu-x64.gni
+++ b/boards/qemu-x64.gni
@@ -2,22 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-target_cpu = "x64"
-
-board_package_labels = [
-  "//garnet/bin/power_manager",
-  "//garnet/bin/power_manager:base_config",
-  "//garnet/bin/thermd",
-  "//garnet/bin/thermd:config",
-
-  # Include all drivers for now.
-  "//garnet/packages/prod:drivers",
-  "//src/hwinfo:default_board_config",
-  "//src/media/audio/bundles:virtual_audio_driver",
-]
-
-max_fuchsia_zbi_size = "16777216"  # 16 MiB
-max_zedboot_zbi_size = "16777216"  # 16 MiB
+import("x64.gni")
 
 add_qemu_to_build_archives = true
 board_name = "qemu-x64"
diff --git a/boards/x64.gni b/boards/x64.gni
index 1f84d4f..07021a0 100644
--- a/boards/x64.gni
+++ b/boards/x64.gni
@@ -20,8 +20,3 @@
 
 max_fuchsia_zbi_size = "16777216"  # 16 MiB
 max_zedboot_zbi_size = "16777216"  # 16 MiB
-
-use_vbmeta = true
-avb_key = "//zircon/third_party/tools/android/avb/test/data/testkey_atx_psk.pem"
-avb_atx_metadata =
-    "//zircon/third_party/tools/android/avb/test/data/atx_metadata.bin"
diff --git a/build/images/vbmeta.gni b/build/images/vbmeta.gni
index 43ea4cd..06e45e4 100644
--- a/build/images/vbmeta.gni
+++ b/build/images/vbmeta.gni
@@ -138,10 +138,10 @@
       # so, we do not care about a partition size checking.
       # `partition_size' is a mandatory option, thus let
       # use obviously big number for the partition size to pass
-      # verification. 200M should be good enough.
+      # verification. 100M should be good enough.
       # TODO (dmitryya@) fix avbtool to do not check partition
       # size if --do_not_append_vbmeta_image is specified.
-      "209715200",
+      "104857600",
     ]
   }
 
diff --git a/zircon/bootloader/src/BUILD.zircon.gn b/zircon/bootloader/src/BUILD.zircon.gn
index 9bb9495..3b6c3623 100644
--- a/zircon/bootloader/src/BUILD.zircon.gn
+++ b/zircon/bootloader/src/BUILD.zircon.gn
@@ -6,7 +6,6 @@
 
 source_set("src") {
   sources = [
-    "abr.c",
     "cmdline.c",
     "device_id.c",
     "diskio.c",
@@ -22,9 +21,7 @@
   include_dirs = [ "." ]
   deps = [
     "$zx/kernel/lib/efi",
-    "$zx/system/ulib/abr",
     "$zx/system/ulib/gfx-font-data",
     "$zx/system/ulib/tftp",
-    "$zx/third_party/ulib/cksum",
   ]
 }
diff --git a/zircon/bootloader/src/abr.c b/zircon/bootloader/src/abr.c
deleted file mode 100644
index c96376e..0000000
--- a/zircon/bootloader/src/abr.c
+++ /dev/null
@@ -1,75 +0,0 @@
-// Copyright 2020 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "abr.h"
-
-#include <lib/cksum.h>
-#include <stdio.h>
-#include <string.h>
-#include <xefi.h>
-#include <zircon/hw/gpt.h>
-
-#include "cmdline.h"
-#include "diskio.h"
-
-#define ABR_PARTITION_NAME "misc"
-#define ABR_OFFSET 0
-#define CMDLINE_SLOTINFO_SIZE 32
-
-/* === ABR sysdeps and Ops === */
-
-uint32_t AbrCrc32(const void* buf, size_t buf_size) { return crc32(0, buf, buf_size); }
-
-static bool read_abr_metadata(void* context, size_t size, uint8_t* buffer) {
-  uint8_t guid_value[GPT_GUID_LEN] = GUID_ABR_META_VALUE;
-  if (read_partition(gImg, gSys, guid_value, GUID_ABR_META_NAME, ABR_OFFSET, buffer, size) != 0) {
-    printf("failed to read A/B/R metadata.\n");
-    return false;
-  }
-  return true;
-}
-
-static bool write_abr_metadata(void* context, const uint8_t* buffer, size_t size) {
-  uint8_t guid_value[GPT_GUID_LEN] = GUID_ABR_META_VALUE;
-  if (write_partition(gImg, gSys, guid_value, GUID_ABR_META_NAME, ABR_OFFSET, buffer, size) != 0) {
-    printf("failed to write A/B/R metadata.\n");
-    return false;
-  }
-  return true;
-}
-
-AbrSlotIndex zircon_abr_get_boot_slot(void) {
-  AbrOps ops = {.read_abr_metadata = read_abr_metadata, .write_abr_metadata = write_abr_metadata};
-  return AbrGetBootSlot(&ops, false, NULL);
-}
-
-void zircon_abr_update_boot_slot_metadata(void) {
-  AbrOps ops = {.read_abr_metadata = read_abr_metadata, .write_abr_metadata = write_abr_metadata};
-
-  // Write ABR metadata updates
-  AbrSlotIndex slot = AbrGetBootSlot(&ops, true, NULL);
-
-  // TODO(puneetha) : Move this logic to verified boot
-  // Write slot info to cmdline
-  char slot_info[CMDLINE_SLOTINFO_SIZE] = {};
-  snprintf(slot_info, sizeof(slot_info), "zvb.current_slot=%s", AbrGetSlotSuffix(slot));
-  cmdline_append(slot_info, strlen(slot_info));
-}
-
-AbrResult zircon_abr_set_slot_active(int slot_number) {
-  AbrOps ops = {.read_abr_metadata = read_abr_metadata, .write_abr_metadata = write_abr_metadata};
-
-  AbrResult ret = AbrMarkSlotActive(&ops, slot_number);
-  if (ret != kAbrResultOk) {
-    printf("Fail to get slot info\n");
-    return ret;
-  }
-
-  return 0;
-}
-
-AbrResult zircon_abr_get_slot_info(int slot_number, AbrSlotInfo* info) {
-  AbrOps ops = {.read_abr_metadata = read_abr_metadata, .write_abr_metadata = write_abr_metadata};
-  return AbrGetSlotInfo(&ops, slot_number, info);
-}
diff --git a/zircon/bootloader/src/abr.h b/zircon/bootloader/src/abr.h
deleted file mode 100644
index 7e04b7c..0000000
--- a/zircon/bootloader/src/abr.h
+++ /dev/null
@@ -1,22 +0,0 @@
-// Copyright 2020 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef ZIRCON_BOOTLOADER_SRC_ABR_H_
-#define ZIRCON_BOOTLOADER_SRC_ABR_H_
-
-#include <lib/abr/abr.h>
-
-// Returns the current boot slot based on the ABR data
-AbrSlotIndex zircon_abr_get_boot_slot(void);
-
-// Returns ABR slot info
-AbrResult zircon_abr_get_slot_info(int slot_number, AbrSlotInfo* info);
-
-// Forces `slot_number` to be the active slot to be booted from
-AbrResult zircon_abr_set_slot_active(int slot_number);
-
-// Update ABR data for the current boot slot. To be called when the boot slot is finalized.
-void zircon_abr_update_boot_slot_metadata(void);
-
-#endif  // ZIRCON_BOOTLOADER_SRC_ABR_H_
diff --git a/zircon/bootloader/src/diskio.c b/zircon/bootloader/src/diskio.c
index bca79ad..28e74860 100644
--- a/zircon/bootloader/src/diskio.c
+++ b/zircon/bootloader/src/diskio.c
@@ -81,11 +81,8 @@
 } disk_t;
 
 static efi_status disk_read(disk_t* disk, size_t offset, void* data, size_t length) {
-  if (disk->first > disk->last) {
-    return EFI_VOLUME_CORRUPTED;
-  }
-
   uint64_t size = (disk->last - disk->first) * disk->blksz;
+
   if ((offset > size) || ((size - offset) < length)) {
     return EFI_INVALID_PARAMETER;
   }
@@ -93,20 +90,6 @@
   return disk->io->ReadDisk(disk->io, disk->id, (disk->first * disk->blksz) + offset, length, data);
 }
 
-static efi_status disk_write(disk_t* disk, size_t offset, void* data, size_t length) {
-  if (disk->first > disk->last) {
-    return EFI_VOLUME_CORRUPTED;
-  }
-
-  uint64_t size = (disk->last - disk->first) * disk->blksz;
-  if ((offset > size) || ((size - offset) < length)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  return disk->io->WriteDisk(disk->io, disk->id, (disk->first * disk->blksz) + offset, length,
-                             data);
-}
-
 static void disk_close(disk_t* disk) {
   disk->bs->CloseProtocol(disk->h, &DiskIoProtocol, disk->img, NULL);
 }
@@ -215,8 +198,8 @@
 
 // Given a disk structure, find the kernel on that disk by reading the partition table
 // and looking for the partition with the supplied guid_name.
-static int disk_find_partition(disk_t* disk, bool verbose, const uint8_t* guid_value,
-                               const char* guid_name) {
+static int disk_find_kernel(disk_t* disk, bool verbose, const uint8_t* guid_value,
+                            const char* guid_name) {
   gpt_header_t gpt;
   efi_status status = disk_read(disk, disk->blksz, &gpt, sizeof(gpt));
   if (status != EFI_SUCCESS) {
@@ -309,7 +292,7 @@
     return NULL;
   }
 
-  if (disk_find_partition(&disk, verbose, guid_value, guid_name)) {
+  if (disk_find_kernel(&disk, verbose, guid_value, guid_name)) {
     printf("Cannot find %s partition on bootloader disk.\n", guid_name);
     goto fail0;
   }
@@ -353,47 +336,3 @@
   disk_close(&disk);
   return NULL;
 }
-
-efi_status read_partition(efi_handle img, efi_system_table* sys, const uint8_t* guid_value,
-                          const char* guid_name, uint64_t offset, unsigned char* data,
-                          size_t size) {
-  static bool verbose = false;
-  disk_t disk;
-
-  if (disk_find_boot(img, sys, verbose, &disk) < 0) {
-    printf("Cannot find bootloader disk.\n");
-    return EFI_NOT_FOUND;
-  }
-
-  if (disk_find_partition(&disk, verbose, guid_value, guid_name)) {
-    printf("Cannot find %s partition on bootloader disk.\n", guid_name);
-    disk_close(&disk);
-    return EFI_NOT_FOUND;
-  }
-
-  efi_status status = disk_read(&disk, offset, data, size);
-  disk_close(&disk);
-  return status;
-}
-
-efi_status write_partition(efi_handle img, efi_system_table* sys, const uint8_t* guid_value,
-                           const char* guid_name, uint64_t offset, const unsigned char* data,
-                           size_t size) {
-  static bool verbose = false;
-  disk_t disk;
-
-  if (disk_find_boot(img, sys, verbose, &disk) < 0) {
-    printf("Cannot find bootloader disk.\n");
-    return EFI_NOT_FOUND;
-  }
-
-  if (disk_find_partition(&disk, verbose, guid_value, guid_name)) {
-    printf("Cannot find %s partition on bootloader disk.\n", guid_name);
-    disk_close(&disk);
-    return EFI_NOT_FOUND;
-  }
-
-  efi_status status = disk_write(&disk, offset, (void*)data, size);
-  disk_close(&disk);
-  return status;
-}
diff --git a/zircon/bootloader/src/diskio.h b/zircon/bootloader/src/diskio.h
deleted file mode 100644
index 4f976cc..0000000
--- a/zircon/bootloader/src/diskio.h
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2020 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef ZIRCON_BOOTLOADER_SRC_DISKIO_H_
-#define ZIRCON_BOOTLOADER_SRC_DISKIO_H_
-
-#include <efi/system-table.h>
-
-efi_status read_partition(efi_handle img, efi_system_table* sys, const uint8_t* guid_value,
-                          const char* guid_name, uint64_t offset, unsigned char* data, size_t size);
-
-efi_status write_partition(efi_handle img, efi_system_table* sys, const uint8_t* guid_value,
-                           const char* guid_name, uint64_t offset, const unsigned char* data,
-                           size_t size);
-
-void* image_load_from_disk(efi_handle img, efi_system_table* sys, size_t* sz,
-                           const uint8_t* guid_value, const char* guid_name);
-
-#endif  // ZIRCON_BOOTLOADER_SRC_DISKIO_H_
diff --git a/zircon/bootloader/src/osboot.c b/zircon/bootloader/src/osboot.c
index 1e27af2..c91554c 100644
--- a/zircon/bootloader/src/osboot.c
+++ b/zircon/bootloader/src/osboot.c
@@ -24,9 +24,7 @@
 #include <efi/protocol/simple-text-input.h>
 #include <efi/system-table.h>
 
-#include "abr.h"
 #include "bootbyte.h"
-#include "diskio.h"
 
 #define DEFAULT_TIMEOUT 10
 
@@ -37,12 +35,6 @@
 static nbfile nbramdisk;
 static nbfile nbcmdline;
 
-static char cmdbuf[CMDLINE_MAX];
-void print_cmdline(void) {
-  cmdline_to_string(cmdbuf, sizeof(cmdbuf));
-  printf("cmdline: %s\n", cmdbuf);
-}
-
 nbfile* netboot_get_buffer(const char* name, size_t size) {
   if (!strcmp(name, NB_KERNEL_FILENAME)) {
     return &nbkernel;
@@ -170,21 +162,6 @@
   return valid_keys[0];
 }
 
-void list_abr_info(void) {
-  for (uint32_t i = 0; i <= kAbrSlotIndexR; i++) {
-    AbrSlotInfo info;
-    AbrResult result;
-    result = zircon_abr_get_slot_info(i, &info);
-    if (result != kAbrResultOk) {
-      printf("Failed to get zircon%s slot info: %d\n", AbrGetSlotSuffix(i), result);
-      return;
-    }
-    printf("Slot zircon%s : Bootable? %d, Successful boot? %d, Active? %d, Retry# %d\n",
-           AbrGetSlotSuffix(i), info.is_bootable, info.is_marked_successful, info.is_active,
-           info.num_tries_remaining);
-  }
-}
-
 void do_select_fb(void) {
   uint32_t cur_mode = get_gfx_mode();
   uint32_t max_mode = get_gfx_max_mode();
@@ -213,40 +190,38 @@
 
 void do_bootmenu(bool have_fb) {
   const char* menukeys;
-  if (have_fb) {
-    menukeys = "rfax";
-  } else {
-    menukeys = "rax";
-  }
-
-  while (true) {
-    printf("  BOOT MENU  \n");
-    printf("  ---------  \n");
-    if (have_fb)
-      printf("  (f) list framebuffer modes\n");
-    printf("  (a) List abr info\n");
-    printf("  (r) reset\n");
-    printf("  (x) exit menu\n");
-    printf("\n");
-    char key = key_prompt(menukeys, INT_MAX);
-    switch (key) {
-      case 'f': {
-        do_select_fb();
-        break;
-      }
-      case 'a':
-        list_abr_info();
-        break;
-      case 'r':
-        gSys->RuntimeServices->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
-        break;
-      case 'x':
-      default:
-        return;
+  if (have_fb)
+    menukeys = "rfx";
+  else
+    menukeys = "rx";
+  printf("  BOOT MENU  \n");
+  printf("  ---------  \n");
+  if (have_fb)
+    printf("  (f) list framebuffer modes\n");
+  printf("  (r) reset\n");
+  printf("  (x) exit menu\n");
+  printf("\n");
+  char key = key_prompt(menukeys, INT_MAX);
+  switch (key) {
+    case 'f': {
+      do_select_fb();
+      break;
     }
+    case 'r':
+      gSys->RuntimeServices->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
+      break;
+    case 'x':
+    default:
+      break;
   }
 }
 
+static char cmdbuf[CMDLINE_MAX];
+void print_cmdline(void) {
+  cmdline_to_string(cmdbuf, sizeof(cmdbuf));
+  printf("cmdline: %s\n", cmdbuf);
+}
+
 static char netboot_cmdline[CMDLINE_MAX];
 void do_netboot(void) {
   efi_physical_addr mem = 0xFFFFFFFF;
@@ -623,33 +598,14 @@
 
   //
   // The first entry in valid_keys will be the default after the timeout.
-
-  // Move the current slot according to ABR to the top.
-  // Then check the bootbyte to override abr decision if necessary.
-  // Lastly use the value of bootloader.default to determine the first entry. If
+  // Check the bootbyte before checking bootloader.default
+  // Use the value of bootloader.default to determine the first entry. If
   // bootloader.default is not set, use "network".
-  // TODO(47049) : Make this logic more simpler
-
-  switch (zircon_abr_get_boot_slot()) {
-    case kAbrSlotIndexA:
-      swap_to_head('1', valid_keys, key_idx);
-      break;
-    case kAbrSlotIndexB:
-      swap_to_head('2', valid_keys, key_idx);
-      break;
-    case kAbrSlotIndexR:
-      swap_to_head('r', valid_keys, key_idx);
-      break;
-    default:
-      printf("Fatal error in ABR metadata!!");
-  }
-
+  //
   if (bootbyte == RTC_BOOT_RECOVERY) {
     swap_to_head('z', valid_keys, key_idx);
   } else if (bootbyte == RTC_BOOT_NORMAL) {
-    // TODO(47049) Commented out to use the ABR choice. Refactor to use a simple boot selection
-    // code.
-    // swap_to_head('m', valid_keys, key_idx);
+    swap_to_head('m', valid_keys, key_idx);
   } else if (bootbyte == RTC_BOOT_BOOTLOADER) {
     // swap_to_head('b', valid_keys, key_idx);
     printf("ERROR: booting to bootloader is not supported!\n");
@@ -657,10 +613,6 @@
     swap_to_head('m', valid_keys, key_idx);
   } else if (!memcmp(defboot, "zedboot", 7)) {
     swap_to_head('z', valid_keys, key_idx);
-  } else if (!memcmp(defboot, "local", 5)) {
-    // TODO(47049) Commented out to use the ABR choice. Refactor to use a simple boot selection
-    // code.
-    // swap_to_head('m', valid_keys, key_idx);
   } else {
     swap_to_head('n', valid_keys, key_idx);
   }
@@ -706,13 +658,6 @@
         break;
       case '1':
       case 'm':
-        printf("Booting ZIRCON-A...\n");
-        // Update current boot slot, in case the user chose differenlty than the ABR data
-        if (zircon_abr_get_boot_slot() != kAbrSlotIndexA) {
-          zircon_abr_set_slot_active(kAbrSlotIndexA);
-        }
-        zircon_abr_update_boot_slot_metadata();
-
         if (ktype == IMAGE_COMBO) {
           zedboot(img, sys, kernel, ksz);
         } else {
@@ -733,36 +678,12 @@
         }
         goto fail;
       case '2':
-        printf("Booting ZIRCON-B...\n");
-        // Update current boot slot, in case the user chose differenlty than the ABR data
-        if (zircon_abr_get_boot_slot() != kAbrSlotIndexB) {
-          zircon_abr_set_slot_active(kAbrSlotIndexB);
-        }
-        zircon_abr_update_boot_slot_metadata();
-
         if (ktype_b == IMAGE_COMBO) {
           zedboot(img, sys, kernel_b, ksz_b);
-        } else {
-          size_t rsz = 0;
-          void* ramdisk = NULL;
-          efi_file_protocol* ramdisk_file = xefi_open_file(L"bootdata.bin");
-          const char* ramdisk_name = "bootdata.bin";
-          if (ramdisk_file == NULL) {
-            ramdisk_file = xefi_open_file(L"ramdisk.bin");
-            ramdisk_name = "ramdisk.bin";
-          }
-          if (ramdisk_file) {
-            printf("Loading %s...\n", ramdisk_name);
-            ramdisk = xefi_read_file(ramdisk_file, &rsz, FRONT_BYTES);
-            ramdisk_file->Close(ramdisk_file);
-          }
-          boot_kernel(gImg, gSys, kernel, ksz, ramdisk, rsz);
         }
-        goto fail;
+        __FALLTHROUGH;
       case 'r':
       case 'z':
-        printf("Booting Recovery...\n");
-        zircon_abr_update_boot_slot_metadata();
         if (zedboot_ktype == IMAGE_COMBO) {
           zedboot(img, sys, zedboot_kernel, zedboot_size);
         } else {
diff --git a/zircon/bootloader/src/osboot.h b/zircon/bootloader/src/osboot.h
index 9813563..55d7803 100644
--- a/zircon/bootloader/src/osboot.h
+++ b/zircon/bootloader/src/osboot.h
@@ -49,6 +49,9 @@
 // otherwise returns 0
 size_t image_getsize(void* imageheader, size_t sz);
 
+void* image_load_from_disk(efi_handle img, efi_system_table* sys, size_t* sz,
+                           const uint8_t* guid_value, const char* guid_name);
+
 // Where to start the kernel from
 extern size_t kernel_zone_size;
 extern efi_physical_addr kernel_zone_base;