Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 17d469bd authored by Yifan Hong's avatar Yifan Hong
Browse files

fastboot driver: RunAndReadBuffer don't allocate too much mem

Instead of allocating the buffer for the whole
upload (which can be arbitrary number of bytes as
the device determines), read 1 MiB at a time.

Test: pass
Bug: 173654501
Change-Id: Ib601b0341b10b7dccbb429cd21aad86a2d3bfda8
parent bbf374db
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ cc_library_host_static {

    header_libs: [
        "bootimg_headers",
        "libstorage_literals_headers",
    ],

    export_header_lib_headers: [
@@ -275,6 +276,9 @@ cc_library_host_static {
    // Only version the final binaries
    use_version_lib: false,
    static_libs: ["libbuildversion"],
    header_libs: [
        "libstorage_literals_headers",
    ],

    generated_headers: ["platform_tools_version"],

+16 −6
Original line number Diff line number Diff line
@@ -45,11 +45,13 @@
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <storage_literals/storage_literals.h>

#include "constants.h"
#include "transport.h"

using android::base::StringPrintf;
using namespace android::storage_literals;

namespace fastboot {

@@ -316,13 +318,21 @@ RetCode FastBootDriver::RunAndReadBuffer(
        return BAD_DEV_RESP;
    }

    std::vector<char> data(dsize);
    if ((ret = ReadBuffer(data.data(), data.size())) != SUCCESS) {
    const uint64_t total_size = dsize;
    const uint64_t buf_size = std::min<uint64_t>(total_size, 1_MiB);
    std::vector<char> data(buf_size);
    uint64_t current_offset = 0;
    while (current_offset < total_size) {
        uint64_t remaining = total_size - current_offset;
        uint64_t chunk_size = std::min(buf_size, remaining);
        if ((ret = ReadBuffer(data.data(), chunk_size)) != SUCCESS) {
            return ret;
        }
    if ((ret = write_fn(data.data(), data.size())) != SUCCESS) {
        if ((ret = write_fn(data.data(), chunk_size)) != SUCCESS) {
            return ret;
        }
        current_offset += chunk_size;
    }
    return HandleResponse(response, info);
}

+5 −0
Original line number Diff line number Diff line
@@ -8,4 +8,9 @@ cc_library_headers {
    host_supported: true,
    recovery_available: true,
    export_include_dirs: ["."],
    target: {
        windows: {
            enabled: true,
        },
    },
}