36 ms·
Deploying Rust in existing firmware codebases
- Kostarrr 2y agoIt's really cool that google is investing so heavily in rust. However, the blog post is more of a "starting point" than a "lessons learned" (which I hoped for).
- Analemma_ 2y agoIt's not a great title for the post, because it's ambiguous and could go either way, but the post itself doesn't pretend to be anything other than a how-to documentation guide for Android integrators.
- nicce 2y agoIn some sense it is bit missleading as well. On firmware level, you typically need to use a lot of unsafe code. Of course, this highlights the potential danger zones very well, if you use it correctly. But there is still chance that human made a mistake that leads into memory bug. It should be addressed, while it greatly reduces the risks.
- timschmidt 2y agoI'm hacking on a firmware in Rust right now, for the ESP32-C3, and the only unsafe bits are buried deeply in the esp-hal crate for the C FFI to ESP-IDF and low level hardware twiddling. esp-hal exposes safe interfaces to all of that, so I've had to write zero unsafe code.
- nicce 2y agoBut someone else just has written that unsafe code. It is there and unsafe is used as intended, to create an abstraction layer and limit the risky area. Note that even if you use safe abstractions, interactions through safe abstractions can leak to issues in unsafe code.
- mecsred 2y agoThis may come as a shock, but it's actually unsafe all the way down. Turns out everything needs to become machine code eventually, and CPUs can do all kinds of nasty stuff. Doesn't make containing the unsafe codes to a region of the program any less of a good idea.
- nicce 2y agoI was just about to edit my comment, because indeed that is the case, and someone will grab into it. But firmware as a term includes typically writing also that part which parent used ”safely”. In Rust world hal is own term and as abstraction more known term, but that is not the case if you advertise Rust in blog in order to make non-Rust users to make an understanding. If you build your own products, there is no hal, you need to write it. And use of unsafe to limit the dangerous code is indeed excellent practice. But why leave that part out from the blog post which gets a lot of audience?
- lilyball 2y agoIf interactions through safe abstractions leak unsafety into your code, that's a bug in the abstractions. Edit: who voted me down? This is literally the definition of writing a safe abstraction around unsafe code in Rust. If the user of your safe abstraction can trigger a segfault or UB or otherwise do something that is supposed to require unsafe, then your safe abstraction is buggy.
- vlovich123 2y agoMemory safety and UB sure you could argue for that. But take for example something like FD reuse. In Rust's std library there's abstractions to prevent that and encourage ownership semantics even though there's no segfault or UB that will happen as a result. So clearly "safety" is a spectrum here. And it should be pretty obvious from the existence of unit tests and various property testing frameworks & sanitizers that bugs exist that aren't memory safety / UB related can't be completely prevented through Rust's abstractions.
- mystified5016 2y agoESP-IDF is a pretty high-level framework on top of FreeRTOS. Both do the vast majority of the heavy lifting. Once you're working on bare metal without these fancy frameworks, things get real hairy real fast. I strongly encourage you to poke around in the lower levels of the IDF. It's equal parts fascinating and horrifying. Espressif's code is... interesting, to say the least.
- timschmidt 2y ago> Espressif's code is... interesting, to say the least. Of that, I have no doubt. I'm anxiously awaiting esp-openmac's completion, so that I can use Wifi without the ESP-IDF as a dependency. It's the only reason I pull that crate in, because I'm aware of the FreeRTOS dependency. In my experience, one never really has to work at the bare metal layer in Rust, unless you are designing new chips yourself. All the existing microcontrollers I would think of using already have well worn hals. Truly impressive what the Rust community has accomplished.
- oll3 2y agoYes, on hal level there will be a bunch of unsafe. On firmware application level it's not often I find the need to reach for unsafe though. It would typically be if the hal is lacking in some sense.
- jsheard 2y agoI was a bit taken aback when I realized that Embassy's low-level PAC crates expose all of the devices raw registers through "safe" interfaces though. I'm pretty sure that letting you configure DMA engines arbitrarily at any time is not in the spirit of safe Rust. AFAIK their high-level HAL crates do aim to be properly safe but their PAC approach makes me nervous. https://docs.embassy.dev/rp-pac/git/default/dma/struct.Channel.html https://docs.embassy.dev/rp-pac/git/default/dma/struct.Chann...
- bacon_waffle 2y agoUsually the HAL is between the main firmware and the PAC, so whether the PAC methods are marked as unsafe could almost be considered an implementation detail. But yes, there has been a lot of discussion around how to handle DMA peripherals - the embedded_dma crate offers some abstractions that I've found handy.
- nicce 2y agoBut firmware is kinda generic term that usually includes the hal. I am not sure what it meant in this blog post.
- zamalek 2y agoI feel like it's a poor starting point, too. embedded-hal or embassy would be a much better starting point. Ultimately if you know enough to know what a memory mapped register is, you can _probably_ become dangerous with Rust really quickly. The alternative is someone using Arduino libraries, who would greatly benefit from a HAL of some kind. What I found is that the Rust HALs are incredibly thin and understandable, I was digging into MCU datasheets within a month of first using embassy (with no prior embedded experience).
- unwind 2y agoUh, I get a mostly blank page with a randomly-placed YouTube icon on the right, and no actual text? Wut? Edit: Firefox on Linux/x64.
- pm215 2y agoIt doesn't seem to display properly on all browsers - I got that on Linux Firefox at work earlier, but Android Firefox seems to manage to display it OK.
- Sindisil 2y agoNot always. I just tried it in FF on my Android phone. No luck. Assuming I'm not missing anything, if they can't be bothered to make a simple blog page loadable. FFS.
- gpm 2y agoTry refreshing a few times. It seems that they've managed to partially break the page on firefox. I'm currently also on x64/firefox/linux and it works roughly half the time for me.
- tux3 2y agoImpressive that they managed to make a plain blog post break. What kind of insane Javascript must be running, that a vertical scroll view with text and pictures has been overcomplicated to the point of breakage
- swatcoder 2y agoLast time I looked up a function in the Android API reference, it involved a 10MB of file transfer (mostly crude custom fonts, IIRC), and the browser gave up trying to resolve the anchor url meant to jump me to the function itself, so I had to Ctrl-f or manually scroll to actually get to my information. In recent years, FAANG web teams are way more invested in automating things than engineering them to be efficient, durable, or gracefully adaptive products.
- yazaddaruvala 2y agoI hope this effort can also translate into more Rust in Linux, rather than potentially Android on a different OS.
- vlovich123 2y agoThis isn’t relevant to either thing you’ve said. The post is about taking the firmware code that’s on various peripherals (eg the microcontroller the IMU is connected to which Android then talks to or the WiFi/BT microcontroller that talks to the actual PHY radio). Rust in Linux is about the Linux kernel having more rust code but Linux is not firmware in the traditional sense (unless you’re talking about ucLinux which no one in my experience uses) and this would have nothing to do with porting Android to another kernel which I can’t see happening considering how much custom kernel code there is (unless you are talking about fuschia but fuschia is not Android even though it implements an Android compat layer).
- monocasa 2y agoThis is a very similar environment to the Linux kernel, and most of what this blog post suggests applies to Rust in Linux.
- vlovich123 2y agoKind of, but Rust in Linux is already an effort that's way ahead of what this blog is talking about and has adapted itself to things specific to the Linux kernel whereas firmware environments are typically even more barebones.
- sophacles 2y agoFirmware is not new territory for rust at all. Oxide compiter has been making their firmware in rust for a few years, they even have an embedded OS (called hubris). This effort may ne new for Android, but its not a "break into a new area" type thing.
- smaudet 2y agoThis page came up for me blank... I had to stick it in brave for it to work. I guess google doesn't make web pages anymore...
- jeroenhd 2y agoAd blockers get really weird with Google blog pages, especially if you use multiple or very strict ones. There are plenty of rule lists where most Google domains are just blacklisted by default in case something tries to load javascript from them, and those will break the blog posts. Page works for me, but the CSS is screwed up by adblock. It is what it is.
- wging 2y agoIn what browser did it fail to work? It works for me in both iOS Safari and desktop Firefox.
- tom_ 2y agoComes up blank for me on Firefox 129.0.2 and Firefox 130.0, both on Windows. I don't use ad blockers.
- monocasa 2y agoComes up essentially blank for me too on Firefox desktop on ubuntu 24.04. Still comes up blank with the only adblocker I use (ublock) disabled as well.
- Gigachad 2y agoLoaded fine for me on Firefox on Macos
- freedomben 2y agoI had to whitelist a script from ajax.googleapis.com to get the page to load.
- tlavoie 2y agoIt was blank for me on Firefox, even with uBlock origin turned off. However, disabling JavaScript on the page did the trick! So, whatever shenanigans they're doing to mess with the page rendering are completely unnecessary for the reader.
- gpvos 2y agohttps://archive.is/HitH8 https://archive.is/HitH8
- deleted 2y ago[deleted]
- the_panopticon 2y agoThis article is a nice addition to the community since rarely is a complete rewrite of a firmware corpus possible. Speaking of Rust and host or boot firmware, the recently published https://techcommunity.microsoft.com/t5/surface-it-pro-blog/surface-uefi-evolution-in-boot-security-amp-device-management-to/ba-p/4159998 https://techcommunity.microsoft.com/t5/surface-it-pro-blog/s... provides a nice experience report. Other work on moving Rust into EDKII for UEFI style firmware https://github.com/tianocore/edk2-staging/tree/edkii-rust https://github.com/tianocore/edk2-staging/tree/edkii-rust is described in https://uefi.org/sites/default/files/resources/Enabling%20RUST%20for%20UEFI%20Firmware_8.19.2020.pdf https://uefi.org/sites/default/files/resources/Enabling%20RU... and https://cfp.osfc.io/media/osfc2020/submissions/SLFJTN/resources/OSFC2020_Rust_EFI_Yao_Zimmer_NDK4Dme.pdf https://cfp.osfc.io/media/osfc2020/submissions/SLFJTN/resour.... There's also guidance on Rust for firmware in a book chapter https://link.springer.com/chapter/10.1007/978-1-4842-6106-4_20 https://link.springer.com/chapter/10.1007/978-1-4842-6106-4_....
- ngneer 2y agoToo bad they do not go deeper into impacts on binary image size.
- pornel 2y agoThey're using "no_std" Rust, which is quite bare-bones. Usually the standard library adds 300KB+, but without it it's just cost of the code you add. With a little care, you can get the same code size as you'd get in C. https://github.com/johnthagen/min-sized-rust https://github.com/johnthagen/min-sized-rust
- petabyt 2y agoThe most painful thing about this is strings. To convert to/from cstrings, you have to use the stdlib FFI function, which will allocate memory. This was a nice surprise in the form of a bunch of undefined GNU ABI references when linking to C. I was writing bare metal firmware. I hadn't imported libgcc or even libc yet. Having to convert every string and use unsafe blocks everywhere is a pain. And last time I tried it wasn't even possible to interface with a varargs function. Im not a big Zig fan but it looks so much better from a FFI perspective. I haven't tried it but it looks like everything just works.
- jihiggins 2y agoFor going from C to Rust, I think you'd just use CString::to_str instead of CString::to_string. I'm pretty sure this just calls std::transmute behind the scenes and doesn't cause an allocation. (In the same way that you generally don't clone vanilla strings and instead pass references around.) iirc, it's still tricky to go the other way. I think if I was working with super C-FFI heavy code, I'd just end up using CString as my default for that code. Custom allocators would also help there, since you could just create the strings on the stack or whatever, but I don't remember if the string version of those is even finished in nightly yet.
- steveklabnik 2y agoYou'd use CStr::from_ptr if you've received a C string (which does not allocate, that's correct, though not via transmute, just a cast), but since Rust strings aren't null terminated, you'll need to allocate some memory to add the null byte on the end. Which yeah, then I'd want to try to use CString as much as possible, in that case. (If you wrote to an array on the stack, you could turn it into a Cstr too, instead of heap allocating, but then you'd have a max string size, which may or may not be something you'd want.)
- ameliaquining 2y agoalloc::string::String (a.k.a. std::string::String) doesn't support custom allocators yet, even on nightly, but that doesn't mean you can't use them; it just means you have to use a third-party crate, like https://docs.rs/string https://docs.rs/string or https://docs.rs/smol_str https://docs.rs/smol_str.