Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mIWY9-0007zq-GX for mharc-grub-devel@gnu.org; Tue, 24 Aug 2021 09:33:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40760) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mIWY8-0007vY-2p for grub-devel@gnu.org; Tue, 24 Aug 2021 09:33:08 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]:35487) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mIWY3-0003Du-DM for grub-devel@gnu.org; Tue, 24 Aug 2021 09:33:07 -0400 Received: by mail-pg1-x52b.google.com with SMTP id e7so19784801pgk.2 for ; Tue, 24 Aug 2021 06:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dH38qC1er5vkdiwQ/uzSBDJGOIa7IpCooE0FwrVsFQ0=; b=DEzJtKBOT2uCEI5slN9RkaB1xTuyrj/RBeuj+Ch/EZGPkqwmizERy1iKFSMPDEXjrN /dE9n8AF1r/VBkNMpeT+Ib6Rxr7oIbvfTJYy+pUoXdX4AAaZue3UMEBS+Bqu88TYYNnB 2vlk4GrY1AKnK74WkcVVH++4AUQi4xuf1P55c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dH38qC1er5vkdiwQ/uzSBDJGOIa7IpCooE0FwrVsFQ0=; b=BZALXUY+XCfjncK8wcHVGqnsUKl1/6JeEVENs90Q6t5VvJ1B2SlteatCvYWeh3NGuU sWsZCeFrBd0QeQYydsHgUnIhRgfWnkJrL+C5cZrRD9XhHtBEMzCqUerAjp2y8QGpIybE eUUOpllRGNPvy1G9+7frMuZOtUM1ypzoeMEfnqDTJwWYc+EKERtls/u0K2sKPS9ivaKr w4sfJvpT5fcvVj/zlgR+pnvO6+OK/8UrymM7CNBhaiwdgJguYG2wdUAz75Ordd2bgI8b gW/fqgr5vvKzhLvaqx1tV1Bxc8H1SekVSN9JPkjZGTvd3j6Bo44rr/R/YZF51UeA54BV N4MQ== X-Gm-Message-State: AOAM532oVTcszIa1HpPbeOEhS9kBhoHYsuWeTBzu8ktSOWgJjlr2w+4f 8oa2dGdghcNMK2il6WpVHtx1pIMpH9PQkg== X-Google-Smtp-Source: ABdhPJwY+GUlWrwaKAQ4dMd/My8mmBLAlqcbh1DpRVHSfSiYPzEeRRx+dkreUF+We9ahM6O0YhOdLg== X-Received: by 2002:a65:6183:: with SMTP id c3mr36960839pgv.73.1629811981825; Tue, 24 Aug 2021 06:33:01 -0700 (PDT) Received: from localhost ([2001:4479:e200:df00:90ee:2749:b9b1:fb92]) by smtp.gmail.com with ESMTPSA id u20sm22285574pgm.4.2021.08.24.06.33.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Aug 2021 06:33:01 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: Daniel Axtens Subject: [RFC PATCH 4/7] Rust: add the rust_hello module Date: Tue, 24 Aug 2021 23:32:40 +1000 Message-Id: <20210824133243.3296813-5-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824133243.3296813-1-dja@axtens.net> References: <20210824133243.3296813-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=dja@axtens.net; helo=mail-pg1-x52b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Aug 2021 13:33:08 -0000 Build an actual module with Rust! This module, built whenever COND_RUST is enabled, does the following: - Prints a message when the module is loaded. - Registers a command which prints hello when run. - Deregisters the command when the module is unloaded. You can build it with the usual make, no special magic required. A test is supplied. A few outstanding to-dos: - The Rust source doesn't get preprocessed in such a way as to allow the normal module to insert the little shim command that loads the module when called. (the command.lst stuff) - I haven't looked at how to specify sections etc in Rust so I use wrap.c to specify a license. - Bindgen uses the 'cty' module to specify c-types. That's MIT licensed, which is GPLv3 compatible (AIUI) but maybe we want to write our own. The linux kernel has one of it's own which is GPLv2 only, I'm hoping to ask if they can dual-license it so we can borrow it. - The module does somewhat expose that more idiomatic layers around the bindings would be very nice. Signed-off-by: Daniel Axtens --- .gitignore | 1 + Makefile.util.def | 6 +++ grub-core/Makefile.core.def | 9 ++++ grub-core/commands/rust-hello/.gitignore | 1 + grub-core/commands/rust-hello/Cargo.lock | 24 ++++++++++ grub-core/commands/rust-hello/Cargo.toml | 16 +++++++ grub-core/commands/rust-hello/src/lib.rs | 57 ++++++++++++++++++++++++ grub-core/commands/rust-hello/wrap.c | 8 ++++ tests/test_rust.in | 19 ++++++++ 9 files changed, 141 insertions(+) create mode 100644 grub-core/commands/rust-hello/.gitignore create mode 100644 grub-core/commands/rust-hello/Cargo.lock create mode 100644 grub-core/commands/rust-hello/Cargo.toml create mode 100644 grub-core/commands/rust-hello/src/lib.rs create mode 100644 grub-core/commands/rust-hello/wrap.c create mode 100644 tests/test_rust.in diff --git a/.gitignore b/.gitignore index f6a1bd051752..68288d6481e9 100644 --- a/.gitignore +++ b/.gitignore @@ -264,6 +264,7 @@ widthspec.bin /stamp-h1 /syslinux_test /tar_test +/test_rust /test_sha512sum /test_unset /tests/syslinux/ubuntu10.04_grub.cfg diff --git a/Makefile.util.def b/Makefile.util.def index f8b356cc1fa4..bca56493f189 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -1211,6 +1211,12 @@ script = { common = tests/syslinux_test.in; }; +script = { + testcase; + name = test_rust; + common = tests/test_rust.in; +}; + program = { testcase; name = example_unit_test; diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 8022e1c0a794..8c3f1be4495e 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2527,3 +2527,12 @@ module = { common = commands/i386/wrmsr.c; enable = x86; }; + +module = { + name = rust_hello; + common = commands/rust-hello/wrap.c; + rust = commands/rust-hello/src/lib.rs; + crate = commands/rust-hello; + + condition = COND_RUST; +}; diff --git a/grub-core/commands/rust-hello/.gitignore b/grub-core/commands/rust-hello/.gitignore new file mode 100644 index 000000000000..eb5a316cbd19 --- /dev/null +++ b/grub-core/commands/rust-hello/.gitignore @@ -0,0 +1 @@ +target diff --git a/grub-core/commands/rust-hello/Cargo.lock b/grub-core/commands/rust-hello/Cargo.lock new file mode 100644 index 000000000000..af55da87ad9a --- /dev/null +++ b/grub-core/commands/rust-hello/Cargo.lock @@ -0,0 +1,24 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cty" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7313c0d620d0cb4dbd9d019e461a4beb501071ff46ec0ab933efb4daa76d73e3" + +[[package]] +name = "grub" +version = "0.1.0" +dependencies = [ + "cty", +] + +[[package]] +name = "rust-hello" +version = "0.1.0" +dependencies = [ + "cty", + "grub", +] diff --git a/grub-core/commands/rust-hello/Cargo.toml b/grub-core/commands/rust-hello/Cargo.toml new file mode 100644 index 000000000000..81535262c3db --- /dev/null +++ b/grub-core/commands/rust-hello/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "rust-hello" +version = "0.1.0" +authors = ["Daniel Axtens "] +edition = "2018" + +[lib] +crate_type = ["staticlib"] + +[profile.release] +panic="abort" +lto = true + +[dependencies] +grub = { path = "../../lib/rust/grub" } +cty = "^0.2" diff --git a/grub-core/commands/rust-hello/src/lib.rs b/grub-core/commands/rust-hello/src/lib.rs new file mode 100644 index 000000000000..db0f43046d5e --- /dev/null +++ b/grub-core/commands/rust-hello/src/lib.rs @@ -0,0 +1,57 @@ +#![no_std] + +use core::mem::replace; +use cty; +use grub::command::GrubCommand; + +/* See https://docs.rust-embedded.org/book/peripherals/singletons.html */ +struct ModuleData { + command: Option, +} + +impl ModuleData { + fn take_command(&mut self) -> GrubCommand { + let c = replace(&mut self.command, None); + c.unwrap() + } +} + +static mut MODULEDATA: ModuleData = ModuleData { command: None }; + +// This _doesn't_ need no-mangle because it's called via a function +// pointer. It does, AIUI, need extern "C" to get the ABI right. +extern "C" fn rust_hello_cmd( + _cmd: *mut grub::bindings::grub_command, + _argc: cty::c_int, + _argv: *mut *mut cty::c_char, +) -> grub::bindings::grub_err_t { + unsafe { + grub::bindings::grub_printf("Hello from command written in Rust\n\0".as_ptr() as *const _) + }; + + grub::bindings::grub_err_t_GRUB_ERR_NONE +} + +#[no_mangle] +pub extern "C" fn grub_rust_hello_init() { + let hello = GrubCommand::new( + "rust_hello\0", + rust_hello_cmd, + "\0", + "say hello from rust\0", + ); + + unsafe { + MODULEDATA.command = Some(hello); + }; + + unsafe { grub::bindings::grub_printf("Hello from Rust\n\0".as_ptr() as *const _) }; +} + +#[no_mangle] +pub extern "C" fn grub_rust_hello_fini() { + // this causes the command to go out of scope, leading to it being unregistered + unsafe { + MODULEDATA.take_command(); + }; +} diff --git a/grub-core/commands/rust-hello/wrap.c b/grub-core/commands/rust-hello/wrap.c new file mode 100644 index 000000000000..97775d6840ae --- /dev/null +++ b/grub-core/commands/rust-hello/wrap.c @@ -0,0 +1,8 @@ +#define RUST_WRAPPER +#include + +GRUB_MOD_LICENSE("GPLv3+"); +/* rust code defines grub_rust_hello_{init,fini}, this is just for the + scripts that determine modules */ +GRUB_MOD_INIT(rust_hello); +GRUB_MOD_FINI(rust_hello); diff --git a/tests/test_rust.in b/tests/test_rust.in new file mode 100644 index 000000000000..b2ceb6f56898 --- /dev/null +++ b/tests/test_rust.in @@ -0,0 +1,19 @@ +#! @BUILD_SHEBANG@ + +. "@builddir@/grub-core/modinfo.sh" + +out=`echo "insmod rust_hello; rust_hello;" | @builddir@/grub-shell` + +if [ ${grub_modinfo_platform} != emu ]; then + if ! echo $out | grep "Hello from Rust" > /dev/null; then + echo "Did not see hello from rust module" + exit 1 + fi +fi + +if echo $out | grep "Hello from command written in Rust" > /dev/null; then + exit 0; +else + echo "Did not see hello from rust command" + exit 1; +fi -- 2.30.2