August 20, 2023

Raspberry Pi Pico - Get linux and udev to help - part 2

In a previous attempt we got shot down by systemd. For some inscrutable reason, systemd is unwilling to allow the "mount" command to be used from within a udev script. (It is always quite surprising when root gets told "permission denied").

Pass the buck

If we can't use mount, maybe we can signal someone outside the udev sandbox to do our bidding. One issue here is that user "tom" cannot invoke mount, so we would need a suid root program or script if we truly want to mount the rp2040 device. My basic idea here was to use a netcat command from within the udev script and send a tcp message, something like "systemd sucks" over a port number like 5678. I would have a program listening on port 5678 that would then mount the rp2040 drive and copy the uf2 file in /home/tom/pico to the waiting device.

As my mind wanders, I wonder just how the existing setup is managed. When I plug in the pico (while holding bootsel), my system detects that a new "flash stick" has shown up on the USB bus, and it generates /dev/sdd and /dev/sdd1 for it (with permissions allowing access by root:disk), but nothing more. When I click on the icon that clues me in to this, it mounts the drive as /run/media/tom/RPI-RP2. This mount point is owned by tom and allows me access to the drive. Mount shows:

/dev/sdd1 on /run/media/tom/RPI-RP2 type vfat (rw,nosuid,nodev,relatime,uid=1004,gid=1004,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
Some of this is managed by the Thunar manager, which is part of XFCE. I launch Thunar, go to Edit--Preferences, and select Advanced. I see "Enable volume management" is already checked. I click on "Configure" and select both "Mount when hot-plugged" and "Mount when inserted". This yields an improvement. Now when I Plug in the Pico with bootsel, it mounts it for me on /dev/sdd1 as /run/media/tom/RPI-RP2 -- no need for me to click the icon, though the icon still appears. Also it does not launch Thunar, which is wonderful. I also get the link /dev/rp2040upl, but not /dev/rp2040upl1 -- but I may not care about these links.

What I could do is to write a script (that could run as tom) that just polls looking for /run/media/tom/RPI-RP2 to show up, and when it does it would copy the uf2 file to it and exit. This could be part of my standard drill in my Makefiles under "make install". In fact I would probably put together a script in /usr/tom/bin to handle all of this, so I could put this entry in my makefile:

install:
	pico-boot xyz.uf2
So, I would type "make install", then go hit reset and bootsel on the Pico and would just sit back until my code started running. This script could time out after some interval (let's say 30 seconds) and announce failure.


Have any comments? Questions? Drop me a line!

Tom's electronics pages / tom@mmto.org