Skip to content

Commit

Permalink
Refactor dtb in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 authored and topjohnwu committed Sep 19, 2023
1 parent 8e1a915 commit 8d7c7c3
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 626 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 @@ -99,7 +99,6 @@ LOCAL_STATIC_LIBRARIES := \
liblzma \
liblz4 \
libbz2 \
libfdt \
libz \
libzopfli \
libboot-rs
Expand All @@ -109,7 +108,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
39 changes: 23 additions & 16 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 @@ -22,6 +22,7 @@ rsa = "0.9"
x509-cert = "0.2"
der = "0.7"
bytemuck = "1.14"
fdt = "0.1"

[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 }
53 changes: 47 additions & 6 deletions native/src/boot/bootimg.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bit>
#include <functional>
#include <memory>

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

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

struct [[gnu::packed]] fdt_header {
struct fdt32_t {
uint32_t byte0: 8;
uint32_t byte1: 8;
uint32_t byte2: 8;
uint32_t byte3: 8;

constexpr operator uint32_t() const {
return bit_cast<uint32_t>(fdt32_t {
.byte0 = byte3,
.byte1 = byte2,
.byte2 = byte1,
.byte3 = byte0
});
}
};

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;
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;
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 != 0x1u)
continue;

return curr - buf;
Expand Down
Loading

0 comments on commit 8d7c7c3

Please sign in to comment.