The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 28 | #include "engine.h" |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 29 | |
Colin Cross | 81c632e | 2013-01-23 19:13:43 -0800 | [diff] [blame] | 30 | #include <errno.h> |
Elliott Hughes | 3ab8b85 | 2015-08-25 19:34:13 -0700 | [diff] [blame] | 31 | #include <stdarg.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include <string.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 35 | #include <sys/stat.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 39 | #include <memory> |
| 40 | #include <vector> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 44 | #include "constants.h" |
Elliott Hughes | 11ff345 | 2018-04-09 13:58:41 -0700 | [diff] [blame] | 45 | #include "transport.h" |
| 46 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 47 | using android::base::StringPrintf; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 49 | static fastboot::FastBootDriver* fb = nullptr; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 50 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 51 | void fb_init(fastboot::FastBootDriver& fbi) { |
| 52 | fb = &fbi; |
| 53 | auto cb = [](std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); }; |
| 54 | fb->SetInfoCallback(cb); |
| 55 | } |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 56 | |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 57 | void fb_reinit(Transport* transport) { |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 58 | if (Transport* old_transport = fb->set_transport(transport)) { |
| 59 | delete old_transport; |
| 60 | } |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 63 | const std::string fb_get_error() { |
| 64 | return fb->Error(); |
| 65 | } |
| 66 | |
| 67 | bool fb_getvar(const std::string& key, std::string* value) { |
| 68 | return !fb->GetVar(key, value); |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 71 | static void HandleResult(double start, int status) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | if (status) { |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 73 | fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); |
| 74 | die("Command failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } else { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 76 | double split = now(); |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 77 | fprintf(stderr, "OKAY [%7.3fs]\n", (split - start)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 81 | #define RUN_COMMAND(command) \ |
| 82 | { \ |
| 83 | double start = now(); \ |
| 84 | auto status = (command); \ |
| 85 | HandleResult(start, status); \ |
| 86 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 88 | void fb_set_active(const std::string& slot) { |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 89 | Status("Setting current slot to '" + slot + "'"); |
| 90 | RUN_COMMAND(fb->SetActive(slot)); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 93 | void fb_erase(const std::string& partition) { |
| 94 | Status("Erasing '" + partition + "'"); |
| 95 | RUN_COMMAND(fb->Erase(partition)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 98 | void fb_flash_fd(const std::string& partition, int fd, uint32_t sz) { |
| 99 | Status(StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024)); |
| 100 | RUN_COMMAND(fb->Download(fd, sz)); |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 101 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 102 | Status("Writing '" + partition + "'"); |
| 103 | RUN_COMMAND(fb->Flash(partition)); |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 106 | void fb_flash(const std::string& partition, const std::vector<char>& data) { |
| 107 | Status(StringPrintf("Sending '%s' (%zu KB)", partition.c_str(), data.size() / 1024)); |
| 108 | RUN_COMMAND(fb->Download(data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 110 | Status("Writing '" + partition + "'"); |
| 111 | RUN_COMMAND(fb->Flash(partition)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 114 | void fb_flash_sparse(const std::string& partition, struct sparse_file* s, uint32_t sz, |
| 115 | size_t current, size_t total) { |
| 116 | Status(StringPrintf("Sending sparse '%s' %zu/%zu (%u KB)", partition.c_str(), current, total, |
| 117 | sz / 1024)); |
| 118 | RUN_COMMAND(fb->Download(s)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 119 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 120 | Status(StringPrintf("Writing sparse '%s' %zu/%zu", partition.c_str(), current, total)); |
| 121 | RUN_COMMAND(fb->Flash(partition)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 124 | void fb_create_partition(const std::string& partition, const std::string& size) { |
| 125 | Status("Creating '" + partition + "'"); |
| 126 | RUN_COMMAND(fb->RawCommand(FB_CMD_CREATE_PARTITION ":" + partition + ":" + size)); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 129 | void fb_delete_partition(const std::string& partition) { |
| 130 | Status("Deleting '" + partition + "'"); |
| 131 | RUN_COMMAND(fb->RawCommand(FB_CMD_DELETE_PARTITION ":" + partition)); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 134 | void fb_resize_partition(const std::string& partition, const std::string& size) { |
| 135 | Status("Resizing '" + partition + "'"); |
| 136 | RUN_COMMAND(fb->RawCommand(FB_CMD_RESIZE_PARTITION ":" + partition + ":" + size)); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 139 | void fb_display(const std::string& label, const std::string& var) { |
| 140 | std::string value; |
| 141 | auto status = fb->GetVar(var, &value); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | if (status) { |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 144 | fprintf(stderr, "getvar:%s FAILED (%s)\n", var.c_str(), fb->Error().c_str()); |
| 145 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | } |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 147 | fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 150 | void fb_reboot() { |
| 151 | fprintf(stderr, "Rebooting"); |
| 152 | fb->Reboot(); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 153 | fprintf(stderr, "\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 156 | void fb_command(const std::string& cmd, const std::string& msg) { |
| 157 | Status(msg); |
| 158 | RUN_COMMAND(fb->RawCommand(cmd)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 161 | void fb_download(const std::string& name, const std::vector<char>& data) { |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 162 | Status("Downloading '" + name + "'"); |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 163 | RUN_COMMAND(fb->Download(data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 166 | void fb_download_fd(const std::string& name, int fd, uint32_t sz) { |
| 167 | Status(StringPrintf("Sending '%s' (%u KB)", name.c_str(), sz / 1024)); |
| 168 | RUN_COMMAND(fb->Download(fd, sz)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 171 | void fb_upload(const std::string& outfile) { |
| 172 | Status("Uploading '" + outfile + "'"); |
| 173 | RUN_COMMAND(fb->Upload(outfile)); |
Jocelyn Bohr | 98cc283 | 2017-01-26 19:20:53 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 176 | void fb_notice(const std::string& notice) { |
| 177 | Status(notice); |
| 178 | fprintf(stderr, "\n"); |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 181 | void fb_wait_for_disconnect() { |
| 182 | fb->WaitForDisconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | } |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 184 | |
| 185 | bool fb_reboot_to_userspace() { |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 186 | Status("Rebooting to userspace fastboot"); |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 187 | verbose("\n"); |
| 188 | |
| 189 | if (fb->RebootTo("fastboot") != fastboot::RetCode::SUCCESS) { |
| 190 | fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); |
| 191 | return false; |
| 192 | } |
| 193 | fprintf(stderr, "OKAY\n"); |
| 194 | |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 195 | fb_reinit(nullptr); |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 196 | return true; |
| 197 | } |