dtb_service: Add setter and getter for device tree.

Adds an interface for platform specific init code to make the device tree
available to later stages.

Bug: 359345227
Test: Map desktop boot params into gsc_service TA.
Change-Id: I5c2166079af1e044edcd943144260cb26eb121b7
diff --git a/lib/dtb_service/dtb_service.cpp b/lib/dtb_service/dtb_service.cpp
index 0e184b8..9cf477f 100644
--- a/lib/dtb_service/dtb_service.cpp
+++ b/lib/dtb_service/dtb_service.cpp
@@ -174,3 +174,29 @@
 
     return NO_ERROR;
 }
+
+static const uint8_t* s_dtb;
+
+int dtb_set(const uint8_t* dtb, size_t size) {
+    assert(s_dtb == NULL);
+    if (fdt_check_full(dtb, size)) {
+        return ERR_NOT_VALID;
+    }
+    s_dtb = dtb;
+    return NO_ERROR;
+}
+
+int dtb_get(const uint8_t** ptr, size_t* size) {
+    if (!ptr || !size) {
+        return ERR_INVALID_ARGS;
+    }
+    if (s_dtb) {
+        *ptr = s_dtb;
+        *size = fdt_totalsize(s_dtb);
+        return NO_ERROR;
+    } else {
+        *ptr = NULL;
+        *size = 0;
+        return ERR_NOT_READY;
+    }
+}
diff --git a/lib/dtb_service/include/lib/dtb_service/dtb_service.h b/lib/dtb_service/include/lib/dtb_service/dtb_service.h
index 953540b..afb6df3 100644
--- a/lib/dtb_service/include/lib/dtb_service/dtb_service.h
+++ b/lib/dtb_service/include/lib/dtb_service/dtb_service.h
@@ -45,4 +45,25 @@
                     const char* dtb_port,
                     struct ktipc_server* server);
 
+/**
+ * dtb_set() - Set the dtb singleton to be provided by dtb_get().
+ * @dtb: Pointer to the base of the dtb in memory.
+ * @size: Size of the buffer pointer to by dtb.
+ *
+ * Return: %NO_ERROR if dtb is a valid device tree. %ERR_NOT_VALID otherwise.
+ */
+int dtb_set(const uint8_t* dtb, size_t size);
+
+/**
+ * dtb_get() - Set the dtb singleton.
+ * @ptr: Output pointer for the dtb.
+ * @size: Output size of the dtb.
+ *
+ * Return: %NO_ERROR in case of success, ptr and size are updated with the
+ * location and size of the dtb.
+ * %ERR_INVALID_ARGS if ptr or size is null
+ * %ERR_NOT_READY if the dtb has not been set. ptr and size are set to zero
+ */
+int dtb_get(const uint8_t** ptr, size_t* size);
+
 __END_CDECLS
diff --git a/lib/dtb_service/rust/bindings.h b/lib/dtb_service/rust/bindings.h
new file mode 100644
index 0000000..354da1d
--- /dev/null
+++ b/lib/dtb_service/rust/bindings.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2025 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 <lib/dtb_service/dtb_service.h>
+#include <uapi/err.h>
diff --git a/lib/dtb_service/rust/rules.mk b/lib/dtb_service/rust/rules.mk
new file mode 100644
index 0000000..5dfefa0
--- /dev/null
+++ b/lib/dtb_service/rust/rules.mk
@@ -0,0 +1,38 @@
+# Copyright (C) 2025 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.
+#
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_SRCS := $(LOCAL_DIR)/src/lib.rs
+
+MODULE_CRATE_NAME := dtb_service
+
+MODULE_SDK_LIB_NAME := dtb_service-rust
+
+MODULE_LIBRARY_DEPS += \
+
+MODULE_BINDGEN_ALLOW_TYPES :=
+
+MODULE_BINDGEN_ALLOW_VARS := \
+	NO_ERROR \
+
+MODULE_BINDGEN_ALLOW_FUNCTIONS := \
+	dtb_get \
+
+MODULE_BINDGEN_SRC_HEADER := $(LOCAL_DIR)/bindings.h
+
+include make/library.mk
diff --git a/lib/dtb_service/rust/src/lib.rs b/lib/dtb_service/rust/src/lib.rs
new file mode 100644
index 0000000..62633b9
--- /dev/null
+++ b/lib/dtb_service/rust/src/lib.rs
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2025 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.
+ */
+
+//! # Interface library for communicating with the system state service.
+
+#![no_std]
+
+#[allow(non_upper_case_globals)]
+#[allow(non_camel_case_types)]
+#[allow(unused)]
+pub mod sys {
+    include!(env!("BINDGEN_INC_FILE"));
+}