Skip to content

Commit

Permalink
Refactor dtb in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Aug 30, 2023
1 parent de00f1d commit b92c970
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 610 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
[submodule "busybox"]
path = native/src/external/busybox
url = https://github.com/topjohnwu/ndk-busybox.git
[submodule "dtc"]
path = native/src/external/dtc
url = https://github.com/dgibson/dtc.git
[submodule "lz4"]
path = native/src/external/lz4
url = https://github.com/lz4/lz4.git
Expand Down
2 changes: 0 additions & 2 deletions native/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ LOCAL_STATIC_LIBRARIES := \
liblzma \
liblz4 \
libbz2 \
libfdt \
libz \
libzopfli \
libboot-rs
Expand All @@ -111,7 +110,6 @@ LOCAL_SRC_FILES := \
boot/bootimg.cpp \
boot/compress.cpp \
boot/format.cpp \
boot/dtb.cpp \
boot/boot-rs.cpp

include $(BUILD_EXECUTABLE)
Expand Down
7 changes: 7 additions & 0 deletions native/src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native/src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ p384 = "0.13"
rsa = "0.9"
x509-cert = "0.2"
der = "0.7"
fdt = "0.1.5"

[workspace.dependencies.argh]
git = "https://github.com/topjohnwu/argh.git"
Expand Down
1 change: 1 addition & 0 deletions native/src/boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ p384 = { workspace = true }
rsa = { workspace = true, features = ["sha2"] }
x509-cert = { workspace = true }
der = { workspace = true, features = ["derive"] }
fdt = { workspace = true }
50 changes: 44 additions & 6 deletions native/src/boot/bootimg.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <functional>
#include <memory>

#include <libfdt.h>
#include <base.hpp>

#include "boot-rs.hpp"
Expand Down Expand Up @@ -183,29 +182,68 @@ boot_img::~boot_img() {
delete hdr;
}

struct [[gnu::packed]] fdt_header {
struct fdt32_t {
uint32_t data;

[[nodiscard]] uint32_t to_cpu() const {
return (extract_byte<0>() << 24) | (extract_byte<1>() << 16) |
(extract_byte<2>() << 8) | extract_byte<3>();
}

template<size_t N> requires(N < 4)
[[nodiscard]] uint32_t extract_byte() const {
return ((uint8_t *)&data)[N];
}
};

struct node_header {
fdt32_t tag;
char name[0];
};

fdt32_t magic; /* magic word FDT_MAGIC */
fdt32_t totalsize; /* total size of DT block */
fdt32_t off_dt_struct; /* offset to structure */
fdt32_t off_dt_strings; /* offset to strings */
fdt32_t off_mem_rsvmap; /* offset to memory reserve map */
fdt32_t version; /* format version */
fdt32_t last_comp_version; /* last compatible version */

/* version 2 fields below */
fdt32_t boot_cpuid_phys; /* Which physical CPU id we're
booting on */
/* version 3 fields below */
fdt32_t size_dt_strings; /* size of the strings block */

/* version 17 fields below */
fdt32_t size_dt_struct; /* size of the structure block */
};


static int find_dtb_offset(const uint8_t *buf, unsigned sz) {
const uint8_t * const end = buf + sz;

for (auto curr = buf; curr < end; curr += sizeof(fdt_header)) {
curr = static_cast<uint8_t*>(memmem(curr, end - curr, DTB_MAGIC, sizeof(fdt32_t)));
curr = static_cast<uint8_t*>(memmem(curr, end - curr, DTB_MAGIC, sizeof(fdt_header::fdt32_t)));
if (curr == nullptr)
return -1;

auto fdt_hdr = reinterpret_cast<const fdt_header *>(curr);

// Check that fdt_header.totalsize does not overflow kernel image size
uint32_t totalsize = fdt32_to_cpu(fdt_hdr->totalsize);
uint32_t totalsize = fdt_hdr->totalsize.to_cpu();
if (totalsize > end - curr)
continue;

// Check that fdt_header.off_dt_struct does not overflow kernel image size
uint32_t off_dt_struct = fdt32_to_cpu(fdt_hdr->off_dt_struct);
uint32_t off_dt_struct = fdt_hdr->off_dt_struct.to_cpu();
if (off_dt_struct > end - curr)
continue;

// Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE
auto fdt_node_hdr = reinterpret_cast<const fdt_node_header *>(curr + off_dt_struct);
if (fdt32_to_cpu(fdt_node_hdr->tag) != FDT_BEGIN_NODE)
auto fdt_node_hdr = reinterpret_cast<const fdt_header::node_header *>(curr + off_dt_struct);
if (fdt_node_hdr->tag.to_cpu() != 0x1)
continue;

return curr - buf;
Expand Down
Loading

0 comments on commit b92c970

Please sign in to comment.