Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ cf_cc_library(
"//cuttlefish/host/libs/config:boot_flow",
"//cuttlefish/host/libs/config:cuttlefish_config",
"//cuttlefish/host/libs/config:kernel_args",
"//cuttlefish/host/libs/config:mkenvimage_slim",
"//cuttlefish/host/libs/vm_manager",
"//cuttlefish/result",
"//libbase",
Expand Down
11 changes: 2 additions & 9 deletions base/cvd/cuttlefish/host/commands/assemble_cvd/boot_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "absl/log/log.h"
#include "absl/strings/str_replace.h"
#include "android-base/strings.h"
#include "gflags/gflags.h"

#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/common/libs/utils/size_utils.h"
Expand All @@ -37,6 +36,7 @@
#include "cuttlefish/host/libs/config/boot_flow.h"
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
#include "cuttlefish/host/libs/config/kernel_args.h"
#include "cuttlefish/host/libs/config/mkenvimage_slim.h"
#include "cuttlefish/host/libs/vm_manager/crosvm_manager.h"
#include "cuttlefish/result/result.h"

Expand Down Expand Up @@ -176,14 +176,7 @@ Result<void> PrepareBootEnvImage(
CF_EXPECTF(WriteEnvironment(instance, flow, kernel_cmdline, uboot_env_path),
"Unable to write out plaintext env '{}'", uboot_env_path);

int mkenvimage_slim_status = Execute({
HostBinaryPath("mkenvimage_slim"),
"-output_path",
tmp_boot_env_image_path,
"-input_path",
uboot_env_path,
});
CF_EXPECT_EQ(mkenvimage_slim_status, 0, "mkenvimage_slim failed.");
CF_EXPECT(MkenvimageSlim(uboot_env_path, tmp_boot_env_image_path));

const off_t boot_env_size_bytes =
AlignToPowerOf2(kMaxAvbMetadataSize + 4096, PARTITION_SIZE_SHIFT);
Expand Down
8 changes: 2 additions & 6 deletions base/cvd/cuttlefish/host/commands/mkenvimage_slim/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ exports_files([".clang-tidy"])

cf_cc_binary(
name = "mkenvimage_slim",
srcs = [
"mkenvimage_slim.cc",
],
srcs = ["mkenvimage_slim.cc"],
deps = [
"//cuttlefish/common/libs/fs",
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/common/libs/utils:tee_logging",
"//cuttlefish/host/libs/config:mkenvimage_slim",
"//cuttlefish/result",
"//libbase",
"@abseil-cpp//absl/log",
"@gflags",
"@zlib",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,19 @@
// https://github.com/u-boot/u-boot/blob/master/tools/mkenvimage.c The bare
// minimum amount of functionality for our application is replicated.

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <zlib.h>

#include <vector>

#include <gflags/gflags.h>
#include "absl/log/log.h"
#include "gflags/gflags.h"

#include "cuttlefish/common/libs/fs/shared_buf.h"
#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/common/libs/utils/tee_logging.h"
#include "cuttlefish/host/libs/config/mkenvimage_slim.h"
#include "cuttlefish/result/result.h"

#define PAD_VALUE (0xff)
#define CRC_SIZE (sizeof(uint32_t))

// One NULL needed at the end of the env.
#define NULL_PAD_LENGTH (1)

DEFINE_int32(env_size, 4096, "file size of resulting env");
DEFINE_string(output_path, "", "output file path");
DEFINE_string(input_path, "", "input file path");

namespace cuttlefish {

static constexpr char kUsageMessage[] =
Expand All @@ -53,48 +40,26 @@ static constexpr char kUsageMessage[] =
"output_path - path to write resulting environment image including CRC "
"to\n";

Result<int> MkenvimageSlimMain(int argc, char** argv) {
Result<void> MkenvimageSlimMain(int argc, char** argv) {
cuttlefish::LogToStderr();
gflags::SetUsageMessage(kUsageMessage);
gflags::ParseCommandLineFlags(&argc, &argv, true);
CF_EXPECT(!FLAGS_output_path.empty(), "Output env path isn't defined.");
CF_EXPECT(FLAGS_env_size != 0, "env size can't be 0.");
CF_EXPECT(!(FLAGS_env_size % 512), "env size must be multiple of 512.");

std::string env_readout = ReadFile(FLAGS_input_path);
CF_EXPECT(env_readout.length(), "Input env is empty");
CF_EXPECT(
env_readout.length() <= (FLAGS_env_size - CRC_SIZE - NULL_PAD_LENGTH),
"Input env must fit within env_size specified.");

std::vector<uint8_t> env_buffer(FLAGS_env_size, PAD_VALUE);
uint8_t* env_ptr = env_buffer.data() + CRC_SIZE;
memcpy(env_ptr, env_readout.c_str(), FileSize(FLAGS_input_path));
env_ptr[env_readout.length()] = 0; // final byte after the env must be NULL
uint32_t crc = crc32(0, env_ptr, FLAGS_env_size - CRC_SIZE);
memcpy(env_buffer.data(), &crc, sizeof(uint32_t));

auto output_fd = // NOLINTNEXTLINE(misc-include-cleaner)
SharedFD::Creat(FLAGS_output_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (!output_fd->IsOpen()) {
return CF_ERR("Couldn't open the output file " + FLAGS_output_path);
} else if (FLAGS_env_size !=
WriteAll(output_fd, (char*)env_buffer.data(), FLAGS_env_size)) {
if (Result<void> res = RemoveFile(FLAGS_output_path); !res.ok()) {
LOG(ERROR) << res.error();
}
return CF_ERR("Couldn't complete write to " + FLAGS_output_path);
}

return 0;
MkenvimageSlim(FLAGS_input_path, FLAGS_output_path, FLAGS_env_size));
return {};
}
} // namespace cuttlefish

int main(int argc, char** argv) {
auto res = cuttlefish::MkenvimageSlimMain(argc, argv);
if (res.ok()) {
return *res;
if (cuttlefish::Result<void> res = cuttlefish::MkenvimageSlimMain(argc, argv);
res.ok()) {
return 0;
} else {
LOG(ERROR) << "mkenvimage_slim failed: \n" << res.error();
return 1;
}
LOG(ERROR) << "mkenvimage_slim failed: \n" << res.error();
abort();
}
13 changes: 13 additions & 0 deletions base/cvd/cuttlefish/host/libs/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,19 @@ cf_cc_library(
],
)

cf_cc_library(
name = "mkenvimage_slim",
srcs = ["mkenvimage_slim.cc"],
hdrs = ["mkenvimage_slim.h"],
deps = [
"//cuttlefish/common/libs/fs",
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
"@zlib",
],
)

cf_cc_library(
name = "openwrt_args",
srcs = ["openwrt_args.cpp"],
Expand Down
68 changes: 68 additions & 0 deletions base/cvd/cuttlefish/host/libs/config/mkenvimage_slim.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cuttlefish/host/libs/config/mkenvimage_slim.h"

#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <string>
#include <vector>

#include <zlib.h>

#include "cuttlefish/common/libs/fs/shared_buf.h"
#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

static constexpr uint8_t kPadValue = 0xff;
static constexpr uint32_t kCrcSize = sizeof(uint32_t);
// One NULL needed at the end of the env.
static constexpr size_t kNullPadLength = 1;

Result<void> MkenvimageSlim(const std::string& input_path,
const std::string& output_path, size_t env_size) {
std::string env_readout = ReadFile(std::string(input_path));
CF_EXPECT_GT(env_readout.length(), 0, "Input env is empty");
CF_EXPECT(env_readout.length() <= (env_size - kCrcSize - kNullPadLength),
"Input env must fit within env_size specified.");

std::vector<uint8_t> env_buffer(env_size, kPadValue);
uint8_t* env_ptr = env_buffer.data() + kCrcSize;
memcpy(env_ptr, env_readout.c_str(), FileSize(std::string(input_path)));
env_ptr[env_readout.length()] = 0; // final byte after the env must be NULL
uint32_t crc = crc32(0, env_ptr, env_size - kCrcSize);
memcpy(env_buffer.data(), &crc, sizeof(uint32_t));

SharedFD output_fd = // NOLINTNEXTLINE(misc-include-cleaner)
SharedFD::Creat(output_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

CF_EXPECTF(output_fd->IsOpen(), "Couldn't open the output file '{}': '{}'",
output_path, output_fd->StrError());

CF_EXPECT_EQ(env_size,
WriteAll(output_fd, (char*)env_buffer.data(), env_size),
"Couldn't complete write to '"
<< output_path << "': " << output_fd->StrError());

return {};
}

} // namespace cuttlefish
30 changes: 30 additions & 0 deletions base/cvd/cuttlefish/host/libs/config/mkenvimage_slim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <stddef.h>

#include <string>

#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

Result<void> MkenvimageSlim(const std::string& input_path,
const std::string& output_path,
size_t env_size = 4096);

} // namespace cuttlefish
Loading